comparison flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java @ 347:a63d8bdb2d79

Improved the out() operation of a Collection - the OutGenerator gets to know about the facets and its theme. flys-artifacts/trunk@1749 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 27 Apr 2011 14:35:44 +0000
parents 16161de47662
children 2465dc296395
comparison
equal deleted inserted replaced
346:16161de47662 347:a63d8bdb2d79
2 2
3 import java.io.IOException; 3 import java.io.IOException;
4 import java.io.OutputStream; 4 import java.io.OutputStream;
5 import java.util.ArrayList; 5 import java.util.ArrayList;
6 import java.util.Date; 6 import java.util.Date;
7 import java.util.HashMap;
7 import java.util.List; 8 import java.util.List;
8 import java.util.Map; 9 import java.util.Map;
9 10
10 import javax.xml.xpath.XPathConstants; 11 import javax.xml.xpath.XPathConstants;
11 12
28 import de.intevation.artifactdatabase.Backend; 29 import de.intevation.artifactdatabase.Backend;
29 import de.intevation.artifactdatabase.Backend.PersistentArtifact; 30 import de.intevation.artifactdatabase.Backend.PersistentArtifact;
30 import de.intevation.artifactdatabase.DefaultArtifactCollection; 31 import de.intevation.artifactdatabase.DefaultArtifactCollection;
31 32
32 import de.intevation.flys.artifacts.context.FLYSContext; 33 import de.intevation.flys.artifacts.context.FLYSContext;
34 import de.intevation.flys.artifacts.model.ManagedFacet;
33 import de.intevation.flys.exports.OutGenerator; 35 import de.intevation.flys.exports.OutGenerator;
34 36
35 37
36 /** 38 /**
37 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> 39 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
138 } 140 }
139 141
140 generator.init(format, out, context); 142 generator.init(format, out, context);
141 143
142 try { 144 try {
143 Object[][] container = getArtifactsWithAttribute(context); 145 doOut(generator, getAttribute(context, name), context);
144 146 }
145 for (Object[] obj: container) { 147 catch (ArtifactDatabaseException adbe) {
146 generator.doOut((Artifact) obj[0], (Document) obj[1]); 148 log.error(adbe, adbe);
149 }
150 }
151
152
153 /**
154 * This method creates the concrete output.
155 *
156 * @param generator The OutGenerator that creates the output.
157 * @param attributes The collection's attributes for this concrete output
158 * type.
159 * @param context The context object.
160 */
161 protected void doOut(
162 OutGenerator generator,
163 Document attributes,
164 CallContext context)
165 throws IOException
166 {
167 log.debug("FLYSArtifactCollection.doOut");
168
169 ThemeList themeList = new ThemeList(attributes);
170
171 int size = themeList.size();
172 log.debug("Output will contain " + size + " elements.");
173
174 try {
175 for (int i = 0; i < size; i++) {
176 ManagedFacet theme = themeList.get(i);
177
178 if (theme == null) {
179 continue;
180 }
181
182 String art = theme.getArtifact();
183
184 if (log.isDebugEnabled()) {
185 log.debug("Do output for...");
186 log.debug("... artifact: " + art);
187 log.debug("... facet: " + theme.getName());
188 }
189
190 generator.doOut(
191 getArtifact(art, context),
192 getArtifactAttribute(art, context));
147 } 193 }
148 } 194 }
149 catch (ArtifactDatabaseException ade) { 195 catch (ArtifactDatabaseException ade) {
150 log.error(ade, ade); 196 log.error(ade, ade);
151 } 197 }
180 } 226 }
181 227
182 aParser.parse(oldAttr); 228 aParser.parse(oldAttr);
183 229
184 return new AttributeWriter(aParser.getOuts(), oParser.getOuts()).write(); 230 return new AttributeWriter(aParser.getOuts(), oParser.getOuts()).write();
231 }
232
233
234 /**
235 * Returns the attribute for a specific output type.
236 *
237 * @param output The name of the desired output type.
238 *
239 * @return the attribute for the desired output type.
240 */
241 protected Document getAttribute(CallContext context, String output)
242 throws ArtifactDatabaseException
243 {
244 Document attr = buildAttributes(
245 context.getDatabase(),
246 context,
247 getAttribute(),
248 getArtifactUUIDs(context));
249
250 Node out = (Node) XMLUtils.xpath(
251 attr,
252 "art:output[@name='" + output + "']",
253 XPathConstants.NODE,
254 ArtifactNamespaceContext.INSTANCE);
255
256
257 if (out != null) {
258 Document o = XMLUtils.newDocument();
259
260 o.appendChild(o.importNode(out, true));
261
262 return o;
263 }
264
265 return null;
185 } 266 }
186 267
187 268
188 /** 269 /**
189 * This method returns the list of artifact UUIDs that this collections 270 * This method returns the list of artifact UUIDs that this collections
271 return null; 352 return null;
272 } 353 }
273 354
274 355
275 /** 356 /**
276 * Returns a list of Artifact/Attribute pairs.
277 *
278 * @param context The CallContext.
279 *
280 * @return a list of Artifact/Attribute pairs.
281 */
282 protected Object[][] getArtifactsWithAttribute(CallContext context)
283 throws ArtifactDatabaseException
284 {
285 log.debug("FLYSArtifactCollection.getArtifactWithAttribute");
286
287 ArtifactDatabase db = context.getDatabase();
288 CallMeta meta = context.getMeta();
289
290 String[] uuids = getArtifactUUIDs(context);
291 Object[][] awa = new Object[uuids.length][2];
292
293 for (int i = 0; i < uuids.length; i++) {
294 try {
295 Artifact artifact = getArtifact(uuids[i], context);
296 Document attribute = getArtifactAttribute(uuids[i], context);
297
298 if (artifact == null) {
299 log.warn("Artifact '" + uuids[i] + "' is not existing.");
300 continue;
301 }
302
303 awa[i][0] = artifact;
304 awa[i][1] = attribute;
305 }
306 catch (ArtifactDatabaseException ade) {
307 log.warn(ade, ade);
308 }
309 }
310
311 return awa;
312 }
313
314
315 /**
316 * Returns the OutGenerator for a specified <i>type</i>. 357 * Returns the OutGenerator for a specified <i>type</i>.
317 * 358 *
318 * @param name The name of the output type. 359 * @param name The name of the output type.
319 * @param type Defines the type of the desired OutGenerator. 360 * @param type Defines the type of the desired OutGenerator.
320 * 361 *
384 ci.appendChild(doc.importNode(outputModes, true)); 425 ci.appendChild(doc.importNode(outputModes, true));
385 } 426 }
386 427
387 return ci; 428 return ci;
388 } 429 }
430
431
432 /**
433 * Inner class to structure/order the themes of a chart.
434 */
435 private class ThemeList {
436 private Logger logger = Logger.getLogger(ThemeList.class);
437 protected Map<Integer, ManagedFacet> themes;
438
439 public ThemeList(Document output) {
440 themes = new HashMap<Integer, ManagedFacet>();
441 parse(output);
442 }
443
444 protected void parse(Document output) {
445 NodeList themes = (NodeList) XMLUtils.xpath(
446 output,
447 "art:output/art:theme",
448 XPathConstants.NODESET,
449 ArtifactNamespaceContext.INSTANCE);
450
451 int num = themes != null ? themes.getLength() : 0;
452
453 logger.debug("Output has " + num + " elements.");
454
455 if (num == 0) {
456 return;
457 }
458
459 for (int i = 0; i < num; i++) {
460 Node theme = themes.item(i);
461
462 String name = XMLUtils.xpathString(
463 theme, "@art:facet", ArtifactNamespaceContext.INSTANCE);
464
465 String uuid = XMLUtils.xpathString(
466 theme, "@art:artifact", ArtifactNamespaceContext.INSTANCE);
467
468 String pos = XMLUtils.xpathString(
469 theme, "@art:pos", ArtifactNamespaceContext.INSTANCE);
470
471 String active = XMLUtils.xpathString(
472 theme, "@art:active", ArtifactNamespaceContext.INSTANCE);
473
474 addTheme(uuid, name, pos, active);
475 }
476 }
477
478 protected void addTheme(
479 String uuid,
480 String name,
481 String position,
482 String active)
483 {
484 if (logger.isDebugEnabled()) {
485 logger.debug("Add theme: ");
486 logger.debug(".. name: " + name);
487 logger.debug(".. uuid: " + uuid);
488 logger.debug(".. position: " + position);
489 logger.debug(".. active: " + active);
490 }
491
492 try {
493 Integer pos = new Integer(position);
494 Integer act = new Integer(active);
495
496 themes.put(pos, new ManagedFacet(name, null, uuid, pos, act));
497 }
498 catch (NumberFormatException nfe) {
499 // do nothing
500 }
501 }
502
503 public ManagedFacet get(int idx) {
504 return themes.get(new Integer(idx));
505 }
506
507 public int size() {
508 return themes.size();
509 }
510 }
389 } 511 }
390 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 512 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org