Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeWriter.java @ 3786:4adc35aa655c
merged flys-artifacts/2.9.1
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:47 +0200 |
parents | c3cd414982fe |
children | 721d55bdab9e |
comparison
equal
deleted
inserted
replaced
3719:e82acd5c86f7 | 3786:4adc35aa655c |
---|---|
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 de.intevation.artifacts.ArtifactDatabase; | |
11 import de.intevation.artifacts.ArtifactDatabaseException; | |
12 | |
13 import de.intevation.artifactdatabase.state.Facet; | |
14 import de.intevation.artifactdatabase.state.FacetActivity; | |
15 import de.intevation.artifactdatabase.state.Output; | |
16 | |
17 import de.intevation.flys.artifacts.FLYSArtifact; | |
18 import de.intevation.flys.artifacts.model.ManagedFacet; | |
19 | |
20 /** | |
21 * Create attribute- element of describe document of an ArtifactCollection. | |
22 * The attribute-element contains the merged output of all outputmodes and | |
23 * facets that are part of the collection. | |
24 */ | |
25 public class AttributeWriter { | |
26 | |
27 /** ArtifactDatabase used to fetch Artifacts. */ | |
28 protected ArtifactDatabase db = null; | |
29 | |
30 protected Map<String, Output> oldAttr; | |
31 | |
32 protected Map<String, Output> newAttr; | |
33 | |
34 /** List of already seen facets. */ | |
35 protected List<Facet> oldFacets; | |
36 | |
37 /** List of "new" facets. */ | |
38 protected List<Facet> newFacets; | |
39 | |
40 /** | |
41 * "Compatibility matrix", mapws list of facet names to output names. | |
42 * Any facet that is not found in the list for a specific output will | |
43 * not be added to the resulting document. | |
44 */ | |
45 protected Map<String, List<String>> compatibilities; | |
46 | |
47 | |
48 /** The result of the <i>write()</i> operation.*/ | |
49 protected CollectionAttribute attribute; | |
50 | |
51 | |
52 private static Logger logger = Logger.getLogger(AttributeWriter.class); | |
53 | |
54 | |
55 /** | |
56 * Create a AttributeWriter. | |
57 * Attributes not present in newAttr will not be included in the document. | |
58 * @param db Database to fetch artifacts. | |
59 * @param oldAttr "Old" (possibly user-changed) outputs. | |
60 * @param newAttr "New" (eventually re-read in its original, unchanged | |
61 * form) outputs. | |
62 */ | |
63 public AttributeWriter( | |
64 ArtifactDatabase db, | |
65 CollectionAttribute attribute, | |
66 Map<String, Output> oldAttr, | |
67 List<Facet> oldFacets, | |
68 Map<String, Output> newAttr, | |
69 List<Facet> newFacets, | |
70 Map<String, List<String>> matrix) | |
71 { | |
72 this.db = db; | |
73 this.attribute = attribute; | |
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 CollectionAttribute write() { | |
94 | |
95 boolean debug = logger.isDebugEnabled(); | |
96 | |
97 for (Map.Entry<String, Output> entry: newAttr.entrySet()) { | |
98 String outName = entry.getKey(); | |
99 Output a = entry.getValue(); | |
100 | |
101 Output exists = attribute.getOutput(outName); | |
102 if (exists == null) { | |
103 attribute.addOutput(outName, a); | |
104 } | |
105 | |
106 attribute.clearFacets(outName); | |
107 | |
108 if (debug) { | |
109 logger.debug("Merge Output: " + outName); | |
110 logger.debug(" old Facets: " + oldFacets.size()); | |
111 logger.debug(" new Facets: " + newFacets.size()); | |
112 } | |
113 | |
114 writeOutput(a.getName(), newFacets, oldFacets); | |
115 } | |
116 | |
117 // THIS CALL IS ABSOLUTELY NECESSARY! | |
118 attribute.cleanEmptyOutputs(); | |
119 | |
120 return attribute; | |
121 } | |
122 | |
123 | |
124 /** | |
125 * @param outputName the "new" outputs name | |
126 * @param newOutFacets Facets of the new outputs | |
127 * @param oldOutFacets Facets of the old outputs (can be null) | |
128 */ | |
129 protected void writeOutput( | |
130 String outputName, | |
131 List<Facet> newOutFacets, | |
132 List<Facet> oldOutFacets | |
133 ) { | |
134 List<String> compatFacets = this.compatibilities.get(outputName); | |
135 | |
136 if (logger.isDebugEnabled() && compatFacets != null) { | |
137 logger.debug("Compabitle Facets:"); | |
138 for (String compatible: compatFacets) { | |
139 logger.debug( "- " + compatible); | |
140 } | |
141 } | |
142 | |
143 try { | |
144 writeFacets(outputName, newOutFacets, oldOutFacets, compatFacets); | |
145 } | |
146 catch (ArtifactDatabaseException ade) { | |
147 logger.error(ade, ade); | |
148 } | |
149 } | |
150 | |
151 | |
152 /** | |
153 * @param newFacets the new facets | |
154 * @param oldFacets the old facets | |
155 * @param compatibleFacets List of facets to accept | |
156 * @return true if any facets are written to the out. | |
157 */ | |
158 protected boolean writeFacets( | |
159 String outputName, | |
160 List<Facet> newFacets, | |
161 List<Facet> oldFacets, | |
162 List<String> compatibleFacets) | |
163 throws ArtifactDatabaseException | |
164 { | |
165 if (compatibleFacets == null) { | |
166 logger.warn("No compatible facets, not generating out."); | |
167 return false; | |
168 } | |
169 | |
170 int num = newFacets.size(); | |
171 | |
172 // Add all new Facets either in their old state or (if really | |
173 // new) as they are. | |
174 List<ManagedFacet> currentFacets = new ArrayList<ManagedFacet>(); | |
175 List<ManagedFacet> genuinelyNewFacets = new ArrayList<ManagedFacet>(); | |
176 | |
177 boolean debug = logger.isDebugEnabled(); | |
178 | |
179 for (int i = 0; i < num; i++) { | |
180 ManagedFacet facet = (ManagedFacet) newFacets.get(i); | |
181 | |
182 if (debug) { | |
183 logger.debug("Try to add Facet: " + facet.getName()); | |
184 } | |
185 | |
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 FacetActivity.Registry registry = FacetActivity.Registry.getInstance(); | |
203 | |
204 // With each genuinely new Facet, figure out whether it comes to live | |
205 // in/activate. | |
206 for (ManagedFacet newMF: genuinelyNewFacets) { | |
207 FLYSArtifact flys = | |
208 (FLYSArtifact)db.getRawArtifact(newMF.getArtifact()); | |
209 | |
210 boolean isActive = registry.isInitialActive( | |
211 flys.getName(), flys, newMF, outputName); | |
212 | |
213 newMF.setActive(isActive ? 1 : 0); | |
214 } | |
215 | |
216 // For each genuinely new Facet check positional conflicts. | |
217 for (ManagedFacet newMF: genuinelyNewFacets) { | |
218 boolean conflicts = true; | |
219 // Loop until all conflicts resolved. | |
220 while (conflicts) { | |
221 conflicts = false; | |
222 for (ManagedFacet oldMF: currentFacets) { | |
223 if (newMF.getPosition() == oldMF.getPosition()) { | |
224 conflicts = true; | |
225 if (debug) { | |
226 logger.debug( | |
227 "Positional conflict while merging " + | |
228 "facets, pushing newest facet 1 up (" + | |
229 newMF.getPosition() + ")"); | |
230 } | |
231 newMF.setPosition(newMF.getPosition() + 1); | |
232 break; | |
233 } | |
234 } | |
235 } | |
236 currentFacets.add(newMF); | |
237 } | |
238 | |
239 // Fill/correct "gaps" (e.g. position 1,2,5 are taken, after gap filling | |
240 // expect positions 1,2,3 [5->3]) | |
241 // Preparations to be able to detect gaps. | |
242 Map<Integer, ManagedFacet> mfmap = | |
243 new HashMap<Integer, ManagedFacet>(); | |
244 int max = 0; | |
245 for (ManagedFacet mf: currentFacets) { | |
246 int pos = mf.getPosition(); | |
247 mfmap.put(Integer.valueOf(pos), mf); | |
248 if (pos > max) max = pos; | |
249 } | |
250 | |
251 // Finally do gap correction. | |
252 if (max != currentFacets.size()) { | |
253 int gap = 0; | |
254 for (int i = 1; i <= max; i++) { | |
255 ManagedFacet mf = mfmap.get(Integer.valueOf(i)); | |
256 if (mf == null) { | |
257 gap++; | |
258 continue; | |
259 } | |
260 mf.setPosition(mf.getPosition() - gap); | |
261 } | |
262 } | |
263 | |
264 // Now add all facets. | |
265 for (ManagedFacet oldMF: currentFacets) { | |
266 attribute.addFacet(outputName, oldMF); | |
267 } | |
268 | |
269 return !currentFacets.isEmpty(); | |
270 } | |
271 | |
272 | |
273 /** | |
274 * Returns the facet to be added to Document. | |
275 * Return the new facet only if the "same" facet was not present before. | |
276 * Return the "old" facet otherwise (user-defined information sticks | |
277 * to it). | |
278 * @param facet the new facet. | |
279 * @param oldFacets the old facets, new facet is compared against each of | |
280 * these. | |
281 * @return facet if genuinely new, matching old facet otherwise. | |
282 */ | |
283 protected ManagedFacet pickFacet(ManagedFacet facet, List<Facet> oldFacets) | |
284 { | |
285 if (oldFacets == null) { | |
286 logger.debug("No old facets to compare a new to found."); | |
287 return facet; | |
288 } | |
289 | |
290 String hash = facet.getName() + facet.getIndex() + facet.getArtifact(); | |
291 | |
292 // Compare "new" facet with all old facets. | |
293 // Take oldFacet if that facet was already present (otherwise | |
294 // information is lost, the new one otherwise. | |
295 for (Facet oFacet: oldFacets) { | |
296 ManagedFacet oldFacet = (ManagedFacet) oFacet; | |
297 String oldHash = oldFacet.getName() | |
298 + oldFacet.getIndex() | |
299 + oldFacet.getArtifact(); | |
300 if (hash.equals(oldHash)) { | |
301 return oldFacet; | |
302 } | |
303 } | |
304 return facet; | |
305 } | |
306 } | |
307 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |