Mercurial > dive4elements > river
changeset 5912:81bdb5c4414d
New exporter and facet for wsplgen calculation result.
author | Raimund Renkert <rrenkert@intevation.de> |
---|---|
date | Tue, 07 May 2013 12:45:57 +0200 |
parents | b96a293d30f3 |
children | 37a0f4f7c54f |
files | artifacts/src/main/java/org/dive4elements/river/artifacts/model/map/ShapeFacet.java artifacts/src/main/java/org/dive4elements/river/exports/ShapeExporter.java |
diffstat | 2 files changed, 183 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/map/ShapeFacet.java Tue May 07 12:45:57 2013 +0200 @@ -0,0 +1,47 @@ +package org.dive4elements.river.artifacts.model.map; + +import java.io.File; + +import org.apache.log4j.Logger; +import org.dive4elements.artifactdatabase.state.DefaultFacet; +import org.dive4elements.artifacts.Artifact; +import org.dive4elements.artifacts.CallContext; +import org.dive4elements.artifacts.common.utils.Config; +import org.dive4elements.river.artifacts.D4EArtifact; +import org.dive4elements.river.artifacts.states.DefaultState.ComputeType; + + +public class ShapeFacet +extends DefaultFacet +{ + + private static Logger logger = Logger.getLogger(ShapeFacet.class); + private static final String BASE_DIR = + "/artifact-database/floodmap/shapefile-path/@value"; + + /** + * Defaults to ADVANCE Compute type. + * @param name Name of the facet. + * @param description maybe localized description of the facet. + */ + public ShapeFacet(String name, String description) { + super(name, description); + } + + /** + * Return computation result. + */ + @Override + public Object getData(Artifact artifact, CallContext context) { + D4EArtifact flys = (D4EArtifact)artifact; + String tmp = Config.getStringXPath(BASE_DIR); + String baseDir = Config.replaceConfigDir(tmp); + baseDir += "/" + flys.identifier(); + File shapeDir = new File(baseDir); + if (shapeDir.exists()) { + return shapeDir; + } + return null; + } + +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/artifacts/src/main/java/org/dive4elements/river/exports/ShapeExporter.java Tue May 07 12:45:57 2013 +0200 @@ -0,0 +1,136 @@ +package org.dive4elements.river.exports; + +import java.io.File; +import java.io.FileFilter; +import java.io.IOException; +import java.io.OutputStream; +import java.util.List; +import java.util.zip.ZipOutputStream; + +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; + +import au.com.bytecode.opencsv.CSVWriter; + + +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); + } +} \ No newline at end of file