Changeset 6090


Ignore:
Timestamp:
Aug 22, 2012, 1:36:15 PM (11 years ago)
Author:
Nicklas Nordborg
Message:

References #1707: Make it possible for a derived bioassay to have multiple physical bioassays as parents

The 'Item overview' should now load parent items correctly. Validation need to be fixed (some parts have been disabled at the moment).

Location:
trunk/src/core/net/sf/basedb/util/overview
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/util/overview/Node.java

    r6046 r6090  
    286286 
    287287  /**
     288    Find all nodes in the parent chain that matches the given
     289    filter.
     290    @param filter The filter to use for matching nodes
     291    @return A list with the matched nodes in the order they matched
     292    @since 3.2
     293  */
     294  public List<Node> findAll(Filter<? super Node> filter)
     295  {
     296    List<Node> result = new LinkedList<Node>();
     297    Node current = this;
     298    while (current != null)
     299    {
     300      if (filter.evaluate(current)) result.add(current);
     301      current = current.getParent();
     302    }
     303    return result;
     304  }
     305 
     306  /**
    288307    Get all child nodes of this node. May return null or an empty
    289308    list if this node has no children.
  • trunk/src/core/net/sf/basedb/util/overview/loader/DerivedBioAssayLoader.java

    r5807 r6090  
    2121*/
    2222package net.sf.basedb.util.overview.loader;
     23
     24import java.util.HashSet;
     25import java.util.List;
     26import java.util.Set;
    2327
    2428import net.sf.basedb.core.DbControl;
     
    3135import net.sf.basedb.core.PhysicalBioAssay;
    3236import net.sf.basedb.core.RawBioAssay;
     37import net.sf.basedb.core.Type;
     38import net.sf.basedb.core.query.Expressions;
    3339import net.sf.basedb.core.query.Hql;
    3440import net.sf.basedb.core.query.Restrictions;
     41import net.sf.basedb.util.overview.Fix;
    3542import net.sf.basedb.util.overview.Node;
    3643import net.sf.basedb.util.overview.NodeAttribute;
    3744import net.sf.basedb.util.overview.OverviewContext;
    38 import net.sf.basedb.util.overview.cache.IndexedCacheKey;
    39 import net.sf.basedb.util.overview.filter.HasAttributeFilter;
     45import net.sf.basedb.util.overview.OverviewUtil;
     46import net.sf.basedb.util.overview.Validator;
    4047import net.sf.basedb.util.overview.filter.ItemTypeFilter;
    4148import net.sf.basedb.util.overview.node.ChildNodeDirection;
     
    6673  {
    6774    Node node = super.createRootNode(dc, context, item);
    68     Extract extract = findExtract(null, item);
     75    Extract extract = getExtract(item);
    6976    if (extract != null) node.setAttribute(NodeAttribute.EXTRACT, extract);
    7077    return node;
     
    184191    ItemQuery<DerivedBioAssay> query = context.initQuery(bioAssay.getRootDerivedBioAssays(), "name");
    185192   
    186     // If there is an extract node in the parent chain we should only load
    187     // derived bioassays with the same or a null extract
    188     Node extractNode = bioAssayNode.getFirstNode(new ItemTypeFilter(Item.EXTRACT));
    189     Extract extract = null;
    190     if (extractNode != null)
    191     {
    192       extract = (Extract)extractNode.getItem();
    193       query.restrict(Restrictions.or(
    194           Restrictions.eq(Hql.property("extract"), Hql.entity(extract)),
    195           Restrictions.eq(Hql.property("extract"), null))
    196         );
    197     }
     193    // We should only load derived bioassays that has a null extract or an
     194    // extract that is found in the parent chain
     195    Set<Integer> extractIds = getExtractChain(bioAssayNode);
     196    if (extractIds.size() > 0)
     197    {
     198      query.restrict(
     199        Restrictions.or(
     200          Restrictions.eq(Hql.property("extract"), null),
     201          Restrictions.in(Hql.property("extract"), Expressions.parameter("extracts"))
     202        ));
     203      query.setParameter("extracts", extractIds, Type.INT);
     204    }
     205
    198206    ItemResultIterator<DerivedBioAssay> it = query.iterate(dc);
    199207    while (it.hasNext())
     
    204212        folderNode = new Node("bioassays", "Bioassays", bioAssayNode, ChildNodeDirection.FORWARD);
    205213      }
    206       Object cacheKey = new IndexedCacheKey(bioAssay, extract);
     214      Object cacheKey = extractIds.size() > 0 ? null : child;
    207215      createItemNode(nf, child, cacheKey, false, folderNode, ChildNodeDirection.FORWARD);
    208216    }
     
    222230    ItemQuery<DerivedBioAssay> query = context.initQuery(parent.getChildren(), "name");
    223231   
    224     // If there is an extract node in the parent chain we should only load
    225     // derived bioassays with the same or a null extract
    226     Node extractNode = parentNode.getFirstNode(new ItemTypeFilter(Item.EXTRACT));
    227     Extract extract = null;
    228     if (extractNode != null)
    229     {
    230       extract = (Extract)extractNode.getItem();
    231       query.restrict(Restrictions.or(
    232           Restrictions.eq(Hql.property("extract"), Hql.entity(extract)),
    233           Restrictions.eq(Hql.property("extract"), null))
    234         );
     232    // We should only load derived bioassays that has a null extract or an
     233    // extract that is found in the parent chain
     234    Set<Integer> extractIds = getExtractChain(parentNode);
     235    if (extractIds.size() > 0)
     236    {
     237      query.restrict(
     238        Restrictions.or(
     239          Restrictions.eq(Hql.property("extract"), null),
     240          Restrictions.in(Hql.property("extract"), Expressions.parameter("extracts"))
     241        ));
     242      query.setParameter("extracts", extractIds, Type.INT);
    235243    }
    236244
     
    243251        folderNode = new Node("bioassays", "Child bioassays", parentNode, ChildNodeDirection.FORWARD);
    244252      }
    245       Object cacheKey = new IndexedCacheKey(child, extract);
     253      Object cacheKey = extractIds.size() > 0 ? null : child;
    246254      createItemNode(nf, child, cacheKey, false, folderNode, ChildNodeDirection.FORWARD);
    247255    }
     
    263271    {
    264272      bioAssay = raw.getParentBioAssay();
    265       // We must clone/set the EXTRACT attribute so that upstream loaders
     273      // We must set the EXTRACT attribute so that upstream loaders
    266274      // follow the correct path when reaching the biomaterials section
    267       if (bioAssay != null)
    268       {
    269         extract = findExtract(rawNode, bioAssay);
    270       }
     275      if (bioAssay != null) extract = getExtract(bioAssay);
    271276    }
    272277    catch (PermissionDeniedException ex)
     
    274279      denied = true;
    275280    }
    276     Object cacheKey = new IndexedCacheKey(bioAssay, extract);
    277     Node bioAssayNode = createItemNode(nf, bioAssay, cacheKey, denied,
     281    Node bioAssayNode = createItemNode(nf, bioAssay, null, denied,
    278282        rawNode, ChildNodeDirection.REVERSE);
    279283    if (extract != null && bioAssayNode != null)
     
    291295  {
    292296    NodeFactory<DerivedBioAssay> nf = getNodeFactory(dc, context);
    293     DerivedBioAssay bioAssay = null;
     297   
     298    ItemQuery<DerivedBioAssay> query = context.initQuery(child.getParents(), "name");
     299    List<DerivedBioAssay> parents = query.list(dc);
     300   
     301    Node folderNode = null;
     302    Node bioAssayNode = null;
     303    if (parents.size() > 1)
     304    {
     305      folderNode = new Node("parents", "Parents", childNode, ChildNodeDirection.REVERSE);
     306    }
     307   
     308    for (DerivedBioAssay dba : parents)
     309    {
     310      // We need to handle circular references inline because
     311      // we can never require that a validator implementation handles this
     312      Node circularNode = OverviewUtil.getCircularRef(childNode, dba);
     313      if (circularNode != null)
     314      {
     315        context.createFailure(Validator.CIRCULAR_REFERENCE, childNode,
     316            "Circular reference to '" + dba.getName() + "'",
     317            new Fix("Modify parents of '" + child.getName() + "'", child),
     318            new Fix("Modify parents of '" + dba.getName() + "'", dba)
     319          );
     320      }
     321      else
     322      {
     323        Extract extract = getExtract(dba);
     324        bioAssayNode = createItemNode(nf, dba, null, false, folderNode == null ? childNode : folderNode, ChildNodeDirection.REVERSE);
     325        if (extract != null && bioAssayNode != null)
     326        {
     327          bioAssayNode.setAttribute(NodeAttribute.EXTRACT, extract);
     328        }
     329      }
     330    }
     331    postValidateFolder(nf, folderNode, childNode, bioAssayNode == null);
     332    return folderNode == null ? bioAssayNode : folderNode;
     333  }
     334
     335  /**
     336    Get the extract that is associated with the current derived bioassay
     337    @return The extract or null if none was found
     338  */
     339  private Extract getExtract(DerivedBioAssay bioAssay)
     340  {
    294341    Extract extract = null;
    295     boolean denied = false;
    296342    try
    297343    {
    298       bioAssay = child.getParent();
    299       // We must clone/set the EXTRACT attribute so that upstream loaders
    300       // follow the correct path when reaching the biomaterials section
    301       extract = findExtract(childNode, bioAssay);
     344      extract = bioAssay.getExtract();
    302345    }
    303346    catch (PermissionDeniedException ex)
    304     {
    305       denied = true;
    306     }
    307 
    308     Object cacheKey = new IndexedCacheKey(bioAssay, extract);
    309     Node bioAssayNode = createItemNode(nf, bioAssay, cacheKey, denied,
    310         childNode, ChildNodeDirection.REVERSE);
    311     if (extract != null && bioAssayNode != null)
    312     {
    313       bioAssayNode.setAttribute(NodeAttribute.EXTRACT, extract);
    314     }
    315     return bioAssayNode;
    316   }
    317 
    318   /**
    319     Try to locate the extract we are following by first
    320     checking if the node or any of it's parents have a
    321     {@link NodeAttribute#EXTRACT} attribute set. If not we
    322     check if the current bioassay has an extract.
    323     @return The extract or null if none was found
    324   */
    325   private Extract findExtract(Node node, DerivedBioAssay bioAssay)
    326   {
    327     Extract extract = null;
    328     Node nodeWithExtract = node == null ?
    329         null : node.getFirstNode(new HasAttributeFilter(NodeAttribute.EXTRACT));
    330     if (nodeWithExtract != null)
    331     {
    332       extract = nodeWithExtract.getAttribute(NodeAttribute.EXTRACT);
    333     }
    334     else
    335     {
    336       try
    337       {
    338         extract = bioAssay.getExtract();
    339       }
    340       catch (PermissionDeniedException ex)
    341       {}
    342     }
     347    {}
    343348    return extract;
    344349  }
     350 
     351  private Set<Integer> getExtractChain(Node node)
     352  {
     353    List<Node> extractNodes = node.findAll(new ItemTypeFilter(Item.EXTRACT));
     354    Set<Integer> extractIds = new HashSet<Integer>(extractNodes.size());
     355    for (Node n : extractNodes)
     356    {
     357      extractIds.add(n.getItem().getId());
     358    }
     359    return extractIds;
     360  }
     361 
    345362}
  • trunk/src/core/net/sf/basedb/util/overview/loader/ExtractLoader.java

    r6043 r6090  
    2121*/
    2222package net.sf.basedb.util.overview.loader;
     23
     24import java.util.HashSet;
     25import java.util.List;
     26import java.util.Set;
    2327
    2428import net.sf.basedb.core.BioMaterialList;
     
    3438import net.sf.basedb.core.RawBioAssay;
    3539import net.sf.basedb.core.Sample;
     40import net.sf.basedb.core.Type;
    3641import net.sf.basedb.core.query.Expressions;
    3742import net.sf.basedb.core.query.Hql;
     
    307312    Node folderNode = null;
    308313    NodeFactory<Extract> nf = getNodeFactory(dc, context);
    309  
    310     // If we have followed this path from an item that has specified an extract
    311     // we should only load that extract when following the path upstreams
    312     Extract extract = null;
    313     Node nodeWithExtract = bioAssayNode.getFirstNode(new HasAttributeFilter(NodeAttribute.EXTRACT));
    314     if (nodeWithExtract != null)
    315     {
    316       extract = nodeWithExtract.getAttribute(NodeAttribute.EXTRACT);
    317     }
    318    
     314
    319315    ItemQuery<Extract> query = context.initQuery(bioAssay.getExtracts(0), "name");
    320     if (extract != null)
    321     {
    322       query.restrict(Restrictions.eq(Hql.property("id"), Expressions.integer(extract.getId())));
     316
     317    // If we have followed this path from derived bioassays that has specified an
     318    // extract we should only load the same extracts when following the path upstreams
     319    Set<Integer> extractIds = getExtractChain(bioAssayNode);
     320    if (extractIds.size() > 0)
     321    {
     322      query.restrict(
     323        Restrictions.in(Hql.property("id"), Expressions.parameter("extracts"))
     324        );
     325      query.setParameter("extracts", extractIds, Type.INT);
    323326    }
    324327    ItemResultIterator<Extract> it = query.iterate(dc);
    325328    while (it.hasNext())
    326329    {
    327       extract = it.next();
     330      Extract extract = it.next();
    328331      if (folderNode == null)
    329332      {
     
    332335      createItemNode(nf, extract, extract, false, folderNode, ChildNodeDirection.REVERSE);
    333336    }
    334     postValidateFolder(nf, folderNode, bioAssayNode, nodeWithExtract == null);
     337    postValidateFolder(nf, folderNode, bioAssayNode, extractIds.size() > 0);
    335338    return folderNode;
    336339  }
     
    442445   
    443446  }
     447 
     448  private Set<Integer> getExtractChain(Node node)
     449  {
     450    List<Node> nodesWithExtract = node.findAll(new HasAttributeFilter(NodeAttribute.EXTRACT));
     451    Set<Integer> extractIds = new HashSet<Integer>(nodesWithExtract.size());
     452    for (Node n : nodesWithExtract)
     453    {
     454      extractIds.add(n.getAttribute(NodeAttribute.EXTRACT).getId());
     455    }
     456    return extractIds;
     457  }
     458 
    444459}
  • trunk/src/core/net/sf/basedb/util/overview/loader/PhysicalBioAssayLoader.java

    r5985 r6090  
    2121*/
    2222package net.sf.basedb.util.overview.loader;
     23
     24import java.util.List;
    2325
    2426import net.sf.basedb.core.ArraySlide;
     
    3335import net.sf.basedb.core.PermissionDeniedException;
    3436import net.sf.basedb.util.overview.Node;
    35 import net.sf.basedb.util.overview.NodeAttribute;
    3637import net.sf.basedb.util.overview.OverviewContext;
    3738import net.sf.basedb.util.overview.OverviewUtil;
    3839import net.sf.basedb.util.overview.cache.IndexedCacheKey;
    39 import net.sf.basedb.util.overview.filter.HasAttributeFilter;
    4040import net.sf.basedb.util.overview.node.ChildNodeDirection;
    4141import net.sf.basedb.util.overview.node.NameableNameGenerator;
     
    103103  {
    104104    NodeFactory<PhysicalBioAssay> nf = getNodeFactory(dc, context);
    105     PhysicalBioAssay bioAssay = null;
    106     Extract extract = null;
    107     boolean denied = false;
    108     try
    109     {
    110       DerivedBioAssay bas = (DerivedBioAssay)derivedBioAssayNode.getItem(dc);
    111       bioAssay = bas.getPhysicalBioAssay();
    112       // We must clone/set the EXTRACT attribute so that upstream loaders
    113       // follow the correct path when reaching the biomaterials section
    114       Node nodeWithExtract = derivedBioAssayNode.getFirstNode(new HasAttributeFilter(NodeAttribute.EXTRACT));
    115       if (nodeWithExtract != null)
    116       {
    117         extract = nodeWithExtract.getAttribute(NodeAttribute.EXTRACT);
    118       }
    119     }
    120     catch (PermissionDeniedException ex)
    121     {
    122       denied = true;
    123     }
    124     Object cacheKey = new IndexedCacheKey(bioAssay, extract);
    125     Node bioAssayNode = createItemNode(nf, bioAssay, cacheKey, denied, derivedBioAssayNode, ChildNodeDirection.REVERSE);
    126     if (extract != null && bioAssayNode != null)
    127     {
    128       bioAssayNode.setAttribute(NodeAttribute.EXTRACT, extract);
    129     }
    130     return bioAssayNode;
     105    DerivedBioAssay dba = (DerivedBioAssay)derivedBioAssayNode.getItem(dc);
     106   
     107    ItemQuery<PhysicalBioAssay> query = context.initQuery(dba.getPhysicalBioAssays(), "name");
     108    List<PhysicalBioAssay> parents = query.list(dc);
     109
     110    Node folderNode = null;
     111    Node bioAssayNode = null;
     112    if (parents.size() > 1)
     113    {
     114      folderNode = new Node("parents", "Physical bioassays", derivedBioAssayNode, ChildNodeDirection.REVERSE);
     115    }
     116
     117    for (PhysicalBioAssay pba : parents)
     118    {
     119      bioAssayNode = createItemNode(nf, pba, null, false, folderNode == null ? derivedBioAssayNode : folderNode, ChildNodeDirection.REVERSE);
     120    }
     121    postValidateFolder(nf, folderNode, derivedBioAssayNode, bioAssayNode == null);
     122    return folderNode == null ? bioAssayNode : folderNode;
    131123  }
    132124 
     
    230222    {
    231223      PhysicalBioAssay bioAssay = it.next();
    232       int position = bioAssay.getCreationEvent().getEventSource(extract).getPosition();
    233       Object cacheKey = position == 0 ? bioAssay : new IndexedCacheKey(bioAssay, position);
     224      Object cacheKey = new IndexedCacheKey(bioAssay, extract);
    234225      if (folderNode == null)
    235226      {
  • trunk/src/core/net/sf/basedb/util/overview/validator/DerivedBioAssayValidator.java

    r5807 r6090  
    111111      }
    112112    }
    113    
     113    /*
    114114    // Check that parent items make sense
    115115    DerivedBioAssay bioAssay = (DerivedBioAssay)node.getItem(dc);
     
    158158      }
    159159    }
     160    */
    160161  }
    161162
Note: See TracChangeset for help on using the changeset viewer.