6047 |
18 Apr 12 |
nicklas |
1 |
/** |
6047 |
18 Apr 12 |
nicklas |
$Id$ |
6047 |
18 Apr 12 |
nicklas |
3 |
|
6047 |
18 Apr 12 |
nicklas |
Copyright (C) 2012 Nicklas Nordborg |
6047 |
18 Apr 12 |
nicklas |
5 |
|
6047 |
18 Apr 12 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
6047 |
18 Apr 12 |
nicklas |
Available at http://base.thep.lu.se/ |
6047 |
18 Apr 12 |
nicklas |
8 |
|
6047 |
18 Apr 12 |
nicklas |
BASE is free software; you can redistribute it and/or |
6047 |
18 Apr 12 |
nicklas |
modify it under the terms of the GNU General Public License |
6047 |
18 Apr 12 |
nicklas |
as published by the Free Software Foundation; either version 3 |
6047 |
18 Apr 12 |
nicklas |
of the License, or (at your option) any later version. |
6047 |
18 Apr 12 |
nicklas |
13 |
|
6047 |
18 Apr 12 |
nicklas |
BASE is distributed in the hope that it will be useful, |
6047 |
18 Apr 12 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
6047 |
18 Apr 12 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
6047 |
18 Apr 12 |
nicklas |
GNU General Public License for more details. |
6047 |
18 Apr 12 |
nicklas |
18 |
|
6047 |
18 Apr 12 |
nicklas |
You should have received a copy of the GNU General Public License |
6047 |
18 Apr 12 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
6047 |
18 Apr 12 |
nicklas |
21 |
*/ |
6047 |
18 Apr 12 |
nicklas |
22 |
package net.sf.basedb.util.overview.validator; |
6047 |
18 Apr 12 |
nicklas |
23 |
|
6047 |
18 Apr 12 |
nicklas |
24 |
|
6047 |
18 Apr 12 |
nicklas |
25 |
import java.util.List; |
6047 |
18 Apr 12 |
nicklas |
26 |
|
7527 |
07 Nov 18 |
nicklas |
27 |
import org.apache.commons.collections4.IteratorUtils; |
6047 |
18 Apr 12 |
nicklas |
28 |
|
6047 |
18 Apr 12 |
nicklas |
29 |
import net.sf.basedb.core.Application; |
6047 |
18 Apr 12 |
nicklas |
30 |
import net.sf.basedb.core.DbControl; |
6047 |
18 Apr 12 |
nicklas |
31 |
import net.sf.basedb.util.extensions.ClientContext; |
6047 |
18 Apr 12 |
nicklas |
32 |
import net.sf.basedb.util.extensions.ExtensionsInvoker; |
6047 |
18 Apr 12 |
nicklas |
33 |
import net.sf.basedb.util.extensions.Registry; |
6088 |
21 Aug 12 |
nicklas |
34 |
import net.sf.basedb.util.extensions.manager.Settings; |
6047 |
18 Apr 12 |
nicklas |
35 |
import net.sf.basedb.util.overview.OverviewContext; |
6047 |
18 Apr 12 |
nicklas |
36 |
import net.sf.basedb.util.overview.Node; |
6047 |
18 Apr 12 |
nicklas |
37 |
import net.sf.basedb.util.overview.extensions.NodeValidatorAction; |
6047 |
18 Apr 12 |
nicklas |
38 |
|
6047 |
18 Apr 12 |
nicklas |
39 |
/** |
6047 |
18 Apr 12 |
nicklas |
Wrapper for node validators that hooks into the extension system when |
6047 |
18 Apr 12 |
nicklas |
validating nodes. The parent (or core) validator is always invoked first, |
6047 |
18 Apr 12 |
nicklas |
then the extension system is queried for additional validators. Methods |
6047 |
18 Apr 12 |
nicklas |
that return a value return the result from the last call (unless nothing |
6047 |
18 Apr 12 |
nicklas |
else is specified). |
6047 |
18 Apr 12 |
nicklas |
45 |
|
6047 |
18 Apr 12 |
nicklas |
@author Nicklas |
6047 |
18 Apr 12 |
nicklas |
@since 3.2 |
6047 |
18 Apr 12 |
nicklas |
48 |
*/ |
6047 |
18 Apr 12 |
nicklas |
49 |
public class ExtensionNodeValidator<I> |
6047 |
18 Apr 12 |
nicklas |
50 |
implements NodeValidator<I> |
6047 |
18 Apr 12 |
nicklas |
51 |
{ |
6047 |
18 Apr 12 |
nicklas |
52 |
|
6047 |
18 Apr 12 |
nicklas |
53 |
private final NodeValidator<I> parent; |
6047 |
18 Apr 12 |
nicklas |
54 |
private final Object key; |
6047 |
18 Apr 12 |
nicklas |
55 |
private List<NodeValidatorAction<I>> xtValidators; |
6047 |
18 Apr 12 |
nicklas |
56 |
|
6047 |
18 Apr 12 |
nicklas |
57 |
/** |
6047 |
18 Apr 12 |
nicklas |
Wrap the given parent node validator. |
6047 |
18 Apr 12 |
nicklas |
59 |
*/ |
6047 |
18 Apr 12 |
nicklas |
60 |
public ExtensionNodeValidator(NodeValidator<I> parent, Object key) |
6047 |
18 Apr 12 |
nicklas |
61 |
{ |
6047 |
18 Apr 12 |
nicklas |
62 |
this.parent = parent; |
6047 |
18 Apr 12 |
nicklas |
63 |
this.key = key; |
6047 |
18 Apr 12 |
nicklas |
64 |
} |
6047 |
18 Apr 12 |
nicklas |
65 |
|
6047 |
18 Apr 12 |
nicklas |
66 |
/* |
6047 |
18 Apr 12 |
nicklas |
From the NodeValidator interface |
6047 |
18 Apr 12 |
nicklas |
68 |
----------------------------- |
6047 |
18 Apr 12 |
nicklas |
69 |
*/ |
6047 |
18 Apr 12 |
nicklas |
70 |
/** |
6047 |
18 Apr 12 |
nicklas |
Call the same method on the parent validator and then on |
6047 |
18 Apr 12 |
nicklas |
all extension validator actions. The result from the last call |
6047 |
18 Apr 12 |
nicklas |
is returned. |
6047 |
18 Apr 12 |
nicklas |
74 |
*/ |
6047 |
18 Apr 12 |
nicklas |
75 |
@Override |
6047 |
18 Apr 12 |
nicklas |
76 |
public boolean preMissingItem(DbControl dc, OverviewContext context, Node parentNode) |
6047 |
18 Apr 12 |
nicklas |
77 |
{ |
6047 |
18 Apr 12 |
nicklas |
78 |
boolean result = parent.preMissingItem(dc, context, parentNode); |
6047 |
18 Apr 12 |
nicklas |
79 |
for (NodeValidatorAction<I> nvAction : getValidators(dc)) |
6047 |
18 Apr 12 |
nicklas |
80 |
{ |
6047 |
18 Apr 12 |
nicklas |
81 |
result = nvAction.preMissingItem(dc, context, parentNode); |
6047 |
18 Apr 12 |
nicklas |
82 |
} |
6047 |
18 Apr 12 |
nicklas |
83 |
return result; |
6047 |
18 Apr 12 |
nicklas |
84 |
} |
6047 |
18 Apr 12 |
nicklas |
85 |
|
6047 |
18 Apr 12 |
nicklas |
86 |
/** |
6047 |
18 Apr 12 |
nicklas |
Call the same method on the parent validator and then on |
6047 |
18 Apr 12 |
nicklas |
all extension validator actions. The result from the last call |
6047 |
18 Apr 12 |
nicklas |
is returned. |
6047 |
18 Apr 12 |
nicklas |
90 |
*/ |
6047 |
18 Apr 12 |
nicklas |
91 |
@Override |
6047 |
18 Apr 12 |
nicklas |
92 |
public boolean preDeniedItem(DbControl dc, OverviewContext context, Node parentNode) |
6047 |
18 Apr 12 |
nicklas |
93 |
{ |
6047 |
18 Apr 12 |
nicklas |
94 |
boolean result = parent.preDeniedItem(dc, context, parentNode); |
6047 |
18 Apr 12 |
nicklas |
95 |
for (NodeValidatorAction<I> nvAction : getValidators(dc)) |
6047 |
18 Apr 12 |
nicklas |
96 |
{ |
6047 |
18 Apr 12 |
nicklas |
97 |
result = nvAction.preDeniedItem(dc, context, parentNode); |
6047 |
18 Apr 12 |
nicklas |
98 |
} |
6047 |
18 Apr 12 |
nicklas |
99 |
return result; |
6047 |
18 Apr 12 |
nicklas |
100 |
} |
6047 |
18 Apr 12 |
nicklas |
101 |
|
6047 |
18 Apr 12 |
nicklas |
102 |
/** |
6047 |
18 Apr 12 |
nicklas |
Call the same method on the parent validator and then on |
6047 |
18 Apr 12 |
nicklas |
all extension validator actions. The result from the last call |
6047 |
18 Apr 12 |
nicklas |
is returned. |
6047 |
18 Apr 12 |
nicklas |
106 |
*/ |
6047 |
18 Apr 12 |
nicklas |
107 |
@Override |
6047 |
18 Apr 12 |
nicklas |
108 |
public boolean preValidate(DbControl dc, OverviewContext context, I item, Node parentNode) |
6047 |
18 Apr 12 |
nicklas |
109 |
{ |
6047 |
18 Apr 12 |
nicklas |
110 |
boolean result = parent.preValidate(dc, context, item, parentNode); |
6047 |
18 Apr 12 |
nicklas |
111 |
for (NodeValidatorAction<I> nvAction : getValidators(dc)) |
6047 |
18 Apr 12 |
nicklas |
112 |
{ |
6047 |
18 Apr 12 |
nicklas |
113 |
result = nvAction.preValidate(dc, context, item, parentNode); |
6047 |
18 Apr 12 |
nicklas |
114 |
} |
6047 |
18 Apr 12 |
nicklas |
115 |
return result; |
6047 |
18 Apr 12 |
nicklas |
116 |
} |
6047 |
18 Apr 12 |
nicklas |
117 |
|
6047 |
18 Apr 12 |
nicklas |
118 |
/** |
6047 |
18 Apr 12 |
nicklas |
Call the same method on the parent validator and then on |
6047 |
18 Apr 12 |
nicklas |
all extension validator actions. |
6047 |
18 Apr 12 |
nicklas |
121 |
*/ |
6047 |
18 Apr 12 |
nicklas |
122 |
@Override |
6047 |
18 Apr 12 |
nicklas |
123 |
public void postMissingItem(DbControl dc, OverviewContext context, Node node, Node parentNode) |
6047 |
18 Apr 12 |
nicklas |
124 |
{ |
6047 |
18 Apr 12 |
nicklas |
125 |
parent.postMissingItem(dc, context, node, parentNode); |
6047 |
18 Apr 12 |
nicklas |
126 |
for (NodeValidatorAction<I> nvAction : getValidators(dc)) |
6047 |
18 Apr 12 |
nicklas |
127 |
{ |
6047 |
18 Apr 12 |
nicklas |
128 |
nvAction.postMissingItem(dc, context, node, parentNode); |
6047 |
18 Apr 12 |
nicklas |
129 |
} |
6047 |
18 Apr 12 |
nicklas |
130 |
} |
6047 |
18 Apr 12 |
nicklas |
131 |
|
6047 |
18 Apr 12 |
nicklas |
132 |
/** |
6047 |
18 Apr 12 |
nicklas |
Call the same method on the parent validator and then on |
6047 |
18 Apr 12 |
nicklas |
all extension validator actions. |
6047 |
18 Apr 12 |
nicklas |
135 |
*/ |
6047 |
18 Apr 12 |
nicklas |
136 |
@Override |
6047 |
18 Apr 12 |
nicklas |
137 |
public void postDeniedItem(DbControl dc, OverviewContext context, Node node, Node parentNode) |
6047 |
18 Apr 12 |
nicklas |
138 |
{ |
6047 |
18 Apr 12 |
nicklas |
139 |
parent.postDeniedItem(dc, context, node, parentNode); |
6047 |
18 Apr 12 |
nicklas |
140 |
for (NodeValidatorAction<I> nvAction : getValidators(dc)) |
6047 |
18 Apr 12 |
nicklas |
141 |
{ |
6047 |
18 Apr 12 |
nicklas |
142 |
nvAction.postDeniedItem(dc, context, node, parentNode); |
6047 |
18 Apr 12 |
nicklas |
143 |
} |
6047 |
18 Apr 12 |
nicklas |
144 |
} |
6047 |
18 Apr 12 |
nicklas |
145 |
|
6047 |
18 Apr 12 |
nicklas |
146 |
/** |
6047 |
18 Apr 12 |
nicklas |
Call the same method on the parent validator and then on |
6047 |
18 Apr 12 |
nicklas |
all extension validator actions. |
6047 |
18 Apr 12 |
nicklas |
149 |
*/ |
6047 |
18 Apr 12 |
nicklas |
150 |
@Override |
6047 |
18 Apr 12 |
nicklas |
151 |
public void postValidate(DbControl dc, OverviewContext context, Node node, Node parentNode) |
6047 |
18 Apr 12 |
nicklas |
152 |
{ |
6047 |
18 Apr 12 |
nicklas |
153 |
parent.postValidate(dc, context, node, parentNode); |
6875 |
20 Apr 15 |
nicklas |
154 |
for (NodeValidatorAction<?> nvAction : getValidators(dc)) |
6047 |
18 Apr 12 |
nicklas |
155 |
{ |
6047 |
18 Apr 12 |
nicklas |
156 |
nvAction.postValidate(dc, context, node, parentNode); |
6047 |
18 Apr 12 |
nicklas |
157 |
} |
6047 |
18 Apr 12 |
nicklas |
158 |
} |
6047 |
18 Apr 12 |
nicklas |
159 |
|
6047 |
18 Apr 12 |
nicklas |
160 |
/** |
6047 |
18 Apr 12 |
nicklas |
Call the same method on the parent validator and then on |
6047 |
18 Apr 12 |
nicklas |
all extension validator actions. |
6047 |
18 Apr 12 |
nicklas |
163 |
*/ |
6047 |
18 Apr 12 |
nicklas |
164 |
@Override |
6047 |
18 Apr 12 |
nicklas |
165 |
public void postValidateFolder(DbControl dc, OverviewContext context, Node folderNode, Node parentNode) |
6047 |
18 Apr 12 |
nicklas |
166 |
{ |
6047 |
18 Apr 12 |
nicklas |
167 |
parent.postValidateFolder(dc, context, folderNode, parentNode); |
6875 |
20 Apr 15 |
nicklas |
168 |
for (NodeValidatorAction<?> nvAction : getValidators(dc)) |
6047 |
18 Apr 12 |
nicklas |
169 |
{ |
6047 |
18 Apr 12 |
nicklas |
170 |
nvAction.postValidateFolder(dc, context, folderNode, parentNode); |
6047 |
18 Apr 12 |
nicklas |
171 |
} |
6047 |
18 Apr 12 |
nicklas |
172 |
} |
6047 |
18 Apr 12 |
nicklas |
173 |
// -------------------------------- |
6047 |
18 Apr 12 |
nicklas |
174 |
|
6047 |
18 Apr 12 |
nicklas |
175 |
@Override |
6047 |
18 Apr 12 |
nicklas |
176 |
public String toString() |
6047 |
18 Apr 12 |
nicklas |
177 |
{ |
6047 |
18 Apr 12 |
nicklas |
178 |
return super.toString() + "-->" + parent.toString(); |
6047 |
18 Apr 12 |
nicklas |
179 |
} |
6047 |
18 Apr 12 |
nicklas |
180 |
|
6047 |
18 Apr 12 |
nicklas |
181 |
private Iterable<NodeValidatorAction<I>> getValidators(DbControl dc) |
6047 |
18 Apr 12 |
nicklas |
182 |
{ |
6047 |
18 Apr 12 |
nicklas |
183 |
if (xtValidators == null) |
6047 |
18 Apr 12 |
nicklas |
184 |
{ |
6047 |
18 Apr 12 |
nicklas |
// Create a context with the parent node as the current item |
6047 |
18 Apr 12 |
nicklas |
186 |
ClientContext clientContext = new ClientContext(dc, key); |
6047 |
18 Apr 12 |
nicklas |
187 |
|
6047 |
18 Apr 12 |
nicklas |
// Find and use any registered extensions |
6047 |
18 Apr 12 |
nicklas |
189 |
Registry registry = Application.getExtensionsManager().getRegistry(); |
6088 |
21 Aug 12 |
nicklas |
190 |
Settings settings = Application.getExtensionsManager().getSettings(); |
7605 |
26 Feb 19 |
nicklas |
191 |
ExtensionsInvoker<NodeValidatorAction<I>> invoker = registry.useExtensions(clientContext, settings, "net.sf.basedb.util.overview.validator"); |
6047 |
18 Apr 12 |
nicklas |
192 |
|
6047 |
18 Apr 12 |
nicklas |
193 |
xtValidators = IteratorUtils.toList(invoker.iterator()); |
6047 |
18 Apr 12 |
nicklas |
194 |
} |
6047 |
18 Apr 12 |
nicklas |
195 |
return xtValidators; |
6047 |
18 Apr 12 |
nicklas |
196 |
} |
6047 |
18 Apr 12 |
nicklas |
197 |
|
6047 |
18 Apr 12 |
nicklas |
198 |
} |