comparison flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeWriter.java @ 3556:afc7bfb4800b

Use the new infrastructure to figure out if a facet should be initially active or not. flys-artifacts/trunk@5152 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 29 Jul 2012 11:04:06 +0000
parents 22732713c54d
children c3cd414982fe
comparison
equal deleted inserted replaced
3555:b1912514e0f5 3556:afc7bfb4800b
9 9
10 import de.intevation.artifacts.ArtifactDatabase; 10 import de.intevation.artifacts.ArtifactDatabase;
11 import de.intevation.artifacts.ArtifactDatabaseException; 11 import de.intevation.artifacts.ArtifactDatabaseException;
12 12
13 import de.intevation.artifactdatabase.state.Facet; 13 import de.intevation.artifactdatabase.state.Facet;
14 import de.intevation.artifactdatabase.state.FacetActivity;
14 import de.intevation.artifactdatabase.state.Output; 15 import de.intevation.artifactdatabase.state.Output;
15 16
16 import de.intevation.flys.artifacts.FLYSArtifact; 17 import de.intevation.flys.artifacts.FLYSArtifact;
17 import de.intevation.flys.artifacts.model.ManagedFacet; 18 import de.intevation.flys.artifacts.model.ManagedFacet;
18 19
88 * are recorded here and not in the new set). 89 * are recorded here and not in the new set).
89 * 90 *
90 * @return document with merged outputs as described. 91 * @return document with merged outputs as described.
91 */ 92 */
92 protected CollectionAttribute write() { 93 protected CollectionAttribute write() {
94
95 boolean debug = logger.isDebugEnabled();
96
93 for (Map.Entry<String, Output> entry: newAttr.entrySet()) { 97 for (Map.Entry<String, Output> entry: newAttr.entrySet()) {
94 String outName = entry.getKey(); 98 String outName = entry.getKey();
95 Output a = entry.getValue(); 99 Output a = entry.getValue();
96 100
97
98 Output exists = attribute.getOutput(outName); 101 Output exists = attribute.getOutput(outName);
99 if (exists == null) { 102 if (exists == null) {
100 attribute.addOutput(outName, a); 103 attribute.addOutput(outName, a);
101 } 104 }
102 105
103 attribute.clearFacets(outName); 106 attribute.clearFacets(outName);
104 107
105 if (logger.isDebugEnabled()) { 108 if (debug) {
106 logger.debug("Merge Output: " + outName); 109 logger.debug("Merge Output: " + outName);
107 logger.debug(" old Facets: " + oldFacets.size()); 110 logger.debug(" old Facets: " + oldFacets.size());
108 logger.debug(" new Facets: " + newFacets.size()); 111 logger.debug(" new Facets: " + newFacets.size());
109 } 112 }
110 113
151 * @param oldFacets the old facets 154 * @param oldFacets the old facets
152 * @param compatibleFacets List of facets to accept 155 * @param compatibleFacets List of facets to accept
153 * @return true if any facets are written to the out. 156 * @return true if any facets are written to the out.
154 */ 157 */
155 protected boolean writeFacets( 158 protected boolean writeFacets(
156 String outputName, 159 String outputName,
157 List<Facet> newFacets, 160 List<Facet> newFacets,
158 List<Facet> oldFacets, 161 List<Facet> oldFacets,
159 List<String> compatibleFacets) 162 List<String> compatibleFacets)
160 throws ArtifactDatabaseException 163 throws ArtifactDatabaseException
161 { 164 {
162 if (compatibleFacets == null) { 165 if (compatibleFacets == null) {
163 logger.warn("No compatible facets, not generating out."); 166 logger.warn("No compatible facets, not generating out.");
164 return false; 167 return false;
169 // Add all new Facets either in their old state or (if really 172 // Add all new Facets either in their old state or (if really
170 // new) as they are. 173 // new) as they are.
171 List<ManagedFacet> currentFacets = new ArrayList<ManagedFacet>(); 174 List<ManagedFacet> currentFacets = new ArrayList<ManagedFacet>();
172 List<ManagedFacet> genuinelyNewFacets = new ArrayList<ManagedFacet>(); 175 List<ManagedFacet> genuinelyNewFacets = new ArrayList<ManagedFacet>();
173 176
177 boolean debug = logger.isDebugEnabled();
178
174 for (int i = 0; i < num; i++) { 179 for (int i = 0; i < num; i++) {
175 ManagedFacet facet = (ManagedFacet) newFacets.get(i); 180 ManagedFacet facet = (ManagedFacet) newFacets.get(i);
176 181
177 logger.debug("Try to add Facet: " + facet.getName()); 182 if (debug) {
183 logger.debug("Try to add Facet: " + facet.getName());
184 }
178 185
179 if (!compatibleFacets.contains(facet.getName())) { 186 if (!compatibleFacets.contains(facet.getName())) {
180 //logger.debug("Have incompatible facet, skip: " + facet.getName()); 187 //logger.debug("Have incompatible facet, skip: " + facet.getName());
181 continue; 188 continue;
182 } 189 }
190 else { 197 else {
191 currentFacets.add(picked); 198 currentFacets.add(picked);
192 } 199 }
193 } 200 }
194 201
195 // With each genuinely new Facet, ask Artifact whether it comes to live 202 FacetActivity.Registry registry = FacetActivity.Registry.getInstance();
203
204 // With each genuinely new Facet, figure out whether it comes to live
196 // in/activate. 205 // in/activate.
197 for (ManagedFacet newMF: genuinelyNewFacets) { 206 for (ManagedFacet newMF: genuinelyNewFacets) {
198 FLYSArtifact flys = (FLYSArtifact) db.getRawArtifact(newMF.getArtifact()); 207 FLYSArtifact flys =
199 newMF.setActive(flys.getInitialFacetActivity( 208 (FLYSArtifact)db.getRawArtifact(newMF.getArtifact());
200 outputName, 209
201 newMF.getName(), 210 FacetActivity activity = registry.getActivity(flys.getName());
202 newMF.getIndex())); 211
212 boolean isActive = activity.isInitialActive(
213 flys, newMF, outputName);
214
215 newMF.setActive(isActive ? 1 : 0);
203 } 216 }
204 217
205 // For each genuinely new Facet check positional conflicts. 218 // For each genuinely new Facet check positional conflicts.
206 for (ManagedFacet newMF: genuinelyNewFacets) { 219 for (ManagedFacet newMF: genuinelyNewFacets) {
207 boolean conflicts = true; 220 boolean conflicts = true;
209 while (conflicts) { 222 while (conflicts) {
210 conflicts = false; 223 conflicts = false;
211 for (ManagedFacet oldMF: currentFacets) { 224 for (ManagedFacet oldMF: currentFacets) {
212 if (newMF.getPosition() == oldMF.getPosition()) { 225 if (newMF.getPosition() == oldMF.getPosition()) {
213 conflicts = true; 226 conflicts = true;
214 logger.debug("Positional conflict while merging " + 227 if (debug) {
215 "facets, pushing newest facet 1 up (" + newMF.getPosition() + ")"); 228 logger.debug(
229 "Positional conflict while merging " +
230 "facets, pushing newest facet 1 up (" +
231 newMF.getPosition() + ")");
232 }
216 newMF.setPosition(newMF.getPosition() + 1); 233 newMF.setPosition(newMF.getPosition() + 1);
217 break; 234 break;
218 } 235 }
219 } 236 }
220 } 237 }
222 } 239 }
223 240
224 // Fill/correct "gaps" (e.g. position 1,2,5 are taken, after gap filling 241 // Fill/correct "gaps" (e.g. position 1,2,5 are taken, after gap filling
225 // expect positions 1,2,3 [5->3]) 242 // expect positions 1,2,3 [5->3])
226 // Preparations to be able to detect gaps. 243 // Preparations to be able to detect gaps.
227 Map<Integer, ManagedFacet> mfmap = new HashMap<Integer, ManagedFacet>(); 244 Map<Integer, ManagedFacet> mfmap =
245 new HashMap<Integer, ManagedFacet>();
228 int max = 0; 246 int max = 0;
229 for (ManagedFacet mf: currentFacets) { 247 for (ManagedFacet mf: currentFacets) {
230 int pos = mf.getPosition(); 248 int pos = mf.getPosition();
231 mfmap.put(Integer.valueOf(pos), mf); 249 mfmap.put(Integer.valueOf(pos), mf);
232 if (pos > max) max = pos; 250 if (pos > max) max = pos;
248 // Now add all facets. 266 // Now add all facets.
249 for (ManagedFacet oldMF: currentFacets) { 267 for (ManagedFacet oldMF: currentFacets) {
250 attribute.addFacet(outputName, oldMF); 268 attribute.addFacet(outputName, oldMF);
251 } 269 }
252 270
253 return currentFacets.size() > 0; 271 return !currentFacets.isEmpty();
254 } 272 }
255 273
256 274
257 /** 275 /**
258 * Returns the facet to be added to Document. 276 * Returns the facet to be added to Document.

http://dive4elements.wald.intevation.org