comparison artifact-database/src/main/java/org/dive4elements/artifactdatabase/state/AbstractState.java @ 473:d0ac790a6c89 dive4elements-move

Moved directories to org.dive4elements
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 10:57:18 +0200
parents artifact-database/src/main/java/de/intevation/artifactdatabase/state/AbstractState.java@402cd5464723
children 415df0fc4fa1
comparison
equal deleted inserted replaced
472:783cc1b6b615 473:d0ac790a6c89
1 /*
2 * Copyright (c) 2011 by Intevation GmbH
3 *
4 * This program is free software under the LGPL (>=v2.1)
5 * Read the file LGPL.txt coming with the software for details
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */
8 package de.intevation.artifactdatabase.state;
9
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14
15 import javax.xml.xpath.XPathConstants;
16
17 import org.apache.log4j.Logger;
18
19 import org.w3c.dom.Document;
20 import org.w3c.dom.Element;
21 import org.w3c.dom.Node;
22 import org.w3c.dom.NodeList;
23
24 import de.intevation.artifacts.Artifact;
25 import de.intevation.artifacts.ArtifactNamespaceContext;
26 import de.intevation.artifacts.CallContext;
27 import de.intevation.artifacts.CallMeta;
28
29 import de.intevation.artifacts.common.utils.Config;
30 import de.intevation.artifacts.common.utils.XMLUtils;
31
32 import de.intevation.artifactdatabase.data.StateData;
33
34
35 /**
36 * An abstract implementation of a {@link State}. It implements some basic
37 * methods that return the id, description and data. The methods
38 * <code>describe()</code> and <code>setup()</code> depend on the concrete class
39 * and need to be implemented by those.
40 */
41 public abstract class AbstractState implements State {
42
43 /** The XPath to the ID of the state relative to the state node in the
44 * configuration. */
45 public static final String XPATH_ID = "@id";
46
47 /** The XPath to the description of the state relative to the state node in
48 * the configuration. */
49 public static final String XPATH_DESCRIPTION = "@description";
50
51 /** The XPath that points to the help text.*/
52 public static final String XPATH_HELP_TEXT = "@helpText";
53
54 /** The XPath to the output nodes of the state configuration. */
55 public static final String XPATH_OUTPUT_MODES = "outputmodes/outputmode";
56
57 /** The XPath to the list of facets relative to the output mode it belongs
58 * to. */
59 public static final String XPATH_FACETS = "facets/facet";
60
61 public static final String XPATH_HELP_URL = "/artifact-database/help-url/text()";
62
63 public static final String HELP_URL = "${help.url}";
64
65
66 /** The logger that is used in this class. */
67 private static Logger logger = Logger.getLogger(AbstractState.class);
68
69
70 /** The ID of the state. */
71 protected String id;
72
73 /** The description of the state. */
74 protected String description;
75
76 /** The help text for this state.*/
77 protected String helpText;
78
79 /** The data provided by this state. */
80 protected Map<String, StateData> data;
81
82 /** A list of output modes which are available for this state. */
83 protected List<Output> outputs;
84
85 private static String helpUrl;
86
87
88 public AbstractState() {
89 outputs = new ArrayList<Output>();
90 }
91
92 public static synchronized final String getHelpUrl() {
93 if (helpUrl == null) {
94 helpUrl = Config.getStringXPath(XPATH_HELP_URL, HELP_URL);
95 }
96 return helpUrl;
97 }
98
99 public static String replaceHelpUrl(String string) {
100 return string.replace(HELP_URL, getHelpUrl());
101 }
102
103
104 /**
105 * The default constructor.
106 *
107 * @param id The ID of the state.
108 * @param description The description of the state.
109 */
110 public AbstractState(String id, String description) {
111 super();
112
113 this.id = id;
114 this.description = description;
115 }
116
117
118 public AbstractState(String id, String description, String helpText) {
119 this(id, description);
120 this.helpText = replaceHelpUrl(helpText);
121 }
122
123
124 /**
125 * Returns the ID of the state.
126 *
127 * @return the ID of the state.
128 */
129 public String getID() {
130 return id;
131 }
132
133
134 /**
135 * Set the ID of the state.
136 *
137 * @param id The ID of the state.
138 */
139 public void setID(String id) {
140 this.id = id;
141 }
142
143
144 /**
145 * Returns the description of the state.
146 *
147 * @return the description of the state.
148 */
149 public String getDescription() {
150 return description;
151 }
152
153
154 /**
155 * Set the description of the state.
156 *
157 * @param description The description of the state.
158 */
159 public void setDescription(String description) {
160 this.description = description;
161 }
162
163
164 /**
165 * Returns the help text of this state.
166 *
167 * @return the help text.
168 */
169 public String getHelpText() {
170 return helpText;
171 }
172
173
174 /**
175 * Set the help text for this state.
176 *
177 * @param helpText The help text.
178 */
179 public void setHelpText(String helpText) {
180 this.helpText = replaceHelpUrl(helpText);
181 }
182
183
184 /**
185 * Returns the data of the state.
186 *
187 * @return the data of the state.
188 */
189 public Map<String, StateData> getData() {
190 return data;
191 }
192
193
194 /**
195 * Returns a specific data object of the state.
196 *
197 * @param name The name of the data object.
198 *
199 * @return a data object of the state or null if no such data object exists.
200 */
201 public StateData getData(String name) {
202 if (data != null) {
203 return data.get(name);
204 }
205
206 return null;
207 }
208
209
210 /**
211 * Add new data to the state. NOTE: If there is already an object existing
212 * with the key <i>name</i>, this object is overwritten by the new value.
213 *
214 * @param name The name of the data object.
215 * @param data The data object.
216 */
217 public void addData(String name, StateData data) {
218 if (this.data == null) {
219 this.data = new HashMap<String, StateData>();
220 }
221
222 this.data.put(name, data);
223 }
224
225
226 /**
227 * Returns the list of possible outputs of this state. The list is empty
228 * if no output is available for this state.
229 *
230 * @return a list of possible outputs of this state.
231 */
232 public List<Output> getOutputs() {
233 return outputs;
234 }
235
236
237 /**
238 * Initialize the state based on the state node in the configuration.
239 *
240 * @param config The state configuration node.
241 */
242 public void setup(Node config) {
243 logger.info("AbstractState.setup");
244
245 id = (String) XMLUtils.xpath(config, XPATH_ID, XPathConstants.STRING);
246
247 description = (String) XMLUtils.xpath(
248 config, XPATH_DESCRIPTION, XPathConstants.STRING);
249
250 helpText = (String) XMLUtils.xpath(
251 config, XPATH_HELP_TEXT, XPathConstants.STRING);
252
253 if (helpUrl != null) {
254 helpUrl = replaceHelpUrl(helpUrl);
255 }
256
257 setupOutputs(config);
258 }
259
260
261 /**
262 * This default implementation does nothing at all.
263 *
264 * @param orig
265 * @param owner
266 * @param context
267 * @param callMeta
268 */
269 public void initialize(
270 Artifact orig,
271 Artifact owner,
272 Object context,
273 CallMeta callMeta
274 ) {
275 // do nothing.
276 }
277
278
279 /**
280 * This method tries reading the available output nodes configured in the
281 * state configuration and adds possible Outputs to the outputs list.
282 *
283 * @param config The state configuration node.
284 */
285 protected void setupOutputs(Node config) {
286 NodeList outs = (NodeList) XMLUtils.xpath(
287 config,
288 XPATH_OUTPUT_MODES,
289 XPathConstants.NODESET,
290 ArtifactNamespaceContext.INSTANCE);
291
292 if (outs == null || outs.getLength() == 0) {
293 return;
294 }
295
296 int size = outs.getLength();
297
298 for (int i = 0; i < size; i++) {
299 addOutput(buildOutput(outs.item(i)));
300 }
301 }
302
303 /**
304 * This methods allows subclasses to manually add outputs
305 *
306 * @param out The output to add
307 */
308 protected void addOutput(Output out) {
309 outputs.add(out);
310 }
311
312 /**
313 * A helper method that creates an Output object based on the <i>out</i>
314 * node.
315 *
316 * @param out The output node configuration.
317 *
318 * @return an Output object.
319 */
320 protected Output buildOutput(Node out) {
321 String name = XMLUtils.xpathString(
322 out, "@name", ArtifactNamespaceContext.INSTANCE);
323
324 String desc = XMLUtils.xpathString(
325 out, "@description", ArtifactNamespaceContext.INSTANCE);
326
327 String mimetype = XMLUtils.xpathString(
328 out, "@mime-type", ArtifactNamespaceContext.INSTANCE);
329
330 String type = XMLUtils.xpathString(
331 out, "@type", ArtifactNamespaceContext.INSTANCE);
332
333 if (name == null) {
334 return null;
335 }
336
337 NodeList facets = (NodeList) XMLUtils.xpath(
338 out,
339 XPATH_FACETS,
340 XPathConstants.NODESET,
341 ArtifactNamespaceContext.INSTANCE);
342
343 if (facets == null || facets.getLength() == 0) {
344 return new DefaultOutput(name, desc, mimetype, type);
345 }
346
347 int num = facets.getLength();
348
349 List<Facet> facetList = new ArrayList<Facet>(num);
350
351 for (int i = 0; i < num; i++) {
352 Facet facet = buildFacet(facets.item(i));
353
354 if (facet != null) {
355 facetList.add(facet);
356 }
357 }
358
359 return new DefaultOutput(name, desc, mimetype, facetList, type);
360 }
361
362
363 /**
364 * A helper method that creates a Facet object based on the <i>facet</i>
365 * node.
366 *
367 * @param facet The facet node.
368 *
369 * @return a Facet object or null if no valid Facet was found.
370 */
371 protected Facet buildFacet(Node facet) {
372 String name = XMLUtils.xpathString(
373 facet, "@name", ArtifactNamespaceContext.INSTANCE);
374
375 String desc = XMLUtils.xpathString(
376 facet, "@description", ArtifactNamespaceContext.INSTANCE);
377
378 return name != null ? new DefaultFacet(name, desc) : null;
379 }
380
381
382 /**
383 * Describes the UI of the state. This method needs to be implemented by
384 * concrete subclasses.
385 *
386 * @param artifact A reference to the artifact this state belongs to.
387 * @param document Describe doucment.
388 * @param rootNode Parent node for all new elements.
389 * @param context The CallContext.
390 * @param uuid The uuid of an artifact.
391 */
392 public abstract Element describe(
393 Artifact artifact,
394 Document document,
395 Node rootNode,
396 CallContext context,
397 String uuid
398 );
399
400
401 @Override
402 public void endOfLife(Artifact artifact, Object context) {
403 // nothing to do here
404 }
405 }
406 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org