Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/collections/CollectionAttribute.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 | 85132c9edd64 |
children | ca6ccf722c24 |
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 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 setSettings(String outputKey, Settings settings) { | |
62 if (settings == null) { | |
63 logger.warn("Tried to set empty Settings for '" + outputKey + "'"); | |
64 return; | |
65 } | |
66 | |
67 if (outputMap == null) { | |
68 logger.warn("Tried to add facet but no Outputs are existing yet."); | |
69 return; | |
70 } | |
71 | |
72 Output output = outputMap.get(outputKey); | |
73 | |
74 if (output == null) { | |
75 logger.warn("Tried to add facet for unknown Output: " + outputKey); | |
76 return; | |
77 } | |
78 | |
79 output.setSettings(settings); | |
80 } | |
81 | |
82 | |
83 public void addFacet(String outputKey, Facet facet) { | |
84 if (facet == null) { | |
85 logger.warn("Tried to add empty facet."); | |
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 logger.debug("Add facet for '" + outputKey + "': " + facet.getName()); | |
102 output.addFacet(facet); | |
103 } | |
104 | |
105 | |
106 public void setLoadedRecommendations(Node loadedRecommendations) { | |
107 // TODO Replace this Node with a Java class object. | |
108 this.loadedRecommendations = loadedRecommendations; | |
109 } | |
110 | |
111 | |
112 public void clearFacets(String outputKey) { | |
113 if (outputKey == null || outputKey.length() == 0) { | |
114 logger.warn("Tried to clear Facets, but no Output key specified!"); | |
115 return; | |
116 } | |
117 | |
118 if (outputMap == null) { | |
119 logger.warn("Tried to clear Facets, but no Outputs existing!"); | |
120 return; | |
121 } | |
122 | |
123 Output output = outputMap.get(outputKey); | |
124 if (output == null) { | |
125 logger.warn("Tried to clear Facets for unknown Out: " + outputKey); | |
126 return; | |
127 } | |
128 | |
129 output.setFacets(new ArrayList<Facet>()); | |
130 } | |
131 | |
132 | |
133 public Document toXML() { | |
134 Document doc = XMLUtils.newDocument(); | |
135 | |
136 ec = new ElementCreator( | |
137 doc, | |
138 ArtifactNamespaceContext.NAMESPACE_URI, | |
139 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
140 | |
141 Element root = ec.create("attribute"); | |
142 | |
143 appendOutputs(root); | |
144 appendLoadedRecommendations(root); | |
145 | |
146 doc.appendChild(root); | |
147 | |
148 return doc; | |
149 } | |
150 | |
151 | |
152 public Map<String, Output> getOutputs() { | |
153 return outputMap; | |
154 } | |
155 | |
156 | |
157 public Output getOutput(String name) { | |
158 if (name == null || name.length() == 0) { | |
159 logger.warn("No Output name specified."); | |
160 return null; | |
161 } | |
162 | |
163 if (outputMap == null || outputMap.size() == 0) { | |
164 logger.warn("Tried to retrieve Output, but no Outputs existing."); | |
165 return null; | |
166 } | |
167 | |
168 return outputMap.get(name); | |
169 } | |
170 | |
171 | |
172 public List<Facet> getFacets(String output) { | |
173 if (output == null || output.length() == 0) { | |
174 logger.warn("No Output name specified."); | |
175 return new ArrayList<Facet>(); | |
176 } | |
177 | |
178 if (outputMap == null) { | |
179 logger.warn("Tried to retrieve facets, but no Outputs existing."); | |
180 return new ArrayList<Facet>(); | |
181 } | |
182 | |
183 Output o = outputMap.get(output); | |
184 | |
185 if (o == null) { | |
186 logger.warn("No Output '" + output + "' existing."); | |
187 return new ArrayList<Facet>(); | |
188 } | |
189 | |
190 return o.getFacets(); | |
191 } | |
192 | |
193 | |
194 public List<Facet> getFacets() { | |
195 List<Facet> allFacets = new ArrayList<Facet>(); | |
196 | |
197 if (outputMap == null || outputMap.size() == 0) { | |
198 logger.warn("No Outputs existing."); | |
199 return allFacets; | |
200 } | |
201 | |
202 Set<String> outputNames = outputMap.keySet(); | |
203 | |
204 for (String outputName: outputNames) { | |
205 allFacets.addAll(getFacets(outputName)); | |
206 } | |
207 | |
208 return allFacets; | |
209 } | |
210 | |
211 | |
212 protected void appendOutputs(Element root) { | |
213 if (outputMap == null || outputMap.size() == 0) { | |
214 logger.warn("No outputs to append."); | |
215 return; | |
216 } | |
217 | |
218 logger.debug("Append " + outputMap.size() + " Output Elements."); | |
219 | |
220 Element outputsEl = ec.create("outputs"); | |
221 | |
222 Set<Map.Entry<String, Output>> entrySet = outputMap.entrySet(); | |
223 | |
224 for (Map.Entry<String, Output> entry: entrySet) { | |
225 appendOutput(outputsEl, entry.getKey(), entry.getValue()); | |
226 } | |
227 | |
228 root.appendChild(outputsEl); | |
229 } | |
230 | |
231 | |
232 protected void appendOutput(Element root, String name, Output output) { | |
233 if (name == null || name.length() == 0 || output == null) { | |
234 logger.warn("Tried to appendOutput, but Output is invalid."); | |
235 return; | |
236 } | |
237 | |
238 logger.debug("Append Output Element for '" + name + "'"); | |
239 | |
240 Element outputEl = ec.create("output"); | |
241 ec.addAttr(outputEl, "name", name); | |
242 | |
243 appendSettings(outputEl, output.getSettings()); | |
244 appendFacets(outputEl, output.getFacets()); | |
245 | |
246 root.appendChild(outputEl); | |
247 } | |
248 | |
249 | |
250 protected void appendSettings(Element root, Settings settings) { | |
251 if (settings == null) { | |
252 logger.warn("Tried to append Settings, but Settings is empty!"); | |
253 return; | |
254 } | |
255 | |
256 settings.toXML(root); | |
257 } | |
258 | |
259 | |
260 protected void appendFacets(Element root, List<Facet> facets) { | |
261 if (facets == null || facets.size() == 0) { | |
262 logger.warn("Tried to append 0 Facets."); | |
263 return; | |
264 } | |
265 | |
266 Document owner = root.getOwnerDocument(); | |
267 | |
268 logger.debug("Append " + facets.size() + " facets."); | |
269 | |
270 for (Facet facet: facets) { | |
271 Node facetNode = facet.toXML(owner); | |
272 | |
273 if (facetNode != null) { | |
274 root.appendChild(facetNode); | |
275 } | |
276 } | |
277 } | |
278 | |
279 | |
280 protected void appendLoadedRecommendations(Element root) { | |
281 if (loadedRecommendations == null) { | |
282 logger.debug("No loaded recommendations existing yet."); | |
283 return; | |
284 } | |
285 | |
286 Document owner = root.getOwnerDocument(); | |
287 | |
288 root.appendChild(owner.importNode(loadedRecommendations, true)); | |
289 } | |
290 } | |
291 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |