view flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java @ 147:76cc1a66a05f

Implemented a FLYSArtifactCollection that will be used in this application now. flys-artifacts/trunk@1565 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 24 Mar 2011 18:47:07 +0000
parents
children 2649ada6b053
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");

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

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

        // 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