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