comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/CollectionMonitor.java @ 937:9e813e9137a5

Added a monitor that creates new artifacts for default themes in charts and maps. flys-artifacts/trunk@2329 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 14 Jul 2011 11:27:01 +0000
parents
children f4439e015278
comparison
equal deleted inserted replaced
936:759808931a2e 937:9e813e9137a5
1 package de.intevation.flys.artifacts;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8
9 import javax.xml.xpath.XPathConstants;
10
11 import org.apache.log4j.Logger;
12
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.artifacts.Artifact;
19 import de.intevation.artifacts.ArtifactDatabase;
20 import de.intevation.artifacts.ArtifactDatabaseException;
21 import de.intevation.artifacts.ArtifactNamespaceContext;
22 import de.intevation.artifacts.CallContext;
23 import de.intevation.artifacts.Hook;
24
25 import de.intevation.artifacts.common.utils.ClientProtocolUtils;
26 import de.intevation.artifacts.common.utils.Config;
27 import de.intevation.artifacts.common.utils.XMLUtils;
28
29
30 public class CollectionMonitor implements Hook {
31
32 public static final String XPATH_UUID = "/art:result/art:uuid/@art:value";
33 public static final String XPATH_HASH = "/art:result/art:hash/@art:value";
34
35
36 public class LoadArtifact {
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
56
57 private static final Logger logger =
58 Logger.getLogger(CollectionMonitor.class);
59
60
61 @Override
62 public void setup(Node cfg) {
63 Element config = (Element) cfg;
64 String xlink = config.getAttribute("xlink:href");
65 xlink = Config.replaceConfigDir(xlink);
66
67 File file = new File(xlink);
68
69 if (file == null || !file.exists()) {
70 logger.error("The config file '" + xlink + "' does not exist.");
71 return;
72 }
73
74 Document outputDefaults = XMLUtils.parseDocument(file);
75
76 NodeList states = (NodeList) XMLUtils.xpath(
77 outputDefaults,
78 XPATH_STATES,
79 XPathConstants.NODESET);
80
81 int len = states != null ? states.getLength() : 0;
82
83 this.states = new HashMap<String, List<LoadArtifact>>(len);
84
85 for (int i = 0; i < len; i++) {
86 Element state = (Element) states.item(i);
87
88 String stateId = state.getAttribute("id");
89 List<LoadArtifact> artifacts = parseLoadArtifacts(state);
90
91 if (artifacts != null) {
92 this.states.put(stateId, artifacts);
93 }
94 }
95 }
96
97
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
138 public void execute(Artifact artifact, CallContext context) {
139 FLYSArtifact flys = (FLYSArtifact) artifact;
140
141 String stateId = flys.getCurrentStateId();
142
143 List<LoadArtifact> loadArtifacts = states.get(stateId);
144
145 if (loadArtifacts == null || loadArtifacts.isEmpty()) {
146 return;
147 }
148
149 ArtifactDatabase db = context.getDatabase();
150 List<String> uuids = new ArrayList<String>();
151
152 for (LoadArtifact rawArtifact: loadArtifacts) {
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 }
169
170
171 protected String[] createArtifact(
172 ArtifactDatabase db,
173 CallContext cc,
174 LoadArtifact raw)
175 {
176 Document create = ClientProtocolUtils.newCreateDocument(raw.factory);
177
178 try {
179 Document result = db.createArtifactWithFactory(
180 raw.factory, cc.getMeta(), create);
181
182 String uuid = XMLUtils.xpathString(
183 result, XPATH_UUID, ArtifactNamespaceContext.INSTANCE);
184 String hash = XMLUtils.xpathString(
185 result, XPATH_HASH, ArtifactNamespaceContext.INSTANCE);
186
187 return new String[] {uuid, hash};
188 }
189 catch (ArtifactDatabaseException ade) {
190 logger.error(ade, ade);
191 }
192
193 return null;
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 }
238 }
239 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org