# HG changeset patch # User Ingo Weinzierl # Date 1314362532 0 # Node ID 7230e087ef8b86bfcb9ca4c1c69ea4f66f4073f9 # Parent 9324839684a433f2514999aeed62e52aba3005c9 Prepare directories for WSPLGEN specific data in FloodMap state and remove those directories when State.endOfLife() is called. flys-artifacts/trunk@2594 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 9324839684a4 -r 7230e087ef8b flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Fri Aug 26 12:40:11 2011 +0000 +++ b/flys-artifacts/ChangeLog Fri Aug 26 12:42:12 2011 +0000 @@ -1,3 +1,16 @@ +2011-08-26 Ingo Weinzierl + + * doc/conf/conf.xml: Added a configuration node that points to the directory + where shapefiles should be stored in. + + * src/main/java/de/intevation/flys/artifacts/model/WSPLGENJob.java: New. + This class is used to save/write the parameter for a WSPLGEN calculation. + WSPLGEN's *.par files are written using the toFile() method. + + * src/main/java/de/intevation/flys/artifacts/states/FloodMapState.java: + A directory for all WSPLGEN stuff is created in computeAdvance() - those + directory and all its contained files are removed in endOfLife(). + 2011-08-26 Felix Wolfsteller * src/main/java/de/intevation/flys/utils/ThemeUtil.java: diff -r 9324839684a4 -r 7230e087ef8b flys-artifacts/doc/conf/conf.xml --- a/flys-artifacts/doc/conf/conf.xml Fri Aug 26 12:40:11 2011 +0000 +++ b/flys-artifacts/doc/conf/conf.xml Fri Aug 26 12:42:12 2011 +0000 @@ -117,6 +117,8 @@ requires a srid, wms and background-wms. Those information are used for creating a Map view.--> + + diff -r 9324839684a4 -r 7230e087ef8b flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WSPLGENJob.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WSPLGENJob.java Fri Aug 26 12:42:12 2011 +0000 @@ -0,0 +1,415 @@ +package de.intevation.flys.artifacts.model; + +import java.io.IOException; +import java.io.File; +import java.io.FileOutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; + + +public class WSPLGENJob { + + public static final String GEL_SPERRE = "SPERRE"; + public static final String GEL_NOSPERRE = "NOSPERRE"; + + + protected String dgm; + protected String pro; + protected String wsp; + protected String wspTag; + protected String lin; + protected String axis; + protected String area; + protected String gel; + protected String outFile; + + protected int out; + + protected double start; + protected double end; + protected double from; + protected double to; + protected double diff; + protected double dist; + + + + public WSPLGENJob() { + out = -1; + start = Double.NaN; + end = Double.NaN; + from = Double.NaN; + to = Double.NaN; + diff = Double.NaN; + dist = Double.NaN; + } + + + public void setWsp(String wsp) { + this.wsp = wsp; + } + + + public String getWsp() { + return wsp; + } + + + public void setWspTag(String wspTag) { + this.wspTag = wspTag; + } + + + public String getWspTag() { + return wspTag; + } + + + public void setLin(String lin) { + this.lin = lin; + } + + + public String getLin() { + return lin; + } + + + public void setAxis(String axis) { + this.axis = axis; + } + + + public String getAxis() { + return axis; + } + + + public void setArea(String area) { + this.area = area; + } + + + public String getArea() { + return area; + } + + + public void setOut(int out) { + this.out = out; + } + + + public int getOut() { + return out; + } + + + public void setOutFile(String outFile) { + this.outFile = outFile; + } + + + public String getOutFile() { + return outFile; + } + + + public void setStart(double start) { + this.start = start; + } + + + public double getStart() { + return start; + } + + + public void setEnd(double end) { + this.end = end; + } + + + public double getEnd() { + return end; + } + + + public void setPro(String pro) { + this.pro = pro; + } + + + public String getPro() { + return pro; + } + + + public void setDgm(String dgm) { + this.dgm = dgm; + } + + + public String getDgm() { + return dgm; + } + + + public void setFrom(double from) { + this.from = from; + } + + + public double getFrom() { + return from; + } + + + public void setTo(double to) { + this.to = to; + } + + + public double getTo() { + return to; + } + + + public void setDiff(double diff) { + this.diff = diff; + } + + + public double getDiff() { + return diff; + } + + + public void setDist(double dist) { + this.dist = dist; + } + + + public double getDist() { + return dist; + } + + + public void setGel(String gel) { + if (gel == null || gel.length() == 0) { + return; + } + + if (gel.equals(GEL_SPERRE) || gel.equals(GEL_NOSPERRE)) { + this.gel = gel; + } + } + + + public String getGel() { + return gel; + } + + + public void toFile(File file) + throws IOException, IllegalArgumentException + { + PrintWriter writer = null; + + try { + writer = + new PrintWriter( + new OutputStreamWriter( + new FileOutputStream(file))); + + write(writer); + } + finally { + if (writer != null) { + writer.flush(); + writer.close(); + } + } + } + + + protected void write(PrintWriter writer) + throws IOException, IllegalArgumentException + { + writeWsp(writer); + writeWspTag(writer); + writeLin(writer); + writeAxis(writer); + writeArea(writer); + writeOut(writer); + writeOutFile(writer); + writeRange(writer); + writeDelta(writer); + writeGel(writer); + writeDist(writer); + writePro(writer); + writeDgm(writer); + } + + + protected void writeWsp(PrintWriter writer) + throws IllegalArgumentException + { + String wsp = getWsp(); + + if (wsp != null && wsp.length() > 0) { + writer.println("-WSP=\"" + wsp + "\""); + return; + } + + throw new IllegalArgumentException("Required WSP missing!"); + } + + protected void writeWspTag(PrintWriter writer) + throws IllegalArgumentException + { + String wspTag = getWspTag(); + + if (wspTag != null && wspTag.length() > 0) { + writer.println("-WSPTAG=\"" + wspTag + "\""); + return; + } + + throw new IllegalArgumentException("Required WSPTAG missing!"); + } + + protected void writeLin(PrintWriter writer) + throws IllegalArgumentException + { + String lin = getLin(); + + if (lin != null && lin.length() > 0) { + writer.println("-LIN=\"" + lin + "\""); + } + } + + protected void writeAxis(PrintWriter writer) + throws IllegalArgumentException + { + String axis = getAxis(); + + if (axis != null && axis.length() > 0) { + writer.println("-ACHSE=\"" + axis + "\""); + } + } + + protected void writeGel(PrintWriter writer) + throws IllegalArgumentException + { + if (area != null && area.length() > 0) { + writer.println("-GEL=" + getGel()); + } + } + + protected void writeArea(PrintWriter writer) + throws IllegalArgumentException + { + String area = getArea(); + + if (area != null && area.length() > 0) { + writer.println("-GEBIET=\"" + area + "\""); + } + } + + + protected void writeOut(PrintWriter writer) + throws IllegalArgumentException + { + int out = getOut(); + + if (out >= 0) { + writer.println("-OUTPUT=" + String.valueOf(out)); + } + } + + protected void writeOutFile(PrintWriter writer) + throws IllegalArgumentException + { + String outFile = getOutFile(); + + if (outFile != null && outFile.length() > 0) { + writer.println("-AUSGABE=\""+ outFile + "\""); + } + } + + protected void writeRange(PrintWriter writer) + throws IllegalArgumentException + { + StringBuilder sb = new StringBuilder("-STRECKE="); + + double start = getStart(); + double end = getEnd(); + + if (Double.isNaN(start) && Double.isNaN(end)) { + return; + } + + if (! Double.isNaN(getStart())) { + sb.append(getStart()); + } + + sb.append(","); + + if (! Double.isNaN(getEnd())) { + sb.append(getEnd()); + } + + writer.println(sb.toString()); + } + + protected void writeDelta(PrintWriter writer) + throws IllegalArgumentException + { + StringBuilder sb = new StringBuilder("-DELTA="); + if (! Double.isNaN(from)) { + sb.append(from); + } + + sb.append(","); + + if (! Double.isNaN(to)) { + sb.append(to); + } + + sb.append(","); + + if (! Double.isNaN(diff)) { + sb.append(diff); + } + + writer.println(sb.toString()); + } + + protected void writeDist(PrintWriter writer) + throws IllegalArgumentException + { + if (! Double.isNaN(getDist())) { + writer.println("-DIST=" + String.valueOf(getDist())); + } + } + + protected void writePro(PrintWriter writer) + throws IllegalArgumentException + { + if (pro != null && pro.length() > 0) { + writer.println("-PRO=\"" + getPro() + "\""); + } + } + + protected void writeDgm(PrintWriter writer) + throws IllegalArgumentException + { + if (dgm != null && dgm.length() > 0) { + writer.println("-DGM=\"" + getDgm() + "\""); + return; + } + + throw new IllegalArgumentException("Required DGM missing!"); + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : diff -r 9324839684a4 -r 7230e087ef8b flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/FloodMapState.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/FloodMapState.java Fri Aug 26 12:40:11 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/FloodMapState.java Fri Aug 26 12:42:12 2011 +0000 @@ -1,16 +1,25 @@ package de.intevation.flys.artifacts.states; +import java.io.File; +import java.io.IOException; import java.util.List; +import javax.xml.xpath.XPathConstants; + import org.apache.log4j.Logger; +import de.intevation.artifacts.Artifact; import de.intevation.artifacts.CallContext; +import de.intevation.artifacts.common.utils.Config; +import de.intevation.artifacts.common.utils.FileTools; + import de.intevation.artifactdatabase.state.Facet; import de.intevation.flys.artifacts.FLYSArtifact; import de.intevation.flys.artifacts.model.FacetTypes; import de.intevation.flys.artifacts.model.WSPLGENFacet; +import de.intevation.flys.artifacts.model.WSPLGENJob; public class FloodMapState @@ -21,6 +30,12 @@ private static Logger logger = Logger.getLogger(FloodMapState.class); + public static final String XPATH_SHAPEFILE_DIR = + "/artifact-database/floodmap/shapefile-path/@value"; + + public static final String WSPLGEN_PARAMETER_FILE = "wsplgen.par"; + + @Override public Object computeAdvance( @@ -30,8 +45,114 @@ List facets, Object old ) { + logger.debug("FloodMapState.computeAdvance"); + + File artifactDir = getDirectory(artifact); + + if (artifactDir == null) { + logger.error("Could not create directory for WSPLGEN results!"); + return null; + } + + WSPLGENJob job = prepareWSPLGENJob(artifact, artifactDir); + + if (job == null) { + removeDirectory(artifact); + + logger.error("No WSPLGEN processing has been started!"); + + return null; + } + facets.add(new WSPLGENFacet(0, FLOODMAP_WSPLGEN, "WSPLGEN")); + //context.afterCall(CallContext.BACKGROUND); + + return null; + } + + + /** + * Returns the shapefile path defined in the configuration. + * + * @return the shapefile path. + */ + protected String getShapefilePath() { + String shapePath = (String) Config.getXPath( + XPATH_SHAPEFILE_DIR, XPathConstants.STRING); + shapePath = Config.replaceConfigDir(shapePath); + + return shapePath; + } + + + /** + * Returns (and creates if not existing) the directory for storing WSPLEN + * data for the owner artifact. + * + * @param artifact The owner Artifact. + * + * @return the directory for WSPLEN data. + */ + protected File getDirectory(FLYSArtifact artifact) { + String shapePath = getShapefilePath(); + + File artifactDir = FileTools.getDirectory( + shapePath, artifact.identifier()); + + return artifactDir; + } + + + /** + * Removes the directory and all its content where the required data and the + * results of WSPLGEN are stored. Should be called in endOfLife(). + */ + protected void removeDirectory(FLYSArtifact artifact) { + String shapePath = getShapefilePath(); + File artifactDir = new File(shapePath, artifact.identifier()); + + if (artifactDir.exists()) { + logger.info("Delete directory: " + artifactDir.getAbsolutePath()); + boolean success = FileTools.deleteRecursive(artifactDir); + } + else { + logger.debug("There is no directory to remove."); + } + } + + + @Override + public void endOfLife(Artifact artifact, Object callContext) { + logger.info("FloodMapState.endOfLife: " + artifact.identifier()); + + FLYSArtifact flys = (FLYSArtifact) artifact; + removeDirectory(flys); + } + + + + protected WSPLGENJob prepareWSPLGENJob( + FLYSArtifact artifact, + File artifactDir + ) { + logger.debug("FloodMapState.prepareWSPLGENJob"); + + WSPLGENJob job = new WSPLGENJob(); + File paraFile = new File(artifactDir, WSPLGEN_PARAMETER_FILE); + + try { + job.toFile(paraFile); + + return job; + } + catch (IOException ioe) { + logger.warn("Cannot write PAR file: " + ioe.getMessage()); + } + catch (IllegalArgumentException iae) { + logger.warn("Cannot write PAR file: " + iae.getMessage()); + } + return null; } }