Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeWriter.java @ 3812:f788d2d901d6
merged flys-artifacts/pre2.6-2011-12-05
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:53 +0200 |
parents | 490ab097f58c |
children | 0b466bd4ab24 |
comparison
equal
deleted
inserted
replaced
3808:5fab0fe3c445 | 3812:f788d2d901d6 |
---|---|
1 package de.intevation.flys.collections; | |
2 | |
3 import java.util.ArrayList; | |
4 import java.util.List; | |
5 import java.util.HashMap; | |
6 import java.util.Map; | |
7 | |
8 import org.apache.log4j.Logger; | |
9 | |
10 import org.w3c.dom.Document; | |
11 import org.w3c.dom.Element; | |
12 import org.w3c.dom.Node; | |
13 | |
14 import de.intevation.artifacts.ArtifactDatabase; | |
15 import de.intevation.artifacts.ArtifactDatabaseException; | |
16 import de.intevation.artifacts.ArtifactNamespaceContext; | |
17 | |
18 import de.intevation.artifactdatabase.state.Facet; | |
19 import de.intevation.artifactdatabase.state.Output; | |
20 | |
21 import de.intevation.artifacts.common.utils.XMLUtils; | |
22 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator; | |
23 | |
24 import de.intevation.flys.artifacts.FLYSArtifact; | |
25 import de.intevation.flys.artifacts.model.ManagedFacet; | |
26 | |
27 /** | |
28 * Create attribute- element of describe document of an ArtifactCollection. | |
29 * The attribute-element contains the merged output of all outputmodes and | |
30 * facets that are part of the collection. | |
31 */ | |
32 public class AttributeWriter { | |
33 | |
34 /** ArtifactDatabase used to fetch Artifacts. */ | |
35 protected ArtifactDatabase db = null; | |
36 | |
37 protected Map<String, Output> oldAttr; | |
38 | |
39 protected Map<String, Output> newAttr; | |
40 | |
41 /** List of already seen facets. */ | |
42 protected List<Facet> oldFacets; | |
43 | |
44 /** List of "new" facets. */ | |
45 protected List<Facet> newFacets; | |
46 | |
47 /** | |
48 * "Compatibility matrix", mapws list of facet names to output names. | |
49 * Any facet that is not found in the list for a specific output will | |
50 * not be added to the resulting document. | |
51 */ | |
52 protected Map<String, List<String>> compatibilities; | |
53 | |
54 private static Logger logger = Logger.getLogger(AttributeWriter.class); | |
55 | |
56 | |
57 /** | |
58 * Create a AttributeWriter. | |
59 * Attributes not present in newAttr will not be included in the document. | |
60 * @param db Database to fetch artifacts. | |
61 * @param oldAttr "Old" (possibly user-changed) outputs. | |
62 * @param newAttr "New" (eventually re-read in its original, unchanged | |
63 * form) outputs. | |
64 */ | |
65 public AttributeWriter( | |
66 ArtifactDatabase db, | |
67 Map<String, Output> oldAttr, | |
68 List<Facet> oldFacets, | |
69 Map<String, Output> newAttr, | |
70 List<Facet> newFacets, | |
71 Map<String, List<String>> matrix) | |
72 { | |
73 this.db = db; | |
74 this.oldAttr = oldAttr; | |
75 this.newAttr = newAttr; | |
76 this.oldFacets = oldFacets; | |
77 this.newFacets = newFacets; | |
78 this.compatibilities = matrix; | |
79 } | |
80 | |
81 | |
82 /** | |
83 * Create document by merging outputs given in | |
84 * constructor. | |
85 * | |
86 * The "new" set rules about existance of attributes, so anything not | |
87 * present in it will not be included in the resulting document. | |
88 * The "old" set rules about the content of attributes (as user changes | |
89 * are recorded here and not in the new set). | |
90 * | |
91 * @return document with merged outputs as described. | |
92 */ | |
93 protected Document write() { | |
94 Document doc = XMLUtils.newDocument(); | |
95 | |
96 ElementCreator cr = new ElementCreator( | |
97 doc, | |
98 ArtifactNamespaceContext.NAMESPACE_URI, | |
99 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
100 | |
101 Element attribute = cr.create("attribute"); | |
102 Element outs = cr.create("outputs"); | |
103 | |
104 attribute.appendChild(outs); | |
105 doc.appendChild(attribute); | |
106 | |
107 for (String outName: newAttr.keySet()) { | |
108 Output a = newAttr.get(outName); | |
109 Output b = oldAttr.get(outName); | |
110 | |
111 writeOutput(doc, outs, cr, a.getName(), newFacets, oldFacets); | |
112 } | |
113 | |
114 return doc; | |
115 } | |
116 | |
117 | |
118 /** | |
119 * @param doc Document to add output nodes to | |
120 * @param outs Node in Document to add output nodes to | |
121 * @param cr ElementCreator in use to modify doc/outs | |
122 * @param outputName the "new" outputs name | |
123 * @param newOutFacets Facets of the new outputs | |
124 * @param oldOutFacets Facets of the old outputs (can be null) | |
125 */ | |
126 protected void writeOutput( | |
127 Document doc, | |
128 Node outs, | |
129 ElementCreator cr, | |
130 String outputName, | |
131 List<Facet> newOutFacets, | |
132 List<Facet> oldOutFacets) | |
133 { | |
134 Element output = cr.create("output"); | |
135 cr.addAttr(output, "name", outputName); | |
136 | |
137 | |
138 List<String> compatibleFacets = this.compatibilities.get(outputName); | |
139 try { | |
140 if (writeFacets( | |
141 doc, cr, output, newOutFacets, oldOutFacets, | |
142 compatibleFacets)) | |
143 { | |
144 // Add output element only if it contains facets. | |
145 outs.appendChild(output); | |
146 } | |
147 } | |
148 catch (ArtifactDatabaseException ade) { | |
149 logger.error(ade, ade); | |
150 } | |
151 } | |
152 | |
153 | |
154 /** | |
155 * @param doc Document to add facet nodes to | |
156 * @param cr ElementCreator to use with output/doc | |
157 * @param output Node in Document to add facet nodes to | |
158 * @param newFacets the new facets | |
159 * @param oldFacets the old facets | |
160 * @param compatibleFacets List of facets to accept | |
161 * @return true if any facets are written to the out. | |
162 */ | |
163 protected boolean writeFacets( | |
164 Document doc, | |
165 ElementCreator cr, | |
166 Element output, | |
167 List<Facet> newFacets, | |
168 List<Facet> oldFacets, | |
169 List<String> compatibleFacets) | |
170 throws ArtifactDatabaseException | |
171 { | |
172 if (compatibleFacets == null) { | |
173 logger.warn("No compatible facets, not generating out."); | |
174 return false; | |
175 } | |
176 | |
177 int num = newFacets.size(); | |
178 | |
179 // Add all new Facets either in their old state or (if really | |
180 // new) as they are. | |
181 List<ManagedFacet> currentFacets = new ArrayList<ManagedFacet>(); | |
182 List<ManagedFacet> genuinelyNewFacets = new ArrayList<ManagedFacet>(); | |
183 | |
184 for (int i = 0; i < num; i++) { | |
185 ManagedFacet facet = (ManagedFacet) newFacets.get(i); | |
186 if (!compatibleFacets.contains(facet.getName())) { | |
187 //logger.debug("Have incompatible facet, skip: " + facet.getName()); | |
188 continue; | |
189 } | |
190 //else logger.debug("Have compatible facet: " + facet.getName()); | |
191 | |
192 ManagedFacet picked = pickFacet(facet, oldFacets); | |
193 | |
194 if (facet.equals(picked)) { | |
195 genuinelyNewFacets.add(picked); | |
196 } | |
197 else { | |
198 currentFacets.add(picked); | |
199 } | |
200 } | |
201 | |
202 // With each genuinely new Facet, ask Artifact whether it comes to live | |
203 // in/activate. | |
204 for (ManagedFacet newMF: genuinelyNewFacets) { | |
205 FLYSArtifact flys = (FLYSArtifact) db.getRawArtifact(newMF.getArtifact()); | |
206 newMF.setActive(flys.getInitialFacetActivity( | |
207 output.getAttribute("name"), | |
208 newMF.getName(), | |
209 newMF.getIndex())); | |
210 } | |
211 | |
212 // For each genuinely new Facet check positional conflicts. | |
213 for (ManagedFacet newMF: genuinelyNewFacets) { | |
214 boolean conflicts = true; | |
215 // Loop until all conflicts resolved. | |
216 while (conflicts) { | |
217 conflicts = false; | |
218 for (ManagedFacet oldMF: currentFacets) { | |
219 if (newMF.getPosition() == oldMF.getPosition()) { | |
220 conflicts = true; | |
221 logger.debug("Positional conflict while merging " + | |
222 "facets, pushing newest facet 1 up (" + newMF.getPosition() + ")"); | |
223 newMF.setPosition(newMF.getPosition() + 1); | |
224 break; | |
225 } | |
226 } | |
227 } | |
228 currentFacets.add(newMF); | |
229 } | |
230 | |
231 // Fill/correct "gaps" (e.g. position 1,2,5 are taken, after gap filling | |
232 // expect positions 1,2,3 [5->3]) | |
233 // Preparations to be able to detect gaps. | |
234 Map<Integer, ManagedFacet> mfmap = new HashMap<Integer, ManagedFacet>(); | |
235 int max = 0; | |
236 for (ManagedFacet mf: currentFacets) { | |
237 int pos = mf.getPosition(); | |
238 mfmap.put(Integer.valueOf(pos), mf); | |
239 if (pos > max) max = pos; | |
240 } | |
241 | |
242 // Finally do gap correction. | |
243 if (max != currentFacets.size()) { | |
244 int gap = 0; | |
245 for (int i = 1; i <= max; i++) { | |
246 ManagedFacet mf = mfmap.get(Integer.valueOf(i)); | |
247 if (mf == null) { | |
248 gap++; | |
249 continue; | |
250 } | |
251 mf.setPosition(mf.getPosition() - gap); | |
252 } | |
253 } | |
254 | |
255 // Now add all facets. | |
256 for (ManagedFacet oldMF: currentFacets) { | |
257 Node node = oldMF.toXML(doc); | |
258 if (node != null) { | |
259 output.appendChild(node); | |
260 } | |
261 } | |
262 | |
263 return currentFacets.size() > 0; | |
264 } | |
265 | |
266 | |
267 /** | |
268 * Returns the facet to be added to Document. | |
269 * Return the new facet only if the "same" facet was not present before. | |
270 * Return the "old" facet otherwise (user-defined information sticks | |
271 * to it). | |
272 * @param facet the new facet. | |
273 * @param oldFacets the old facets, new facet is compared against each of | |
274 * these. | |
275 * @return facet if genuinely new, matching old facet otherwise. | |
276 */ | |
277 protected ManagedFacet pickFacet(ManagedFacet facet, | |
278 List<Facet> oldFacets) | |
279 { | |
280 if (oldFacets == null) { | |
281 logger.debug("No old facets to compare a new to found."); | |
282 return facet; | |
283 } | |
284 | |
285 String hash = facet.getName() + facet.getIndex() + facet.getArtifact(); | |
286 | |
287 // Compare "new" facet with all old facets. | |
288 // Take oldFacet if that facet was already present (otherwise | |
289 // information is lost, the new one otherwise. | |
290 for (Facet oFacet: oldFacets) { | |
291 ManagedFacet oldFacet = (ManagedFacet) oFacet; | |
292 String oldHash = oldFacet.getName() | |
293 + oldFacet.getIndex() | |
294 + oldFacet.getArtifact(); | |
295 if (hash.equals(oldHash)) { | |
296 return oldFacet; | |
297 } | |
298 } | |
299 return facet; | |
300 } | |
301 } | |
302 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |