[Dive4elements-commits] [PATCH 1 of 2] New exporter and facet for wsplgen calculation result
Wald Commits
scm-commit at wald.intevation.org
Tue May 7 12:47:40 CEST 2013
# HG changeset patch
# User Raimund Renkert <rrenkert at intevation.de>
# Date 1367923557 -7200
# Node ID 81bdb5c4414d4932c2ca0c22ae671409f61d5117
# Parent b96a293d30f3b15caf1c7dc60872df07a55d789b
New exporter and facet for wsplgen calculation result.
diff -r b96a293d30f3 -r 81bdb5c4414d artifacts/src/main/java/org/dive4elements/river/artifacts/model/map/ShapeFacet.java
--- /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
diff -r b96a293d30f3 -r 81bdb5c4414d artifacts/src/main/java/org/dive4elements/river/exports/ShapeExporter.java
--- /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
More information about the Dive4elements-commits
mailing list