view artifacts/src/main/java/org/dive4elements/river/exports/ShapeExporter.java @ 6140:60b94dec104b

Add handling of bound artifacts. When an artifact is bound to an out its facets will only be shown in that Out. They will be removed in the blackboard pass and marked as incompatible by the AttributeWriter
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 31 May 2013 15:29:30 +0200
parents c35323148b98
children 1b35b2ddfc28
line wrap: on
line source
package org.dive4elements.river.exports;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.log4j.Logger;
import org.dive4elements.artifactdatabase.state.ArtifactAndFacet;
import org.dive4elements.artifactdatabase.state.Settings;
import org.dive4elements.artifacts.Artifact;
import org.dive4elements.artifacts.CallContext;
import org.dive4elements.artifacts.common.ArtifactNamespaceContext;
import org.dive4elements.artifacts.common.utils.FileTools;
import org.dive4elements.artifacts.common.utils.XMLUtils;
import org.dive4elements.river.collections.D4EArtifactCollection;
import org.w3c.dom.Document;


public class ShapeExporter implements OutGenerator
{
    private static final String XPATH_FACET = "/art:action/@art:type";
    private static Logger logger = Logger.getLogger(ShapeExporter.class);
    private Artifact master;
    private Document request;
    private OutputStream out;
    private CallContext context;
    private D4EArtifactCollection collection;
    private String facet;
    private File dir;

    @Override
    public void init(Document request, OutputStream out, CallContext context) {
        this.request = request;
        this.out = out;
        this.context = context;
    }

    @Override
    public void setMasterArtifact(Artifact master) {
        this.master = master;
    }

    @Override
    public void setCollection(D4EArtifactCollection collection) {
        this.collection = collection;
    }

    @Override
    public void doOut(ArtifactAndFacet bundle, Document attr, boolean visible) {
        String name = bundle.getFacetName();

        if (!isFacetValid(name)) {
            logger.debug("Facet '" + name + "' is not valid for this exporter!");
            return;
        }

        addData(bundle.getData(context));
    }

    private void addData(Object data) {
        if (data instanceof File) {
            this.dir = (File)data;
        }
    }

    private boolean isFacetValid(String name) {
        String thisFacet = getFacet();
        if (thisFacet == null || thisFacet.length() == 0) {
            return false;
        }
        else if (facet == null || facet.length() == 0) {
            return false;
        }
        else {
            return thisFacet.equals(facet);
        }
    }


    /**
     * Returns the name of the desired facet.
     *
     * @return the name of the desired facet.
     */
    protected String getFacet() {
        if (facet == null) {
            facet = getFacetFromRequest();
        }

        return facet;
    }

    @Override
    public void generate() throws IOException {
        FileFilter filter = new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                if (pathname.getName().startsWith("wsplgen") &&
                    !pathname.getName().endsWith(".par")) {
                    return true;
                }
                else {
                    return false;
                }
            }
        };
        FileTools.createZipArchive(this.dir, out, filter);
        out.close();
    }

    @Override
    public void setSettings(Settings settings) {
        //Do nothing.
    }

    @Override
    public Settings getSettings() {
        // This exporter has no settings.
        return null;
    }

    /**
     * Extracts the name of the requested facet from request document.
     *
     * @return the name of the requested facet.
     */
    protected String getFacetFromRequest() {
        return XMLUtils.xpathString(
            request, XPATH_FACET, ArtifactNamespaceContext.INSTANCE);
    }
}

http://dive4elements.wald.intevation.org