src/core/net/sf/basedb/util/overview/loader/BioWellLoader.java

Code
Comments
Other
Rev Date Author Line
6044 03 Apr 12 nicklas 1 /**
6044 03 Apr 12 nicklas 2   $Id$
6044 03 Apr 12 nicklas 3
6044 03 Apr 12 nicklas 4   Copyright (C) 2012 Nicklas Nordborg
6044 03 Apr 12 nicklas 5
6044 03 Apr 12 nicklas 6   This file is part of BASE - BioArray Software Environment.
6044 03 Apr 12 nicklas 7   Available at http://base.thep.lu.se/
6044 03 Apr 12 nicklas 8
6044 03 Apr 12 nicklas 9   BASE is free software; you can redistribute it and/or
6044 03 Apr 12 nicklas 10   modify it under the terms of the GNU General Public License
6044 03 Apr 12 nicklas 11   as published by the Free Software Foundation; either version 3
6044 03 Apr 12 nicklas 12   of the License, or (at your option) any later version.
6044 03 Apr 12 nicklas 13
6044 03 Apr 12 nicklas 14   BASE is distributed in the hope that it will be useful,
6044 03 Apr 12 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
6044 03 Apr 12 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6044 03 Apr 12 nicklas 17   GNU General Public License for more details.
6044 03 Apr 12 nicklas 18
6044 03 Apr 12 nicklas 19   You should have received a copy of the GNU General Public License
6044 03 Apr 12 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
6044 03 Apr 12 nicklas 21 */
6044 03 Apr 12 nicklas 22 package net.sf.basedb.util.overview.loader;
6044 03 Apr 12 nicklas 23
6044 03 Apr 12 nicklas 24 import net.sf.basedb.core.BioPlate;
6044 03 Apr 12 nicklas 25 import net.sf.basedb.core.BioWell;
6044 03 Apr 12 nicklas 26 import net.sf.basedb.core.DbControl;
6044 03 Apr 12 nicklas 27 import net.sf.basedb.core.Extract;
6044 03 Apr 12 nicklas 28 import net.sf.basedb.core.Item;
6044 03 Apr 12 nicklas 29 import net.sf.basedb.core.ItemQuery;
6044 03 Apr 12 nicklas 30 import net.sf.basedb.core.ItemResultIterator;
6044 03 Apr 12 nicklas 31 import net.sf.basedb.core.MeasuredBioMaterial;
6044 03 Apr 12 nicklas 32 import net.sf.basedb.core.PermissionDeniedException;
6044 03 Apr 12 nicklas 33 import net.sf.basedb.core.Sample;
6044 03 Apr 12 nicklas 34 import net.sf.basedb.core.query.Hql;
6044 03 Apr 12 nicklas 35 import net.sf.basedb.core.query.Orders;
6044 03 Apr 12 nicklas 36 import net.sf.basedb.util.overview.Node;
6044 03 Apr 12 nicklas 37 import net.sf.basedb.util.overview.OverviewContext;
6044 03 Apr 12 nicklas 38 import net.sf.basedb.util.overview.node.BioWellNameGenerator;
6044 03 Apr 12 nicklas 39 import net.sf.basedb.util.overview.node.ChildNodeDirection;
6044 03 Apr 12 nicklas 40 import net.sf.basedb.util.overview.node.NodeFactory;
6044 03 Apr 12 nicklas 41 import net.sf.basedb.util.overview.validator.NodeValidator;
6044 03 Apr 12 nicklas 42
6044 03 Apr 12 nicklas 43 /**
6044 03 Apr 12 nicklas 44   Node loader implementation that load biomaterial on a bioplate.
6044 03 Apr 12 nicklas 45   This loader is only user in the forward-loading direction from a
6044 03 Apr 12 nicklas 46   bioplate to the biomaterial. Note that the loader returns biomaterial nodes,
6044 03 Apr 12 nicklas 47   NOT biowell nodes. It can be used as a root node or in the reverse direction.
6044 03 Apr 12 nicklas 48   
6044 03 Apr 12 nicklas 49   @author nicklas
6044 03 Apr 12 nicklas 50   @since 3.2
6044 03 Apr 12 nicklas 51 */
6044 03 Apr 12 nicklas 52 public class BioWellLoader 
6044 03 Apr 12 nicklas 53   extends BasicItemNodeLoader<MeasuredBioMaterial> 
6044 03 Apr 12 nicklas 54 {
6044 03 Apr 12 nicklas 55   
6044 03 Apr 12 nicklas 56   public BioWellLoader()
6044 03 Apr 12 nicklas 57   {
6044 03 Apr 12 nicklas 58     super(Item.BIOWELL, DENY_ROOT_NODE, null);
6044 03 Apr 12 nicklas 59   }
6044 03 Apr 12 nicklas 60
6044 03 Apr 12 nicklas 61   
6044 03 Apr 12 nicklas 62   /*
6044 03 Apr 12 nicklas 63     From the NodeLoader interface
6044 03 Apr 12 nicklas 64     ------------------------------
6044 03 Apr 12 nicklas 65   */
6044 03 Apr 12 nicklas 66   /**
6044 03 Apr 12 nicklas 67     Create forward-loading biomaterial nodes from a given bioplate
6044 03 Apr 12 nicklas 68     The biomaterial nodes are created directly as child nodes to the
6044 03 Apr 12 nicklas 69     bioplate. No intermediate folder node is used so this method return 
6044 03 Apr 12 nicklas 70     null.
6044 03 Apr 12 nicklas 71     @return Always null (but child nodes are added to the parent node)
6044 03 Apr 12 nicklas 72   */
6044 03 Apr 12 nicklas 73   @Override
6044 03 Apr 12 nicklas 74   public Node createForwardNode(DbControl dc, OverviewContext context, Node parentNode)
6044 03 Apr 12 nicklas 75   {
6044 03 Apr 12 nicklas 76     Node returnNode = null;
6044 03 Apr 12 nicklas 77     Item parentType = parentNode.getItemType();
6044 03 Apr 12 nicklas 78     if (parentType == Item.BIOPLATE)
6044 03 Apr 12 nicklas 79     {
6214 13 Dec 12 nicklas 80       returnNode = createForwardNode((BioPlate)parentNode.getItem(dc), dc, context, parentNode);
6044 03 Apr 12 nicklas 81     }
6044 03 Apr 12 nicklas 82     return returnNode;
6044 03 Apr 12 nicklas 83   }
6044 03 Apr 12 nicklas 84   
6044 03 Apr 12 nicklas 85   /**
6044 03 Apr 12 nicklas 86     Delegate loading of child nodes to the actual item loader that this node
6044 03 Apr 12 nicklas 87     is representing.
6044 03 Apr 12 nicklas 88   */
6044 03 Apr 12 nicklas 89   @Override
6044 03 Apr 12 nicklas 90   public boolean loadChildNodes(DbControl dc, OverviewContext context, Node node)
6044 03 Apr 12 nicklas 91   {
6044 03 Apr 12 nicklas 92     return getNodeLoader(context, node.getItemType()).loadChildNodes(dc, context, node);
6044 03 Apr 12 nicklas 93   }
6044 03 Apr 12 nicklas 94   // --------------------------------
6044 03 Apr 12 nicklas 95
6044 03 Apr 12 nicklas 96   /**
6044 03 Apr 12 nicklas 97     Create all-loading biomaterial nodes for each non-empty well on the bioplate.
6044 03 Apr 12 nicklas 98   */
6044 03 Apr 12 nicklas 99   @SuppressWarnings("unchecked")
6214 13 Dec 12 nicklas 100   private Node createForwardNode(BioPlate plate, DbControl dc, OverviewContext context, Node plateNode)
6044 03 Apr 12 nicklas 101   {
6044 03 Apr 12 nicklas 102     // Name generate that prefixes each name with the well coordinate
6044 03 Apr 12 nicklas 103     BioWellNameGenerator nameGenerator = new BioWellNameGenerator();
6214 13 Dec 12 nicklas 104     Node folderNode = null;
6044 03 Apr 12 nicklas 105     
6044 03 Apr 12 nicklas 106     // Validator and node factory for wells with samples
6044 03 Apr 12 nicklas 107     NodeValidator<Sample> sampleNodeValidator = (NodeValidator<Sample>)getNodeValidatorFactory(context).createNodeValidator(Item.SAMPLE);
6044 03 Apr 12 nicklas 108     NodeFactory<Sample> sampleNodeFactory = new NodeFactory<Sample>(dc, context, sampleNodeValidator, nameGenerator);
6044 03 Apr 12 nicklas 109
6044 03 Apr 12 nicklas 110     // Validator and node factory for wells with extracts
6044 03 Apr 12 nicklas 111     NodeValidator<Extract> extractNodeValidator = (NodeValidator<Extract>)getNodeValidatorFactory(context).createNodeValidator(Item.EXTRACT);
6044 03 Apr 12 nicklas 112     NodeFactory<Extract> extractNodeFactory = new NodeFactory<Extract>(dc, context, extractNodeValidator, nameGenerator);
6044 03 Apr 12 nicklas 113
6044 03 Apr 12 nicklas 114     // Find all used wells
6044 03 Apr 12 nicklas 115     ItemQuery<BioWell> query = context.initQuery(plate.getBioWells(), Orders.asc(Hql.property("row")), Orders.asc(Hql.property("column")));
6044 03 Apr 12 nicklas 116     query.join(Hql.innerJoin(null, "bioMaterial", "bm", true)); // Important to use inner join for filtering
6044 03 Apr 12 nicklas 117     ItemResultIterator<BioWell> it = query.iterate(dc);
6044 03 Apr 12 nicklas 118     while (it.hasNext())
6044 03 Apr 12 nicklas 119     {
6044 03 Apr 12 nicklas 120       BioWell well = it.next();
6044 03 Apr 12 nicklas 121       nameGenerator.setCurrentWell(well); // The name generator must know about the current well
6044 03 Apr 12 nicklas 122       MeasuredBioMaterial bioMaterial = null;
6044 03 Apr 12 nicklas 123       boolean denied = false;
6044 03 Apr 12 nicklas 124       try
6044 03 Apr 12 nicklas 125       {
6044 03 Apr 12 nicklas 126         bioMaterial = well.getBioMaterial();
6044 03 Apr 12 nicklas 127       }
6044 03 Apr 12 nicklas 128       catch (PermissionDeniedException ex)
6044 03 Apr 12 nicklas 129       {
6044 03 Apr 12 nicklas 130         denied = true;
6044 03 Apr 12 nicklas 131       }
6044 03 Apr 12 nicklas 132       
6214 13 Dec 12 nicklas 133       if (folderNode == null)
6214 13 Dec 12 nicklas 134       {
6214 13 Dec 12 nicklas 135         folderNode = new Node("biomaterial", "Biomaterial", plateNode, ChildNodeDirection.FORWARD);
6214 13 Dec 12 nicklas 136       }
6044 03 Apr 12 nicklas 137       if (well.getBioMaterialType() == Item.SAMPLE)
6044 03 Apr 12 nicklas 138       {
6044 03 Apr 12 nicklas 139         // Create sample node
6044 03 Apr 12 nicklas 140         Sample sample = (Sample)bioMaterial;
6214 13 Dec 12 nicklas 141         createItemNode(sampleNodeFactory, sample, well, denied, folderNode, ChildNodeDirection.ALL);
6044 03 Apr 12 nicklas 142       }
6044 03 Apr 12 nicklas 143       else
6044 03 Apr 12 nicklas 144       {
6044 03 Apr 12 nicklas 145         // Create extract node
6044 03 Apr 12 nicklas 146         Extract extract = (Extract)bioMaterial;
6214 13 Dec 12 nicklas 147         createItemNode(extractNodeFactory, extract, well, denied, folderNode, ChildNodeDirection.ALL);
6044 03 Apr 12 nicklas 148       }
6044 03 Apr 12 nicklas 149     }
6214 13 Dec 12 nicklas 150     return folderNode;
6044 03 Apr 12 nicklas 151   }
6044 03 Apr 12 nicklas 152
6044 03 Apr 12 nicklas 153 }