teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5994: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.artifacts.states; ingo@927: gernotbelger@9277: import java.io.File; gernotbelger@9277: gernotbelger@9277: import org.apache.log4j.Logger; teichmann@5831: import org.dive4elements.artifacts.Artifact; teichmann@5831: import org.dive4elements.artifacts.CallContext; teichmann@5831: import org.dive4elements.artifacts.CallMeta; teichmann@5831: import org.dive4elements.artifacts.common.utils.FileTools; teichmann@5831: import org.dive4elements.artifacts.common.utils.XMLUtils; teichmann@5867: import org.dive4elements.river.artifacts.D4EArtifact; teichmann@5831: import org.dive4elements.river.artifacts.resources.Resources; teichmann@5865: import org.dive4elements.river.utils.RiverUtils; ingo@927: import org.w3c.dom.Element; ingo@927: ingo@927: /** ingo@927: * @author Ingo Weinzierl ingo@927: */ ingo@927: public class ScenarioSelect extends DefaultState { ingo@927: gernotbelger@9277: /** The log that is used in this class. */ teichmann@8202: private static Logger log = Logger.getLogger(ScenarioSelect.class); ingo@927: gernotbelger@9277: public static final String FIELD_MODE = "scenario"; ingo@927: gernotbelger@9277: public static final String SCENARIO_CURRENT = "scenario.current"; ingo@927: public static final String SCENARIO_POTENTIEL = "scenario.potentiel"; gernotbelger@9277: public static final String SCENARIO_SCENRAIO = "scenario.scenario"; ingo@927: gernotbelger@9277: public static final String[] SCENARIOS = { SCENARIO_CURRENT, SCENARIO_POTENTIEL, SCENARIO_SCENRAIO }; ingo@927: ingo@1064: @Override ingo@1064: protected String getUIProvider() { rrenkert@5312: return ""; ingo@1064: } ingo@1064: ingo@1159: @Override gernotbelger@9277: protected Element[] createItems(final XMLUtils.ElementCreator cr, final Artifact artifact, final String name, final CallContext context) { gernotbelger@9277: final CallMeta meta = context.getMeta(); ingo@927: ingo@1064: if (name.equals(FIELD_MODE)) { gernotbelger@9277: final Element[] scenarios = new Element[SCENARIOS.length]; ingo@1064: ingo@1064: int i = 0; ingo@1064: gernotbelger@9277: for (final String scenario : SCENARIOS) { gernotbelger@9277: scenarios[i++] = createItem(cr, new String[] { Resources.getMsg(meta, scenario, scenario), scenario }); ingo@1064: } ingo@1064: ingo@1064: return scenarios; gernotbelger@9277: } else { gernotbelger@9277: final D4EArtifact flys = (D4EArtifact) artifact; gernotbelger@9277: final String data = flys.getDataAsString(name); ingo@927: gernotbelger@9277: return new Element[] { createItem(cr, new String[] { Resources.getMsg(meta, name, name), data }) }; ingo@1064: } ingo@927: } ingo@927: gernotbelger@9277: // ist mit super identisch gernotbelger@9277: // @Override gernotbelger@9277: // protected Element createItem(XMLUtils.ElementCreator cr, Object obj) { gernotbelger@9277: // Element item = ProtocolUtils.createArtNode(cr, "item", null, null); gernotbelger@9277: // Element label = ProtocolUtils.createArtNode(cr, "label", null, null); gernotbelger@9277: // Element value = ProtocolUtils.createArtNode(cr, "value", null, null); gernotbelger@9277: // gernotbelger@9277: // String[] arr = (String[]) obj; gernotbelger@9277: // gernotbelger@9277: // label.setTextContent(arr[0]); gernotbelger@9277: // value.setTextContent(arr[1]); gernotbelger@9277: // gernotbelger@9277: // item.appendChild(label); gernotbelger@9277: // item.appendChild(value); gernotbelger@9277: // gernotbelger@9277: // return item; gernotbelger@9277: // } ingo@927: christian@4656: @Override gernotbelger@9277: public void endOfLife(final Artifact artifact, final Object callContext) { raimund@2639: super.endOfLife(artifact, callContext); teichmann@8202: log.info("ScenarioSelect.endOfLife: " + artifact.identifier()); raimund@2639: gernotbelger@9277: final D4EArtifact flys = (D4EArtifact) artifact; raimund@2639: removeDirectory(flys); raimund@2639: } raimund@2639: raimund@2639: /** raimund@2639: * Removes the directory and all its content where the required data and the raimund@2639: * results of WSPLGEN are stored. Should be called in endOfLife(). raimund@2639: */ christian@4656: // FIXME: I've seen this code somewhere else... gernotbelger@9277: protected void removeDirectory(final D4EArtifact artifact) { gernotbelger@9277: final String shapePath = RiverUtils.getXPathString(RiverUtils.XPATH_FLOODMAP_SHAPEFILE_DIR); raimund@2639: gernotbelger@9277: final File artifactDir = new File(shapePath, artifact.identifier()); raimund@2639: raimund@2639: if (artifactDir.exists()) { teichmann@8202: log.debug("Delete directory: " + artifactDir.getAbsolutePath()); gernotbelger@9277: final boolean success = FileTools.deleteRecursive(artifactDir); teichmann@4049: if (!success) { teichmann@8202: log.warn("could not remove dir '" + artifactDir + "'"); teichmann@4049: } gernotbelger@9277: } else { teichmann@8202: log.debug("There is no directory to remove."); raimund@2639: } raimund@2639: } raimund@2639: ingo@927: } ingo@927: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :