Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java @ 1190:f514894ec2fd
merged flys-artifacts/2.5
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:17 +0200 |
parents | 47ecf98f09eb |
children | 16c74ca3586e |
comparison
equal
deleted
inserted
replaced
917:b48c36076e17 | 1190:f514894ec2fd |
---|---|
1 package de.intevation.flys.artifacts; | |
2 | |
3 import de.intevation.artifactdatabase.ArtifactDatabaseImpl; | |
4 import de.intevation.artifactdatabase.DefaultArtifact; | |
5 | |
6 import de.intevation.artifactdatabase.data.StateData; | |
7 | |
8 import de.intevation.artifactdatabase.state.DefaultFacet; | |
9 import de.intevation.artifactdatabase.state.DefaultOutput; | |
10 import de.intevation.artifactdatabase.state.Facet; | |
11 import de.intevation.artifactdatabase.state.Output; | |
12 import de.intevation.artifactdatabase.state.State; | |
13 import de.intevation.artifactdatabase.state.StateEngine; | |
14 | |
15 import de.intevation.artifactdatabase.transition.TransitionEngine; | |
16 | |
17 import de.intevation.artifacts.Artifact; | |
18 import de.intevation.artifacts.ArtifactDatabase; | |
19 import de.intevation.artifacts.ArtifactDatabaseException; | |
20 import de.intevation.artifacts.ArtifactFactory; | |
21 import de.intevation.artifacts.CallContext; | |
22 import de.intevation.artifacts.CallMeta; | |
23 | |
24 import de.intevation.artifacts.common.ArtifactNamespaceContext; | |
25 | |
26 import de.intevation.artifacts.common.utils.XMLUtils; | |
27 | |
28 import de.intevation.flys.artifacts.cache.CacheFactory; | |
29 | |
30 import de.intevation.flys.artifacts.context.FLYSContext; | |
31 | |
32 import de.intevation.flys.artifacts.states.DefaultState.ComputeType; | |
33 | |
34 import de.intevation.flys.artifacts.states.DefaultState; | |
35 | |
36 import java.util.ArrayList; | |
37 import java.util.Collection; | |
38 import java.util.HashMap; | |
39 import java.util.HashSet; | |
40 import java.util.List; | |
41 import java.util.Map; | |
42 import java.util.Set; | |
43 import java.util.TreeMap; | |
44 | |
45 import javax.xml.xpath.XPathConstants; | |
46 | |
47 import net.sf.ehcache.Cache; | |
48 | |
49 import org.apache.log4j.Logger; | |
50 | |
51 import org.w3c.dom.Document; | |
52 import org.w3c.dom.Element; | |
53 import org.w3c.dom.NodeList; | |
54 | |
55 /** | |
56 * The defaul FLYS artifact. | |
57 * | |
58 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
59 */ | |
60 public abstract class FLYSArtifact extends DefaultArtifact { | |
61 | |
62 /** The logger that is used in this artifact.*/ | |
63 private static Logger logger = Logger.getLogger(FLYSArtifact.class); | |
64 | |
65 | |
66 public static final String COMPUTING_CACHE = "computed.values"; | |
67 | |
68 /** The XPath that points to the input data elements of the FEED document.*/ | |
69 public static final String XPATH_FEED_INPUT = | |
70 "/art:action/art:data/art:input"; | |
71 | |
72 /** The XPath that points to the name of the target state of ADVANCE.*/ | |
73 public static final String XPATH_ADVANCE_TARGET = | |
74 "/art:action/art:target/@art:name"; | |
75 | |
76 public static final String XPATH_MODEL_ARTIFACT = | |
77 "/art:action/art:template/@uuid"; | |
78 | |
79 public static final String XPATH_FILTER = | |
80 "/art:action/art:filter/art:out"; | |
81 | |
82 /** The constant string that shows that an operation was successful.*/ | |
83 public static final String OPERATION_SUCCESSFUL = "SUCCESS"; | |
84 | |
85 /** The constant string that shows that an operation failed.*/ | |
86 public static final String OPERATION_FAILED = "FAILURE"; | |
87 | |
88 | |
89 /** The identifier of the current state. */ | |
90 protected String currentStateId; | |
91 | |
92 /** The identifiers of previous states on a stack.*/ | |
93 protected List<String> previousStateIds; | |
94 | |
95 /** The name of the artifact.*/ | |
96 protected String name; | |
97 | |
98 /** The data that have been inserted into this artifact.*/ | |
99 protected Map<String, StateData> data; | |
100 | |
101 /** The list of facets supported by this artifact.*/ | |
102 protected Map<String, List<Facet>> facets; | |
103 | |
104 /** out -> facets */ | |
105 protected Map<String, List<Facet>> filterFacets; | |
106 | |
107 | |
108 /** | |
109 * The default constructor that creates an empty FLYSArtifact. | |
110 */ | |
111 public FLYSArtifact() { | |
112 data = new TreeMap<String, StateData>(); | |
113 previousStateIds = new ArrayList<String>(); | |
114 facets = new HashMap<String, List<Facet>>(); | |
115 } | |
116 | |
117 | |
118 /** | |
119 * Returns the name of the concrete artifact. | |
120 * | |
121 * @return the name of the concrete artifact. | |
122 */ | |
123 public String getName() { | |
124 return name; | |
125 } | |
126 | |
127 /** | |
128 * Returns the FLYSContext from context object. | |
129 * | |
130 * @param context The CallContext or the FLYSContext. | |
131 * | |
132 * @return the FLYSContext. | |
133 */ | |
134 protected static FLYSContext getFlysContext(Object context) { | |
135 return context instanceof FLYSContext | |
136 ? (FLYSContext) context | |
137 : (FLYSContext) ((CallContext) context).globalContext(); | |
138 } | |
139 | |
140 | |
141 /** | |
142 * Initialize the artifact and insert new data if <code>data</code> contains | |
143 * information necessary for this artifact. | |
144 * | |
145 * @param identifier The UUID. | |
146 * @param factory The factory that is used to create this artifact. | |
147 * @param context The CallContext. | |
148 * @param data Some optional data. | |
149 */ | |
150 @Override | |
151 public void setup( | |
152 String identifier, | |
153 ArtifactFactory factory, | |
154 Object context, | |
155 CallMeta callMeta, | |
156 Document data) | |
157 { | |
158 logger.debug("Setup this artifact with the uuid: " + identifier); | |
159 | |
160 super.setup(identifier, factory, context, callMeta, data); | |
161 | |
162 FLYSContext flysContext = getFlysContext(context); | |
163 | |
164 List<State> states = getStates(context); | |
165 | |
166 String name = getName(); | |
167 logger.debug("Set initial state for artifact '" + name + "'"); | |
168 | |
169 setCurrentState(states.get(0)); | |
170 | |
171 String model = XMLUtils.xpathString( | |
172 data, | |
173 XPATH_MODEL_ARTIFACT, | |
174 ArtifactNamespaceContext.INSTANCE); | |
175 | |
176 if (model != null && model.length() > 0) { | |
177 ArtifactDatabase db = (ArtifactDatabase) flysContext.get( | |
178 ArtifactDatabaseImpl.GLOBAL_CONTEXT_KEY); | |
179 | |
180 try { | |
181 initialize(db.getRawArtifact(model), context, callMeta); | |
182 } | |
183 catch (ArtifactDatabaseException adbe) { | |
184 logger.error(adbe, adbe); | |
185 } | |
186 } | |
187 | |
188 filterFacets = buildFilterFacets(data); | |
189 } | |
190 | |
191 protected List<String> clonePreviousStateIds() { | |
192 return new ArrayList<String>(previousStateIds); | |
193 } | |
194 | |
195 protected Map<String, StateData> cloneData() { | |
196 Map<String, StateData> copy = new TreeMap<String, StateData>(); | |
197 | |
198 for (Map.Entry<String, StateData> entry: data.entrySet()) { | |
199 copy.put(entry.getKey(), entry.getValue().deepCopy()); | |
200 } | |
201 | |
202 return copy; | |
203 } | |
204 | |
205 protected Map<String, List<Facet>> cloneFacets() { | |
206 Map copy = new HashMap<String, List<Facet>>(); | |
207 | |
208 for (Map.Entry<String, List<Facet>> entry: facets.entrySet()) { | |
209 List<Facet> facets = entry.getValue(); | |
210 List<Facet> facetCopies = new ArrayList<Facet>(facets.size()); | |
211 for (Facet facet: facets) { | |
212 facetCopies.add(facet.deepCopy()); | |
213 } | |
214 copy.put(entry.getKey(), facetCopies); | |
215 } | |
216 | |
217 return copy; | |
218 } | |
219 | |
220 protected void initialize( | |
221 Artifact artifact, | |
222 Object context, | |
223 CallMeta callMeta) | |
224 { | |
225 if (!(artifact instanceof FLYSArtifact)) { | |
226 return; | |
227 } | |
228 | |
229 FLYSArtifact flys = (FLYSArtifact)artifact; | |
230 | |
231 currentStateId = flys.currentStateId; | |
232 previousStateIds = flys.clonePreviousStateIds(); | |
233 name = flys.name; | |
234 data = flys.cloneData(); | |
235 facets = flys.cloneFacets(); | |
236 // Do not clone filter facets! | |
237 } | |
238 | |
239 protected Map<String, List<Facet>> buildFilterFacets(Document document) { | |
240 | |
241 NodeList nodes = (NodeList)XMLUtils.xpath( | |
242 document, | |
243 XPATH_FILTER, | |
244 XPathConstants.NODESET, | |
245 ArtifactNamespaceContext.INSTANCE); | |
246 | |
247 if (nodes == null || nodes.getLength() == 0) { | |
248 return null; | |
249 } | |
250 | |
251 Map<String, List<Facet>> result = new HashMap<String, List<Facet>>(); | |
252 | |
253 for (int i = 0, N = nodes.getLength(); i < N; ++i) { | |
254 Element element = (Element)nodes.item(i); | |
255 String oName = element.getAttribute("name"); | |
256 if (oName.length() == 0) { | |
257 continue; | |
258 } | |
259 | |
260 List<Facet> facets = new ArrayList<Facet>(); | |
261 | |
262 NodeList facetNodes = element.getElementsByTagNameNS( | |
263 ArtifactNamespaceContext.NAMESPACE_URI, | |
264 "facet"); | |
265 | |
266 for (int j = 0, M = facetNodes.getLength(); j < M; ++j) { | |
267 Element facetElement = (Element)facetNodes.item(j); | |
268 | |
269 String fName = facetElement.getAttribute("name"); | |
270 | |
271 int index; | |
272 try { | |
273 index = Integer.parseInt(facetElement.getAttribute("index")); | |
274 } | |
275 catch (NumberFormatException nfe) { | |
276 logger.warn(nfe); | |
277 index = 0; | |
278 } | |
279 facets.add(new DefaultFacet(index, fName, "")); | |
280 } | |
281 | |
282 if (!facets.isEmpty()) { | |
283 result.put(oName, facets); | |
284 } | |
285 } | |
286 | |
287 return result; | |
288 } | |
289 | |
290 | |
291 /** | |
292 * Insert new data included in <code>input</code> into the current state. | |
293 * | |
294 * @param target XML document that contains new data. | |
295 * @param context The CallContext. | |
296 * | |
297 * @return a document that contains a SUCCESS or FAILURE message. | |
298 */ | |
299 @Override | |
300 public Document feed(Document target, CallContext context) { | |
301 logger.info("FLYSArtifact.feed()"); | |
302 | |
303 Document doc = XMLUtils.newDocument(); | |
304 | |
305 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
306 doc, | |
307 ArtifactNamespaceContext.NAMESPACE_URI, | |
308 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
309 | |
310 Element result = creator.create("result"); | |
311 doc.appendChild(result); | |
312 | |
313 try { | |
314 saveData(target, XPATH_FEED_INPUT, context); | |
315 | |
316 compute(context, ComputeType.FEED, true); | |
317 | |
318 return describe(target, context); | |
319 } | |
320 catch (IllegalArgumentException iae) { | |
321 // do not store state if validation fails. | |
322 context.afterCall(CallContext.NOTHING); | |
323 creator.addAttr(result, "type", OPERATION_FAILED, true); | |
324 | |
325 result.setTextContent(iae.getMessage()); | |
326 } | |
327 | |
328 return doc; | |
329 } | |
330 | |
331 | |
332 /** | |
333 * This method handles request for changing the current state of an | |
334 * artifact. It is possible to step forward or backward. | |
335 * | |
336 * @param target The incoming ADVANCE document. | |
337 * @param context The CallContext. | |
338 * | |
339 * @return a document that contains a SUCCESS or FAILURE message. | |
340 */ | |
341 public Document advance(Document target, CallContext context) { | |
342 Document doc = XMLUtils.newDocument(); | |
343 | |
344 XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator( | |
345 doc, | |
346 ArtifactNamespaceContext.NAMESPACE_URI, | |
347 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
348 | |
349 Element result = ec.create("result"); | |
350 | |
351 String targetState = XMLUtils.xpathString( | |
352 target, XPATH_ADVANCE_TARGET, ArtifactNamespaceContext.INSTANCE); | |
353 | |
354 logger.info("FLYSArtifact.advance() to '" + targetState + "'"); | |
355 | |
356 if (isStateReachable(targetState, context)) { | |
357 logger.info("Advance: Step forward"); | |
358 | |
359 List<String> prev = getPreviousStateIds(); | |
360 prev.add(getCurrentStateId()); | |
361 | |
362 setCurrentStateId(targetState); | |
363 | |
364 logger.debug("Compute data for state: " + targetState); | |
365 compute(context, ComputeType.ADVANCE, true); | |
366 | |
367 return describe(target, context); | |
368 } | |
369 else if (isPreviousState(targetState, context)) { | |
370 logger.info("Advance: Step back to"); | |
371 | |
372 List<String> prevs = getPreviousStateIds(); | |
373 int targetIdx = prevs.indexOf(targetState); | |
374 int start = prevs.size() - 1; | |
375 | |
376 destroyStates(prevs, context); | |
377 | |
378 for (int i = start; i >= targetIdx; i--) { | |
379 String prev = prevs.get(i); | |
380 logger.debug("Remove state id '" + prev + "'"); | |
381 | |
382 prevs.remove(prev); | |
383 facets.remove(prev); | |
384 } | |
385 | |
386 destroyState(getCurrentStateId(), context); | |
387 setCurrentStateId(targetState); | |
388 | |
389 return describe(target, context); | |
390 } | |
391 | |
392 logger.warn("Advance: Cannot advance to '" + targetState + "'"); | |
393 ec.addAttr(result, "type", OPERATION_FAILED, true); | |
394 | |
395 doc.appendChild(result); | |
396 | |
397 return doc; | |
398 } | |
399 | |
400 | |
401 /** | |
402 * Returns the identifier of the current state. | |
403 * | |
404 * @return the identifier of the current state. | |
405 */ | |
406 public String getCurrentStateId() { | |
407 return currentStateId; | |
408 } | |
409 | |
410 | |
411 /** | |
412 * Sets the identifier of the current state. | |
413 * | |
414 * @param id the identifier of a state. | |
415 */ | |
416 protected void setCurrentStateId(String id) { | |
417 currentStateId = id; | |
418 } | |
419 | |
420 | |
421 /** | |
422 * Set the current state of this artifact. <b>NOTE</b>We don't store the | |
423 * State object itself - which is not necessary - but its identifier. So | |
424 * this method will just call the setCurrentStateId() method with the | |
425 * identifier of <i>state</i>. | |
426 * | |
427 * @param state The new current state. | |
428 */ | |
429 protected void setCurrentState(State state) { | |
430 setCurrentStateId(state.getID()); | |
431 } | |
432 | |
433 | |
434 /** | |
435 * Returns the current state of the artifact. | |
436 * | |
437 * @return the current State of the artifact. | |
438 */ | |
439 protected State getCurrentState(Object context) { | |
440 return getState(context, getCurrentStateId()); | |
441 } | |
442 | |
443 | |
444 /** | |
445 * Get list of existant states for this Artifact. | |
446 * @param context Contex to get StateEngine from. | |
447 * @return list of states. | |
448 */ | |
449 protected List<State> getStates(Object context) { | |
450 FLYSContext flysContext = getFlysContext(context); | |
451 StateEngine engine = (StateEngine) flysContext.get( | |
452 FLYSContext.STATE_ENGINE_KEY); | |
453 return engine.getStates(getName()); | |
454 } | |
455 | |
456 | |
457 /** | |
458 * Get state with given ID. | |
459 * @param context Context to get StateEngine from. | |
460 * @param stateID ID of state to get. | |
461 * @return state with given ID. | |
462 */ | |
463 protected State getState(Object context, String stateID) { | |
464 FLYSContext flysContext = getFlysContext(context); | |
465 StateEngine engine = (StateEngine) flysContext.get( | |
466 FLYSContext.STATE_ENGINE_KEY); | |
467 return engine.getState(stateID); | |
468 } | |
469 | |
470 | |
471 /** | |
472 * Returns the vector of previous state identifiers. | |
473 * | |
474 * @return the vector of previous state identifiers. | |
475 */ | |
476 protected List<String> getPreviousStateIds() { | |
477 return previousStateIds; | |
478 } | |
479 | |
480 | |
481 /** | |
482 * Adds a new StateData item to the data pool of this artifact. | |
483 * | |
484 * @param name the name of the data object. | |
485 * @param data the data object itself. | |
486 */ | |
487 protected void addData(String name, StateData data) { | |
488 this.data.put(name, data); | |
489 } | |
490 | |
491 | |
492 /** | |
493 * This method returns a specific StateData object that is stored in the | |
494 * data pool of this artifact. | |
495 * | |
496 * @param name The name of the data object. | |
497 * | |
498 * @return the StateData object if existing, otherwise null. | |
499 */ | |
500 public StateData getData(String name) { | |
501 return data.get(name); | |
502 } | |
503 | |
504 | |
505 public String getDataAsString(String name) { | |
506 StateData data = getData(name); | |
507 return data != null ? (String) data.getValue() : null; | |
508 } | |
509 | |
510 public Collection<StateData> getAllData() { | |
511 return data.values(); | |
512 } | |
513 | |
514 | |
515 public Facet getNativeFacet(Facet facet) { | |
516 String name = facet.getName(); | |
517 int index = facet.getIndex(); | |
518 | |
519 for (Map.Entry<String, List<Facet>> entry: facets.entrySet()) { | |
520 for (Facet f: entry.getValue()) { | |
521 if (f.getIndex() == index && f.getName().equals(name)) { | |
522 return f; | |
523 } | |
524 } | |
525 } | |
526 | |
527 logger.warn("Could not find facet: " + name + " at " + index); | |
528 return null; | |
529 } | |
530 | |
531 | |
532 /** | |
533 * This method stores the data that is contained in the FEED document. | |
534 * | |
535 * @param feed The FEED document. | |
536 * @param xpath The XPath that points to the data nodes. | |
537 */ | |
538 public void saveData(Document feed, String xpath, CallContext context) | |
539 throws IllegalArgumentException | |
540 { | |
541 if (feed == null || xpath == null || xpath.length() == 0) { | |
542 throw new IllegalArgumentException("error_feed_no_data"); | |
543 } | |
544 | |
545 NodeList nodes = (NodeList) XMLUtils.xpath( | |
546 feed, | |
547 xpath, | |
548 XPathConstants.NODESET, | |
549 ArtifactNamespaceContext.INSTANCE); | |
550 | |
551 if (nodes == null || nodes.getLength() == 0) { | |
552 throw new IllegalArgumentException("error_feed_no_data"); | |
553 } | |
554 | |
555 int count = nodes.getLength(); | |
556 logger.debug("Try to save " + count + " data items."); | |
557 | |
558 String uri = ArtifactNamespaceContext.NAMESPACE_URI; | |
559 | |
560 DefaultState current = (DefaultState) getCurrentState(context); | |
561 | |
562 for (int i = 0; i < count; i++) { | |
563 Element node = (Element)nodes.item(i); | |
564 | |
565 String name = node.getAttributeNS(uri, "name"); | |
566 String value = node.getAttributeNS(uri, "value"); | |
567 | |
568 if (name.length() > 0 && value.length() > 0) { | |
569 logger.debug("Save data item for '" + name + "' : " + value); | |
570 | |
571 addData(name, current.transform(this, context, name, value)); | |
572 } | |
573 } | |
574 | |
575 current.validate(this); | |
576 } | |
577 | |
578 | |
579 /** | |
580 * Determines if the state with the identifier <i>stateId</i> is reachable | |
581 * from the current state. The determination itself takes place in the | |
582 * TransitionEngine. | |
583 * | |
584 * @param stateId The identifier of a state. | |
585 * @param context The context object. | |
586 * | |
587 * @return true, if the state specified by <i>stateId</i> is reacahble, | |
588 * otherwise false. | |
589 */ | |
590 protected boolean isStateReachable(String stateId, Object context) { | |
591 logger.debug("Determine if the state '" + stateId + "' is reachable."); | |
592 | |
593 FLYSContext flysContext = getFlysContext(context); | |
594 | |
595 State currentState = getCurrentState(context); | |
596 StateEngine sEngine = (StateEngine) flysContext.get( | |
597 FLYSContext.STATE_ENGINE_KEY); | |
598 | |
599 TransitionEngine tEngine = (TransitionEngine) flysContext.get( | |
600 FLYSContext.TRANSITION_ENGINE_KEY); | |
601 | |
602 return tEngine.isStateReachable(this, stateId, currentState, sEngine); | |
603 } | |
604 | |
605 | |
606 /** | |
607 * Determines if the state with the identifier <i>stateId</i> is a previous | |
608 * state of the current state. | |
609 * | |
610 * @param stateId The target state identifier. | |
611 * @param context The context object. | |
612 */ | |
613 protected boolean isPreviousState(String stateId, Object context) { | |
614 logger.debug("Determine if the state '" + stateId + "' is old."); | |
615 | |
616 List<String> prevs = getPreviousStateIds(); | |
617 if (prevs.contains(stateId)) { | |
618 return true; | |
619 } | |
620 | |
621 return false; | |
622 } | |
623 | |
624 | |
625 /** | |
626 * Computes the hash code of the entered values. | |
627 * | |
628 * @return a hash code. | |
629 */ | |
630 @Override | |
631 public String hash() { | |
632 Set<Map.Entry<String, StateData>> entries = data.entrySet(); | |
633 | |
634 long hash = 0L; | |
635 int shift = 3; | |
636 | |
637 for (Map.Entry<String, StateData> entry: entries) { | |
638 String key = entry.getKey(); | |
639 Object value = entry.getValue().getValue(); | |
640 | |
641 hash ^= ((long)key.hashCode() << shift) | |
642 | ((long)value.hashCode() << (shift + 3)); | |
643 shift += 2; | |
644 } | |
645 | |
646 return getCurrentStateId() + hash; | |
647 } | |
648 | |
649 protected List<Output> filterOutputs(List<Output> outs) { | |
650 | |
651 if (filterFacets == null || filterFacets.isEmpty()) { | |
652 return outs; | |
653 } | |
654 | |
655 List<Output> filtered = new ArrayList<Output>(); | |
656 | |
657 for (Output out: outs) { | |
658 | |
659 List<Facet> fFacets = filterFacets.get(out.getName()); | |
660 if (fFacets != null) { | |
661 | |
662 List<Facet> resultFacets = new ArrayList<Facet>(); | |
663 | |
664 for (Facet facet: out.getFacets()) { | |
665 for (Facet fFacet: fFacets) { | |
666 if (facet.getIndex() == fFacet.getIndex() | |
667 && facet.getName().equals(fFacet.getName())) { | |
668 resultFacets.add(facet); | |
669 break; | |
670 } | |
671 } | |
672 } | |
673 | |
674 if (!resultFacets.isEmpty()) { | |
675 DefaultOutput nout = new DefaultOutput( | |
676 out.getName(), | |
677 out.getDescription(), | |
678 out.getMimeType(), | |
679 resultFacets); | |
680 filtered.add(nout); | |
681 } | |
682 } | |
683 } | |
684 | |
685 return filtered; | |
686 } | |
687 | |
688 | |
689 public List<Output> getOutputs(Object context) { | |
690 List<String> stateIds = getPreviousStateIds(); | |
691 List<Output> generated = new ArrayList<Output>(); | |
692 | |
693 for (String stateId: stateIds) { | |
694 DefaultState state = (DefaultState) getState(context, stateId); | |
695 generated.addAll(getOutputForState(state)); | |
696 } | |
697 | |
698 generated.addAll(getCurrentOutputs(context)); | |
699 | |
700 return filterOutputs(generated); | |
701 } | |
702 | |
703 | |
704 public List<Output> getCurrentOutputs(Object context) { | |
705 DefaultState cur = (DefaultState) getCurrentState(context); | |
706 | |
707 try { | |
708 if (cur.validate(this)) { | |
709 return getOutputForState(cur); | |
710 } | |
711 } | |
712 catch (IllegalArgumentException iae) { } | |
713 | |
714 return new ArrayList<Output>(); | |
715 } | |
716 | |
717 | |
718 protected List<Output> getOutputForState(DefaultState state) { | |
719 List<Output> list = state.getOutputs(); | |
720 if (list == null || list.size() == 0) { | |
721 logger.debug("-> No output modes for this state."); | |
722 return new ArrayList<Output>(); | |
723 } | |
724 | |
725 List<Facet> fs = facets.get(state.getID()); | |
726 if (fs == null || fs.size() == 0) { | |
727 logger.debug("No facets found."); | |
728 return new ArrayList<Output>(); | |
729 } | |
730 | |
731 return generateOutputs(list, fs); | |
732 } | |
733 | |
734 | |
735 protected List<Output> generateOutputs(List<Output> list, List<Facet> fs) { | |
736 List<Output> generated = new ArrayList<Output>(); | |
737 | |
738 boolean debug = logger.isDebugEnabled(); | |
739 | |
740 for (Output out: list) { | |
741 Output o = new DefaultOutput( | |
742 out.getName(), | |
743 out.getDescription(), | |
744 out.getMimeType(), | |
745 out.getType()); | |
746 | |
747 Set<String> outTypes = new HashSet<String>(); | |
748 | |
749 for (Facet f: out.getFacets()) { | |
750 if (outTypes.add(f.getName()) && debug) { | |
751 logger.debug("configured facet " + f); | |
752 } | |
753 } | |
754 | |
755 boolean facetAdded = false; | |
756 for (Facet f: fs) { | |
757 String type = f.getName(); | |
758 | |
759 if (outTypes.contains(type)) { | |
760 if (debug) { | |
761 logger.debug("Add facet " + f); | |
762 } | |
763 facetAdded = true; | |
764 o.addFacet(f); | |
765 } | |
766 } | |
767 | |
768 if (facetAdded) { | |
769 generated.add(o); | |
770 } | |
771 } | |
772 | |
773 return generated; | |
774 } | |
775 | |
776 | |
777 /** | |
778 * Dispatches the computation request to compute(CallContext context, String | |
779 * hash) with the current hash value of the artifact which is provided by | |
780 * hash(). | |
781 * | |
782 * @param context The CallContext. | |
783 */ | |
784 public Object compute( | |
785 CallContext context, | |
786 ComputeType type, | |
787 boolean generateFacets | |
788 ) { | |
789 return compute(context, hash(), type, generateFacets); | |
790 } | |
791 | |
792 | |
793 /** | |
794 * Dispatches computation requests to the current state which needs to | |
795 * implement a createComputeCallback(String hash, FLYSArtifact artifact) | |
796 * method. | |
797 * | |
798 * @param context The CallContext. | |
799 * @param hash The hash value which is used to fetch computed data from | |
800 * cache. | |
801 * | |
802 * @return the computed data. | |
803 */ | |
804 public Object compute( | |
805 CallContext context, | |
806 String hash, | |
807 ComputeType type, | |
808 boolean generateFacets | |
809 ) { | |
810 DefaultState current = (DefaultState) getCurrentState(context); | |
811 return compute(context, hash, current, type, generateFacets); | |
812 } | |
813 | |
814 public Object compute( | |
815 CallContext context, | |
816 String hash, | |
817 String stateID, | |
818 ComputeType type, | |
819 boolean generateFacets | |
820 ) { | |
821 DefaultState current = stateID == null | |
822 ? (DefaultState)getCurrentState(context) | |
823 : (DefaultState)getState(context, stateID); | |
824 | |
825 if (hash == null) { | |
826 hash = hash(); | |
827 } | |
828 | |
829 return compute(context, hash, current, type, generateFacets); | |
830 } | |
831 | |
832 | |
833 /** | |
834 * @param key key of state | |
835 * @param state state | |
836 * @param type Type of compute | |
837 * @param generateFacets Whether new facets shall be generated. | |
838 */ | |
839 public Object compute( | |
840 CallContext context, | |
841 String key, | |
842 DefaultState state, | |
843 ComputeType type, | |
844 boolean generateFacets | |
845 ) { | |
846 String stateID = state.getID(); | |
847 | |
848 List<Facet> fs = (generateFacets) ? new ArrayList<Facet>() : null; | |
849 | |
850 try { | |
851 Cache cache = CacheFactory.getCache(COMPUTING_CACHE); | |
852 | |
853 Object old = null; | |
854 | |
855 if (cache != null) { | |
856 net.sf.ehcache.Element element = cache.get(key); | |
857 if (element != null) { | |
858 logger.debug("Got computation result from cache."); | |
859 old = element.getValue(); | |
860 } | |
861 } | |
862 | |
863 Object res; | |
864 switch (type) { | |
865 case FEED: | |
866 res = state.computeFeed(this, key, context, fs, old); | |
867 break; | |
868 case ADVANCE: | |
869 res = state.computeAdvance(this, key, context, fs, old); | |
870 break; | |
871 case INIT: | |
872 res = state.computeInit(this, key, context, context.getMeta(), fs); | |
873 default: | |
874 res = null; | |
875 } | |
876 | |
877 if (cache != null && old != res && res != null) { | |
878 logger.debug("Store computation result to cache."); | |
879 net.sf.ehcache.Element element = | |
880 new net.sf.ehcache.Element(key, res); | |
881 cache.put(element); | |
882 } | |
883 | |
884 return res; | |
885 } | |
886 finally { | |
887 if (generateFacets) { | |
888 if (fs.isEmpty()) { | |
889 facets.remove(stateID); | |
890 } | |
891 else { | |
892 facets.put(stateID, fs); | |
893 } | |
894 } | |
895 } | |
896 } | |
897 | |
898 | |
899 /** | |
900 * Method to dump the artifacts state/data. | |
901 */ | |
902 protected void dumpArtifact() { | |
903 if (logger.isDebugEnabled()) { | |
904 logger.debug("++++++++++++++ DUMP ARTIFACT DATA +++++++++++++++++"); | |
905 | |
906 logger.debug("------ DUMP DATA ------"); | |
907 Collection<StateData> allData = data.values(); | |
908 | |
909 for (StateData d: allData) { | |
910 String name = d.getName(); | |
911 String value = (String) d.getValue(); | |
912 | |
913 logger.debug("- " + name + ": " + value); | |
914 } | |
915 | |
916 logger.debug("------ DUMP PREVIOUS STATES ------"); | |
917 List<String> stateIds = getPreviousStateIds(); | |
918 | |
919 for (String id: stateIds) { | |
920 logger.debug("- State: " + id); | |
921 } | |
922 | |
923 logger.debug("CURRENT STATE: " + getCurrentStateId()); | |
924 | |
925 logger.debug("++++++++++++++ END ARTIFACT DUMP +++++++++++++++++"); | |
926 } | |
927 } | |
928 | |
929 | |
930 protected void destroyState(String id, Object context) { | |
931 State s = getState(context, id); | |
932 s.endOfLife(this, context); | |
933 } | |
934 | |
935 | |
936 /** | |
937 * Calls endOfLife() for each state in the list <i>ids</i>. | |
938 * | |
939 * @param ids The State IDs that should be destroyed. | |
940 * @param context The FLYSContext. | |
941 */ | |
942 protected void destroyStates(List<String> ids, Object context) { | |
943 for (int i = 0, num = ids.size(); i < num; i++) { | |
944 destroyState(ids.get(i), context); | |
945 } | |
946 } | |
947 | |
948 | |
949 @Override | |
950 public void endOfLife(Object context) { | |
951 logger.info("FLYSArtifact.endOfLife: " + identifier()); | |
952 | |
953 List<String> ids = getPreviousStateIds(); | |
954 ids.add(getCurrentStateId()); | |
955 | |
956 destroyStates(ids, context); | |
957 } | |
958 } | |
959 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |