Changeset 7834


Ignore:
Timestamp:
Jun 26, 2020, 12:51:46 PM (19 months ago)
Author:
Nicklas Nordborg
Message:

Fixes #2216: Improve API for adding and removing items to an item list

Added a test case for the new add and remove methods.

Existing functionality in the web client has been updated to use the new API.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/test/TestItemList.java

    r7599 r7834  
    3737import net.sf.basedb.core.Listable;
    3838import net.sf.basedb.core.Permission;
     39import net.sf.basedb.core.query.Expressions;
     40import net.sf.basedb.core.query.Hql;
     41import net.sf.basedb.core.query.Restrictions;
    3942
    4043public class TestItemList
     
    7982    test_list_members(id7, 1);
    8083   
     84    test_add_by_query(id7, "bs.%", 6); // 'id7' should now have all bs.* + the original one
     85   
    8186    int bs7 = test_union(id3, 6, id1, id2); // 'id3' is now the union of 'id1' and 'id2' + one new item
    8287    test_list_members(id3, 6);
     
    96101    test_remove_members(id1, bs1, bs2, bs7, bs8);
    97102    test_list_members(id1, 0);
     103   
     104    test_remove_by_query(id7, "bs.%", 1); // 'id7' should now only the original biosource
     105
    98106    if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter();
    99107   
     
    295303    }
    296304  }
     305 
     306  static void test_add_by_query(int id, String nameFilter, int expectedSize)
     307  {
     308    if (id == 0 ) return;
     309    DbControl dc = null;
     310    try
     311    {
     312      dc = TestUtil.getDbControl();
     313      ItemList bl = ItemList.getById(dc, id);
     314     
     315      ItemQuery<BioSource> query = BioSource.getQuery();
     316      query.restrict(Restrictions.like(Hql.property("name"), Expressions.string(nameFilter)));
     317      bl.add(query);
     318      dc.commit();
     319     
     320      dc = TestUtil.getDbControl();
     321      bl = ItemList.getById(dc, id);
     322      if (bl.getSize() != expectedSize)
     323      {
     324        throw new BaseException("Incorrect size; expected=" + expectedSize + "; actual=" + bl.getSize());
     325      }
     326      write("--Add items to list by query OK (new size=" + expectedSize + ")");
     327    }
     328    catch (Throwable ex)
     329    {
     330      write("--Add items to list by query FAILED");
     331      ex.printStackTrace();
     332      ok = false;
     333    }
     334    finally
     335    {
     336      if (dc != null) dc.close();
     337    }
     338  }
     339 
     340  static void test_remove_by_query(int id, String nameFilter, int expectedSize)
     341  {
     342    if (id == 0 ) return;
     343    DbControl dc = null;
     344    try
     345    {
     346      dc = TestUtil.getDbControl();
     347      ItemList bl = ItemList.getById(dc, id);
     348     
     349      ItemQuery<BioSource> query = BioSource.getQuery();
     350      query.restrict(Restrictions.like(Hql.property("name"), Expressions.string(nameFilter)));
     351      bl.removeItems(query);
     352      dc.commit();
     353     
     354      dc = TestUtil.getDbControl();
     355      bl = ItemList.getById(dc, id);
     356      if (bl.getSize() != expectedSize)
     357      {
     358        throw new BaseException("Incorrect size; expected=" + expectedSize + "; actual=" + bl.getSize());
     359      }
     360      write("--Remove items from list by query OK (new size=" + expectedSize + ")");
     361    }
     362    catch (Throwable ex)
     363    {
     364      write("--Remove items from list by query FAILED");
     365      ex.printStackTrace();
     366      ok = false;
     367    }
     368    finally
     369    {
     370      if (dc != null) dc.close();
     371    }
     372  }
    297373
    298374  static int test_create_and_add_member(int id)
  • trunk/www/views/itemlists/index.jsp

    r7813 r7834  
    316316          // else -- no modifications to the query mean that we only get the current page
    317317         
    318           if (!useSyncFilter) list.add(query.iterate(dc));
     318          if (!useSyncFilter) list.add(query);
    319319        }
    320320       
     
    588588    // else -- no modifications to the query mean that we only get the current page
    589589
    590     int count = list.add(query.iterate(dc));
     590    int count = list.add(query);
    591591    dc.commit();
    592592    message = count + " members added to list";
  • trunk/www/views/itemlists/members/index.jsp

    r7604 r7834  
    223223      query.setMaxResults(-1);
    224224    }
    225     int count = list.add(query.iterate(dc));
     225    int count = list.add(query);
    226226    dc.commit();
    227227    message = count + " members added to list";
     
    231231    // Delete all selected items on the list page
    232232    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, subContext, pageContext, defaultContext);
    233     for (int itemId : cc.getSelected())
    234     {
    235       list.removeItem((Listable)itemType.getById(dc, itemId));
    236     }
     233    list.removeItems(cc.getSelected());
    237234    dc.commit();
    238235    redirect = listPage;
Note: See TracChangeset for help on using the changeset viewer.