Changeset 4559


Ignore:
Timestamp:
Oct 3, 2008, 4:23:49 PM (15 years ago)
Author:
Martin Svensson
Message:

Fixes #1092 Add listing of child items that are the same type as the parent

Location:
trunk
Files:
7 edited

Legend:

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

    r4517 r4559  
    2727
    2828import net.sf.basedb.core.data.MeasuredBioMaterialData;
     29import net.sf.basedb.core.query.Expressions;
     30import net.sf.basedb.core.query.Hql;
     31import net.sf.basedb.core.query.Restrictions;
    2932
    3033/**
     
    292295    return BioMaterialEvent.getQuery(this);
    293296  }
     297 
     298  /**
     299    Get a query that returns all events where this biomaterial
     300    has been used as a source.
     301      @return An {@link ItemQuery} object
     302      @since 2.9
     303   */
     304  public ItemQuery<BioMaterialEvent> getPoolingEvents()
     305  {
     306    ItemQuery<BioMaterialEvent> query = new ItemQuery<BioMaterialEvent>(BioMaterialEvent.class, null);
     307    query.setDistinct(true);  // Otherwise, the join to sources can result in one copy for each source
     308    query.joinPermanent(Hql.leftJoin("sources", "src"));
     309    query.joinPermanent(Hql.leftJoin("bioMaterial", "bm"));
     310    query.restrictPermanent(
     311      Restrictions.and(
     312        Restrictions.eq(
     313          Hql.index("src", null),
     314          Hql.entity(this)
     315        ),
     316        Restrictions.eq(
     317          Hql.property("bm", "pooled"),
     318          Expressions.integer(1)
     319        ),
     320        Restrictions.eq(
     321          Hql.property("eventType"),
     322          Expressions.integer(BioMaterialEvent.Type.CREATION.getValue())
     323        )
     324      )
     325    );
     326    return query;   
     327  }
    294328}
    295 
  • trunk/src/test/TestExtract.java

    r4514 r4559  
    6060    test_list_sources(id3, 1); // The sample
    6161    test_list_sources(id4, 3); // The three id, id2 and id3 extracts
     62    test_list_pooledChildren(id2, 1);
    6263
    6364    if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter();
     
    401402    }
    402403  }
    403 
     404  static void test_list_pooledChildren(int extractId, int expectedResults)
     405  {
     406    if (extractId == 0) return;
     407    DbControl dc = null;
     408    try
     409    {
     410      dc = TestUtil.getDbControl();
     411      Extract e = Extract.getById(dc, extractId);
     412      ItemResultList<BioMaterialEvent> ev = e.getPoolingEvents().list(dc);
     413     
     414      for (int i=0; i<ev.size(); i++)
     415      {
     416        write_item(i, (Extract)ev.get(i).getBioMaterial());
     417      }
     418      if (expectedResults >= 0 && expectedResults != ev.size())
     419      {
     420        throw new BaseException("Expected "+expectedResults+" results, not "+ev.size());
     421      }
     422      write("--List pools from extract OK ("+ev.size()+")");
     423    }
     424    catch (Throwable ex)
     425    {
     426      write("--List pools for extract FAILED");
     427      ex.printStackTrace();
     428      ok = false;
     429    }
     430    finally
     431    {
     432      if (dc != null) dc.close();
     433    }
     434  }
    404435}
  • trunk/src/test/TestLabeledExtract.java

    r4514 r4559  
    6060    test_list_sources(id3, 1); // The extract
    6161    test_list_sources(id4, 3); // The three id, id2 and id3 labeled extracts
     62    test_list_pooledChildren(id2, 1);
    6263    test_list_hybridizations(id, 0);
    6364
     
    440441    }
    441442  }
    442 
     443 
     444  static void test_list_pooledChildren(int labeledExtractId, int expectedResults)
     445  {
     446    if (labeledExtractId == 0) return;
     447    DbControl dc = null;
     448    try
     449    {
     450      dc = TestUtil.getDbControl();
     451      LabeledExtract le = LabeledExtract.getById(dc, labeledExtractId);
     452      ItemResultList<BioMaterialEvent> ev = le.getPoolingEvents().list(dc);
     453     
     454      for (int i=0; i<ev.size(); i++)
     455      {
     456        write_item(i, (LabeledExtract)ev.get(i).getBioMaterial());
     457      }
     458      if (expectedResults >= 0 && expectedResults != ev.size())
     459      {
     460        throw new BaseException("Expected "+expectedResults+" results, not "+ev.size());
     461      }
     462      write("--List pools from labeledExtract OK ("+ev.size()+")");
     463    }
     464    catch (Throwable ex)
     465    {
     466      write("--List pools for labeled extract FAILED");
     467      ex.printStackTrace();
     468      ok = false;
     469    }
     470    finally
     471    {
     472      if (dc != null) dc.close();
     473    }
     474  }
    443475}
  • trunk/src/test/TestSample.java

    r4514 r4559  
    5959    write_sources_header();
    6060    test_list_sources(id4, 3); // The three id, id2 and id3 samples
     61    test_list_pooledChildren(id2, 1);
    6162
    6263    // Standard test: Delete
     
    400401    }
    401402  }
    402 
     403  static void test_list_pooledChildren(int sampleId, int expectedResults)
     404  {
     405    if (sampleId == 0) return;
     406    DbControl dc = null;
     407    try
     408    {
     409      dc = TestUtil.getDbControl();
     410      Sample s = Sample.getById(dc, sampleId);
     411      ItemResultList<BioMaterialEvent> ev = s.getPoolingEvents().list(dc);
     412     
     413      for (int i=0; i<ev.size(); i++)
     414      {
     415        write_item(i, (Sample)ev.get(i).getBioMaterial());
     416      }
     417      if (expectedResults >= 0 && expectedResults != ev.size())
     418      {
     419        throw new BaseException("Expected "+expectedResults+" results, not "+ev.size());
     420      }
     421      write("--List pools from sample OK ("+ev.size()+")");
     422    }
     423    catch (Throwable ex)
     424    {
     425      write("--List pools for sample FAILED");
     426      ex.printStackTrace();
     427      ok = false;
     428    }
     429    finally
     430    {
     431      if (dc != null) dc.close();
     432    }
     433  }
    403434}
  • trunk/www/biomaterials/extracts/view_extract.jsp

    r4539 r4559  
    388388
    389389      // Extracts this item is pooled in.
    390       Set<ItemProxy> childSet = extract.getUsingItems();
    391       List<Extract> childExtracts = new ArrayList<Extract>();
    392       for (ItemProxy itemProxy : childSet)
    393       {
    394         if (Item.EXTRACT.equals(itemProxy.getType()))
    395         {
    396           childExtracts.add((Extract)itemProxy.getItem(dc));
    397         }
    398       }
    399       if (childExtracts.size() == 0)
     390      ItemQuery<BioMaterialEvent> poolingQuery = extract.getPoolingEvents();
     391      ItemResultList<BioMaterialEvent> poolingEvents = poolingQuery.list(dc);
     392      if (poolingEvents.size() == 0)
    400393      {
    401394        %>
     
    434427            <tbl:rows>
    435428            <%
    436             for (Extract e : childExtracts)
     429            for (BioMaterialEvent poolEvt : poolingEvents)
    437430            {
    438               BioMaterialEvent poolEvent = e.getCreationEvent();
    439               ItemQuery<Extract> extractsQuery = (ItemQuery<Extract>)poolEvent.getSources();
     431              ItemQuery<Extract> extractsQuery = (ItemQuery<Extract>)poolEvt.getSources();
     432              Extract child = (Extract)poolEvt.getBioMaterial();
    440433              extractsQuery.include(Include.ALL);
    441434              extractsQuery.order(Orders.asc(Hql.property("name")));
     
    447440                    image="deleted.gif"
    448441                    tooltip="This item has been scheduled for deletion"
    449                     visible="<%=e.isRemoved()%>"
     442                    visible="<%=child.isRemoved()%>"
    450443                  />
    451                   <%=Base.getLinkedName(ID, e, false, true)%>
     444                  <%=Base.getLinkedName(ID, child, false, true)%>
    452445                </tbl:cell>
    453                 <tbl:cell column="quantity"><%=Values.formatNumber(e.getOriginalQuantity(), 2)%></tbl:cell>
     446                <tbl:cell column="quantity"><%=Values.formatNumber(child.getOriginalQuantity(), 2)%></tbl:cell>
    454447                <tbl:cell column="parents">
    455448                <%
     
    462455                  else                   
    463456                    out.write(Base.getLinkedName(ID, parent, false, true));
    464                   out.write("[" + Values.formatNumber(poolEvent.getUsedQuantity(parent), 2) + "µg]");
     457                  out.write("[" + Values.formatNumber(poolEvt.getUsedQuantity(parent), 2) + "µg]");
    465458                  separator = ", ";
    466459                }
    467460                %>
    468461                </tbl:cell>
    469                 <tbl:cell column="description"><%=HTML.encodeTags(e.getDescription())%></tbl:cell>
     462                <tbl:cell column="description"><%=HTML.encodeTags(child.getDescription())%></tbl:cell>
    470463              </tbl:row>
    471464              <%
  • trunk/www/biomaterials/labeledextracts/view_labeledextract.jsp

    r4510 r4559  
    3939  import="net.sf.basedb.core.Protocol"
    4040  import="net.sf.basedb.core.User"
     41  import="net.sf.basedb.core.ItemProxy"
    4142  import="net.sf.basedb.core.ItemQuery"
    4243  import="net.sf.basedb.core.ItemResultIterator"
    4344  import="net.sf.basedb.core.ItemResultList"
    4445  import="net.sf.basedb.core.Include"
     46  import="net.sf.basedb.core.MeasuredBioMaterial"
    4547  import="net.sf.basedb.core.MultiPermissions"
    4648  import="net.sf.basedb.core.query.Orders"
     
    5759  import="net.sf.basedb.util.formatter.Formatter"
    5860  import="net.sf.basedb.clients.web.formatter.FormatterFactory"
     61  import="java.util.ArrayList"
    5962  import="java.util.Collections"
    6063  import="java.util.Date"
     
    387390        <%
    388391      }
     392     
     393      // Pooled item this labeled extract is used in.     
     394      ItemQuery<BioMaterialEvent> poolingQuery = extract.getPoolingEvents();
     395      ItemResultList<BioMaterialEvent> poolingEvents = poolingQuery.list(dc);
     396      if (poolingEvents.size() == 0)
     397      {
     398        %>
     399        <h4>Pooled in labeled extracts</h4>
     400        No labeled extracts have been pooled from this labeled extract
     401        (or, you don't have permission to view them).
     402        <%
     403      }
     404      else
     405      {
     406        %>
     407        <h4 class="docked">Pooled in labeled extracts</h4>
     408        <tbl:table
     409          id="poolChilds"
     410          clazz="itemlist"
     411          columns="all"
     412          >
     413          <tbl:columndef
     414            id="name"
     415            title="Name"
     416          />
     417          <tbl:columndef
     418            id="quantity"
     419            title="Original quantity (µg)"
     420          />
     421          <tbl:columndef
     422            id="parents"
     423            title="Used labeled extracts [quantity]"
     424          />
     425          <tbl:columndef
     426            id="description"
     427            title="Description"
     428          />
     429          <tbl:data>
     430            <tbl:columns></tbl:columns>
     431            <tbl:rows>
     432            <%
     433            for (BioMaterialEvent poolEvt : poolingEvents)
     434            {
     435              LabeledExtract ch = (LabeledExtract)poolEvt.getBioMaterial();
     436              ItemQuery<LabeledExtract> extractsQuery = (ItemQuery<LabeledExtract>)poolEvt.getSources();
     437              extractsQuery.include(Include.ALL);
     438              extractsQuery.order(Orders.asc(Hql.property("name")));
     439              ItemResultList<LabeledExtract> parentExtracts = extractsQuery.list(dc);
     440              %>
     441              <tbl:row>
     442                <tbl:cell column="name">
     443                  <base:icon
     444                    image="deleted.gif"
     445                    tooltip="This item has been scheduled for deletion"
     446                    visible="<%=ch.isRemoved() %>"
     447                  />
     448                  <%=Base.getLinkedName(ID, ch, false, true)%>
     449                </tbl:cell>
     450                <tbl:cell column="quantity"><%=Values.formatNumber(ch.getOriginalQuantity(), 2)%></tbl:cell>
     451                <tbl:cell column="parents">
     452                <%
     453                String separator = "";
     454                for (LabeledExtract parent : parentExtracts)
     455                {
     456                  out.write(separator);
     457                  if (parent.equals(extract))
     458                      out.write(HTML.encodeTags(parent.getName()));
     459                  else                   
     460                    out.write(Base.getLinkedName(ID, parent, false, true));
     461                  out.write("[" + Values.formatNumber(poolEvt.getUsedQuantity(parent), 2) + "µg]");
     462                  separator = ", ";
     463                }
     464                %>
     465                </tbl:cell>
     466                <tbl:cell column="description"><%=HTML.encodeTags(ch.getDescription())%></tbl:cell>
     467              </tbl:row>
     468              <%
     469            }
     470            %>           
     471            </tbl:rows>
     472          </tbl:data>
     473        </tbl:table>
     474        <%
     475      }
     476     
    389477      ItemQuery<Hybridization> hybridizationQuery = extract.getHybridizations();
    390478      hybridizationQuery.include(Include.ALL);
  • trunk/www/biomaterials/samples/view_sample.jsp

    r4539 r4559  
    373373     
    374374      // Samples this item is pooled in.
    375       Set<ItemProxy> childSet = sample.getUsingItems();
    376       List<Sample> childSamples = new ArrayList<Sample>();
    377       for (ItemProxy itemProxy : childSet)
    378       {
    379         if (Item.SAMPLE.equals(itemProxy.getType()))
    380         {
    381           childSamples.add((Sample)itemProxy.getItem(dc));
    382         }
    383       }
    384       if (childSamples.size() == 0)
     375      ItemQuery<BioMaterialEvent> poolingQuery = sample.getPoolingEvents();
     376      ItemResultList<BioMaterialEvent> poolingEvents = poolingQuery.list(dc);
     377      if (poolingEvents.size() == 0)
    385378      {
    386379        %>
     
    419412            <tbl:rows>
    420413            <%
    421             for (Sample s : childSamples)
     414            for (BioMaterialEvent poolingEvt : poolingEvents)
    422415            {
    423               BioMaterialEvent poolEvent = s.getCreationEvent();
    424               ItemQuery<Sample> samplesQuery = (ItemQuery<Sample>)poolEvent.getSources();
     416              Sample child = (Sample)poolingEvt.getBioMaterial();
     417              ItemQuery<Sample> samplesQuery = (ItemQuery<Sample>)poolingEvt.getSources();
    425418              samplesQuery.include(Include.ALL);
    426419              samplesQuery.order(Orders.asc(Hql.property("name")));
     
    432425                    image="deleted.gif"
    433426                    tooltip="This item has been scheduled for deletion"
    434                     visible="<%=s.isRemoved()%>"
     427                    visible="<%=child.isRemoved()%>"
    435428                  />
    436                   <%=Base.getLinkedName(ID, s, false, true)%>
     429                  <%=Base.getLinkedName(ID, child, false, true)%>
    437430                </tbl:cell>
    438                 <tbl:cell column="quantity"><%=Values.formatNumber(s.getOriginalQuantity(), 2)%></tbl:cell>
     431                <tbl:cell column="quantity"><%=Values.formatNumber(child.getOriginalQuantity(), 2)%></tbl:cell>
    439432                <tbl:cell column="parents">
    440433                <%
     
    447440                  else                   
    448441                    out.write(Base.getLinkedName(ID, parent, false, true));
    449                   out.write("[" + Values.formatNumber(poolEvent.getUsedQuantity(parent), 2) + "µg]");
     442                  out.write("[" + Values.formatNumber(poolingEvt.getUsedQuantity(parent), 2) + "µg]");
    450443                  separator = ", ";
    451444                }
    452445                %>
    453446                </tbl:cell>
    454                 <tbl:cell column="description"><%=HTML.encodeTags(s.getDescription())%></tbl:cell>
     447                <tbl:cell column="description"><%=HTML.encodeTags(child.getDescription())%></tbl:cell>
    455448              </tbl:row>
    456449              <%
Note: See TracChangeset for help on using the changeset viewer.