view flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java @ 293:3419b1c8ca28

Removed the Backend reference from FLYSArtifactCollection. flys-artifacts/trunk@1629 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 30 Mar 2011 16:00:51 +0000
parents d03b8bbeb853
children 8d0932c2c2ef
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 org.w3c.dom.NodeList;

import de.intevation.artifacts.ArtifactDatabase;
import de.intevation.artifacts.ArtifactDatabaseException;
import de.intevation.artifacts.ArtifactNamespaceContext;
import de.intevation.artifacts.CallContext;
import de.intevation.artifacts.CallMeta;

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

import de.intevation.artifactdatabase.DefaultArtifactCollection;


/**
 * @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";

    public static final String XPATH_COLLECTION_ITEMS =
        "/art:result/art:artifact-collection/art:collection-item";


    @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));
        }

        try {
            ArtifactDatabase db = context.getDatabase();
            CallMeta meta       = context.getMeta();

            Document itemList = db.listCollectionArtifacts(identifier(), meta);
            NodeList items    = (NodeList) XMLUtils.xpath(
                itemList,
                XPATH_COLLECTION_ITEMS,
                XPathConstants.NODESET,
                ArtifactNamespaceContext.INSTANCE);

            if (items == null || items.getLength() == 0) {
                log.debug("No collection items found.");
                return doc;
            }

            int num = items.getLength();

            for (int i = 0; i < num; i++) {
                String uuid = XMLUtils.xpathString(
                    items.item(i),
                    "@art:uuid",
                    ArtifactNamespaceContext.INSTANCE);

                try {
                    artifacts.appendChild(
                        buildArtifactNode(db, uuid, context, ec));
                }
                catch (ArtifactDatabaseException dbe) {
                    log.warn(dbe, dbe);
                }
            }
        }
        catch (ArtifactDatabaseException ade) {
            log.error(ade, ade);
        }

        return doc;
    }


    protected Element buildArtifactNode(
        ArtifactDatabase        database,
        String                  uuid,
        CallContext             context,
        XMLUtils.ElementCreator ec)
    throws ArtifactDatabaseException
    {
        log.debug("Append artifact '" + uuid + "' to collection description");

        // XXX I am not sure if it works well every time with an empty document
        // in the describe operation of an artifact.
        Document description = database.describe(uuid, null, context.getMeta());

        de.intevation.flys.artifacts.XMLDebug.out(description);

        String hash = "MYHASH";
        // TODO

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

        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