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

Code
Comments
Other
Rev Date Author Line
4740 05 Feb 09 nicklas 1 /**
4740 05 Feb 09 nicklas 2   $Id$
4740 05 Feb 09 nicklas 3
4740 05 Feb 09 nicklas 4   Copyright (C) 2008 Nicklas Nordborg
4740 05 Feb 09 nicklas 5
4740 05 Feb 09 nicklas 6   This file is part of BASE - BioArray Software Environment.
4740 05 Feb 09 nicklas 7   Available at http://base.thep.lu.se/
4740 05 Feb 09 nicklas 8
4740 05 Feb 09 nicklas 9   BASE is free software; you can redistribute it and/or
4740 05 Feb 09 nicklas 10   modify it under the terms of the GNU General Public License
4740 05 Feb 09 nicklas 11   as published by the Free Software Foundation; either version 3
4740 05 Feb 09 nicklas 12   of the License, or (at your option) any later version.
4740 05 Feb 09 nicklas 13
4740 05 Feb 09 nicklas 14   BASE is distributed in the hope that it will be useful,
4740 05 Feb 09 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
4740 05 Feb 09 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4740 05 Feb 09 nicklas 17   GNU General Public License for more details.
4740 05 Feb 09 nicklas 18
4740 05 Feb 09 nicklas 19   You should have received a copy of the GNU General Public License
4740 05 Feb 09 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
4740 05 Feb 09 nicklas 21 */
4740 05 Feb 09 nicklas 22 package net.sf.basedb.util.overview.loader;
4740 05 Feb 09 nicklas 23
4740 05 Feb 09 nicklas 24 import net.sf.basedb.core.BasicItem;
4740 05 Feb 09 nicklas 25 import net.sf.basedb.core.DbControl;
4740 05 Feb 09 nicklas 26 import net.sf.basedb.util.overview.OverviewContext;
4740 05 Feb 09 nicklas 27 import net.sf.basedb.util.overview.node.ChildNodeDirection;
4740 05 Feb 09 nicklas 28 import net.sf.basedb.util.overview.Node;
4740 05 Feb 09 nicklas 29 import net.sf.basedb.util.overview.node.NodeFactory;
4740 05 Feb 09 nicklas 30 import net.sf.basedb.util.overview.node.NodeNameGenerator;
4740 05 Feb 09 nicklas 31 import net.sf.basedb.util.overview.validator.NodeValidator;
4740 05 Feb 09 nicklas 32 import net.sf.basedb.util.overview.validator.NodeValidatorFactory;
4740 05 Feb 09 nicklas 33
4740 05 Feb 09 nicklas 34 /**
4740 05 Feb 09 nicklas 35   Abstract class that is useful for all node loaders that works
4740 05 Feb 09 nicklas 36   with {@link BasicItem}:s.
4740 05 Feb 09 nicklas 37   
4740 05 Feb 09 nicklas 38   @author Nicklas
4740 05 Feb 09 nicklas 39   @version 2.10
4740 05 Feb 09 nicklas 40   @base.modified $Date$
4740 05 Feb 09 nicklas 41 */
4740 05 Feb 09 nicklas 42 public abstract class BasicItemNodeLoader<I extends BasicItem>
4740 05 Feb 09 nicklas 43   extends AbstractNodeLoader<I>
4740 05 Feb 09 nicklas 44 {
6444 09 Apr 14 nicklas 45   private static final org.slf4j.Logger log = 
6444 09 Apr 14 nicklas 46     org.slf4j.LoggerFactory.getLogger(BasicItemNodeLoader.class);
4740 05 Feb 09 nicklas 47   private static final boolean debug = log.isDebugEnabled();
4740 05 Feb 09 nicklas 48
4740 05 Feb 09 nicklas 49   public static final boolean ALLOW_ROOT_NODE = true;
4740 05 Feb 09 nicklas 50   public static final boolean DENY_ROOT_NODE = false;
4740 05 Feb 09 nicklas 51   
4740 05 Feb 09 nicklas 52   protected final Object factoryKey;
4740 05 Feb 09 nicklas 53   protected final boolean allowAsRootNode;
4740 05 Feb 09 nicklas 54   protected final NodeNameGenerator<I> nameGenerator;
4740 05 Feb 09 nicklas 55   
4740 05 Feb 09 nicklas 56   /**
4740 05 Feb 09 nicklas 57     Create a new node loader.
4740 05 Feb 09 nicklas 58     
4740 05 Feb 09 nicklas 59     @param factoryKey The key to use when looking up a {@link NodeValidator}
4740 05 Feb 09 nicklas 60       with {@link NodeValidatorFactory#createNodeValidator(Object)}
4740 05 Feb 09 nicklas 61     @param allowAsRootNode If FALSE, the {@link 
4740 05 Feb 09 nicklas 62       #createRootNode(DbControl, OverviewContext, BasicItem)} method
4740 05 Feb 09 nicklas 63       throws an execption
4740 05 Feb 09 nicklas 64     @param nameGenerator A name generator implementation that know how to
4740 05 Feb 09 nicklas 65       create name and title for nodes
4740 05 Feb 09 nicklas 66   */
4740 05 Feb 09 nicklas 67   protected BasicItemNodeLoader(Object factoryKey, boolean allowAsRootNode, NodeNameGenerator<I> nameGenerator)
4740 05 Feb 09 nicklas 68   {
4740 05 Feb 09 nicklas 69     this.factoryKey = factoryKey;
4740 05 Feb 09 nicklas 70     this.allowAsRootNode = allowAsRootNode;
4740 05 Feb 09 nicklas 71     this.nameGenerator = nameGenerator;
4740 05 Feb 09 nicklas 72   }
4740 05 Feb 09 nicklas 73   
4740 05 Feb 09 nicklas 74   /*
4740 05 Feb 09 nicklas 75     From the NodeLoader interface
4740 05 Feb 09 nicklas 76     ------------------------------
4740 05 Feb 09 nicklas 77   */
4740 05 Feb 09 nicklas 78   @Override
4740 05 Feb 09 nicklas 79   public Node createRootNode(DbControl dc, OverviewContext context, I item)
4740 05 Feb 09 nicklas 80   {
4740 05 Feb 09 nicklas 81     if (debug) log.debug("Create root node: " + item);
4740 05 Feb 09 nicklas 82     if (!allowAsRootNode) throw new UnsupportedOperationException();
4740 05 Feb 09 nicklas 83     NodeFactory<I> nf = getNodeFactory(dc, context);
4740 05 Feb 09 nicklas 84     Node node = nf.createNode(item, item, null, ChildNodeDirection.ALL);
4740 05 Feb 09 nicklas 85     if (debug) log.debug("Created: " + node);
4740 05 Feb 09 nicklas 86     return node;
4740 05 Feb 09 nicklas 87   }
4740 05 Feb 09 nicklas 88   // -------------------------------
4740 05 Feb 09 nicklas 89   
4740 05 Feb 09 nicklas 90   /**
4740 05 Feb 09 nicklas 91     Create a new item node using the supplied node factory. If <code>denied==true</code>
4740 05 Feb 09 nicklas 92     the node will be created with {@link NodeFactory#createDeniedNode(Node)}. If
4740 05 Feb 09 nicklas 93     <code>item==null</code> the node will be created with 
4740 05 Feb 09 nicklas 94     {@link NodeFactory#createMissingNode(Node)}. Otherwise the node is created with
4740 05 Feb 09 nicklas 95     {@link NodeFactory#createNode(BasicItem, Object, Node, ChildNodeDirection)}
4740 05 Feb 09 nicklas 96     
4740 05 Feb 09 nicklas 97     @param factory The node factory to use
4740 05 Feb 09 nicklas 98     @param item The item that is contained by the node (can be null)
4740 05 Feb 09 nicklas 99     @param cacheKey Optional key for cache lookup. If not null and an
4740 05 Feb 09 nicklas 100       entry is found in the cache, the entire sub-tree is duplicated
4740 05 Feb 09 nicklas 101       to avoid re-loading the same items again
4740 05 Feb 09 nicklas 102     @param denied TRUE if the logged in user was denied access to the item
4740 05 Feb 09 nicklas 103     @param parentNode The parent node
4740 05 Feb 09 nicklas 104     @param direction The direction for loading children
4740 05 Feb 09 nicklas 105   */
6042 03 Apr 12 nicklas 106   protected <S> Node createItemNode(NodeFactory<? super S> factory, S item, Object cacheKey, boolean denied, 
4740 05 Feb 09 nicklas 107     Node parentNode, ChildNodeDirection direction)
4740 05 Feb 09 nicklas 108   {
4740 05 Feb 09 nicklas 109     Node node = null;
4740 05 Feb 09 nicklas 110     if (denied)
4740 05 Feb 09 nicklas 111     {
4740 05 Feb 09 nicklas 112       if (debug) log.debug("Create denied node: parent=" + parentNode);
4740 05 Feb 09 nicklas 113       node = factory.createDeniedNode(parentNode);
4740 05 Feb 09 nicklas 114     }
4740 05 Feb 09 nicklas 115     else if (item == null)
4740 05 Feb 09 nicklas 116     {
4740 05 Feb 09 nicklas 117       if (debug) log.debug("Create missing node: parent=" + parentNode);
4740 05 Feb 09 nicklas 118       node = factory.createMissingNode(parentNode);
4740 05 Feb 09 nicklas 119     }
4740 05 Feb 09 nicklas 120     else
4740 05 Feb 09 nicklas 121     {
4740 05 Feb 09 nicklas 122       if (debug) log.debug("Create node: item=" + item + "; parent=" + parentNode);
4740 05 Feb 09 nicklas 123       node = factory.createNode(item, cacheKey, parentNode, direction);
4740 05 Feb 09 nicklas 124     }
4740 05 Feb 09 nicklas 125     if (debug) log.debug("Node created: " + node);
4740 05 Feb 09 nicklas 126     return node;
4740 05 Feb 09 nicklas 127   }
4740 05 Feb 09 nicklas 128   
4740 05 Feb 09 nicklas 129   protected void postValidateFolder(NodeFactory<I> nf, Node folderNode, Node parentNode, boolean createMissingNode)
4740 05 Feb 09 nicklas 130   {
4740 05 Feb 09 nicklas 131     if (createMissingNode && folderNode == null) nf.createMissingNode(parentNode);
4740 05 Feb 09 nicklas 132     nf.postValidateFolder(folderNode, parentNode);
4740 05 Feb 09 nicklas 133   }
4740 05 Feb 09 nicklas 134   
4740 05 Feb 09 nicklas 135   
4740 05 Feb 09 nicklas 136   /**
4740 05 Feb 09 nicklas 137     Get the node loader factory used by the current context. The factory
4740 05 Feb 09 nicklas 138     must be a factory that create BasicItem node loaders.
4740 05 Feb 09 nicklas 139     @param context The current context
4740 05 Feb 09 nicklas 140     @see OverviewContext#getNodeLoaderFactory()
4740 05 Feb 09 nicklas 141   */
4740 05 Feb 09 nicklas 142   @SuppressWarnings("unchecked")
4740 05 Feb 09 nicklas 143   protected NodeLoaderFactory<BasicItem, Object> getNodeLoaderFactory(OverviewContext context)
4740 05 Feb 09 nicklas 144   {
4740 05 Feb 09 nicklas 145     return context.getNodeLoaderFactory();
4740 05 Feb 09 nicklas 146   }
4740 05 Feb 09 nicklas 147   
4740 05 Feb 09 nicklas 148   /**
4740 05 Feb 09 nicklas 149     Get a node loader for a specific item type using the current context's 
4740 05 Feb 09 nicklas 150     node loader factory.
4740 05 Feb 09 nicklas 151     @param context The current context
4740 05 Feb 09 nicklas 152     @param key The item type that the factory should create a node loader for
4740 05 Feb 09 nicklas 153     @return A node loader object
4740 05 Feb 09 nicklas 154     @see OverviewContext#getNodeLoaderFactory()
4740 05 Feb 09 nicklas 155   */
4740 05 Feb 09 nicklas 156   protected NodeLoader<? extends BasicItem> getNodeLoader(OverviewContext context, Object key)
4740 05 Feb 09 nicklas 157   {
4740 05 Feb 09 nicklas 158     return getNodeLoaderFactory(context).createNodeLoader(key);
4740 05 Feb 09 nicklas 159   }
4740 05 Feb 09 nicklas 160   
4740 05 Feb 09 nicklas 161   /**
4740 05 Feb 09 nicklas 162     Get the node validator factory used by the current context. The factory
4740 05 Feb 09 nicklas 163     must be a factory that create BasicItem validators.
4740 05 Feb 09 nicklas 164     @param context The current context
4740 05 Feb 09 nicklas 165     @see OverviewContext#getNodeValidatorFactory()
4740 05 Feb 09 nicklas 166   */
4740 05 Feb 09 nicklas 167   @SuppressWarnings("unchecked")
4740 05 Feb 09 nicklas 168   protected NodeValidatorFactory<BasicItem, Object> getNodeValidatorFactory(OverviewContext context)
4740 05 Feb 09 nicklas 169   {
4740 05 Feb 09 nicklas 170     return context.getNodeValidatorFactory();
4740 05 Feb 09 nicklas 171   }
4740 05 Feb 09 nicklas 172   
4740 05 Feb 09 nicklas 173   /**
4740 05 Feb 09 nicklas 174     Get the default node validator for nodes of this type. The node validator is
4740 05 Feb 09 nicklas 175     looked up using the factory key provided in the constructor.
4740 05 Feb 09 nicklas 176     This method is a shortcut for
4740 05 Feb 09 nicklas 177     <code>context.getNodeValidatorFactory().createNodeValidator(factoryKey)</code>
4740 05 Feb 09 nicklas 178     @param context The current overview context
4740 05 Feb 09 nicklas 179     @return A node validator, or null if no validator exists
4740 05 Feb 09 nicklas 180   */
4740 05 Feb 09 nicklas 181   @SuppressWarnings("unchecked")
4740 05 Feb 09 nicklas 182   protected NodeValidator<I> getNodeValidator(OverviewContext context)
4740 05 Feb 09 nicklas 183   {
4740 05 Feb 09 nicklas 184     return (NodeValidator<I>)getNodeValidatorFactory(context).createNodeValidator(factoryKey);
4740 05 Feb 09 nicklas 185   }
4740 05 Feb 09 nicklas 186   
4740 05 Feb 09 nicklas 187   /**
4740 05 Feb 09 nicklas 188     Get a node factory that can be used to create item nodes. The node factory
4740 05 Feb 09 nicklas 189     will use the default node validator as returned by {@link #getNodeValidator(OverviewContext)}
4740 05 Feb 09 nicklas 190     and the name generator as provided in the constructor.
4740 05 Feb 09 nicklas 191     
4740 05 Feb 09 nicklas 192     @param dc A DbControl that can be used for database access
4740 05 Feb 09 nicklas 193     @param context The current overview context
4740 05 Feb 09 nicklas 194     @return A node factory
4740 05 Feb 09 nicklas 195     @see #getNodeFactory(DbControl, OverviewContext, NodeValidator, NodeNameGenerator)
4740 05 Feb 09 nicklas 196   */
4740 05 Feb 09 nicklas 197   protected NodeFactory<I> getNodeFactory(DbControl dc, OverviewContext context)
4740 05 Feb 09 nicklas 198   {
4740 05 Feb 09 nicklas 199     NodeValidator<I> nv = getNodeValidator(context);
4740 05 Feb 09 nicklas 200     return new NodeFactory<I>(dc, context, nv, nameGenerator);
4740 05 Feb 09 nicklas 201   }
4740 05 Feb 09 nicklas 202   
4740 05 Feb 09 nicklas 203   /**
4740 05 Feb 09 nicklas 204     Get a node factory that can be used to create item nodes using
4740 05 Feb 09 nicklas 205     a non-default name generator and validator.
4740 05 Feb 09 nicklas 206     
4740 05 Feb 09 nicklas 207     @param dc A DbControl that can be used for database access
4740 05 Feb 09 nicklas 208     @param context The current overview context
4740 05 Feb 09 nicklas 209     @param validator A node validator, or null if no validation is required
4740 05 Feb 09 nicklas 210     @param nameGenerator A name generator
4740 05 Feb 09 nicklas 211     @return A node factory
4740 05 Feb 09 nicklas 212     @see #getNodeFactory(DbControl, OverviewContext)
4740 05 Feb 09 nicklas 213   */
4740 05 Feb 09 nicklas 214   protected NodeFactory<I> getNodeFactory(DbControl dc, OverviewContext context, 
6042 03 Apr 12 nicklas 215     NodeValidator<? super I> validator, NodeNameGenerator<? super I> nameGenerator)
4740 05 Feb 09 nicklas 216   {
4740 05 Feb 09 nicklas 217     return new NodeFactory<I>(dc, context, validator, nameGenerator);
4740 05 Feb 09 nicklas 218   }
4740 05 Feb 09 nicklas 219   
6041 02 Apr 12 nicklas 220   /**
6041 02 Apr 12 nicklas 221     Get the name generator that is used by this loader.
6041 02 Apr 12 nicklas 222     @since 3.2
6041 02 Apr 12 nicklas 223   */
6041 02 Apr 12 nicklas 224   protected NodeNameGenerator<I> getNodeNameGenerator()
6041 02 Apr 12 nicklas 225   {
6041 02 Apr 12 nicklas 226     return nameGenerator;
6041 02 Apr 12 nicklas 227   }
6041 02 Apr 12 nicklas 228   
4740 05 Feb 09 nicklas 229 }