comparison flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeWriter.java @ 1976:0b466bd4ab24

Introduced a CollectionAttribute class that stores the information provided by the Collection's attribute document. flys-artifacts/trunk@3400 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 13 Dec 2011 11:55:47 +0000
parents 490ab097f58c
children 9c0acaa369ea
comparison
equal deleted inserted replaced
1975:b30e1710df1d 1976:0b466bd4ab24
11 import org.w3c.dom.Element; 11 import org.w3c.dom.Element;
12 import org.w3c.dom.Node; 12 import org.w3c.dom.Node;
13 13
14 import de.intevation.artifacts.ArtifactDatabase; 14 import de.intevation.artifacts.ArtifactDatabase;
15 import de.intevation.artifacts.ArtifactDatabaseException; 15 import de.intevation.artifacts.ArtifactDatabaseException;
16 import de.intevation.artifacts.ArtifactNamespaceContext;
17 16
18 import de.intevation.artifactdatabase.state.Facet; 17 import de.intevation.artifactdatabase.state.Facet;
19 import de.intevation.artifactdatabase.state.Output; 18 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 19
24 import de.intevation.flys.artifacts.FLYSArtifact; 20 import de.intevation.flys.artifacts.FLYSArtifact;
25 import de.intevation.flys.artifacts.model.ManagedFacet; 21 import de.intevation.flys.artifacts.model.ManagedFacet;
26 22
27 /** 23 /**
48 * "Compatibility matrix", mapws list of facet names to output names. 44 * "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 45 * Any facet that is not found in the list for a specific output will
50 * not be added to the resulting document. 46 * not be added to the resulting document.
51 */ 47 */
52 protected Map<String, List<String>> compatibilities; 48 protected Map<String, List<String>> compatibilities;
49
50
51 /** The result of the <i>write()</i> operation.*/
52 protected CollectionAttribute attribute;
53
53 54
54 private static Logger logger = Logger.getLogger(AttributeWriter.class); 55 private static Logger logger = Logger.getLogger(AttributeWriter.class);
55 56
56 57
57 /** 58 /**
88 * The "old" set rules about the content of attributes (as user changes 89 * The "old" set rules about the content of attributes (as user changes
89 * are recorded here and not in the new set). 90 * are recorded here and not in the new set).
90 * 91 *
91 * @return document with merged outputs as described. 92 * @return document with merged outputs as described.
92 */ 93 */
93 protected Document write() { 94 protected CollectionAttribute write() {
94 Document doc = XMLUtils.newDocument(); 95 attribute = new CollectionAttribute();
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 96
107 for (String outName: newAttr.keySet()) { 97 for (String outName: newAttr.keySet()) {
108 Output a = newAttr.get(outName); 98 Output a = newAttr.get(outName);
109 Output b = oldAttr.get(outName); 99
110 100 attribute.addOutput(a.getName(), a);
111 writeOutput(doc, outs, cr, a.getName(), newFacets, oldFacets); 101
112 } 102 writeOutput(a.getName(), newFacets, oldFacets);
113 103 }
114 return doc; 104
115 } 105 return attribute;
116 106 }
117 107
118 /** 108
119 * @param doc Document to add output nodes to 109 /**
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 110 * @param outputName the "new" outputs name
123 * @param newOutFacets Facets of the new outputs 111 * @param newOutFacets Facets of the new outputs
124 * @param oldOutFacets Facets of the old outputs (can be null) 112 * @param oldOutFacets Facets of the old outputs (can be null)
125 */ 113 */
126 protected void writeOutput( 114 protected void writeOutput(
127 Document doc, 115 String outputName,
128 Node outs, 116 List<Facet> newOutFacets,
129 ElementCreator cr, 117 List<Facet> oldOutFacets
130 String outputName, 118 ) {
131 List<Facet> newOutFacets, 119 List<String> compatFacets = this.compatibilities.get(outputName);
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 { 120 try {
140 if (writeFacets( 121 writeFacets(outputName, newOutFacets, oldOutFacets, compatFacets);
141 doc, cr, output, newOutFacets, oldOutFacets,
142 compatibleFacets))
143 {
144 // Add output element only if it contains facets.
145 outs.appendChild(output);
146 }
147 } 122 }
148 catch (ArtifactDatabaseException ade) { 123 catch (ArtifactDatabaseException ade) {
149 logger.error(ade, ade); 124 logger.error(ade, ade);
150 } 125 }
151 } 126 }
152 127
153 128
154 /** 129 /**
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 130 * @param newFacets the new facets
159 * @param oldFacets the old facets 131 * @param oldFacets the old facets
160 * @param compatibleFacets List of facets to accept 132 * @param compatibleFacets List of facets to accept
161 * @return true if any facets are written to the out. 133 * @return true if any facets are written to the out.
162 */ 134 */
163 protected boolean writeFacets( 135 protected boolean writeFacets(
164 Document doc, 136 String outputName,
165 ElementCreator cr,
166 Element output,
167 List<Facet> newFacets, 137 List<Facet> newFacets,
168 List<Facet> oldFacets, 138 List<Facet> oldFacets,
169 List<String> compatibleFacets) 139 List<String> compatibleFacets)
170 throws ArtifactDatabaseException 140 throws ArtifactDatabaseException
171 { 141 {
202 // With each genuinely new Facet, ask Artifact whether it comes to live 172 // With each genuinely new Facet, ask Artifact whether it comes to live
203 // in/activate. 173 // in/activate.
204 for (ManagedFacet newMF: genuinelyNewFacets) { 174 for (ManagedFacet newMF: genuinelyNewFacets) {
205 FLYSArtifact flys = (FLYSArtifact) db.getRawArtifact(newMF.getArtifact()); 175 FLYSArtifact flys = (FLYSArtifact) db.getRawArtifact(newMF.getArtifact());
206 newMF.setActive(flys.getInitialFacetActivity( 176 newMF.setActive(flys.getInitialFacetActivity(
207 output.getAttribute("name"), 177 outputName,
208 newMF.getName(), 178 newMF.getName(),
209 newMF.getIndex())); 179 newMF.getIndex()));
210 } 180 }
211 181
212 // For each genuinely new Facet check positional conflicts. 182 // For each genuinely new Facet check positional conflicts.
213 for (ManagedFacet newMF: genuinelyNewFacets) { 183 for (ManagedFacet newMF: genuinelyNewFacets) {
214 boolean conflicts = true; 184 boolean conflicts = true;
215 // Loop until all conflicts resolved. 185 // Loop until all conflicts resolved.
216 while (conflicts) { 186 while (conflicts) {
252 } 222 }
253 } 223 }
254 224
255 // Now add all facets. 225 // Now add all facets.
256 for (ManagedFacet oldMF: currentFacets) { 226 for (ManagedFacet oldMF: currentFacets) {
257 Node node = oldMF.toXML(doc); 227 attribute.addFacet(outputName, oldMF);
258 if (node != null) {
259 output.appendChild(node);
260 }
261 } 228 }
262 229
263 return currentFacets.size() > 0; 230 return currentFacets.size() > 0;
264 } 231 }
265 232
272 * @param facet the new facet. 239 * @param facet the new facet.
273 * @param oldFacets the old facets, new facet is compared against each of 240 * @param oldFacets the old facets, new facet is compared against each of
274 * these. 241 * these.
275 * @return facet if genuinely new, matching old facet otherwise. 242 * @return facet if genuinely new, matching old facet otherwise.
276 */ 243 */
277 protected ManagedFacet pickFacet(ManagedFacet facet, 244 protected ManagedFacet pickFacet(ManagedFacet facet, List<Facet> oldFacets)
278 List<Facet> oldFacets)
279 { 245 {
280 if (oldFacets == null) { 246 if (oldFacets == null) {
281 logger.debug("No old facets to compare a new to found."); 247 logger.debug("No old facets to compare a new to found.");
282 return facet; 248 return facet;
283 } 249 }
284 250
285 String hash = facet.getName() + facet.getIndex() + facet.getArtifact(); 251 String hash = facet.getName() + facet.getIndex() + facet.getArtifact();
286 252
287 // Compare "new" facet with all old facets. 253 // Compare "new" facet with all old facets.
288 // Take oldFacet if that facet was already present (otherwise 254 // Take oldFacet if that facet was already present (otherwise
289 // information is lost, the new one otherwise. 255 // information is lost, the new one otherwise.

http://dive4elements.wald.intevation.org