comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/FloodMapState.java @ 1091:7230e087ef8b

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
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 26 Aug 2011 12:42:12 +0000
parents 13784581ab0c
children 9f88cc54570c
comparison
equal deleted inserted replaced
1090:9324839684a4 1091:7230e087ef8b
1 package de.intevation.flys.artifacts.states; 1 package de.intevation.flys.artifacts.states;
2 2
3 import java.io.File;
4 import java.io.IOException;
3 import java.util.List; 5 import java.util.List;
6
7 import javax.xml.xpath.XPathConstants;
4 8
5 import org.apache.log4j.Logger; 9 import org.apache.log4j.Logger;
6 10
11 import de.intevation.artifacts.Artifact;
7 import de.intevation.artifacts.CallContext; 12 import de.intevation.artifacts.CallContext;
13
14 import de.intevation.artifacts.common.utils.Config;
15 import de.intevation.artifacts.common.utils.FileTools;
8 16
9 import de.intevation.artifactdatabase.state.Facet; 17 import de.intevation.artifactdatabase.state.Facet;
10 18
11 import de.intevation.flys.artifacts.FLYSArtifact; 19 import de.intevation.flys.artifacts.FLYSArtifact;
12 import de.intevation.flys.artifacts.model.FacetTypes; 20 import de.intevation.flys.artifacts.model.FacetTypes;
13 import de.intevation.flys.artifacts.model.WSPLGENFacet; 21 import de.intevation.flys.artifacts.model.WSPLGENFacet;
22 import de.intevation.flys.artifacts.model.WSPLGENJob;
14 23
15 24
16 public class FloodMapState 25 public class FloodMapState
17 extends DefaultState 26 extends DefaultState
18 implements FacetTypes 27 implements FacetTypes
19 { 28 {
20 /** The logger that is used in this state.*/ 29 /** The logger that is used in this state.*/
21 private static Logger logger = Logger.getLogger(FloodMapState.class); 30 private static Logger logger = Logger.getLogger(FloodMapState.class);
31
32
33 public static final String XPATH_SHAPEFILE_DIR =
34 "/artifact-database/floodmap/shapefile-path/@value";
35
36 public static final String WSPLGEN_PARAMETER_FILE = "wsplgen.par";
22 37
23 38
24 39
25 @Override 40 @Override
26 public Object computeAdvance( 41 public Object computeAdvance(
28 String hash, 43 String hash,
29 CallContext context, 44 CallContext context,
30 List<Facet> facets, 45 List<Facet> facets,
31 Object old 46 Object old
32 ) { 47 ) {
48 logger.debug("FloodMapState.computeAdvance");
49
50 File artifactDir = getDirectory(artifact);
51
52 if (artifactDir == null) {
53 logger.error("Could not create directory for WSPLGEN results!");
54 return null;
55 }
56
57 WSPLGENJob job = prepareWSPLGENJob(artifact, artifactDir);
58
59 if (job == null) {
60 removeDirectory(artifact);
61
62 logger.error("No WSPLGEN processing has been started!");
63
64 return null;
65 }
66
33 facets.add(new WSPLGENFacet(0, FLOODMAP_WSPLGEN, "WSPLGEN")); 67 facets.add(new WSPLGENFacet(0, FLOODMAP_WSPLGEN, "WSPLGEN"));
68
69 //context.afterCall(CallContext.BACKGROUND);
70
71 return null;
72 }
73
74
75 /**
76 * Returns the shapefile path defined in the configuration.
77 *
78 * @return the shapefile path.
79 */
80 protected String getShapefilePath() {
81 String shapePath = (String) Config.getXPath(
82 XPATH_SHAPEFILE_DIR, XPathConstants.STRING);
83 shapePath = Config.replaceConfigDir(shapePath);
84
85 return shapePath;
86 }
87
88
89 /**
90 * Returns (and creates if not existing) the directory for storing WSPLEN
91 * data for the owner artifact.
92 *
93 * @param artifact The owner Artifact.
94 *
95 * @return the directory for WSPLEN data.
96 */
97 protected File getDirectory(FLYSArtifact artifact) {
98 String shapePath = getShapefilePath();
99
100 File artifactDir = FileTools.getDirectory(
101 shapePath, artifact.identifier());
102
103 return artifactDir;
104 }
105
106
107 /**
108 * Removes the directory and all its content where the required data and the
109 * results of WSPLGEN are stored. Should be called in endOfLife().
110 */
111 protected void removeDirectory(FLYSArtifact artifact) {
112 String shapePath = getShapefilePath();
113 File artifactDir = new File(shapePath, artifact.identifier());
114
115 if (artifactDir.exists()) {
116 logger.info("Delete directory: " + artifactDir.getAbsolutePath());
117 boolean success = FileTools.deleteRecursive(artifactDir);
118 }
119 else {
120 logger.debug("There is no directory to remove.");
121 }
122 }
123
124
125 @Override
126 public void endOfLife(Artifact artifact, Object callContext) {
127 logger.info("FloodMapState.endOfLife: " + artifact.identifier());
128
129 FLYSArtifact flys = (FLYSArtifact) artifact;
130 removeDirectory(flys);
131 }
132
133
134
135 protected WSPLGENJob prepareWSPLGENJob(
136 FLYSArtifact artifact,
137 File artifactDir
138 ) {
139 logger.debug("FloodMapState.prepareWSPLGENJob");
140
141 WSPLGENJob job = new WSPLGENJob();
142 File paraFile = new File(artifactDir, WSPLGEN_PARAMETER_FILE);
143
144 try {
145 job.toFile(paraFile);
146
147 return job;
148 }
149 catch (IOException ioe) {
150 logger.warn("Cannot write PAR file: " + ioe.getMessage());
151 }
152 catch (IllegalArgumentException iae) {
153 logger.warn("Cannot write PAR file: " + iae.getMessage());
154 }
34 155
35 return null; 156 return null;
36 } 157 }
37 } 158 }
38 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : 159 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org