comparison gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/fis/FISArtifact.java @ 71:e4ecf3188bdf

Integrated FIS-Artifact gnv-artifacts/trunk@62 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Thu, 10 Sep 2009 13:11:24 +0000
parents
children 504570de21fd
comparison
equal deleted inserted replaced
70:0035862b0295 71:e4ecf3188bdf
1 /**
2 *
3 */
4 package de.intevation.gnv.artifacts.fis;
5
6 import java.util.ArrayList;
7 import java.util.Collection;
8 import java.util.HashMap;
9 import java.util.Iterator;
10 import java.util.Map;
11
12 import org.apache.log4j.Logger;
13 import org.w3c.dom.Document;
14 import org.w3c.dom.Element;
15 import org.w3c.dom.Node;
16 import org.w3c.dom.NodeList;
17
18 import de.intevation.artifactdatabase.Config;
19 import de.intevation.artifactdatabase.DefaultArtifact;
20 import de.intevation.artifactdatabase.XMLUtils;
21 import de.intevation.artifacts.Artifact;
22 import de.intevation.artifacts.ArtifactFactory;
23 import de.intevation.gnv.artifacts.GNVArtifactBase;
24 import de.intevation.gnv.artifacts.context.GNVArtifactContext;
25 import de.intevation.gnv.artifacts.fis.product.DefaultProduct;
26 import de.intevation.gnv.artifacts.fis.product.Product;
27 import de.intevation.gnv.transition.DefaultInputData;
28 import de.intevation.gnv.transition.InputData;
29 import de.intevation.gnv.transition.InputValue;
30 import de.intevation.gnv.transition.OutputMode;
31 import de.intevation.gnv.transition.OutputTransition;
32 import de.intevation.gnv.utils.ArtifactFactoryUtilities;
33 import de.intevation.gnv.utils.ArtifactXMLUtilities;
34
35 /**
36 * @author Tim Englich <tim.englich@intevation.de>
37 *
38 */
39 public class FISArtifact extends DefaultArtifact {
40
41 /**
42 * the logger, used to log exceptions and additonaly information
43 */
44 private static Logger log = Logger.getLogger(GNVArtifactBase.class);
45 /**
46 * The UID of this Class
47 */
48 private static final long serialVersionUID = 2874044542701727083L;
49
50 /**
51 * The Identifier for the Replacement of the Artifactname
52 */
53 public static final String XPATH_IDENTIFIER_REPLACE = "IDENTIFIER";
54
55 /**
56 * The XPATH to the XML-Fragment that should be used for the Configuration
57 */
58 public static final String XPATH_ARTIFACT_CONFIGURATION= "/artifact-database/artifacts/artifact[@name='"+XPATH_IDENTIFIER_REPLACE+"']";
59
60 /**
61 * The Name of the Artifact
62 */
63 private String name = null;
64
65 private Map<String, Product> products = null;
66
67 private Artifact productArtifact = null;
68
69 private Product current = null;
70
71 private ArtifactXMLUtilities xmlUtilities = new ArtifactXMLUtilities();
72
73
74 /**
75 * @see de.intevation.artifactdatabase.DefaultArtifact#advance(org.w3c.dom.Document, java.lang.Object)
76 */
77 @Override
78 public Document advance(Document target, Object context) {
79 if (this.productArtifact == null){
80 if (this.current != null){
81 String uuid = Config.getStringXPath(target, "action/uuid/@value"); // TODO: müssen wir für das subartifact eine veränderte uuid führen?
82 String hash = Config.getStringXPath(target, "action/hash/@value");
83 this.productArtifact = this.current.getArtifactFactory().createArtifact(uuid, context);
84 Document feedDocument = xmlUtilities.reInitDocument(this.createFeedProductArtifactDocument(uuid, hash));
85 log.debug("Feed ==> "+this.xmlUtilities.writeDocument2String(feedDocument));
86 Document descibeDocument = xmlUtilities.reInitDocument(this.productArtifact.describe(context));
87 log.debug("Descibe ==> "+this.xmlUtilities.writeDocument2String(descibeDocument));
88 this.productArtifact.feed(feedDocument, context);
89 String targetName = Config.getStringXPath(descibeDocument, "result/reachable-states/state/@name");
90 Document advanceDocument = xmlUtilities.reInitDocument(this.createAdvanceProductArtifactDocument(uuid, hash, targetName));
91 log.debug("Advance ==> "+this.xmlUtilities.writeDocument2String(advanceDocument));
92 return this.productArtifact.advance(advanceDocument, context);
93 }else{
94 log.warn("Artifact is not configured properly. Call feed first.");
95 // TODO Fehlerdokument erzeugen.
96 }
97 return null;
98 }else{
99 return this.productArtifact.advance(target, context);
100 }
101 }
102 private Document createAdvanceProductArtifactDocument(String uuid, String hash, String targetName){
103 Document document = XMLUtils.newDocument();
104 Element rootNode = xmlUtilities.createArtifactElement(document, "action");
105
106 Element typeNode = xmlUtilities.createArtifactElement(document, "type");
107 typeNode.setAttribute("name", "advanve");
108 rootNode.appendChild(typeNode);
109
110 Element uuidNode = xmlUtilities.createArtifactElement(document, "uuid");
111 uuidNode.setAttribute("value", uuid);
112 rootNode.appendChild(uuidNode);
113
114 Element hashNode = xmlUtilities.createArtifactElement(document, "hash");
115 hashNode.setAttribute("value", hash);
116 rootNode.appendChild(hashNode);
117 Element targetNode = xmlUtilities.createArtifactElement(document, "target");
118 targetNode.setAttribute("name", targetName);
119 rootNode.appendChild(targetNode);
120
121 document.appendChild(rootNode);
122 return document;
123 }
124
125 private Document createFeedProductArtifactDocument(String uuid, String hash){
126 Document document = XMLUtils.newDocument();
127 Element rootNode = xmlUtilities.createArtifactElement(document, "action");
128
129 Element typeNode = xmlUtilities.createArtifactElement(document, "type");
130 typeNode.setAttribute("name", "feed");
131 rootNode.appendChild(typeNode);
132
133 Element uuidNode = xmlUtilities.createArtifactElement(document, "uuid");
134 uuidNode.setAttribute("value", uuid);
135 rootNode.appendChild(uuidNode);
136
137 Element hashNode = xmlUtilities.createArtifactElement(document, "hash");
138 hashNode.setAttribute("value", hash);
139 rootNode.appendChild(hashNode);
140
141 Element dataNode = xmlUtilities.createArtifactElement(document, "data");
142 rootNode.appendChild(dataNode);
143
144
145 Collection<InputData> parameter = this.current.getParameter();
146 if (parameter != null){
147 Iterator<InputData> parameterIt = parameter.iterator();
148 while(parameterIt.hasNext()){
149 InputData inputData = parameterIt.next();
150 Element inputNode = xmlUtilities.createArtifactElement(document, "input");
151 inputNode.setAttribute("name", inputData.getName());
152 inputNode.setAttribute("value", inputData.getValue());
153 dataNode.appendChild(inputNode);
154 }
155 }
156 document.appendChild(rootNode);
157 return document;
158
159 }
160
161 /**
162 * @see de.intevation.artifactdatabase.DefaultArtifact#describe(java.lang.Object)
163 */
164 @Override
165 public Document describe(Object context) {
166 if (this.productArtifact == null){
167 return this.createDescibeOutput();
168 }else{
169 return this.productArtifact.describe(context);
170 }
171 }
172
173 /**
174 * @see de.intevation.artifactdatabase.DefaultArtifact#feed(org.w3c.dom.Document, java.lang.Object)
175 */
176 @Override
177 public Document feed(Document target, Object context) {
178 if (this.productArtifact == null){
179 String productName = Config.getStringXPath(target, "action/data/input[@name='product']/@value");
180 if (this.products.containsKey(productName)) {
181 this.current = this.products.get(productName);
182 }else{
183 log.error("Product does not exists for "+productName);
184 // TODO: Fehlerdokument erzeugen.
185 }
186 return null;
187 }else{
188 return this.productArtifact.feed(target, context);
189 }
190 }
191
192 /**
193 * @see de.intevation.artifactdatabase.DefaultArtifact#out(org.w3c.dom.Document, java.lang.Object)
194 */
195 @Override
196 public byte[] out(Document format, Object context) {
197 byte[] returnValue = null;
198 if (this.productArtifact != null){
199 returnValue = this.productArtifact.out(format, context);
200 }
201 return returnValue;
202 }
203
204 /**
205 * Constructor
206 */
207 public FISArtifact() {
208 super();
209 }
210
211 /**
212 * @see de.intevation.artifactdatabase.DefaultArtifact#setup(java.lang.String, de.intevation.artifacts.ArtifactFactory, java.lang.Object)
213 */
214 @Override
215 public void setup(String identifier, ArtifactFactory factory, Object context) {
216 log.debug("FISArtifact.setup");
217 this.name = factory.getName();
218 super.setup(identifier,factory, context);
219 if (context instanceof GNVArtifactContext){
220 GNVArtifactContext gnvContext = (GNVArtifactContext)context;
221 Document doc = gnvContext.getConfig();
222 Node artifactNode = this.getConfigurationFragment(doc);
223
224 NodeList products = Config.getNodeSetXPath(artifactNode,"products/product");
225 if (products != null){
226 this.products = new HashMap<String, Product>(products.getLength());
227 for (int i = 0; i < products.getLength(); i++){
228 Node productNode = products.item(i);
229 String productName = Config.getStringXPath(productNode, "@name");
230 NodeList parameterNodes = Config.getNodeSetXPath(productNode, "parameters/parameter");
231 Collection<InputData> parameter = null;
232 if (parameterNodes != null){
233 parameter = new ArrayList<InputData>(parameterNodes.getLength());
234 for (int j = 0; j < parameterNodes.getLength(); j++){
235 Node parameterNode = parameterNodes.item(j);
236 String name = Config.getStringXPath(parameterNode, "@name");
237 String value = Config.getStringXPath(parameterNode, "@value");
238 parameter.add(new DefaultInputData(name, value));
239 }
240 }
241 Node artifactFactoryNode = Config.getNodeXPath(productNode, "artifact-factory");
242 ArtifactFactory artifactFactory =new ArtifactFactoryUtilities().createArtitfactFactor(doc, artifactFactoryNode);
243 this.products.put(productName, new DefaultProduct(productName, parameter,artifactFactory));
244 }
245 }
246
247
248 }
249 }
250
251 protected Node getConfigurationFragment(Document document){
252 log.debug("GNVArtifactBase.getConfigurationFragment");
253 String xpathQuery = XPATH_ARTIFACT_CONFIGURATION.replaceAll(XPATH_IDENTIFIER_REPLACE, this.name);
254 log.debug(xpathQuery);
255 return Config.getNodeXPath(document,xpathQuery);
256 }
257
258 protected Document createDescibeOutput(){
259 log.debug("GNVArtifactBase.createDescibeOutput");
260 Document document = XMLUtils.newDocument();
261 Element rootNode = this.createRootNode(document);
262 this.createHeader(rootNode, document, "describe");
263 this.createOutputs(rootNode, document);
264 this.createCurrentState(rootNode, document);
265 this.createReachableStates(rootNode, document);
266 this.createModel(rootNode, document);
267 this.createUserInterface(rootNode, document);
268
269 return document;
270 }
271
272 protected Element createRootNode(Document document){
273 Element rootNode = xmlUtilities.createArtifactElement(document,"result");
274 document.appendChild(rootNode);
275 return rootNode;
276 }
277
278 protected void createHeader(Element parent, Document document, String documentType){
279 Element typeNode = xmlUtilities.createArtifactElement(document,"type");
280 typeNode.setAttribute("name", documentType);
281 parent.appendChild(typeNode);
282
283 Element uuidNode = xmlUtilities.createArtifactElement(document,"uuid");
284 uuidNode.setAttribute("value", super.identifier);
285 parent.appendChild(uuidNode);
286
287 Element hashNode = xmlUtilities.createArtifactElement(document,"hash");
288 hashNode.setAttribute("value", this.hash());
289 parent.appendChild(hashNode);
290
291
292 }
293 protected void createReachableStates(Element parent,Document document){
294 Element stateNode = xmlUtilities.createArtifactElement(document,"reachable-states");
295 if (this.products != null){
296 Iterator<Product> products = this.products.values().iterator();
297 while(products.hasNext()){
298 Product product = products.next();
299 Element currentNode = xmlUtilities.createArtifactElement(document,"state");
300 currentNode.setAttribute("name", product.getName());
301 currentNode.setAttribute("description", product.getName());
302 stateNode.appendChild(currentNode);
303 }
304 }
305 parent.appendChild(stateNode);
306 }
307
308 protected void createCurrentState(Element parent, Document document){
309 Element stateNode = xmlUtilities.createArtifactElement(document,"state");
310 stateNode.setAttribute("name", "choose-product");
311 stateNode.setAttribute("description", "Initialer Stand Auswahl des products");
312 parent.appendChild(stateNode);
313 }
314
315
316 protected void createModel(Element parent, Document document){
317 Element modelNode = xmlUtilities.createArtifactElement(document,"model");
318
319 Element inputNode = xmlUtilities.createArtifactElement(document,"input");
320 inputNode.setAttribute("name", "product");
321 inputNode.setAttribute("type", "String");
322 modelNode.appendChild(inputNode);
323
324 parent.appendChild(modelNode);
325 }
326
327 protected void createUserInterface(Element parent, Document document){
328 Element uiNode = xmlUtilities.createArtifactElement(document,"ui");
329
330 // TODO hier selectbox mit den produktnamen
331
332 parent.appendChild(uiNode);
333 }
334
335 protected void createOutputs(Element parent, Document document){
336 log.debug("GNVArtifactBase.createOutputs");
337 Element outputsNode = xmlUtilities.createArtifactElement(document,"outputs");
338 parent.appendChild(outputsNode);
339 }
340
341 }

http://dive4elements.wald.intevation.org