felix@2172: package de.intevation.flys.artifacts.states;
felix@2172: 
felix@2172: import java.util.List;
felix@2172: 
felix@2172: import org.apache.log4j.Logger;
felix@3198: import org.json.JSONArray;
felix@3198: import org.json.JSONException;
felix@3198: 
christian@3883: import de.intevation.artifactdatabase.state.Facet;
felix@2172: import de.intevation.artifacts.CallContext;
christian@3883: import de.intevation.artifacts.CallMeta;
felix@2172: import de.intevation.flys.artifacts.FLYSArtifact;
felix@2206: import de.intevation.flys.artifacts.ManualPointsArtifact;
felix@3198: import de.intevation.flys.artifacts.model.CrossSectionWaterLineFacet;
felix@3198: import de.intevation.flys.artifacts.model.FacetTypes;
felix@2172: import de.intevation.flys.artifacts.model.ManualPointsFacet;
felix@2172: import de.intevation.flys.artifacts.resources.Resources;
felix@2172: 
felix@2172: /**
felix@2172:  * The only state for an ManualPointArtifact.
felix@2172:  */
felix@2172: public class ManualPointsSingleState
felix@2172: extends      DefaultState
felix@2172: implements   FacetTypes
felix@2172: {
felix@2172:     /** Developer-centric description of facet. */
felix@3198:     public static final String I18N_DESCRIPTION
felix@3198:         = "facet.longitudinal_section.manualpoint";
felix@3198: 
felix@3198:     /** Part of data key. */
felix@3198:     protected static final String DOT_DATA
felix@3198:         = ".data";
felix@3198: 
felix@3198:     /** Part of data key. */
felix@3198:     protected static final String DOT_LINES
felix@3198:         = ".lines";
felix@2172: 
felix@2172:     /** The logger that is used in this state. */
felix@3198:     private static final Logger logger =
felix@3198:         Logger.getLogger(ManualPointsSingleState.class);
felix@2172: 
felix@2172: 
felix@2172:     /**
felix@3198:      * Add ManualPointsFacets to list of Facets.
felix@2172:      *
felix@2172:      * @param artifact Ignored.
felix@2172:      * @param hash Ignored.
felix@2172:      * @param meta CallMeta to be used for internationalization.
felix@2172:      * @param facets List to add ManualPointsFacet to.
felix@2172:      *
felix@2172:      * @return null.
felix@2172:      */
felix@2172:     public Object compute(
felix@2172:         FLYSArtifact artifact,
felix@2172:         String       hash,
felix@2172:         CallMeta     meta,
felix@2172:         List<Facet>  facets
felix@2172:     ) {
felix@2206:         logger.debug("ManualPointsSingleState.compute()");
felix@2206:         ManualPointsArtifact points = (ManualPointsArtifact) artifact;
felix@2172: 
felix@2206:         // Add Facet per Diagram type if data given.
felix@2206:         for (ChartType ct: ChartType.values()) {
felix@3198:             // Handle points.
felix@3198:             String pointData = points.getDataAsString(ct + "." + MANUALPOINTS +
felix@3198:                 DOT_DATA);
sascha@3453:             if (pointData != null && pointData.length() != 0
felix@3198:                 && !pointData.equals("[]")) {
felix@2206:                 String fName = ct + "." + MANUALPOINTS;
felix@2206:                 ManualPointsFacet facet = new ManualPointsFacet(
felix@2206:                     0,
felix@2206:                     fName,
felix@4212:                     Resources.getMsg(meta, MANUALPOINTS, "Manual Points"));
felix@2206:                 facets.add(facet);
christian@3917:                 logger.debug("compute(): ManualPointsFacet for " + ct + " created");
felix@2206:             }
felix@2206:             else {
christian@3883:                 logger.debug("compute(): No points for " + ct);
felix@2206:             }
felix@3198: 
felix@3198:             // Handle lines.
felix@3198:             String linesData = points.getDataAsString(ct + "." + MANUALPOINTS +
felix@3198:                 DOT_LINES);
sascha@3453:             if (linesData != null && linesData.length() != 0
felix@3198:                 && !linesData.equals("[]")) {
felix@3198:                 try {
christian@3883:                     JSONArray lines = new JSONArray(linesData);
felix@3198:                     for (int i = 0, P = lines.length(); i < P; i++) {
felix@3198:                         JSONArray array = lines.getJSONArray(i);
felix@3198:                         double y    = array.getDouble(0);
felix@3198:                         String name = array.getString(1);
felix@3198:                         String fName = ct + "." + MANUALLINE;
felix@3198:                         logger.debug("have facet: " + y + " / " + name + " -> " + fName);
felix@3198:                         CrossSectionWaterLineFacet facet = new CrossSectionWaterLineFacet(
felix@3198:                             i,
felix@3198:                             fName,
felix@3198:                             name);
felix@3198: 
felix@3198:                         facets.add(facet);
felix@3198:                     }
felix@3198:                 }
felix@3198:                 catch(JSONException e){
felix@3198:                     logger.error("Could not decode json.");
felix@3198:                 }
felix@3198: 
felix@3198:             }
felix@3198:             else {
felix@3198:                 //logger.debug("No points for " + ct);
felix@3198:             }
felix@2206:         }
felix@2172: 
felix@2172:         return null;
felix@2172:     }
felix@2172: 
felix@2172: 
felix@3198:     /** Call compute. */
felix@2172:     @Override
felix@2172:     public Object computeInit(
felix@2172:         FLYSArtifact artifact,
felix@2172:         String       hash,
felix@2172:         Object       context,
felix@2172:         CallMeta     meta,
felix@2172:         List<Facet>  facets
felix@2172:      ) {
felix@2172:         return compute(artifact, hash, meta, facets);
felix@2172:     }
felix@2172: 
felix@2172: 
felix@3198:     /** Call compute. */
felix@2172:     @Override
felix@2172:     public Object computeFeed(
felix@2172:         FLYSArtifact artifact,
felix@2172:         String hash,
felix@2172:         CallContext context,
felix@2172:         List<Facet> facets,
felix@2172:         Object old
felix@2172:     ) {
felix@2172:         return compute(artifact, hash, context.getMeta(), facets);
felix@2172:     }
felix@2172: }
felix@2172: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :