4740 |
05 Feb 09 |
nicklas |
1 |
/** |
4740 |
05 Feb 09 |
nicklas |
$Id$ |
4740 |
05 Feb 09 |
nicklas |
3 |
|
4740 |
05 Feb 09 |
nicklas |
Copyright (C) 2008 Nicklas Nordborg |
4740 |
05 Feb 09 |
nicklas |
5 |
|
4740 |
05 Feb 09 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
4740 |
05 Feb 09 |
nicklas |
Available at http://base.thep.lu.se/ |
4740 |
05 Feb 09 |
nicklas |
8 |
|
4740 |
05 Feb 09 |
nicklas |
BASE is free software; you can redistribute it and/or |
4740 |
05 Feb 09 |
nicklas |
modify it under the terms of the GNU General Public License |
4740 |
05 Feb 09 |
nicklas |
as published by the Free Software Foundation; either version 3 |
4740 |
05 Feb 09 |
nicklas |
of the License, or (at your option) any later version. |
4740 |
05 Feb 09 |
nicklas |
13 |
|
4740 |
05 Feb 09 |
nicklas |
BASE is distributed in the hope that it will be useful, |
4740 |
05 Feb 09 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
4740 |
05 Feb 09 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4740 |
05 Feb 09 |
nicklas |
GNU General Public License for more details. |
4740 |
05 Feb 09 |
nicklas |
18 |
|
4740 |
05 Feb 09 |
nicklas |
You should have received a copy of the GNU General Public License |
4740 |
05 Feb 09 |
nicklas |
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 |
Abstract class that is useful for all node loaders that works |
4740 |
05 Feb 09 |
nicklas |
with {@link BasicItem}:s. |
4740 |
05 Feb 09 |
nicklas |
37 |
|
4740 |
05 Feb 09 |
nicklas |
@author Nicklas |
4740 |
05 Feb 09 |
nicklas |
@version 2.10 |
4740 |
05 Feb 09 |
nicklas |
@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 |
Create a new node loader. |
4740 |
05 Feb 09 |
nicklas |
58 |
|
4740 |
05 Feb 09 |
nicklas |
@param factoryKey The key to use when looking up a {@link NodeValidator} |
4740 |
05 Feb 09 |
nicklas |
with {@link NodeValidatorFactory#createNodeValidator(Object)} |
4740 |
05 Feb 09 |
nicklas |
@param allowAsRootNode If FALSE, the {@link |
4740 |
05 Feb 09 |
nicklas |
#createRootNode(DbControl, OverviewContext, BasicItem)} method |
4740 |
05 Feb 09 |
nicklas |
throws an execption |
4740 |
05 Feb 09 |
nicklas |
@param nameGenerator A name generator implementation that know how to |
4740 |
05 Feb 09 |
nicklas |
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 |
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 |
Create a new item node using the supplied node factory. If <code>denied==true</code> |
4740 |
05 Feb 09 |
nicklas |
the node will be created with {@link NodeFactory#createDeniedNode(Node)}. If |
4740 |
05 Feb 09 |
nicklas |
<code>item==null</code> the node will be created with |
4740 |
05 Feb 09 |
nicklas |
{@link NodeFactory#createMissingNode(Node)}. Otherwise the node is created with |
4740 |
05 Feb 09 |
nicklas |
{@link NodeFactory#createNode(BasicItem, Object, Node, ChildNodeDirection)} |
4740 |
05 Feb 09 |
nicklas |
96 |
|
4740 |
05 Feb 09 |
nicklas |
@param factory The node factory to use |
4740 |
05 Feb 09 |
nicklas |
@param item The item that is contained by the node (can be null) |
4740 |
05 Feb 09 |
nicklas |
@param cacheKey Optional key for cache lookup. If not null and an |
4740 |
05 Feb 09 |
nicklas |
entry is found in the cache, the entire sub-tree is duplicated |
4740 |
05 Feb 09 |
nicklas |
to avoid re-loading the same items again |
4740 |
05 Feb 09 |
nicklas |
@param denied TRUE if the logged in user was denied access to the item |
4740 |
05 Feb 09 |
nicklas |
@param parentNode The parent node |
4740 |
05 Feb 09 |
nicklas |
@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 |
Get the node loader factory used by the current context. The factory |
4740 |
05 Feb 09 |
nicklas |
must be a factory that create BasicItem node loaders. |
4740 |
05 Feb 09 |
nicklas |
@param context The current context |
4740 |
05 Feb 09 |
nicklas |
@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 |
Get a node loader for a specific item type using the current context's |
4740 |
05 Feb 09 |
nicklas |
node loader factory. |
4740 |
05 Feb 09 |
nicklas |
@param context The current context |
4740 |
05 Feb 09 |
nicklas |
@param key The item type that the factory should create a node loader for |
4740 |
05 Feb 09 |
nicklas |
@return A node loader object |
4740 |
05 Feb 09 |
nicklas |
@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 |
Get the node validator factory used by the current context. The factory |
4740 |
05 Feb 09 |
nicklas |
must be a factory that create BasicItem validators. |
4740 |
05 Feb 09 |
nicklas |
@param context The current context |
4740 |
05 Feb 09 |
nicklas |
@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 |
Get the default node validator for nodes of this type. The node validator is |
4740 |
05 Feb 09 |
nicklas |
looked up using the factory key provided in the constructor. |
4740 |
05 Feb 09 |
nicklas |
This method is a shortcut for |
4740 |
05 Feb 09 |
nicklas |
<code>context.getNodeValidatorFactory().createNodeValidator(factoryKey)</code> |
4740 |
05 Feb 09 |
nicklas |
@param context The current overview context |
4740 |
05 Feb 09 |
nicklas |
@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 |
Get a node factory that can be used to create item nodes. The node factory |
4740 |
05 Feb 09 |
nicklas |
will use the default node validator as returned by {@link #getNodeValidator(OverviewContext)} |
4740 |
05 Feb 09 |
nicklas |
and the name generator as provided in the constructor. |
4740 |
05 Feb 09 |
nicklas |
191 |
|
4740 |
05 Feb 09 |
nicklas |
@param dc A DbControl that can be used for database access |
4740 |
05 Feb 09 |
nicklas |
@param context The current overview context |
4740 |
05 Feb 09 |
nicklas |
@return A node factory |
4740 |
05 Feb 09 |
nicklas |
@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 |
Get a node factory that can be used to create item nodes using |
4740 |
05 Feb 09 |
nicklas |
a non-default name generator and validator. |
4740 |
05 Feb 09 |
nicklas |
206 |
|
4740 |
05 Feb 09 |
nicklas |
@param dc A DbControl that can be used for database access |
4740 |
05 Feb 09 |
nicklas |
@param context The current overview context |
4740 |
05 Feb 09 |
nicklas |
@param validator A node validator, or null if no validation is required |
4740 |
05 Feb 09 |
nicklas |
@param nameGenerator A name generator |
4740 |
05 Feb 09 |
nicklas |
@return A node factory |
4740 |
05 Feb 09 |
nicklas |
@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 |
Get the name generator that is used by this loader. |
6041 |
02 Apr 12 |
nicklas |
@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 |
} |