comparison flys-artifacts/src/main/java/de/intevation/flys/collections/CollectionAttribute.java @ 3818:dc18457b1cef

merged flys-artifacts/pre2.7-2012-03-16
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:59 +0200
parents ca6ccf722c24
children 2a8919e0ed28
comparison
equal deleted inserted replaced
2456:60ab1054069d 3818:dc18457b1cef
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 import de.intevation.artifactdatabase.state.Settings;
24
25
26 public class CollectionAttribute {
27
28 private static final Logger logger =
29 Logger.getLogger(CollectionAttribute.class);
30
31
32 protected ElementCreator ec;
33
34 protected Map<String, Output> outputMap;
35
36 protected Node loadedRecommendations;
37
38
39 public CollectionAttribute() {
40 }
41
42
43 public void addOutput(String key, Output output) {
44 if (outputMap == null) {
45 outputMap = new HashMap<String, Output>();
46 }
47
48 if (key != null && key.length() > 0 && output != null) {
49 outputMap.put(
50 key,
51 new DefaultOutput(
52 output.getName(),
53 output.getDescription(),
54 output.getMimeType(),
55 new ArrayList<Facet>(),
56 output.getType()));
57 }
58 }
59
60
61 public void cleanEmptyOutputs() {
62 if (outputMap == null) {
63 return;
64 }
65
66 List<String> removeUs = new ArrayList<String>();
67
68 Set<Map.Entry<String, Output>> entries = outputMap.entrySet();
69 for (Map.Entry<String, Output> entry: entries) {
70 Output o = entry.getValue();
71
72 List<Facet> facets = o.getFacets();
73 if (facets == null || facets.isEmpty()) {
74 removeUs.add(entry.getKey());
75 }
76 }
77
78 for (String key: removeUs) {
79 outputMap.remove(key);
80 }
81 }
82
83
84 public void setSettings(String outputKey, Settings settings) {
85 if (settings == null) {
86 logger.warn("Tried to set empty Settings for '" + outputKey + "'");
87 return;
88 }
89
90 if (outputMap == null) {
91 logger.warn("Tried to add facet but no Outputs are existing yet.");
92 return;
93 }
94
95 Output output = outputMap.get(outputKey);
96
97 if (output == null) {
98 logger.warn("Tried to add facet for unknown Output: " + outputKey);
99 return;
100 }
101
102 output.setSettings(settings);
103 }
104
105
106 public void addFacet(String outputKey, Facet facet) {
107 if (facet == null) {
108 logger.warn("Tried to add empty facet.");
109 return;
110 }
111
112 if (outputMap == null) {
113 logger.warn("Tried to add facet but no Outputs are existing yet.");
114 return;
115 }
116
117 Output output = outputMap.get(outputKey);
118
119 if (output == null) {
120 logger.warn("Tried to add facet for unknown Output: " + outputKey);
121 return;
122 }
123
124 logger.debug("Add facet for '" + outputKey + "': " + facet.getName());
125 output.addFacet(facet);
126 }
127
128
129 public void setLoadedRecommendations(Node loadedRecommendations) {
130 // TODO Replace this Node with a Java class object.
131 this.loadedRecommendations = loadedRecommendations;
132 }
133
134
135 public void clearFacets(String outputKey) {
136 if (outputKey == null || outputKey.length() == 0) {
137 logger.warn("Tried to clear Facets, but no Output key specified!");
138 return;
139 }
140
141 if (outputMap == null) {
142 logger.warn("Tried to clear Facets, but no Outputs existing!");
143 return;
144 }
145
146 Output output = outputMap.get(outputKey);
147 if (output == null) {
148 logger.warn("Tried to clear Facets for unknown Out: " + outputKey);
149 return;
150 }
151
152 output.setFacets(new ArrayList<Facet>());
153 }
154
155
156 public Document toXML() {
157 Document doc = XMLUtils.newDocument();
158
159 ec = new ElementCreator(
160 doc,
161 ArtifactNamespaceContext.NAMESPACE_URI,
162 ArtifactNamespaceContext.NAMESPACE_PREFIX);
163
164 Element root = ec.create("attribute");
165
166 appendOutputs(root);
167 appendLoadedRecommendations(root);
168
169 doc.appendChild(root);
170
171 return doc;
172 }
173
174
175 public Map<String, Output> getOutputs() {
176 return outputMap;
177 }
178
179
180 public Output getOutput(String name) {
181 if (name == null || name.length() == 0) {
182 logger.warn("No Output name specified.");
183 return null;
184 }
185
186 if (outputMap == null || outputMap.size() == 0) {
187 logger.warn("Tried to retrieve Output, but no Outputs existing.");
188 return null;
189 }
190
191 return outputMap.get(name);
192 }
193
194
195 public List<Facet> getFacets(String output) {
196 if (output == null || output.length() == 0) {
197 logger.warn("No Output name specified.");
198 return new ArrayList<Facet>();
199 }
200
201 if (outputMap == null) {
202 logger.warn("Tried to retrieve facets, but no Outputs existing.");
203 return new ArrayList<Facet>();
204 }
205
206 Output o = outputMap.get(output);
207
208 if (o == null) {
209 logger.warn("No Output '" + output + "' existing.");
210 return new ArrayList<Facet>();
211 }
212
213 return o.getFacets();
214 }
215
216
217 public List<Facet> getFacets() {
218 List<Facet> allFacets = new ArrayList<Facet>();
219
220 if (outputMap == null || outputMap.size() == 0) {
221 logger.warn("No Outputs existing.");
222 return allFacets;
223 }
224
225 Set<String> outputNames = outputMap.keySet();
226
227 for (String outputName: outputNames) {
228 allFacets.addAll(getFacets(outputName));
229 }
230
231 return allFacets;
232 }
233
234
235 protected void appendOutputs(Element root) {
236 if (outputMap == null || outputMap.size() == 0) {
237 logger.warn("No outputs to append.");
238 return;
239 }
240
241 logger.debug("Append " + outputMap.size() + " Output Elements.");
242
243 Element outputsEl = ec.create("outputs");
244
245 Set<Map.Entry<String, Output>> entrySet = outputMap.entrySet();
246
247 for (Map.Entry<String, Output> entry: entrySet) {
248 appendOutput(outputsEl, entry.getKey(), entry.getValue());
249 }
250
251 root.appendChild(outputsEl);
252 }
253
254
255 protected void appendOutput(Element root, String name, Output output) {
256 if (name == null || name.length() == 0 || output == null) {
257 logger.warn("Tried to appendOutput, but Output is invalid.");
258 return;
259 }
260
261 logger.debug("Append Output Element for '" + name + "'");
262
263 Element outputEl = ec.create("output");
264 ec.addAttr(outputEl, "name", name);
265
266 appendSettings(outputEl, output.getSettings());
267 appendFacets(outputEl, output.getFacets());
268
269 root.appendChild(outputEl);
270 }
271
272
273 protected void appendSettings(Element root, Settings settings) {
274 if (settings == null) {
275 logger.warn("Tried to append Settings, but Settings is empty!");
276 return;
277 }
278
279 settings.toXML(root);
280 }
281
282
283 protected void appendFacets(Element root, List<Facet> facets) {
284 if (facets == null || facets.size() == 0) {
285 logger.warn("Tried to append 0 Facets.");
286 return;
287 }
288
289 Document owner = root.getOwnerDocument();
290
291 logger.debug("Append " + facets.size() + " facets.");
292
293 for (Facet facet: facets) {
294 Node facetNode = facet.toXML(owner);
295
296 if (facetNode != null) {
297 root.appendChild(facetNode);
298 }
299 }
300 }
301
302
303 protected void appendLoadedRecommendations(Element root) {
304 if (loadedRecommendations == null) {
305 logger.debug("No loaded recommendations existing yet.");
306 return;
307 }
308
309 Document owner = root.getOwnerDocument();
310
311 root.appendChild(owner.importNode(loadedRecommendations, true));
312 }
313 }
314 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org