comparison flys-artifacts/src/main/java/de/intevation/flys/collections/OutputParser.java @ 3814:8083f6384023

merged flys-artifacts/pre2.6-2012-01-04
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:56 +0200
parents 2fe270661b20
children 22732713c54d
comparison
equal deleted inserted replaced
1491:2a00f4849738 3814:8083f6384023
1 package de.intevation.flys.collections;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 import org.apache.log4j.Logger;
9
10 import de.intevation.artifacts.ArtifactDatabase;
11 import de.intevation.artifacts.ArtifactDatabaseException;
12 import de.intevation.artifacts.CallContext;
13 import de.intevation.artifacts.CallMeta;
14
15 import de.intevation.artifactdatabase.state.DefaultOutput;
16 import de.intevation.artifactdatabase.state.Facet;
17 import de.intevation.artifactdatabase.state.Output;
18
19 import de.intevation.flys.artifacts.FLYSArtifact;
20 import de.intevation.flys.artifacts.model.ManagedFacetAdapter;
21
22
23 public class OutputParser {
24
25 /** Constant XPath that points to the outputmodes of an artifact. */
26 public static final String XPATH_ARTIFACT_OUTPUTMODES =
27 "/art:result/art:outputmodes/art:output";
28
29 private static Logger logger = Logger.getLogger(OutputParser.class);
30
31 protected ArtifactDatabase db;
32 protected CallMeta meta;
33 protected CallContext context;
34
35 /** Map outputs name to Output. */
36 protected Map<String, Output> outs;
37
38 /** Map facets name to list of Facets. */
39 protected List<Facet> facets;
40
41
42 /**
43 * @param db Database used to fetch artifacts, outputs and facets.
44 */
45 public OutputParser(ArtifactDatabase db, CallContext context) {
46 this.db = db;
47 this.meta = context.getMeta();
48 this.context = context;
49 this.outs = new HashMap<String, Output>();
50 this.facets = new ArrayList<Facet>();
51 }
52
53
54 /**
55 * Gets raw artifact with given id and sorts outputs in mapping.
56 * Converts Facets to ManagedFacets on the way.
57 * @param uuid uuid of artifact to load from database.
58 */
59 public void parse(String uuid)
60 throws ArtifactDatabaseException
61 {
62 logger.debug("OutputParser.parse: " + uuid);
63
64 FLYSArtifact flys = (FLYSArtifact) db.getRawArtifact(uuid);
65
66 List<Output> outList = flys.getOutputs(context);
67
68 for (Output out: outList) {
69 String name = out.getName();
70
71 Output o = outs.get(name);
72 int pos = 1;
73
74 if (o == null) {
75 o = new DefaultOutput(
76 out.getName(),
77 out.getDescription(),
78 out.getMimeType(),
79 new ArrayList<Facet>(),
80 out.getType());
81 outs.put(name, o);
82 }
83 else {
84 logger.debug("OutputParser.parse: Use 'old' Output");
85 pos = o.getFacets().size() + 1;
86 }
87
88 List<Facet> mfacets = facet2ManagedFacet(uuid, out.getFacets(), pos);
89 o.addFacets(mfacets);
90 this.facets.addAll(mfacets);
91 }
92 }
93
94
95 /**
96 * Access mapping of Outputname to Output.
97 */
98 public Map<String, Output> getOuts() {
99 return outs;
100 }
101
102
103 /**
104 * Access all facets.
105 */
106 public List<Facet> getFacets() {
107 return this.facets;
108 }
109
110
111 /**
112 * Creates a list of ManagedFacets from list of Facets.
113 * @param pos Position of first facet (for each other the positions
114 * will be increased).
115 */
116 protected List<Facet> facet2ManagedFacet(
117 String uuid,
118 List<Facet> old,
119 int pos)
120 {
121 List<Facet> newFacets = new ArrayList<Facet>(old.size());
122
123 for (Facet f: old) {
124 newFacets.add(new ManagedFacetAdapter(f, uuid, pos++, 1, 1));
125 }
126
127 return newFacets;
128 }
129 }
130 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org