comparison flys-artifacts/src/main/java/org/dive4elements/river/collections/CollectionAttribute.java @ 5831:bd047b71ab37

Repaired internal references
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 12:06:39 +0200
parents flys-artifacts/src/main/java/de/intevation/flys/collections/CollectionAttribute.java@5cb8e941da1e
children
comparison
equal deleted inserted replaced
5830:160f53ee0870 5831:bd047b71ab37
1 package org.dive4elements.river.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.w3c.dom.Document;
9 import org.w3c.dom.Element;
10 import org.w3c.dom.Node;
11
12 import org.apache.log4j.Logger;
13
14 import org.dive4elements.artifacts.ArtifactNamespaceContext;
15
16 import org.dive4elements.artifacts.common.utils.XMLUtils;
17 import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator;
18
19 import org.dive4elements.artifactdatabase.state.DefaultOutput;
20 import org.dive4elements.artifactdatabase.state.Facet;
21 import org.dive4elements.artifactdatabase.state.Output;
22 import org.dive4elements.artifactdatabase.state.Settings;
23
24
25 /** Create attribute part of collection document. */
26 public class CollectionAttribute {
27
28 /** Privately owned logger. */
29 private static final Logger logger =
30 Logger.getLogger(CollectionAttribute.class);
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 for (Map.Entry<String, Output> entry: outputMap.entrySet()) {
69 Output o = entry.getValue();
70
71 List<Facet> facets = o.getFacets();
72 if (facets == null || facets.isEmpty()) {
73 removeUs.add(entry.getKey());
74 }
75 }
76
77 for (String key: removeUs) {
78 outputMap.remove(key);
79 }
80 }
81
82
83 public void setSettings(String outputKey, Settings settings) {
84 if (settings == null) {
85 logger.warn("Tried to set empty Settings for '" + outputKey + "'");
86 return;
87 }
88
89 if (outputMap == null) {
90 logger.warn("Tried to add facet but no Outputs are existing yet.");
91 return;
92 }
93
94 Output output = outputMap.get(outputKey);
95
96 if (output == null) {
97 logger.warn("Tried to add facet for unknown Output: " + outputKey);
98 return;
99 }
100
101 output.setSettings(settings);
102 }
103
104
105 public void addFacet(String outputKey, Facet facet) {
106 if (facet == null) {
107 logger.warn("Tried to add empty facet.");
108 return;
109 }
110
111 if (outputMap == null) {
112 logger.warn("Tried to add facet but no Outputs are existing yet.");
113 return;
114 }
115
116 Output output = outputMap.get(outputKey);
117
118 if (output == null) {
119 logger.warn("Tried to add facet for unknown Output: " + outputKey);
120 return;
121 }
122
123 logger.debug("Add facet for '" + outputKey + "': " + facet.getName());
124 output.addFacet(facet);
125 }
126
127
128 public void setLoadedRecommendations(Node loadedRecommendations) {
129 // TODO Replace this Node with a Java class object.
130 this.loadedRecommendations = loadedRecommendations;
131 }
132
133
134 public void clearFacets(String outputKey) {
135 if (outputKey == null || outputKey.length() == 0) {
136 logger.warn("Tried to clear Facets, but no Output key specified!");
137 return;
138 }
139
140 if (outputMap == null) {
141 logger.warn("Tried to clear Facets, but no Outputs existing!");
142 return;
143 }
144
145 Output output = outputMap.get(outputKey);
146 if (output == null) {
147 logger.warn("Tried to clear Facets for unknown Out: " + outputKey);
148 return;
149 }
150
151 output.setFacets(new ArrayList<Facet>());
152 }
153
154
155 public Document toXML() {
156 Document doc = XMLUtils.newDocument();
157
158 ec = new ElementCreator(
159 doc,
160 ArtifactNamespaceContext.NAMESPACE_URI,
161 ArtifactNamespaceContext.NAMESPACE_PREFIX);
162
163 Element root = ec.create("attribute");
164
165 appendOutputs(root);
166 appendLoadedRecommendations(root);
167
168 doc.appendChild(root);
169
170 return doc;
171 }
172
173 /** True if output with outputName is found. */
174 public boolean hasOutput(String outputName) {
175 return getOutput(outputName) != null;
176 }
177
178 public Map<String, Output> getOutputs() {
179 return outputMap;
180 }
181
182
183 public Output getOutput(String name) {
184 if (name == null || name.length() == 0) {
185 logger.warn("No Output name specified.");
186 return null;
187 }
188
189 if (outputMap == null || outputMap.isEmpty()) {
190 logger.warn("Tried to retrieve Output, but no Outputs existing.");
191 return null;
192 }
193
194 return outputMap.get(name);
195 }
196
197
198 public List<Facet> getFacets(String output) {
199 if (output == null || output.length() == 0) {
200 logger.warn("No Output name specified.");
201 return new ArrayList<Facet>();
202 }
203
204 if (outputMap == null) {
205 logger.warn("Tried to retrieve facets, but no Outputs existing.");
206 return new ArrayList<Facet>();
207 }
208
209 Output o = outputMap.get(output);
210
211 if (o == null) {
212 logger.warn("No Output '" + output + "' existing.");
213 return new ArrayList<Facet>();
214 }
215
216 return o.getFacets();
217 }
218
219
220 public List<Facet> getFacets() {
221 List<Facet> allFacets = new ArrayList<Facet>();
222
223 if (outputMap == null || outputMap.isEmpty()) {
224 logger.warn("No Outputs existing.");
225 return allFacets;
226 }
227
228 for (String outputName: outputMap.keySet()) {
229 allFacets.addAll(getFacets(outputName));
230 }
231
232 return allFacets;
233 }
234
235
236 protected void appendOutputs(Element root) {
237 if (outputMap == null || outputMap.isEmpty()) {
238 logger.warn("No outputs to append.");
239 return;
240 }
241
242 logger.debug("Append " + outputMap.size() + " Output Elements.");
243
244 Element outputsEl = ec.create("outputs");
245
246 for (Map.Entry<String, Output> entry: outputMap.entrySet()) {
247 appendOutput(outputsEl, entry.getKey(), entry.getValue());
248 }
249
250 root.appendChild(outputsEl);
251 }
252
253
254 protected void appendOutput(Element root, String name, Output output) {
255 if (name == null || name.length() == 0 || output == null) {
256 logger.warn("Tried to appendOutput, but Output is invalid.");
257 return;
258 }
259
260 logger.debug("Append Output Element for '" + name + "'");
261
262 Element outputEl = ec.create("output");
263 ec.addAttr(outputEl, "name", name);
264
265 appendSettings(outputEl, output.getSettings());
266 appendFacets(outputEl, output.getFacets());
267
268 root.appendChild(outputEl);
269 }
270
271
272 protected void appendSettings(Element root, Settings settings) {
273 if (settings == null) {
274 logger.warn("Tried to append Settings, but Settings is empty!");
275 return;
276 }
277
278 settings.toXML(root);
279 }
280
281
282 protected void appendFacets(Element root, List<Facet> facets) {
283 if (facets == null || facets.isEmpty()) {
284 logger.warn("Tried to append 0 Facets.");
285 return;
286 }
287
288 Document owner = root.getOwnerDocument();
289
290 logger.debug("Append " + facets.size() + " facets.");
291
292 for (Facet facet: facets) {
293 Node facetNode = facet.toXML(owner);
294
295 if (facetNode != null) {
296 root.appendChild(facetNode);
297 }
298 }
299 }
300
301
302 protected void appendLoadedRecommendations(Element root) {
303 if (loadedRecommendations == null) {
304 logger.debug("No loaded recommendations existing yet.");
305 return;
306 }
307
308 Document owner = root.getOwnerDocument();
309
310 root.appendChild(owner.importNode(loadedRecommendations, true));
311 }
312 }
313 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org