comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/CollectionMonitor.java @ 940:f4439e015278

Append artifact recommendations to the artifact's describe document. flys-artifacts/trunk@2334 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 14 Jul 2011 15:30:55 +0000
parents 9e813e9137a5
children ff6ce301c472
comparison
equal deleted inserted replaced
939:32d0a543e3e8 940:f4439e015278
14 import org.w3c.dom.Element; 14 import org.w3c.dom.Element;
15 import org.w3c.dom.Node; 15 import org.w3c.dom.Node;
16 import org.w3c.dom.NodeList; 16 import org.w3c.dom.NodeList;
17 17
18 import de.intevation.artifacts.Artifact; 18 import de.intevation.artifacts.Artifact;
19 import de.intevation.artifacts.ArtifactDatabase;
20 import de.intevation.artifacts.ArtifactDatabaseException;
21 import de.intevation.artifacts.ArtifactNamespaceContext; 19 import de.intevation.artifacts.ArtifactNamespaceContext;
22 import de.intevation.artifacts.CallContext; 20 import de.intevation.artifacts.CallContext;
23 import de.intevation.artifacts.Hook; 21 import de.intevation.artifacts.Hook;
24 22
25 import de.intevation.artifacts.common.utils.ClientProtocolUtils;
26 import de.intevation.artifacts.common.utils.Config; 23 import de.intevation.artifacts.common.utils.Config;
27 import de.intevation.artifacts.common.utils.XMLUtils; 24 import de.intevation.artifacts.common.utils.XMLUtils;
25 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
28 26
29 27
30 public class CollectionMonitor implements Hook { 28 public class CollectionMonitor implements Hook {
31 29
32 public static final String XPATH_UUID = "/art:result/art:uuid/@art:value"; 30 public static final String XPATH_STATES = "output-defaults/state";
33 public static final String XPATH_HASH = "/art:result/art:hash/@art:value"; 31 public static final String XPATH_RESULT = "/art:result";
34 32
35 33
36 public class LoadArtifact { 34 protected Map<String, List<String>> states;
37 public String factory;
38 public List<String> parameters;
39
40 public LoadArtifact(String factory) {
41 this.factory = factory;
42 this.parameters = new ArrayList<String>();
43 }
44
45 public void addParameter(String parameter) {
46 parameters.add(parameter);
47 }
48 } // end of class LoadArtifact
49
50
51 public static final String XPATH_STATES = "output-defaults/state";
52
53
54 protected Map<String, List<LoadArtifact>> states;
55 35
56 36
57 private static final Logger logger = 37 private static final Logger logger =
58 Logger.getLogger(CollectionMonitor.class); 38 Logger.getLogger(CollectionMonitor.class);
59 39
78 XPATH_STATES, 58 XPATH_STATES,
79 XPathConstants.NODESET); 59 XPathConstants.NODESET);
80 60
81 int len = states != null ? states.getLength() : 0; 61 int len = states != null ? states.getLength() : 0;
82 62
83 this.states = new HashMap<String, List<LoadArtifact>>(len); 63 this.states = new HashMap<String, List<String>>(len);
84 64
85 for (int i = 0; i < len; i++) { 65 for (int i = 0; i < len; i++) {
86 Element state = (Element) states.item(i); 66 Element state = (Element) states.item(i);
87 67
88 String stateId = state.getAttribute("id"); 68 String stateId = state.getAttribute("id");
89 List<LoadArtifact> artifacts = parseLoadArtifacts(state); 69 String factory = state.getAttribute("artifact-factory");
90 70
91 if (artifacts != null) { 71 if (stateId != null && factory != null) {
92 this.states.put(stateId, artifacts); 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);
93 } 80 }
94 } 81 }
95 } 82 }
96 83
97 84
98 protected List<LoadArtifact> parseLoadArtifacts(Element state) {
99 NodeList artifacts = (NodeList) XMLUtils.xpath(
100 state, "artifact", XPathConstants.NODESET);
101
102 int len = artifacts != null ? artifacts.getLength() : 0;
103
104 List<LoadArtifact> loadArtifacts = new ArrayList<LoadArtifact>(len);
105
106 for (int i = 0; i < len; i++) {
107 LoadArtifact la = parseLoadArtifact((Element) artifacts.item(i));
108
109 if (la != null) {
110 loadArtifacts.add(la);
111 }
112 }
113
114 return loadArtifacts;
115 }
116
117
118 protected LoadArtifact parseLoadArtifact(Element art) {
119 String factory = art.getAttribute("factory");
120
121 LoadArtifact artifact = new LoadArtifact(factory);
122
123 NodeList parameters = (NodeList) XMLUtils.xpath(
124 art, "parameter", XPathConstants.NODESET);
125
126 int len = parameters != null ? parameters.getLength() : 0;
127
128 for (int i = 0; i < len; i++) {
129 Element parameter = (Element) parameters.item(i);
130 artifact.addParameter(parameter.getAttribute("name"));
131 }
132
133 return artifact;
134 }
135
136
137 @Override 85 @Override
138 public void execute(Artifact artifact, CallContext context) { 86 public void execute(Artifact artifact, CallContext context, Document doc) {
139 FLYSArtifact flys = (FLYSArtifact) artifact; 87 FLYSArtifact flys = (FLYSArtifact) artifact;
140 88
141 String stateId = flys.getCurrentStateId(); 89 String stateId = flys.getCurrentStateId();
142 90
143 List<LoadArtifact> loadArtifacts = states.get(stateId); 91 List<String> factories = states.get(stateId);
144 92
145 if (loadArtifacts == null || loadArtifacts.isEmpty()) { 93 if (factories == null || factories.isEmpty()) {
146 return; 94 return;
147 } 95 }
148 96
149 ArtifactDatabase db = context.getDatabase(); 97 logger.info("Found " + factories.size() + " recommended artifacts.");
150 List<String> uuids = new ArrayList<String>();
151 98
152 for (LoadArtifact rawArtifact: loadArtifacts) { 99 appendRecommendations(doc, factories);
153 String[] art = createArtifact(db, context, rawArtifact);
154
155 if (art != null && art.length >= 2) {
156 Document feed = prepareFeed(
157 artifact, rawArtifact, art[0], art[1]);
158
159 boolean success = initializeArtifact(db, context, feed, art[0]);
160
161 if (success) {
162 uuids.add(art[0]);
163 }
164 }
165 }
166
167 // TODO ADD UUIDS TO DESCRIBE
168 } 100 }
169 101
170 102
171 protected String[] createArtifact( 103 protected void appendRecommendations(Document doc, List<String> factories) {
172 ArtifactDatabase db, 104 Element result = (Element) XMLUtils.xpath(
173 CallContext cc, 105 doc,
174 LoadArtifact raw) 106 XPATH_RESULT,
175 { 107 XPathConstants.NODE,
176 Document create = ClientProtocolUtils.newCreateDocument(raw.factory); 108 ArtifactNamespaceContext.INSTANCE);
177 109
178 try { 110 ElementCreator creator = new ElementCreator(
179 Document result = db.createArtifactWithFactory( 111 doc,
180 raw.factory, cc.getMeta(), create); 112 ArtifactNamespaceContext.NAMESPACE_URI,
113 ArtifactNamespaceContext.NAMESPACE_PREFIX);
181 114
182 String uuid = XMLUtils.xpathString( 115 Element recommended = creator.create("recommended-artifacts");
183 result, XPATH_UUID, ArtifactNamespaceContext.INSTANCE);
184 String hash = XMLUtils.xpathString(
185 result, XPATH_HASH, ArtifactNamespaceContext.INSTANCE);
186 116
187 return new String[] {uuid, hash}; 117 for (String factory: factories) {
188 } 118 Element fac = creator.create("artifact-factory");
189 catch (ArtifactDatabaseException ade) { 119 creator.addAttr(fac, "name", factory);
190 logger.error(ade, ade); 120
121 recommended.appendChild(fac);
191 } 122 }
192 123
193 return null; 124 result.appendChild(recommended);
194 }
195
196
197 protected Document prepareFeed(
198 Artifact artifact,
199 LoadArtifact rawArtifact,
200 String uuid,
201 String hash)
202 {
203 FLYSArtifact flys = (FLYSArtifact) artifact;
204
205 String[][] data = new String[rawArtifact.parameters.size()][2];
206
207 for (int i = 0, len = rawArtifact.parameters.size(); i < len; i++) {
208 String param = rawArtifact.parameters.get(i);
209 String value = flys.getDataAsString(param);
210
211 if (value != null) {
212 data[i][0] = param;
213 data[i][1] = value;
214 }
215 }
216
217 return ClientProtocolUtils.newFeedDocument(uuid, hash, data);
218 }
219
220
221 protected boolean initializeArtifact(
222 ArtifactDatabase db,
223 CallContext cc,
224 Document feed,
225 String uuid)
226 {
227 try {
228 db.feed(uuid, feed, cc.getMeta());
229
230 return true;
231 }
232 catch (ArtifactDatabaseException adbe) {
233 logger.error(adbe, adbe);
234 }
235
236 return false;
237 } 125 }
238 } 126 }
239 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 127 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org