view flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java @ 207:2b745b5e731c 0.1

merged flys-backend/0.1
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:07 +0200
parents d03b8bbeb853
children 3419b1c8ca28
line wrap: on
line source
package de.intevation.flys.collections;

import java.util.Date;

import javax.xml.xpath.XPathConstants;

import org.apache.log4j.Logger;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import de.intevation.artifacts.Artifact;
import de.intevation.artifacts.ArtifactNamespaceContext;
import de.intevation.artifacts.CallContext;
import de.intevation.artifacts.CollectionItem;

import de.intevation.artifacts.common.utils.XMLUtils;

import de.intevation.artifactdatabase.DefaultArtifactCollection;
import de.intevation.artifactdatabase.Backend;
import de.intevation.artifactdatabase.Backend.PersistentArtifact;


/**
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class FLYSArtifactCollection extends DefaultArtifactCollection {

    /** The logger used in this class.*/
    private static Logger log = Logger.getLogger(FLYSArtifactCollection.class);


    /** Constant XPath that points to the outputmodes of an artifact.*/
    public static final String XPATH_ARTIFACT_OUTPUTMODES =
        "/art:result/art:outputmodes";


    @Override
    public Document describe(CallContext context) {
        log.debug("FLYSArtifactCollection.describe: " + identifier);

        Document doc = XMLUtils.newDocument();

        XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
            doc,
            ArtifactNamespaceContext.NAMESPACE_URI,
            ArtifactNamespaceContext.NAMESPACE_PREFIX);

        Date creationTime = getCreationTime();
        String creation   = creationTime != null
            ? Long.toString(creationTime.getTime())
            : "";

        Element collection = ec.create("artifact-collection");
        Element artifacts  = ec.create("artifacts");

        ec.addAttr(collection, "name", getName(), true);
        ec.addAttr(collection, "uuid", identifier(), true);
        ec.addAttr(collection, "creation", creation,  true);

        collection.appendChild(artifacts);
        doc.appendChild(collection);

        Document attribute = getAttribute();
        if (attribute != null) {
            Node child = attribute.getFirstChild();
            collection.appendChild(doc.importNode(child, true));
        }

        Backend backend        = Backend.getInstance();
        CollectionItem[] items = backend.listCollectionArtifacts(identifier());

        for (CollectionItem item: items) {
            artifacts.appendChild(
                buildArtifactNode(item, backend, context, ec));
        }

        return doc;
    }


    protected Element buildArtifactNode(
        CollectionItem          item,
        Backend                 backend,
        CallContext             context,
        XMLUtils.ElementCreator ec)
    {
        String uuid = item.getArtifactIdentifier();

        log.debug("Append artifact '"+ uuid +"' to collection description");

        PersistentArtifact pArtifact = backend.getArtifact(uuid);
        Artifact artifact            = pArtifact.getArtifact();

        Element ci   = ec.create("artifact");
        ec.addAttr(ci, "uuid", uuid, true);
        ec.addAttr(ci, "hash", artifact.hash(), true);

        // XXX I am not sure if it works well every time with an empty document
        // in the describe operation of an artifact.
        Document description = artifact.describe(null, context);
        Node outputModes     = (Node) XMLUtils.xpath(
            description,
            XPATH_ARTIFACT_OUTPUTMODES,
            XPathConstants.NODE,
            ArtifactNamespaceContext.INSTANCE);

        if (outputModes != null) {
            Document doc = ci.getOwnerDocument();
            ci.appendChild(doc.importNode(outputModes, true));
        }

        return ci;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org