Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/GNVArtifactBase.java @ 127:f6f0e4ce4a35
merged gnv-artifacts/0.1
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:13:41 +0200 |
parents | ef157bd2fa92 |
children | 7fb9441dd8af |
comparison
equal
deleted
inserted
replaced
49:94a07d1d9316 | 127:f6f0e4ce4a35 |
---|---|
1 /** | |
2 * | |
3 */ | |
4 package de.intevation.gnv.artifacts; | |
5 | |
6 import java.io.IOException; | |
7 import java.io.OutputStream; | |
8 import java.util.Collection; | |
9 import java.util.HashMap; | |
10 import java.util.Iterator; | |
11 import java.util.Map; | |
12 | |
13 import javax.xml.xpath.XPathConstants; | |
14 | |
15 import org.apache.log4j.Logger; | |
16 import org.w3c.dom.Document; | |
17 import org.w3c.dom.Element; | |
18 import org.w3c.dom.Node; | |
19 import org.w3c.dom.NodeList; | |
20 | |
21 import de.intevation.artifactdatabase.Config; | |
22 import de.intevation.artifactdatabase.DefaultArtifact; | |
23 import de.intevation.artifactdatabase.XMLUtils; | |
24 import de.intevation.artifacts.ArtifactFactory; | |
25 import de.intevation.artifacts.ArtifactNamespaceContext; | |
26 import de.intevation.artifacts.CallContext; | |
27 import de.intevation.artifacts.CallMeta; | |
28 import de.intevation.gnv.artifacts.context.GNVArtifactContext; | |
29 import de.intevation.gnv.transition.DefaultInputData; | |
30 import de.intevation.gnv.transition.InputData; | |
31 import de.intevation.gnv.transition.InputValue; | |
32 import de.intevation.gnv.transition.OutputMode; | |
33 import de.intevation.gnv.transition.OutputTransition; | |
34 import de.intevation.gnv.transition.Transition; | |
35 import de.intevation.gnv.transition.TransitionFactory; | |
36 import de.intevation.gnv.transition.exception.TransitionException; | |
37 import de.intevation.gnv.utils.ArtifactXMLUtilities; | |
38 | |
39 /** | |
40 * @author Tim Englich <tim.englich@intevation.de> | |
41 * | |
42 */ | |
43 public abstract class GNVArtifactBase extends DefaultArtifact { | |
44 /** | |
45 * the logger, used to log exceptions and additonaly information | |
46 */ | |
47 private static Logger log = Logger.getLogger(GNVArtifactBase.class); | |
48 /** | |
49 * The UID of this Class | |
50 */ | |
51 private static final long serialVersionUID = -8907096744400741458L; | |
52 | |
53 /** | |
54 * The Identifier for the Replacement of the Artifactname | |
55 */ | |
56 public static final String XPATH_IDENTIFIER_REPLACE = "IDENTIFIER"; | |
57 | |
58 /** | |
59 * The XPATH to the XML-Fragment that should be used for the Configuration | |
60 */ | |
61 public static final String XPATH_ARTIFACT_CONFIGURATION= "/artifact-database/artifacts/artifact[@name='"+XPATH_IDENTIFIER_REPLACE+"']"; | |
62 | |
63 | |
64 /** | |
65 * The current Transition | |
66 */ | |
67 protected Transition current = null; | |
68 | |
69 /** | |
70 * The Transitions that can be used | |
71 */ | |
72 protected Map<String, Transition> transitions = null; | |
73 | |
74 /** | |
75 * The Name of the Artifact | |
76 */ | |
77 protected String name = null; | |
78 | |
79 private ArtifactXMLUtilities xmlUtilities = new ArtifactXMLUtilities(); | |
80 | |
81 /** | |
82 * Constructor | |
83 */ | |
84 public GNVArtifactBase() { | |
85 super(); | |
86 } | |
87 | |
88 /** | |
89 * @see de.intevation.artifactdatabase.DefaultArtifact#advance(org.w3c.dom.Document, de.intevation.artifacts.CallContext) | |
90 */ | |
91 @Override | |
92 public Document advance(Document target, CallContext context) { | |
93 log.debug("GNVArtifactBase.advance"); | |
94 String uuid = Config.getStringXPath(target, "action/uuid/@value"); | |
95 Document result = XMLUtils.newDocument(); | |
96 try { | |
97 if (this.current != null){ | |
98 String transitionName = this.readTransitionName(target); | |
99 log.debug("Transitionsname: "+transitionName); | |
100 if (this.current.isTransitionReachable(transitionName)){ | |
101 // 1. Prüfung ob Transition valide ist | |
102 if (this.current.validate()){ | |
103 | |
104 try { | |
105 Transition nextStep = this.transitions.get(transitionName); | |
106 // 2.Ergebnisse Berechnen | |
107 this.current.advance(uuid,context.getMeta()); | |
108 // 3. Ergebnisse übergeben | |
109 nextStep.setDescibeData(this.current.getDescibeData()); | |
110 nextStep.putInputData(this.current.getInputData(),uuid); | |
111 // 4. Umschalten auf neue Transistion | |
112 this.current = nextStep; | |
113 result = new ArtifactXMLUtilities().createSuccessReport("Advance success", XMLUtils.newDocument()); | |
114 } catch (TransitionException e) { | |
115 log.error(e,e); | |
116 result = new ArtifactXMLUtilities().createExceptionReport(e.getLocalizedMessage(), XMLUtils.newDocument()); | |
117 } | |
118 | |
119 }else{ | |
120 String msg = "Advance nicht möglich, da die Bedingungen für den Übergang " + | |
121 "in den neuen Zustand noch nicht gegeben ist."; | |
122 log.error(msg); | |
123 result = new ArtifactXMLUtilities().createExceptionReport(msg, XMLUtils.newDocument()); | |
124 } | |
125 | |
126 }else{ | |
127 String msg = "Transitionsübergang wird nicht unterstützt."; | |
128 log.error(msg); | |
129 result = new ArtifactXMLUtilities().createExceptionReport(msg, XMLUtils.newDocument()); | |
130 } | |
131 }else{ | |
132 String msg = "Kein Transitionsschritt aktiviert."; | |
133 log.error(msg); | |
134 result = new ArtifactXMLUtilities().createExceptionReport(msg, XMLUtils.newDocument()); | |
135 } | |
136 } catch (Exception e) { | |
137 log.error(e,e); | |
138 result = new ArtifactXMLUtilities().createExceptionReport(e.getLocalizedMessage(), XMLUtils.newDocument()); | |
139 } | |
140 return result; | |
141 } | |
142 | |
143 protected String readTransitionName(Document document) { | |
144 String returnValue = Config.getStringXPath(document, "action/target/@name"); | |
145 return returnValue; | |
146 } | |
147 | |
148 protected Node getConfigurationFragment(Document document){ | |
149 log.debug("GNVArtifactBase.getConfigurationFragment"); | |
150 String xpathQuery = XPATH_ARTIFACT_CONFIGURATION.replaceAll(XPATH_IDENTIFIER_REPLACE, this.name); | |
151 log.debug(xpathQuery); | |
152 return Config.getNodeXPath(document,xpathQuery); | |
153 } | |
154 | |
155 /** | |
156 * @see de.intevation.artifactdatabase.DefaultArtifact#feed(org.w3c.dom.Document, de.intevation.artifacts.CallContext) | |
157 */ | |
158 @Override | |
159 public Document feed(Document target, CallContext context) { | |
160 log.debug("GNVArtifactBase.feed"); | |
161 Document result = XMLUtils.newDocument(); | |
162 try { | |
163 if (this.current != null){ | |
164 String uuid = Config.getStringXPath(target, "action/uuid/@value"); | |
165 this.current.putInputData(this.parseInputData(target, "/action/data/input"),uuid); | |
166 result = new ArtifactXMLUtilities().createSuccessReport("Feed success", XMLUtils.newDocument()); | |
167 }else{ | |
168 String msg = "No Transition instantiated"; | |
169 log.warn(msg); | |
170 result = new ArtifactXMLUtilities().createExceptionReport(msg, XMLUtils.newDocument()); | |
171 } | |
172 } catch (TransitionException e) { | |
173 log.error(e,e); | |
174 result = new ArtifactXMLUtilities().createExceptionReport(e.getLocalizedMessage(), XMLUtils.newDocument()); | |
175 } | |
176 return result; | |
177 } | |
178 | |
179 /** | |
180 * @see de.intevation.artifactdatabase.DefaultArtifact#setup(java.lang.String, java.lang.Object) | |
181 */ | |
182 @Override | |
183 public void setup(String identifier, ArtifactFactory factory, Object context) { | |
184 log.debug("GNVArtifactBase.setup"); | |
185 super.setup(identifier, factory, context); | |
186 | |
187 Object localContext = context; | |
188 if (context instanceof CallContext){ | |
189 localContext = ((CallContext)context).globalContext(); | |
190 | |
191 } | |
192 | |
193 if (localContext instanceof GNVArtifactContext){ | |
194 GNVArtifactContext gnvContext = (GNVArtifactContext)localContext; | |
195 Document doc = gnvContext.getConfig(); | |
196 Node artifactNode = this.getConfigurationFragment(doc); | |
197 NodeList transitionList = Config.getNodeSetXPath(artifactNode, "transitions/transition"); | |
198 this.transitions = new HashMap<String, Transition>(transitionList.getLength()); | |
199 for (int i = 0 ; i < transitionList.getLength(); i++){ | |
200 Transition tmpTransition = TransitionFactory.getInstance().createTransition(transitionList.item(i)); | |
201 if (tmpTransition != null){ | |
202 this.transitions.put(tmpTransition.getID(), tmpTransition); | |
203 if (this.current == null){ | |
204 this.current = tmpTransition; | |
205 } | |
206 } | |
207 } | |
208 | |
209 } | |
210 } | |
211 | |
212 | |
213 protected Document createDescibeOutput(CallMeta callMeta){ | |
214 log.debug("GNVArtifactBase.createDescibeOutput"); | |
215 Document document = XMLUtils.newDocument(); | |
216 Element rootNode = this.createRootNode(document); | |
217 this.createHeader(rootNode, document, "describe"); | |
218 this.createOutputs(rootNode, document); | |
219 this.createCurrentState(rootNode, document); | |
220 this.createReachableStates(rootNode, document); | |
221 this.createModel(rootNode, document); | |
222 this.createUserInterface(rootNode, document,callMeta); | |
223 return document; | |
224 } | |
225 | |
226 protected Element createRootNode(Document document){ | |
227 Element rootNode = xmlUtilities.createArtifactElement(document,"result"); | |
228 document.appendChild(rootNode); | |
229 return rootNode; | |
230 } | |
231 | |
232 protected void createHeader(Element parent, Document document, String documentType){ | |
233 Element typeNode = xmlUtilities.createArtifactElement(document,"type"); | |
234 typeNode.setAttribute("name", documentType); | |
235 parent.appendChild(typeNode); | |
236 | |
237 Element uuidNode = xmlUtilities.createArtifactElement(document,"uuid"); | |
238 uuidNode.setAttribute("value", super.identifier); | |
239 parent.appendChild(uuidNode); | |
240 | |
241 Element hashNode = xmlUtilities.createArtifactElement(document,"hash"); | |
242 hashNode.setAttribute("value", this.hash()); | |
243 parent.appendChild(hashNode); | |
244 } | |
245 | |
246 protected void createReachableStates(Element parent,Document document){ | |
247 Element stateNode = xmlUtilities.createArtifactElement(document,"reachable-states"); | |
248 if (this.current != null){ | |
249 Iterator<String> states = this.current.reachableTransitions().iterator(); | |
250 while(states.hasNext()){ | |
251 String value = states.next(); | |
252 Element currentNode = xmlUtilities.createArtifactElement(document,"state"); | |
253 currentNode.setAttribute("name", value); | |
254 log.debug("Reachable State: "+value); | |
255 currentNode.setAttribute("description", transitions.get(value).getDescription()); | |
256 stateNode.appendChild(currentNode); | |
257 } | |
258 } | |
259 parent.appendChild(stateNode); | |
260 } | |
261 | |
262 protected void createCurrentState(Element parent, Document document){ | |
263 Element stateNode = xmlUtilities.createArtifactElement(document,"state"); | |
264 stateNode.setAttribute("name", this.current.getID()); | |
265 stateNode.setAttribute("description", this.current.getDescription()); | |
266 parent.appendChild(stateNode); | |
267 } | |
268 | |
269 | |
270 protected void createModel(Element parent, Document document){ | |
271 Element modelNode = xmlUtilities.createArtifactElement(document,"model"); | |
272 if (this.current != null){ | |
273 Collection<InputValue> inputValues = this.current.getRequiredInputValues(); | |
274 if (inputValues != null){ | |
275 Iterator<InputValue> it = inputValues.iterator(); | |
276 while(it.hasNext()){ | |
277 InputValue inputValue = it.next(); | |
278 Element inputNode = xmlUtilities.createArtifactElement(document,"input"); | |
279 inputNode.setAttribute("name", inputValue.getName()); | |
280 inputNode.setAttribute("type", inputValue.getType()); | |
281 modelNode.appendChild(inputNode); | |
282 } | |
283 } | |
284 } | |
285 parent.appendChild(modelNode); | |
286 } | |
287 | |
288 protected void createUserInterface(Element parent, Document document, CallMeta callMeta){ | |
289 Element uiNode = xmlUtilities.createArtifactElement(document,"ui"); | |
290 | |
291 if (this.current != null){ | |
292 this.current.describe(document, uiNode,callMeta); | |
293 } | |
294 | |
295 parent.appendChild(uiNode); | |
296 } | |
297 | |
298 protected void createOutputs(Element parent, Document document){ | |
299 log.debug("GNVArtifactBase.createOutputs"); | |
300 Element outputsNode = xmlUtilities.createArtifactElement(document,"outputs"); | |
301 if (this.current instanceof OutputTransition){ | |
302 Collection<OutputMode> outputModes = ((OutputTransition)this.current).getOutputModes(); | |
303 if (outputModes != null){ | |
304 Iterator<OutputMode> it = outputModes.iterator(); | |
305 while(it.hasNext()){ | |
306 OutputMode outputMode = it.next(); | |
307 log.debug("Write Outputnode for "+ outputMode.toString()); | |
308 Element outputModeNode = xmlUtilities.createArtifactElement(document,"output"); | |
309 outputModeNode.setAttribute("name", outputMode.getName()); | |
310 outputModeNode.setAttribute("description", outputMode.getDescription()); | |
311 outputModeNode.setAttribute("mime-type", outputMode.getMimeType()); | |
312 outputsNode.appendChild(outputModeNode); | |
313 | |
314 Collection<InputValue> inputParameters = outputMode.getInputParameters(); | |
315 if (inputParameters != null){ | |
316 Element inputParametersNode = xmlUtilities.createArtifactElement(document,"parameter"); | |
317 outputModeNode.appendChild(inputParametersNode); | |
318 Iterator<InputValue> it2 = inputParameters.iterator(); | |
319 while (it2.hasNext()){ | |
320 InputValue inputValue = it2.next(); | |
321 Element inputParameterNode = xmlUtilities.createArtifactElement(document,"parameter"); | |
322 inputParametersNode.appendChild(inputParameterNode); | |
323 inputParameterNode.setAttribute("name", inputValue.getName()); | |
324 inputParameterNode.setAttribute("type", inputValue.getType()); | |
325 inputParameterNode.setAttribute("value", inputValue.getDefaultValue()); | |
326 } | |
327 } | |
328 } | |
329 }else{ | |
330 log.warn("No Outputmodes given."); | |
331 } | |
332 } | |
333 parent.appendChild(outputsNode); | |
334 } | |
335 | |
336 | |
337 protected Collection<InputData> parseInputData(Document document, String xPath){ | |
338 log.debug("GNVArtifactBase.parseInputData"); | |
339 HashMap<String,InputData> returnValue = null; | |
340 | |
341 log.debug(new ArtifactXMLUtilities().writeDocument2String(document)); | |
342 | |
343 NodeList inputElemets = (NodeList)XMLUtils.xpath(document, xPath,XPathConstants.NODESET, ArtifactNamespaceContext.INSTANCE);//Config.getNodeSetXPath(document, ""); | |
344 if(inputElemets != null){ | |
345 returnValue = new HashMap<String,InputData>(inputElemets.getLength()); | |
346 for (int i = 0; i < inputElemets.getLength(); i++){ | |
347 Node inputDataNode = inputElemets.item(i); | |
348 String name = Config.getStringXPath(inputDataNode,"@name"); | |
349 String value = Config.getStringXPath(inputDataNode,"@value"); | |
350 | |
351 if (returnValue.containsKey(name)){ | |
352 InputData inputData = returnValue.get(name); | |
353 inputData.concartValue(value); | |
354 log.debug(inputData.toString()); | |
355 returnValue.put(name, inputData); | |
356 }else{ | |
357 InputData inputData = new DefaultInputData(name,value); | |
358 | |
359 returnValue.put(name,inputData); | |
360 } | |
361 } | |
362 } | |
363 return returnValue.values(); | |
364 } | |
365 | |
366 | |
367 /** | |
368 * @see de.intevation.artifactdatabase.DefaultArtifact#out(org.w3c.dom.Document, | |
369 * java.io.OutputStream, de.intevation.artifacts.CallContext) | |
370 */ | |
371 @Override | |
372 public void out(Document format, OutputStream outputStream, | |
373 CallContext context) throws IOException { | |
374 log.debug("TGNVArtifactBase.out"); | |
375 try { | |
376 | |
377 if (current != null && current instanceof OutputTransition) { | |
378 String uuid = Config.getStringXPath(format, | |
379 "action/uuid/@value"); | |
380 ((OutputTransition) current) | |
381 .out(this.readOutputType(format), this.parseInputData( | |
382 format, "/action/out/params/input"), | |
383 outputStream, uuid, context.getMeta()); | |
384 } | |
385 } catch (TransitionException e) { | |
386 log.error(e, e); | |
387 throw new IOException(e.getMessage()); | |
388 } | |
389 } | |
390 | |
391 | |
392 protected String readOutputType(Document document){ | |
393 String value = Config.getStringXPath(document,"action/out/@name"); | |
394 return value; | |
395 } | |
396 } |