Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/CollectionMonitor.java @ 964:ff6ce301c472
Modified the CollectionMonitor (for recommended artifacts) to use the DataCage.
flys-artifacts/trunk@2384 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Thu, 21 Jul 2011 10:45:52 +0000 |
parents | f4439e015278 |
children | 79251b1d47da |
comparison
equal
deleted
inserted
replaced
963:f48cef242e7f | 964:ff6ce301c472 |
---|---|
1 package de.intevation.flys.artifacts; | 1 package de.intevation.flys.artifacts; |
2 | 2 |
3 import java.io.File; | |
4 import java.util.ArrayList; | |
5 import java.util.HashMap; | 3 import java.util.HashMap; |
6 import java.util.List; | 4 import java.util.List; |
7 import java.util.Map; | 5 import java.util.Map; |
8 | 6 |
9 import javax.xml.xpath.XPathConstants; | 7 import javax.xml.xpath.XPathConstants; |
11 import org.apache.log4j.Logger; | 9 import org.apache.log4j.Logger; |
12 | 10 |
13 import org.w3c.dom.Document; | 11 import org.w3c.dom.Document; |
14 import org.w3c.dom.Element; | 12 import org.w3c.dom.Element; |
15 import org.w3c.dom.Node; | 13 import org.w3c.dom.Node; |
16 import org.w3c.dom.NodeList; | |
17 | 14 |
18 import de.intevation.artifacts.Artifact; | 15 import de.intevation.artifacts.Artifact; |
19 import de.intevation.artifacts.ArtifactNamespaceContext; | 16 import de.intevation.artifacts.ArtifactNamespaceContext; |
20 import de.intevation.artifacts.CallContext; | 17 import de.intevation.artifacts.CallContext; |
21 import de.intevation.artifacts.Hook; | 18 import de.intevation.artifacts.Hook; |
22 | 19 |
23 import de.intevation.artifacts.common.utils.Config; | |
24 import de.intevation.artifacts.common.utils.XMLUtils; | 20 import de.intevation.artifacts.common.utils.XMLUtils; |
25 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator; | 21 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator; |
22 | |
23 import de.intevation.artifactdatabase.state.Output; | |
24 | |
25 import de.intevation.flys.artifacts.services.meta.DataCage; | |
26 | 26 |
27 | 27 |
28 public class CollectionMonitor implements Hook { | 28 public class CollectionMonitor implements Hook { |
29 | 29 |
30 public static final String XPATH_STATES = "output-defaults/state"; | |
31 public static final String XPATH_RESULT = "/art:result"; | 30 public static final String XPATH_RESULT = "/art:result"; |
32 | |
33 | |
34 protected Map<String, List<String>> states; | |
35 | 31 |
36 | 32 |
37 private static final Logger logger = | 33 private static final Logger logger = |
38 Logger.getLogger(CollectionMonitor.class); | 34 Logger.getLogger(CollectionMonitor.class); |
39 | 35 |
40 | 36 |
41 @Override | 37 @Override |
42 public void setup(Node cfg) { | 38 public void setup(Node cfg) { |
43 Element config = (Element) cfg; | |
44 String xlink = config.getAttribute("xlink:href"); | |
45 xlink = Config.replaceConfigDir(xlink); | |
46 | |
47 File file = new File(xlink); | |
48 | |
49 if (file == null || !file.exists()) { | |
50 logger.error("The config file '" + xlink + "' does not exist."); | |
51 return; | |
52 } | |
53 | |
54 Document outputDefaults = XMLUtils.parseDocument(file); | |
55 | |
56 NodeList states = (NodeList) XMLUtils.xpath( | |
57 outputDefaults, | |
58 XPATH_STATES, | |
59 XPathConstants.NODESET); | |
60 | |
61 int len = states != null ? states.getLength() : 0; | |
62 | |
63 this.states = new HashMap<String, List<String>>(len); | |
64 | |
65 for (int i = 0; i < len; i++) { | |
66 Element state = (Element) states.item(i); | |
67 | |
68 String stateId = state.getAttribute("id"); | |
69 String factory = state.getAttribute("artifact-factory"); | |
70 | |
71 if (stateId != null && factory != null) { | |
72 List<String> factories = this.states.get(stateId); | |
73 | |
74 if (factories == null) { | |
75 factories = new ArrayList<String>(); | |
76 this.states.put(stateId, factories); | |
77 } | |
78 | |
79 factories.add(factory); | |
80 } | |
81 } | |
82 } | 39 } |
83 | 40 |
84 | 41 |
85 @Override | 42 @Override |
86 public void execute(Artifact artifact, CallContext context, Document doc) { | 43 public void execute(Artifact artifact, CallContext context, Document doc) { |
87 FLYSArtifact flys = (FLYSArtifact) artifact; | 44 FLYSArtifact flys = (FLYSArtifact) artifact; |
88 | 45 |
89 String stateId = flys.getCurrentStateId(); | |
90 | |
91 List<String> factories = states.get(stateId); | |
92 | |
93 if (factories == null || factories.isEmpty()) { | |
94 return; | |
95 } | |
96 | |
97 logger.info("Found " + factories.size() + " recommended artifacts."); | |
98 | |
99 appendRecommendations(doc, factories); | |
100 } | |
101 | |
102 | |
103 protected void appendRecommendations(Document doc, List<String> factories) { | |
104 Element result = (Element) XMLUtils.xpath( | 46 Element result = (Element) XMLUtils.xpath( |
105 doc, | 47 doc, |
106 XPATH_RESULT, | 48 XPATH_RESULT, |
107 XPathConstants.NODE, | 49 XPathConstants.NODE, |
108 ArtifactNamespaceContext.INSTANCE); | 50 ArtifactNamespaceContext.INSTANCE); |
111 doc, | 53 doc, |
112 ArtifactNamespaceContext.NAMESPACE_URI, | 54 ArtifactNamespaceContext.NAMESPACE_URI, |
113 ArtifactNamespaceContext.NAMESPACE_PREFIX); | 55 ArtifactNamespaceContext.NAMESPACE_PREFIX); |
114 | 56 |
115 Element recommended = creator.create("recommended-artifacts"); | 57 Element recommended = creator.create("recommended-artifacts"); |
58 result.appendChild(recommended); | |
116 | 59 |
117 for (String factory: factories) { | 60 String[] outs = extractOutputNames(flys, context); |
118 Element fac = creator.create("artifact-factory"); | 61 Map<String, Object> params = getDataCageParameters(flys, context); |
119 creator.addAttr(fac, "name", factory); | |
120 | 62 |
121 recommended.appendChild(fac); | 63 DataCage dc = DataCage.getInstance(); |
64 dc.recommend(flys, outs, params, recommended); | |
65 } | |
66 | |
67 | |
68 public static String[] extractOutputNames( | |
69 FLYSArtifact flys, | |
70 CallContext context) | |
71 { | |
72 List<Output> outs = flys.getOutputs(context); | |
73 String[] names = new String[outs.size()]; | |
74 | |
75 for (int i = 0, num = outs != null ? outs.size() : 0; i < num; i++) { | |
76 names[i] = outs.get(i).getName(); | |
122 } | 77 } |
123 | 78 |
124 result.appendChild(recommended); | 79 return names; |
80 } | |
81 | |
82 | |
83 protected Map<String, Object> getDataCageParameters( | |
84 FLYSArtifact flys, | |
85 CallContext context) | |
86 { | |
87 Map<String, Object> params = new HashMap<String, Object>(1); | |
88 params.put("recommended", "true"); | |
89 | |
90 return params; | |
125 } | 91 } |
126 } | 92 } |
127 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 93 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |