comparison gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/GNVArtifactBase.java @ 470:b7bb66440cc8

Added mechanism for advancing to previous states. gnv-artifacts/trunk@533 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 12 Jan 2010 15:25:32 +0000
parents 70df44021a9f
children a6a33ef35809
comparison
equal deleted inserted replaced
469:62fc63d0f71d 470:b7bb66440cc8
19 import org.w3c.dom.Node; 19 import org.w3c.dom.Node;
20 import org.w3c.dom.NodeList; 20 import org.w3c.dom.NodeList;
21 21
22 import de.intevation.artifactdatabase.Config; 22 import de.intevation.artifactdatabase.Config;
23 import de.intevation.artifactdatabase.DefaultArtifact; 23 import de.intevation.artifactdatabase.DefaultArtifact;
24 import de.intevation.artifactdatabase.ProxyArtifact;
24 import de.intevation.artifactdatabase.XMLUtils; 25 import de.intevation.artifactdatabase.XMLUtils;
25 import de.intevation.artifacts.ArtifactFactory; 26 import de.intevation.artifacts.ArtifactFactory;
26 import de.intevation.artifacts.ArtifactNamespaceContext; 27 import de.intevation.artifacts.ArtifactNamespaceContext;
27 import de.intevation.artifacts.CallContext; 28 import de.intevation.artifacts.CallContext;
28 import de.intevation.artifacts.CallMeta; 29 import de.intevation.artifacts.CallMeta;
29 import de.intevation.gnv.artifacts.context.GNVArtifactContext; 30 import de.intevation.gnv.artifacts.context.GNVArtifactContext;
31 import de.intevation.gnv.artifacts.fis.FISSelectArtifact;
30 import de.intevation.gnv.artifacts.fis.product.Product; 32 import de.intevation.gnv.artifacts.fis.product.Product;
31 import de.intevation.gnv.artifacts.ressource.RessourceFactory; 33 import de.intevation.gnv.artifacts.ressource.RessourceFactory;
32 import de.intevation.gnv.state.DefaultInputData; 34 import de.intevation.gnv.state.DefaultInputData;
33 import de.intevation.gnv.state.InputData; 35 import de.intevation.gnv.state.InputData;
34 import de.intevation.gnv.state.InputValue; 36 import de.intevation.gnv.state.InputValue;
78 80
79 public static final String XPATH_OUTPUT_NAME = "/art:action/art:out/@name"; 81 public static final String XPATH_OUTPUT_NAME = "/art:action/art:out/@name";
80 82
81 public static final String XPATH_OUTPUT_PARAMS = "/art:action/art:out/art:params/art:input"; 83 public static final String XPATH_OUTPUT_PARAMS = "/art:action/art:out/art:params/art:input";
82 84
85 public static final String INITIAL_STATE = "start";
86
83 /** 87 /**
84 * The current State 88 * The current State
85 */ 89 */
86 protected State current = null; 90 protected State current = null;
87 91
104 /** 108 /**
105 * The Name of the Artifact 109 * The Name of the Artifact
106 */ 110 */
107 protected String name = null; 111 protected String name = null;
108 112
109 private ArtifactXMLUtilities xmlUtilities = new ArtifactXMLUtilities();
110
111 /** 113 /**
112 * Constructor 114 * Constructor
113 */ 115 */
114 public GNVArtifactBase() { 116 public GNVArtifactBase() {
115 super(); 117 super();
116 } 118 }
117 119
118 /** 120
119 * @see de.intevation.artifactdatabase.DefaultArtifact#advance(org.w3c.dom.Document,
120 * de.intevation.artifacts.CallContext)
121 */
122 @Override 121 @Override
123 public Document advance(Document target, CallContext context) { 122 public Document advance(Document target, CallContext context) {
124 log.debug("GNVArtifactBase.advance"); 123 log.debug("GNVArtifactBase.advance()");
125 Document result = XMLUtils.newDocument(); 124
125 Document result = XMLUtils.newDocument();
126 String targetState = XMLUtils.xpathString(
127 target, XPATH_TARGET_NAME, ArtifactNamespaceContext.INSTANCE
128 );
129
130 // no current state...
131 if (current == null) {
132 log.debug("No current state. Advance not possible.");
133
134 result = createReport(
135 result,
136 "exceptionreport",
137 "exception",
138 "No State activated."
139 );
140
141 return result;
142 }
143
144 State next = null;
145
126 try { 146 try {
127 if (this.current != null) { 147
128 String stateName = this.readStateName(target); 148 // step forward
129 log.debug("Statename: " + stateName); 149 if (isStateCurrentlyReachable(targetState)) {
130 if (this.isStateCurrentlyReachable(stateName)) { 150 next = states.get(targetState);
131 try { 151
132 State nextStep = this.states 152 // 2. Transfer Results
133 .get(stateName); 153 next.putInputData(current.getInputData(), identifier);
134 // 1.Calculate Results 154 next.setParent(current);
135 this.current.advance(super.identifier, context.getMeta()); 155
136 // 2. Transfer Results 156 // 3. Switch to next State
137 nextStep.putInputData(this.current.getInputData(), 157 current = next;
138 super.identifier); 158
139 // 3. Switch to next State 159 // 4. Initialize next Step
140 this.current = nextStep; 160 current.initialize(identifier, context.getMeta());
141 161
142 // 4. Initialize next Step 162 result = createReport(
143 this.current.initialize(super.identifier, context.getMeta()); 163 result, "result", "success", "Advance success"
144 164 );
145 result = new ArtifactXMLUtilities() 165 }
146 .createSuccessReport("Advance success", 166
147 XMLUtils.newDocument()); 167 // step backward
148 } catch (StateException e) { 168 else if((next = getPreviousState(current, targetState)) != null) {
149 log.error(e, e); 169 resetFutureStates(current, targetState);
150 result = new ArtifactXMLUtilities() 170 current = next;
151 .createExceptionReport(e 171
152 .getLocalizedMessage(), XMLUtils 172 result = createReport(
153 .newDocument()); 173 result, "result", "success", "Advance success"
154 } 174 );
155 } else { 175 }
156 String msg = "Statetransition is not supported"; 176
157 log.error(msg); 177 // goto initial step
158 result = new ArtifactXMLUtilities().createExceptionReport( 178 else if(targetState.equals(INITIAL_STATE)) {
159 msg, XMLUtils.newDocument()); 179 FISSelectArtifact select = new FISSelectArtifact();
160 } 180 select.setup(identifier, new GNVArtifactFactory(), context);
161 } else { 181 context.putContextValue(ProxyArtifact.REPLACE_PROXY, select);
162 String msg = "No State activated."; 182
163 log.error(msg); 183 result = createReport(
164 result = new ArtifactXMLUtilities().createExceptionReport(msg, 184 result, "result", "success", "Advance success"
165 XMLUtils.newDocument()); 185 );
166 } 186 }
167 } catch (Exception e) { 187
168 log.error(e, e); 188 // advance not possible
169 result = new ArtifactXMLUtilities().createExceptionReport(e 189 else {
170 .getLocalizedMessage(), XMLUtils.newDocument()); 190 log.warn("advance not possible for target: " + targetState);
171 } 191 result = createReport(
192 result,
193 "exceptionreport",
194 "exception",
195 "Statetransition not supported"
196 );
197 }
198 }
199 catch (StateException se) {
200 log.error(se, se);
201 result = createReport(
202 result,
203 "exceptionreport",
204 "exception",
205 se.getLocalizedMessage()
206 );
207 }
208
172 return result; 209 return result;
173 } 210 }
174 211
175 212
213 protected Document createReport(
214 Document document,
215 String nodeName,
216 String state,
217 String msg
218 ) {
219 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
220 document,
221 ArtifactNamespaceContext.NAMESPACE_URI,
222 ArtifactNamespaceContext.NAMESPACE_PREFIX
223 );
224
225 Element reportNode = creator.create(nodeName);
226 Element stateNode = creator.create(state);
227
228 stateNode.setTextContent(msg);
229 reportNode.appendChild(stateNode);
230 document.appendChild(reportNode);
231
232 return document;
233
234 }
235
236
237 protected State getPreviousState(State current, String name) {
238 if (current == null) {
239 return null;
240 }
241
242 if (current.getID().equals(name)) {
243 return current;
244 }
245 else {
246 return getPreviousState(current.getParent(), name);
247 }
248 }
249
250
251 protected void resetFutureStates(State current, String name) {
252 if (current == null) {
253 return;
254 }
255
256 if (current.getID().equals(name)) {
257 return;
258 }
259 else {
260 current.reset(identifier);
261 resetFutureStates(current.getParent(), name);
262 }
263 }
264
265
176 private boolean isStateCurrentlyReachable(String stateid){ 266 private boolean isStateCurrentlyReachable(String stateid){
177 log.debug("GNVArtifactBase.isStateCurrentlyReachable "+stateid); 267 log.debug("GNVArtifactBase.isStateCurrentlyReachable "+stateid);
178 Iterator<Transition> it = this.transitions.iterator(); 268 Iterator<Transition> it = this.transitions.iterator();
179 String from = this.current.getID(); 269 String from = this.current.getID();
180 while (it.hasNext()){ 270 while (it.hasNext()){
183 if (transition.getTo().equals(stateid) && transition.isValid(this.current)){ 273 if (transition.getTo().equals(stateid) && transition.isValid(this.current)){
184 return true; 274 return true;
185 } 275 }
186 } 276 }
187 } 277 }
278 log.debug("State is not reachable.");
188 return false; 279 return false;
189 } 280 }
190 281
191 public Document initialize (CallContext context) { 282 public Document initialize (CallContext context) {
192 Document result = XMLUtils.newDocument(); 283 Document result = XMLUtils.newDocument();
326 .getLength()); 417 .getLength());
327 for (int i = 0; i < stateList.getLength(); i++) { 418 for (int i = 0; i < stateList.getLength(); i++) {
328 State tmpState = StateFactory.getInstance() 419 State tmpState = StateFactory.getInstance()
329 .createState(stateList.item(i)); 420 .createState(stateList.item(i));
330 if (tmpState != null) { 421 if (tmpState != null) {
422 log.debug("Initiate new state: " + tmpState.getID());
331 this.states.put(tmpState.getID(), tmpState); 423 this.states.put(tmpState.getID(), tmpState);
332 if (this.current == null) { 424 if (this.current == null) {
333 this.current = tmpState; 425 this.current = tmpState;
334 } 426 }
335 } 427 }
357 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( 449 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
358 document, 450 document,
359 ArtifactNamespaceContext.NAMESPACE_URI, 451 ArtifactNamespaceContext.NAMESPACE_URI,
360 ArtifactNamespaceContext.NAMESPACE_PREFIX 452 ArtifactNamespaceContext.NAMESPACE_PREFIX
361 ); 453 );
362 Element rootNode = this.createRootNode(document); 454 Element rootNode = this.createRootNode(creator, document);
363 this.createHeader(creator, rootNode, document, "describe"); 455 this.createHeader(creator, rootNode, document, "describe");
364 this.createOutputs(creator, rootNode, document); 456 this.createOutputs(creator, rootNode, document);
365 this.createCurrentState(creator, rootNode, document); 457 this.createCurrentState(creator, rootNode, document);
366 this.createReachableStates(creator, rootNode, document); 458 this.createReachableStates(creator, rootNode, document);
367 this.createModel(creator, rootNode, document); 459 this.createModel(creator, rootNode, document);
382 } 474 }
383 log.debug("INCLUDE UI? " + includeUI); 475 log.debug("INCLUDE UI? " + includeUI);
384 return includeUI; 476 return includeUI;
385 } 477 }
386 478
387 protected Element createRootNode(Document document) { 479 protected Element createRootNode(
388 Element rootNode = xmlUtilities.createArtifactElement(document, 480 XMLUtils.ElementCreator creator,
389 "result"); 481 Document document
482 ) {
483 Element rootNode = creator.create("result");
390 document.appendChild(rootNode); 484 document.appendChild(rootNode);
391 return rootNode; 485 return rootNode;
392 } 486 }
393 487
394 protected void createHeader( 488 protected void createHeader(

http://dive4elements.wald.intevation.org