Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/collections/CollectionAttribute.java @ 1976:0b466bd4ab24
Introduced a CollectionAttribute class that stores the information provided by the Collection's attribute document.
flys-artifacts/trunk@3400 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Tue, 13 Dec 2011 11:55:47 +0000 |
parents | |
children | a7c437c9547e |
comparison
equal
deleted
inserted
replaced
1975:b30e1710df1d | 1976:0b466bd4ab24 |
---|---|
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 import java.util.Set; | |
8 | |
9 import org.w3c.dom.Document; | |
10 import org.w3c.dom.Element; | |
11 import org.w3c.dom.Node; | |
12 | |
13 import org.apache.log4j.Logger; | |
14 | |
15 import de.intevation.artifacts.ArtifactNamespaceContext; | |
16 | |
17 import de.intevation.artifacts.common.utils.XMLUtils; | |
18 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator; | |
19 | |
20 import de.intevation.artifactdatabase.state.DefaultOutput; | |
21 import de.intevation.artifactdatabase.state.Facet; | |
22 import de.intevation.artifactdatabase.state.Output; | |
23 | |
24 | |
25 public class CollectionAttribute { | |
26 | |
27 private static final Logger logger = | |
28 Logger.getLogger(CollectionAttribute.class); | |
29 | |
30 | |
31 protected ElementCreator ec; | |
32 | |
33 protected Map<String, Output> outputMap; | |
34 | |
35 protected Node loadedRecommendations; | |
36 | |
37 | |
38 public CollectionAttribute() { | |
39 } | |
40 | |
41 | |
42 public void addOutput(String key, Output output) { | |
43 if (outputMap == null) { | |
44 outputMap = new HashMap<String, Output>(); | |
45 } | |
46 | |
47 if (key != null && key.length() > 0 && output != null) { | |
48 outputMap.put( | |
49 key, | |
50 new DefaultOutput( | |
51 output.getName(), | |
52 output.getDescription(), | |
53 output.getMimeType(), | |
54 new ArrayList<Facet>(), | |
55 output.getType())); | |
56 } | |
57 } | |
58 | |
59 | |
60 public void addFacet(String outputKey, Facet facet) { | |
61 if (facet == null) { | |
62 logger.warn("Tried to add empty facet."); | |
63 return; | |
64 } | |
65 | |
66 if (outputMap == null) { | |
67 logger.warn("Tried to add facet but no Outputs are existing yet."); | |
68 return; | |
69 } | |
70 | |
71 Output output = outputMap.get(outputKey); | |
72 | |
73 if (output == null) { | |
74 logger.warn("Tried to add facet for unknown Output: " + outputKey); | |
75 return; | |
76 } | |
77 | |
78 logger.debug("Add facet for '" + outputKey + "': " + facet.getName()); | |
79 output.addFacet(facet); | |
80 } | |
81 | |
82 | |
83 public void setLoadedRecommendations(Node loadedRecommendations) { | |
84 // TODO Replace this Node with a Java class object. | |
85 this.loadedRecommendations = loadedRecommendations; | |
86 } | |
87 | |
88 | |
89 public Document toXML() { | |
90 Document doc = XMLUtils.newDocument(); | |
91 | |
92 ec = new ElementCreator( | |
93 doc, | |
94 ArtifactNamespaceContext.NAMESPACE_URI, | |
95 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
96 | |
97 Element root = ec.create("attribute"); | |
98 | |
99 appendOutputs(root); | |
100 appendLoadedRecommendations(root); | |
101 | |
102 doc.appendChild(root); | |
103 | |
104 return doc; | |
105 } | |
106 | |
107 | |
108 public Map<String, Output> getOutputs() { | |
109 return outputMap; | |
110 } | |
111 | |
112 | |
113 public Output getOutput(String name) { | |
114 if (name == null || name.length() == 0) { | |
115 logger.warn("No Output name specified."); | |
116 return null; | |
117 } | |
118 | |
119 if (outputMap == null || outputMap.size() == 0) { | |
120 logger.warn("Tried to retrieve Output, but no Outputs existing."); | |
121 return null; | |
122 } | |
123 | |
124 return outputMap.get(name); | |
125 } | |
126 | |
127 | |
128 public List<Facet> getFacets(String output) { | |
129 if (output == null || output.length() == 0) { | |
130 logger.warn("No Output name specified."); | |
131 return new ArrayList<Facet>(); | |
132 } | |
133 | |
134 if (outputMap == null) { | |
135 logger.warn("Tried to retrieve facets, but no Outputs existing."); | |
136 return new ArrayList<Facet>(); | |
137 } | |
138 | |
139 Output o = outputMap.get(output); | |
140 | |
141 if (o == null) { | |
142 logger.warn("No Output '" + output + "' existing."); | |
143 return new ArrayList<Facet>(); | |
144 } | |
145 | |
146 return o.getFacets(); | |
147 } | |
148 | |
149 | |
150 public List<Facet> getFacets() { | |
151 List<Facet> allFacets = new ArrayList<Facet>(); | |
152 | |
153 if (outputMap == null || outputMap.size() == 0) { | |
154 logger.warn("No Outputs existing."); | |
155 return allFacets; | |
156 } | |
157 | |
158 Set<String> outputNames = outputMap.keySet(); | |
159 | |
160 for (String outputName: outputNames) { | |
161 allFacets.addAll(getFacets(outputName)); | |
162 } | |
163 | |
164 return allFacets; | |
165 } | |
166 | |
167 | |
168 protected void appendOutputs(Element root) { | |
169 if (outputMap == null || outputMap.size() == 0) { | |
170 logger.warn("No outputs to append."); | |
171 return; | |
172 } | |
173 | |
174 logger.debug("Append " + outputMap.size() + " Output Elements."); | |
175 | |
176 Element outputsEl = ec.create("outputs"); | |
177 | |
178 Set<Map.Entry<String, Output>> entrySet = outputMap.entrySet(); | |
179 | |
180 for (Map.Entry<String, Output> entry: entrySet) { | |
181 appendOutput(outputsEl, entry.getKey(), entry.getValue()); | |
182 } | |
183 | |
184 root.appendChild(outputsEl); | |
185 } | |
186 | |
187 | |
188 protected void appendOutput(Element root, String name, Output output) { | |
189 if (name == null || name.length() == 0 || output == null) { | |
190 logger.warn("Tried to appendOutput, but Output is invalid."); | |
191 return; | |
192 } | |
193 | |
194 logger.debug("Append Output Element for '" + name + "'"); | |
195 | |
196 Element outputEl = ec.create("output"); | |
197 ec.addAttr(outputEl, "name", name); | |
198 | |
199 appendFacets(outputEl, output.getFacets()); | |
200 | |
201 root.appendChild(outputEl); | |
202 } | |
203 | |
204 | |
205 protected void appendFacets(Element root, List<Facet> facets) { | |
206 if (facets == null || facets.size() == 0) { | |
207 logger.warn("Tried to append 0 Facets."); | |
208 return; | |
209 } | |
210 | |
211 Document owner = root.getOwnerDocument(); | |
212 | |
213 logger.debug("Append " + facets.size() + " facets."); | |
214 | |
215 for (Facet facet: facets) { | |
216 Node facetNode = facet.toXML(owner); | |
217 | |
218 if (facetNode != null) { | |
219 root.appendChild(facetNode); | |
220 } | |
221 } | |
222 } | |
223 | |
224 | |
225 protected void appendLoadedRecommendations(Element root) { | |
226 if (loadedRecommendations == null) { | |
227 logger.debug("No loaded recommendations existing yet."); | |
228 return; | |
229 } | |
230 | |
231 Document owner = root.getOwnerDocument(); | |
232 | |
233 root.appendChild(owner.importNode(loadedRecommendations, true)); | |
234 } | |
235 } | |
236 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |