Changeset 4712


Ignore:
Timestamp:
Dec 19, 2008, 2:55:13 PM (14 years ago)
Author:
Nicklas Nordborg
Message:

References #1190: API and interface for creating biomaterial lists

Parts of the basic functionality is in place. Needs to be polished up a bit with more error handling.

Location:
trunk
Files:
9 added
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/core/BioMaterialList.java

    r4705 r4712  
    331331 
    332332  /**
     333    Add multiple biomaterials to this list in one go.
     334    @param it An iterator that returns all biomaterials to be added,
     335      null items are ignored
     336    @return The numer of added biomaterials
     337    @throws PermissionDeniedException If the logged in user doesn't have
     338      write permission
     339    @throws InvalidDataException If the iterator returns a biomaterial
     340      of a different type than {@link #getMemberType()}
     341  */
     342  public int add(Iterator<? extends BioMaterial> it)
     343    throws PermissionDeniedException, InvalidDataException
     344  {
     345    checkPermission(Permission.WRITE);
     346    if (it == null) return 0;
     347    int added = 0;
     348    Set<BioMaterialData> data = getData().getBioMaterials();
     349    while (it.hasNext())
     350    {
     351      BioMaterial bm = it.next();
     352      if (bm == null) continue;
     353     
     354      checkMemberType(bm.getType());
     355      if (data.add((BioMaterialData)bm.getData())) added++;
     356    }
     357    getData().setSize(getSize() + added);
     358    return added;
     359  }
     360 
     361  /**
    333362    Remove a biomaterial from the list.
    334363    @param bioMaterial The biomaterial to remove.
  • trunk/src/test/TestSample.java

    r4705 r4712  
    2323*/
    2424import net.sf.basedb.core.*;
    25 
     25import net.sf.basedb.util.biomaterial.PooledChildrenTransformer;
     26
     27import java.util.ArrayList;
     28import java.util.Collections;
    2629import java.util.Date;
     30import java.util.List;
    2731
    2832public class TestSample
     
    6064    test_list_sources(id4, 3); // The three id, id2 and id3 samples
    6165    test_list_pooledChildren(id2, 1);
     66    test_pooledChildrenTransformer(id2, 1);
    6267
    6368    // Standard test: Delete
     
    432437    }
    433438  }
     439
     440  static void test_pooledChildrenTransformer(int sampleId, int expectedResults)
     441  {
     442    if (sampleId == 0) return;
     443    DbControl dc = null;
     444    try
     445    {
     446      dc = TestUtil.getDbControl();
     447      Sample s = Sample.getById(dc, sampleId);
     448      PooledChildrenTransformer<Sample> pct = new PooledChildrenTransformer<Sample>(dc, Sample.getQuery());
     449      List<Sample> children = new ArrayList<Sample>();
     450      int numAdded = pct.transform(Collections.singleton(s), children);
     451
     452      for (int i=0; i<children.size(); i++)
     453      {
     454        write_item(i, children.get(i));
     455      }
     456      if (expectedResults >= 0 && expectedResults != numAdded && numAdded != children.size())
     457      {
     458        throw new BaseException("Expected "+expectedResults+" results, not "+numAdded);
     459      }
     460      write("--List pools from sample using transformer OK ("+numAdded+")");
     461    }
     462    catch (Throwable ex)
     463    {
     464      write("--List pools for sample using transformer FAILED");
     465      ex.printStackTrace();
     466      ok = false;
     467    }
     468    finally
     469    {
     470      if (dc != null) dc.close();
     471    }
     472  }
     473
    434474}
  • trunk/www/biomaterials/biosources/index.jsp

    r4587 r4712  
    282282    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
    283283  }
     284  else if ("CreateBioMaterialList".equals(cmd))
     285  {
     286    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
     287    final ItemQuery<BioSource> query = BioSource.getQuery();
     288    cc.configureQuery(query, true);
     289    cc.setQuery(query);
     290    redirect = "../lists/index.jsp?ID="+ID+"&cmd=NewItem&addBioMaterials=1&formId=biosources&fromContext=BIOSOURCE";
     291  }
    284292  else
    285293  {
  • trunk/www/biomaterials/biosources/list_biosources.jsp

    r4698 r4712  
    186186      Main.viewOrEditItem('<%=ID%>', 'SAMPLE', 0, true, '&biosource_id='+bioSourceId);
    187187    }
     188    function newBioMaterialList()
     189    {
     190      var controller = Main.getController('BIOMATERIALLIST');
     191      Table.submitToPopup(formId, 'CreateBioMaterialList', controller.width, controller.height);
     192    }
    188193    </script>
    189194  </base:head>
     
    362367          title="Columns&hellip;"
    363368          tooltip="Show, hide and re-order columns"
     369        />
     370        <tbl:button
     371          image="add.png"
     372          onclick="newBioMaterialList()"
     373          title="New biomaterial list&hellip;"
     374          tooltip="Create a new biomaterial list from matching biosources"
     375          visible="<%=sc.hasPermission(Permission.CREATE, Item.BIOMATERIALLIST)%>"
    364376        />
    365377        <tbl:button
  • trunk/www/biomaterials/extracts/index.jsp

    r4587 r4712  
    345345    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
    346346  }
     347  else if ("CreateBioMaterialList".equals(cmd))
     348  {
     349    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
     350    final ItemQuery<Extract> query = Extract.getQuery();
     351    cc.configureQuery(query, true);
     352    cc.setQuery(query);
     353    redirect = "../lists/index.jsp?ID="+ID+"&cmd=NewItem&addBioMaterials=1&formId=extracts&fromContext=EXTRACT";
     354  }
    347355  else
    348356  {
  • trunk/www/biomaterials/extracts/list_extracts.jsp

    r4613 r4712  
    196196      Table.presetOnChange('<%=ID%>', formId, '<%=itemType.name()%>', '<%=(String)cc.getObject("defaultColumns")%>');
    197197    }
     198    function newBioMaterialList()
     199    {
     200      var controller = Main.getController('BIOMATERIALLIST');
     201      Table.submitToPopup(formId, 'CreateBioMaterialList', controller.width, controller.height);
     202    }
    198203    </script>
    199204  </base:head>
     
    437442          title="Columns&hellip;"
    438443          tooltip="Show, hide and re-order columns"
     444        />
     445        <tbl:button
     446          image="add.png"
     447          onclick="newBioMaterialList()"
     448          title="New biomaterial list&hellip;"
     449          tooltip="Create a new biomaterial list from matching extracts"
     450          visible="<%=sc.hasPermission(Permission.CREATE, Item.BIOMATERIALLIST)%>"
    439451        />
    440452        <tbl:button
  • trunk/www/biomaterials/labeledextracts/index.jsp

    r4587 r4712  
    359359    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
    360360  }
     361  else if ("CreateBioMaterialList".equals(cmd))
     362  {
     363    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
     364    final ItemQuery<LabeledExtract> query = LabeledExtract.getQuery();
     365    cc.configureQuery(query, true);
     366    cc.setQuery(query);
     367    redirect = "../lists/index.jsp?ID="+ID+"&cmd=NewItem&addBioMaterials=1&formId=labeledextracts&fromContext=LABELEDEXTRACT";
     368  }
    361369  else
    362370  {
  • trunk/www/biomaterials/labeledextracts/list_labeledextracts.jsp

    r4613 r4712  
    192192      Table.presetOnChange('<%=ID%>', formId, '<%=itemType.name()%>', '<%=(String)cc.getObject("defaultColumns")%>');
    193193    }
     194    function newBioMaterialList()
     195    {
     196      var controller = Main.getController('BIOMATERIALLIST');
     197      Table.submitToPopup(formId, 'CreateBioMaterialList', controller.width, controller.height);
     198    }
    194199    </script>
    195200  </base:head>
     
    452457          title="Columns&hellip;"
    453458          tooltip="Show, hide and re-order columns"
     459        />
     460        <tbl:button
     461          image="add.png"
     462          onclick="newBioMaterialList()"
     463          title="New biomaterial list&hellip;"
     464          tooltip="Create a new biomaterial list from matching labeled extracts"
     465          visible="<%=sc.hasPermission(Permission.CREATE, Item.BIOMATERIALLIST)%>"
    454466        />
    455467        <tbl:button
  • trunk/www/biomaterials/lists/edit_list.jsp

    r4705 r4712  
    5050  BioMaterialList list = null;
    5151  Item memberType = null;
     52  boolean addBioMaterials = false;
    5253  boolean mergeLists = false;
    5354  int numSelectedLists = cc.getSelected().size();
    54  
     55  String formId = request.getParameter("formId");
     56  String fromContext = Values.getString(request.getParameter("fromContext"));
     57  String subContext = request.getParameter("subContext");
     58
    5559  if (itemId == 0)
    5660  {
    5761    title = "Create biomaterial list";
    5862    cc.removeObject("item");
    59     memberType = Item.fromValue(Values.getInt(cc.getPropertyValue("memberType")));
     63    addBioMaterials = Values.getBoolean(request.getParameter("addBioMaterials"));
     64    memberType = fromContext != null ? Item.valueOf(fromContext) : Item.fromValue(Values.getInt(cc.getPropertyValue("memberType")));
    6065    mergeLists = numSelectedLists > 0;
    6166  }
     
    95100      if (TabControl.validateActiveTab('settings'))
    96101      {
     102        if (frm.source) Forms.createHidden(frm, 'items', getSelectedItems().join(','));
     103        frm.memberType.disabled = false;
    97104        frm.submit();
    98105      }
     
    101108    function init()
    102109    {
     110      var frm = document.forms['list'];
    103111      <%
    104112      if (list == null)
    105113      {
    106114        %>
    107         var frm = document.forms['list'];
    108115        frm.name.focus();
    109116        frm.name.select();
     
    111118      }
    112119      %>
     120      if (frm.source)
     121      {
     122        if (getSelectedItems().length == 0)
     123        {
     124          frm.source[0].disabled = true;
     125          var aSelected = document.getElementById('source.selected');
     126          aSelected.href = 'javascript:void(0)';
     127          aSelected.disabled = true;
     128          Main.addClass(aSelected, 'disabled');
     129        }
     130        else
     131        {
     132          Forms.checkRadio(frm.source, 'selected');
     133        }
     134      }
    113135    }
    114136    function checkRadio(radio, name)
     
    116138      Forms.checkRadio(radio, name);
    117139    }
     140    function setAddToList(memberType, selectedOption)
     141    {
     142      var frm = document.forms['list'];
     143      if (memberType)
     144      {
     145        Forms.selectListOption(frm.memberType, memberType);
     146      }
     147      if (selectedOption)
     148      {
     149        Forms.checkRadio(frm.add, selectedOption);
     150      }
     151    }
     152    // Get the ID:s of all selected items
     153    var selectedItems = new Array();
     154    var hasCheckSelectedItems = false;
     155    function getSelectedItems()
     156    {
     157      <%
     158      if (formId != null)
     159      {
     160        %>
     161        if (!hasCheckSelectedItems)
     162        {
     163          selectedItems = window.opener.Table.getSelected('<%=formId%>');
     164          hasCheckedSelectedItems = true;
     165        }
     166        <%
     167      }
     168      %>
     169      return selectedItems;
     170    }
     171   
    118172    </script>
    119173  </base:head>
     
    122176    <form action="index.jsp?ID=<%=ID%>" method="post" name="list" onsubmit="return false;">
    123177    <input type="hidden" name="cmd" value="UpdateItem">
     178    <input type="hidden" name="fromContext" value="<%=fromContext%>">
     179    <%
     180    if (subContext != null)
     181    {
     182      %>
     183      <input type="hidden" name="subContext" value="<%=subContext%>">
     184      <%
     185    }
     186    %>
    124187    <h3 class="docked"><%=title%> <base:help tabcontrol="settings"/></h3>
    125188    <t:tabcontrol id="settings" contentstyle="<%="height: "+(int)(scale*370)+"px;"%>"
     
    136199        <td class="prompt">Member type</td>
    137200        <td>
    138           <select name="memberType">
     201          <select name="memberType" <%=fromContext != null ? "disabled" : ""%>>
    139202            <option value="BIOSOURCE" <%=memberType == Item.BIOSOURCE ? "selected" : ""%>>Biosource
    140203            <option value="SAMPLE" <%=memberType == Item.SAMPLE ? "selected" : ""%>>Sample
     
    145208      </tr>
    146209      <%
    147       if (mergeLists)
     210      if (addBioMaterials)
     211      {
     212        boolean hasSourceOption = true;
     213        boolean hasPooledOption = true;
     214        boolean hasChildOption = true;
     215        Item childType = null;
     216        if (memberType == Item.BIOSOURCE)
     217        {
     218          hasPooledOption = false;
     219          childType = Item.SAMPLE;
     220        }
     221        else if (memberType == Item.SAMPLE)
     222        {
     223          childType = Item.EXTRACT;
     224        }
     225        else if (memberType == Item.EXTRACT)
     226        {
     227          childType = Item.LABELEDEXTRACT;
     228        }
     229        else if (memberType == Item.LABELEDEXTRACT)
     230        {
     231          hasChildOption = false;
     232        }
     233        %>
     234        <tr valign="top">
     235          <td class="prompt">Source biomaterials</td>
     236          <td>
     237            <input type="radio" name="source" value="selected"><a
     238              id="source.selected"
     239              href="javascript:checkRadio(document.forms['list'].source, 'selected');"
     240              >Selected items</a><br>
     241            <input type="radio" name="source" value="page"><a
     242              href="javascript:checkRadio(document.forms['list'].source, 'page');"
     243              >Current page</a><br>
     244            <input type="radio" name="source" value="all" checked><a
     245              href="javascript:checkRadio(document.forms['list'].source, 'all');"
     246              >All pages</a><br>
     247          </td>
     248        </tr>
     249        <tr>
     250          <td class="prompt">Add to list</td>
     251          <td>
     252            <%
     253            if (hasSourceOption)
     254            {
     255              %>
     256              <input type="radio" name="add" value="source" checked
     257                onchange="setAddToList('<%=memberType.name()%>')"><a
     258                href="javascript:setAddToList('<%=memberType.name()%>', 'source');"
     259                >Source items only</a><br>
     260              <%
     261            }
     262            if (hasPooledOption)
     263            {
     264              %>
     265              <input type="radio" name="add" value="pooled"
     266                onchange="setAddToList('<%=memberType.name()%>')"><a
     267                href="javascript:setAddToList('<%=memberType.name()%>', 'pooled');"
     268                >Source items + pooled children of same type (<%=memberType%>)</a><br>
     269              <%
     270            }
     271            if (hasChildOption)
     272            {
     273              %>
     274              <input type="radio" name="add" value="children"
     275                onchange="setAddToList('<%=childType.name()%>')"><a
     276                href="javascript:setAddToList('<%=childType.name()%>', 'children');"
     277                >Children of next type (<%=childType%>)</a><br>
     278              <% 
     279            }
     280            %>
     281          </td>
     282        </tr>
     283        <%
     284      }
     285      else if (mergeLists)
    148286      {
    149287        %>
  • trunk/www/biomaterials/lists/index.jsp

    r4705 r4712  
    2727  import="net.sf.basedb.core.DbControl"
    2828  import="net.sf.basedb.core.Item"
     29  import="net.sf.basedb.core.Type"
    2930  import="net.sf.basedb.core.Include"
     31  import="net.sf.basedb.core.BioMaterial"
    3032  import="net.sf.basedb.core.BioMaterialList"
    3133  import="net.sf.basedb.core.ItemQuery"
    3234  import="net.sf.basedb.core.Permission"
    3335  import="net.sf.basedb.core.ItemContext"
     36  import="net.sf.basedb.core.ItemResultIterator"
    3437  import="net.sf.basedb.core.ItemQuery"
    3538  import="net.sf.basedb.core.MultiPermissions"
     
    3740  import="net.sf.basedb.core.PermissionDeniedException"
    3841  import="net.sf.basedb.core.ItemAlreadyExistsException"
     42  import="net.sf.basedb.core.query.Restrictions"
     43  import="net.sf.basedb.core.query.Hql"
     44  import="net.sf.basedb.core.query.Expressions"
    3945  import="net.sf.basedb.util.RemovableUtil"
    4046  import="net.sf.basedb.util.ShareableUtil"
    4147  import="net.sf.basedb.util.OwnableUtil"
     48  import="net.sf.basedb.util.biomaterial.BioSourceToSampleTransformer"
     49  import="net.sf.basedb.util.biomaterial.PooledChildrenTransformer"
     50  import="net.sf.basedb.util.collections.CollectionTransformer"
     51  import="net.sf.basedb.util.biomaterial.ListUtil"
    4252  import="net.sf.basedb.clients.web.Base"
    4353  import="net.sf.basedb.clients.web.WebException"
     
    5060  import="java.util.ArrayList"
    5161  import="java.util.Collections"
     62  import="java.util.Arrays"
    5263%>
    5364<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
     
    133144      dc.saveItem(list);
    134145
     146      // Add individual items from selected from a list page
     147      String source = request.getParameter("source");
     148      String add = request.getParameter("add");
     149      if (source != null)
     150      {
     151        Item fromContext = Item.valueOf(request.getParameter("fromContext"));
     152        String subContext = Values.getString(request.getParameter("subContext"), "");
     153        ItemQuery<? extends BioMaterial> query =
     154          (ItemQuery<? extends BioMaterial>)sc.getCurrentContext(fromContext, subContext).getQuery();
     155        if ("all".equals(source))
     156        {
     157          query.setFirstResult(0);
     158          query.setMaxResults(-1);
     159        }
     160        else if ("selected".equals(source))
     161        {
     162          query.setFirstResult(0);
     163          query.setMaxResults(-1);
     164          Integer[] itemIds = Values.getInt(request.getParameter("items").split(","));
     165          query.restrict(
     166            Restrictions.in(
     167              Hql.property("id"),
     168              Expressions.parameter("_selected_")
     169            )
     170          );
     171          query.setParameter("_selected_", Arrays.asList(itemIds), Type.INT);     
     172        }
     173        // else -- no modifications to the query mean that we only get the current page
     174        List<? extends BioMaterial> sources = query.list(dc);
     175        CollectionTransformer transformer = null;
     176        if ("source".equals(add))
     177        {
     178          // No transformer needed
     179        }
     180        else if ("pooled".equals(add))
     181        {
     182          ListUtil.addToList(list, sources, null);
     183          ItemQuery<? extends BioMaterial> pooledQuery = list.getAllBioMaterials();
     184          transformer = new PooledChildrenTransformer(dc, pooledQuery);
     185        }
     186        else if ("children".equals(add))
     187        {
     188          if (memberType == Item.SAMPLE)
     189          {
     190            transformer = new BioSourceToSampleTransformer(dc);
     191          }
     192          // TODO -- other biomaterial types
     193        }
     194        ListUtil.addToList(list, sources, transformer);
     195      }
     196     
    135197      // Add items from other lists?
    136198      String create = Values.getStringOrNull(request.getParameter("create"));
  • trunk/www/biomaterials/samples/index.jsp

    r4587 r4712  
    345345    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
    346346  }
     347  else if ("CreateBioMaterialList".equals(cmd))
     348  {
     349    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
     350    final ItemQuery<Sample> query = Sample.getQuery();
     351    cc.configureQuery(query, true);
     352    cc.setQuery(query);
     353    redirect = "../lists/index.jsp?ID="+ID+"&cmd=NewItem&addBioMaterials=1&formId=samples&fromContext=SAMPLE";
     354  }
    347355  else
    348356  {
  • trunk/www/biomaterials/samples/list_samples.jsp

    r4613 r4712  
    197197      Table.presetOnChange('<%=ID%>', formId, '<%=itemType.name()%>', '<%=(String)cc.getObject("defaultColumns")%>');
    198198    }
     199    function newBioMaterialList()
     200    {
     201      var controller = Main.getController('BIOMATERIALLIST');
     202      Table.submitToPopup(formId, 'CreateBioMaterialList', controller.width, controller.height);
     203    }
    199204    </script>
    200205  </base:head>
     
    447452          title="Columns&hellip;"
    448453          tooltip="Show, hide and re-order columns"
     454        />
     455        <tbl:button
     456          image="add.png"
     457          onclick="newBioMaterialList()"
     458          title="New biomaterial list&hellip;"
     459          tooltip="Create a new biomaterial list from matching samples"
     460          visible="<%=sc.hasPermission(Permission.CREATE, Item.BIOMATERIALLIST)%>"
    449461        />
    450462        <tbl:button
Note: See TracChangeset for help on using the changeset viewer.