ingo@346: package de.intevation.flys.collections; ingo@346: ingo@346: import java.util.List; ingo@346: import java.util.Map; ingo@346: ingo@346: import org.apache.log4j.Logger; ingo@346: ingo@346: import org.w3c.dom.Document; ingo@346: import org.w3c.dom.Element; ingo@346: import org.w3c.dom.Node; ingo@346: ingo@346: import de.intevation.artifacts.ArtifactNamespaceContext; ingo@346: ingo@346: import de.intevation.artifactdatabase.state.Facet; ingo@346: import de.intevation.artifactdatabase.state.Output; ingo@346: ingo@346: import de.intevation.artifacts.common.utils.XMLUtils; ingo@346: import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator; ingo@346: ingo@346: import de.intevation.flys.artifacts.model.ManagedFacet; ingo@346: felix@1634: /** felix@1634: * Create attribute- element of describe document of an ArtifactCollection. felix@1634: * The attribute-element contains the merged output of all outputmodes and felix@1634: * facets that are part of the collection. felix@1634: */ ingo@346: public class AttributeWriter { ingo@346: ingo@346: protected Map oldAttr; ingo@346: protected Map newAttr; ingo@346: ingo@346: private static Logger logger = Logger.getLogger(AttributeWriter.class); ingo@346: ingo@346: felix@1634: /** felix@1634: * Create a AttributeWriter. felix@1634: * Attributes not present in newAttr will not be included in the document. felix@1634: * @param oldAttr "Old" (possibly user-changed) outputs. felix@1634: * @param newAttr "New" (eventually re-read in its original, unchagnged felix@1634: * form) outputs. felix@1634: */ ingo@346: public AttributeWriter( ingo@346: Map oldAttr, ingo@346: Map newAttr) ingo@346: { ingo@346: this.oldAttr = oldAttr; ingo@346: this.newAttr = newAttr; ingo@346: } ingo@346: ingo@346: felix@1628: /** felix@1634: * Create document by merging outputs given in felix@1634: * constructor. felix@1634: * The "new" set rules about existance of attributes, so anything not felix@1634: * present in it will not be included in the resulting document. felix@1634: * The "old" set rules about the content of attributes (as user changes felix@1634: * are recorded here and not in the new set). felix@1634: * @return document with merged outputs as described. felix@1628: */ ingo@346: protected Document write() { ingo@346: Document doc = XMLUtils.newDocument(); ingo@346: ingo@346: ElementCreator cr = new ElementCreator( ingo@346: doc, ingo@346: ArtifactNamespaceContext.NAMESPACE_URI, ingo@346: ArtifactNamespaceContext.NAMESPACE_PREFIX); ingo@346: ingo@638: Element attribute = cr.create("attribute"); sascha@705: Element outs = cr.create("outputs"); ingo@638: ingo@638: attribute.appendChild(outs); ingo@638: doc.appendChild(attribute); ingo@353: sascha@705: for (String outName: newAttr.keySet()) { ingo@346: ingo@346: Output a = newAttr.get(outName); ingo@346: Output b = oldAttr.get(outName); ingo@945: ingo@353: writeOutput(doc, outs, cr, a, b); ingo@346: } ingo@346: ingo@346: return doc; ingo@346: } ingo@346: felix@1634: felix@1628: /** felix@1628: * @param doc Document to add output nodes to felix@1628: * @param outs Node in Document to add output nodes to felix@1628: * @param a the new output felix@1628: * @param b the old output felix@1628: */ ingo@346: protected void writeOutput( ingo@346: Document doc, ingo@353: Node outs, ingo@346: ElementCreator cr, ingo@1171: Output a, /* new output */ ingo@1171: Output b) /* old output */ ingo@346: { ingo@346: Element output = cr.create("output"); ingo@346: cr.addAttr(output, "name", a.getName()); ingo@346: ingo@353: outs.appendChild(output); ingo@346: ingo@346: List facetsA = a.getFacets(); ingo@346: List facetsB = null; ingo@346: ingo@346: if (b != null) { ingo@346: facetsB = b.getFacets(); ingo@346: } ingo@346: ingo@346: writeFacets(doc, cr, output, facetsA, facetsB); ingo@346: } ingo@346: ingo@346: felix@1628: /** felix@1628: * @param doc Document to add facet nodes to felix@1628: * @param output Node in Document to add facet nodes to felix@1628: * @param a the new facets felix@1628: * @param b the old facets felix@1628: */ ingo@346: protected void writeFacets( ingo@346: Document doc, ingo@346: ElementCreator cr, ingo@346: Element output, felix@1635: List newFacets, felix@1635: List oldFacets) ingo@346: { felix@1635: int num = newFacets.size(); ingo@346: felix@1635: // Add all new Facets either in their old state or (if really felix@1635: // new) as they are. ingo@346: for (int i = 0; i < num; i++) { felix@1635: ManagedFacet facet = (ManagedFacet) newFacets.get(i); ingo@346: felix@1635: ManagedFacet picked = pickFacet(facet, oldFacets); felix@1635: Node node = picked.toXML(doc); felix@1635: if (node != null) { felix@1635: output.appendChild(node); ingo@346: } ingo@346: } ingo@346: } ingo@346: ingo@346: felix@1628: /** felix@1635: * Returns the facet to be added to Document. felix@1635: * Return the new facet only if the "same" facet was not present before. felix@1635: * Return the "old" facet otherwise (user-defined information sticks felix@1635: * to it). felix@1635: * @param facet the new facet. felix@1635: * @param oldFacets the old facets, new facet is compared against each of felix@1635: * these. felix@1635: * @return facet if genuinely new, matching old facet otherwise. felix@1628: */ felix@1635: protected ManagedFacet pickFacet(ManagedFacet facet, felix@1635: List oldFacets) { felix@1635: if (oldFacets == null) { felix@1635: logger.debug("No old facets to compare a new to found."); felix@1635: return facet; ingo@346: } felix@1635: felix@1635: String hash = facet.getName() + facet.getIndex() + facet.getArtifact(); ingo@346: felix@1635: // Compare "new" facet with all old facets. felix@1635: // Take oldFacet if that facet was already present (otherwise felix@1635: // information is lost, the new one otherwise. felix@1635: for (Facet oFacet: oldFacets) { felix@1635: ManagedFacet oldFacet = (ManagedFacet) oFacet; felix@1635: String oldHash = oldFacet.getName() felix@1635: + oldFacet.getIndex() felix@1635: + oldFacet.getArtifact(); felix@1635: if (hash.equals(oldHash)) { felix@1635: return oldFacet; ingo@346: } ingo@346: } felix@1635: return facet; ingo@346: } ingo@346: } sascha@705: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :