felix@1151: package de.intevation.flys.artifacts.states;
felix@1151: 
felix@1643: import java.util.ArrayList;
felix@1151: import java.util.List;
felix@1151: 
felix@1151: import org.apache.log4j.Logger;
felix@1151: 
felix@1643: import de.intevation.artifactdatabase.state.Facet;
felix@1643: import de.intevation.artifactdatabase.data.StateData;
felix@1151: 
felix@1643: import de.intevation.artifacts.CallContext;
felix@1184: import de.intevation.artifacts.Artifact;
felix@1151: import de.intevation.flys.artifacts.FLYSArtifact;
felix@1732: import de.intevation.flys.artifacts.StaticWKmsArtifact;
felix@1151: import de.intevation.flys.artifacts.WINFOArtifact;
raimund@2136: import de.intevation.flys.artifacts.ChartArtifact;
felix@1151: 
felix@1151: import de.intevation.flys.artifacts.math.WKmsOperation;
felix@1151: 
felix@1643: import de.intevation.flys.artifacts.model.CalculationResult;
felix@1643: import de.intevation.flys.artifacts.model.DataFacet;
felix@1643: import de.intevation.flys.artifacts.model.DifferenceCurveFacet;
raimund@2136: import de.intevation.flys.artifacts.model.EmptyFacet;
raimund@2136: 
felix@1151: import de.intevation.flys.artifacts.model.FacetTypes;
felix@1151: import de.intevation.flys.artifacts.model.WKms;
felix@1151: import de.intevation.flys.artifacts.model.WQKms;
felix@1151: 
felix@1151: import de.intevation.flys.utils.FLYSUtils;
felix@1655: import de.intevation.flys.utils.StringUtil;
felix@1151: 
ingo@1158: 
felix@1151: public class WDifferencesState
felix@1151: extends      DefaultState
felix@1151: implements   FacetTypes
felix@1151: {
felix@1151:     /** The logger that is used in this state. */
felix@1151:     private static Logger logger = Logger.getLogger(WDifferencesState.class);
felix@1151: 
felix@1173: 
felix@1151:     public WDifferencesState() {
felix@1151:     }
felix@1151: 
felix@1151: 
felix@1173:     /** Specify to display nothing (this is kind of a "final" state). */
felix@1151:     @Override
felix@1151:     protected String getUIProvider() {
felix@1173:         return "noinput";
felix@1151:     }
felix@1151: 
felix@1151: 
felix@1151:     @Override
felix@1183:     public boolean validate(Artifact artifact)
felix@1183:     throws IllegalArgumentException
felix@1183:     {
felix@1183:         FLYSArtifact flys = (FLYSArtifact) artifact;
felix@1183: 
felix@1183:         StateData data = flys.getData("diffids");
felix@1183: 
felix@1183:         if (data == null) {
felix@1183:             throw new IllegalArgumentException("diffids is empty");
felix@1183:         }
felix@1183: 
felix@1183:         // TODO: Also validate format.
felix@1183: 
felix@1183:         return true;
felix@1183:     }
felix@1183: 
felix@1183: 
felix@1732:     protected WKms getWKms(String mingle, CallContext context) {
felix@1732:         String[] def  = mingle.split(";");
felix@1732:         String   uuid = def[0];
felix@1732:         String   name = def[1];
felix@1732:         int      idx  = Integer.parseInt(def[2]);
felix@1732: 
felix@1732:         if (name.startsWith("staticwkms")) {
felix@1732:             StaticWKmsArtifact staticWKms =
felix@1732:                 (StaticWKmsArtifact) FLYSUtils.getArtifact(
felix@1732:                     uuid,
felix@1732:                     context);
felix@1732:             logger.debug("WDifferencesState obtain data from StaticWKms");
felix@1732:             WKms wkms = staticWKms.getWKms(idx);
felix@1732:             if (wkms == null)
felix@1732:                 logger.error("No WKms from artifact.");
felix@1732:             return wkms;
felix@1732:         }
sascha@3076: 
felix@1732:         WINFOArtifact flys = (WINFOArtifact) FLYSUtils.getArtifact(
felix@1732:             uuid,
felix@1732:             context);
felix@1732: 
felix@1732:         if (flys == null) {
felix@1732:             logger.warn("One of the artifacts (1) for diff calculation could not be loaded");
felix@1732:             return null;
felix@1732:         }
felix@1732:         else{
felix@1732:             WQKms[] wqkms = (WQKms[]) flys.getWaterlevelData().
felix@1732:                                               getData();
felix@1732:             if (wqkms == null)
felix@1732:             logger.warn("not  waterlevels in artifact");
felix@1732:             else if (wqkms.length < idx)
felix@1732:             logger.warn("not enough waterlevels in artifact");
felix@1732:             return wqkms[idx];
felix@1732:         }
felix@1732:     }
felix@1732: 
felix@1732: 
felix@1643:     /**
felix@1652:      * Return CalculationResult with Array of WKms that are difference of
felix@1652:      * Waterlevels. Add respective facets (DifferencesCurveFacet, DataFacet).
felix@1643:      */
felix@1183:     @Override
felix@1151:     public Object computeAdvance(
felix@1151:         FLYSArtifact artifact,
felix@1151:         String       hash,
felix@1151:         CallContext  context,
felix@1151:         List<Facet>  facets,
felix@1151:         Object       old
felix@1151:     ) {
raimund@2136:         if (artifact instanceof ChartArtifact) {
raimund@2136:             ChartArtifact chart = (ChartArtifact)artifact;
raimund@2136:             facets.add(new EmptyFacet());
raimund@2136:             return null;
raimund@2136:         }
felix@1732:         WINFOArtifact winfo = (WINFOArtifact) artifact;
felix@1173:         String id = getID();
felix@1151: 
felix@1183:         // Load the Artifacts/facets that we want to subtract and display.
felix@1183:         // Expected format is:
felix@1183:         // [42537f1e-3522-42ef-8968-635b03d8e9c6;longitudinal_section.w;0]#[1231f2-....]
felix@1661:         String diffids = winfo.getDataAsString("diffids");
felix@1732:         logger.debug("WDifferencesState has: " + diffids);
felix@1661:         String datas[] = diffids.split("#");
felix@1183: 
felix@1183:         // Validate the Data-Strings.
felix@1183:         for (String s: datas) {
felix@1184:             if (!WaterlevelSelectState.isValueValid(winfo.getDataAsString("diffids"))) {
felix@1183:                 // TODO: escalate.
felix@1183:             }
felix@1183:         }
felix@1183: 
felix@1626:         if (datas.length < 2) {
felix@1183:             // TODO crash with style
felix@1183:         }
felix@1151: 
felix@1643:         List<WKms> wkmss = new ArrayList<WKms>();
felix@1151: 
felix@1661:         for(int i = 0; i < datas.length; i+=2) {
felix@1652:             // e.g.:
felix@1652:             // 42537f1e-3522-42ef-8968-635b03d8e9c6;longitudinal_section.w;1
felix@1732:             WKms minuendWKms = getWKms(StringUtil.unbracket(datas[i+0]),
felix@1626:                 context);
felix@1732:             WKms subtrahendWKms = getWKms(StringUtil.unbracket(datas[i+1]),
felix@1732:                 context);
felix@1732: 
felix@1626:             String facetName = "diff ()";
felix@1626: 
felix@1732:             if (minuendWKms != null && subtrahendWKms != null) {
sascha@3076:                 facetName = StringUtil.wWrap(minuendWKms.getName())
felix@1732:                     + " - " + StringUtil.wWrap(subtrahendWKms.getName());
felix@1732:                 WKms wkms = WKmsOperation.SUBTRACTION.operate(minuendWKms,
felix@1732:                      subtrahendWKms);
felix@1643:                 wkms.setName(facetName);
felix@1643:                 wkmss.add(wkms);
felix@1626:                 logger.debug("WKMSSubtraction happened");
felix@1626:             }
felix@1626:             if (facets != null) {
felix@1696:                 facets.add(new DifferenceCurveFacet(i/2, W_DIFFERENCES, facetName,
felix@1643:                     ComputeType.ADVANCE, id, hash));
felix@1626:             }
raimund@2180:         }
raimund@2180: 
raimund@2180:         if (facets != null) {
raimund@2180:             facets.add(new DataFacet(CSV, "CSV data"));
raimund@2180:             facets.add(new DataFacet(PDF, "PDF data"));
raimund@2180:             logger.debug("Adding facets in WDifferencesState.");
raimund@2180:         }
raimund@2180:         else {
raimund@2180:             logger.debug("Not adding facets in WDifferencesState.");
felix@1151:         }
felix@1151: 
felix@1652:         // TODO Evaluate whether null is okay as reports.
felix@1643:         WKms[] diffs = wkmss.toArray(new WKms[wkmss.size()]);
felix@1652:         CalculationResult result = new CalculationResult(diffs, null);
felix@1643:         return result;
felix@1151:     }
felix@1151: }
felix@1151: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :