raimund@2113: package de.intevation.flys.artifacts;
raimund@2113: 
raimund@2113: import org.apache.log4j.Logger;
raimund@2113: 
raimund@2113: import java.util.List;
raimund@2113: 
raimund@2113: import org.w3c.dom.Document;
raimund@2113: import org.w3c.dom.Element;
raimund@2113: 
raimund@2113: import de.intevation.artifacts.Artifact;
raimund@2113: 
raimund@2113: import de.intevation.artifacts.ArtifactFactory;
raimund@2113: import de.intevation.artifacts.CallMeta;
raimund@2113: import de.intevation.artifacts.CallContext;
raimund@2113: 
raimund@2113: import de.intevation.artifacts.common.utils.XMLUtils;
raimund@2113: import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
raimund@2113: 
raimund@2113: import de.intevation.artifactdatabase.ProtocolUtils;
raimund@2113: import de.intevation.artifactdatabase.state.Facet;
raimund@2113: import de.intevation.artifactdatabase.state.State;
raimund@2113: import de.intevation.artifactdatabase.state.StateEngine;
raimund@2113: import de.intevation.artifactdatabase.state.Output;
raimund@2113: 
raimund@2113: import de.intevation.flys.utils.FLYSUtils;
raimund@2113: 
raimund@2113: import de.intevation.flys.artifacts.states.DefaultState;
raimund@2113: import de.intevation.flys.artifacts.context.FLYSContext;
raimund@2113: import de.intevation.flys.artifacts.resources.Resources;
raimund@2113: 
raimund@2113: 
ingo@3650: /** Artifact, open to generate any (?) out. */
raimund@2113: public class ChartArtifact extends FLYSArtifact {
raimund@2113: 
raimund@2113:     private static final Logger logger =
raimund@2113:         Logger.getLogger(ChartArtifact.class);
raimund@2113: 
raimund@2113:     @Override
raimund@2113:     public void setup(
raimund@2113:         String          identifier,
raimund@2113:         ArtifactFactory factory,
raimund@2113:         Object          context,
raimund@2113:         CallMeta        callmeta,
raimund@2113:         Document        data)
raimund@2113:     {
raimund@2113:         logger.debug("ChartArtifact.setup");
raimund@2113:         this.identifier = identifier;
raimund@2113:         name = "new_chart";
raimund@2113: 
raimund@2113:         List<State> states = getStates(context);
raimund@2113: 
raimund@2113:         setCurrentState(states.get(0));
raimund@2113:     }
raimund@2113: 
raimund@2113:     @Override
sascha@3193:     protected void appendBackgroundActivity(
raimund@2113:         ElementCreator cr,
raimund@2113:         Element        root,
raimund@2113:         CallContext    context
raimund@2113:     ) {
raimund@2113:         Element inBackground = cr.create("background-processing");
raimund@2113:         root.appendChild(inBackground);
raimund@2113: 
raimund@2113:         cr.addAttr(
raimund@2113:             inBackground,
raimund@2113:             "value",
raimund@2113:             String.valueOf(context.isInBackground()),
raimund@2113:             true);
raimund@2113:     }
raimund@2113: 
raimund@2113: 
raimund@2113:     /**
raimund@2113:      * Append output mode nodes to a document.
raimund@2113:      */
sascha@3193:     @Override
raimund@2113:     protected void appendOutputModes(
raimund@2113:         Document    doc,
raimund@2113:         Element     outs,
raimund@2113:         CallContext context,
raimund@2113:         String      uuid)
raimund@2113:     {
raimund@2113:         List<String> stateIds = getPreviousStateIds();
raimund@2113: 
raimund@2113:         FLYSContext flysContext = FLYSUtils.getFlysContext(context);
raimund@2113:         StateEngine engine      = (StateEngine) flysContext.get(
raimund@2113:             FLYSContext.STATE_ENGINE_KEY);
raimund@2113: 
raimund@2113:         for (String stateId: stateIds) {
raimund@2113:             logger.debug("Append output modes for state: " + stateId);
raimund@2113:             DefaultState state = (DefaultState) engine.getState(stateId);
raimund@2113: 
raimund@2113:             List<Output> list = state.getOutputs();
sascha@3555:             if (list == null || list.isEmpty()) {
raimund@2113:                 logger.debug("-> No output modes for this state.");
raimund@2113:                 continue;
raimund@2113:             }
raimund@2113: 
bjoern@4497:             List<Facet> fs = getFacets(stateId);
raimund@2113: 
sascha@3555:             if (fs == null || fs.isEmpty()) {
raimund@2113:                 logger.debug("No facets for previous state found.");
raimund@2113:                 continue;
raimund@2113:             }
raimund@2113: 
raimund@2113:             logger.debug("Found " + fs.size() + " facets in previous states.");
raimund@2113: 
raimund@2113:             List<Output> generated = generateOutputs(list, fs);
raimund@2113: 
raimund@2113:             ProtocolUtils.appendOutputModes(doc, outs, generated);
raimund@2113:         }
raimund@2113: 
raimund@2113:         try {
raimund@2113:             DefaultState cur = (DefaultState) getCurrentState(context);
raimund@2158:             List<Output> list = cur.getOutputs();
raimund@2158:             if (list != null && list.size() > 0) {
raimund@2158:                 logger.debug(
raimund@2158:                     "Append output modes for current state: " + cur.getID());
raimund@2113: 
bjoern@4497:                 List<Facet> fs = getFacets(cur.getID());
raimund@2113: 
raimund@2158:                 if (fs != null && fs.size() > 0) {
raimund@2158:                     List<Output> generated = generateOutputs(list, fs);
raimund@2158: 
raimund@2158:                     logger.debug("Found " + fs.size() + " current facets.");
raimund@2158:                     if (!generated.isEmpty()) {
raimund@2158:                         ProtocolUtils.appendOutputModes(
raimund@2158:                             doc, outs, generated);
raimund@2113:                     }
raimund@2158:                 }
raimund@2158:                 else {
raimund@2158:                     logger.debug("No facets found for the current state.");
raimund@2113:                 }
raimund@2113:             }
raimund@2113:         }
raimund@2113:         catch (IllegalArgumentException iae) {
raimund@2113:             // state is not valid, so we do not append its outputs.
raimund@2113:         }
raimund@2113:     }
raimund@2113: 
raimund@2113:     public static class ChartState extends DefaultState {
raimund@2113: 
raimund@2113:         public static final String FIELD_MODE = "chart_type";
raimund@2113: 
raimund@2113:         public static final String DURATION_CURVE =
raimund@2113:             "chart.new.durationcurve";
raimund@2113: 
raimund@2113:         public static final String COMPUTED_DISCHARGE_CURVE =
raimund@2113:             "chart.new.computeddischargecurve";
raimund@2113: 
raimund@2113:         public static final String DISCHARGE_LONGITUDINAL_CURVE =
raimund@2153:             "chart.new.longitudinal_section";
raimund@2113: 
raimund@2113:         public static final String W_DIFFERENCES =
raimund@2113:             "chart.new.w_differences";
raimund@2113: 
raimund@2113:         public static final String WATERLEVEL =
raimund@2153:             "chart.new.crosssection";
raimund@2113: 
raimund@2113:         public static final String[] CHARTS = {
raimund@2113:             COMPUTED_DISCHARGE_CURVE,
raimund@2113:             DURATION_CURVE,
raimund@2113:             DISCHARGE_LONGITUDINAL_CURVE,
raimund@2113:             W_DIFFERENCES,
raimund@2113:             WATERLEVEL };
raimund@2113: 
raimund@2113: 
raimund@2113: 
raimund@2113:         @Override
raimund@2113:         public Object computeAdvance(
raimund@2113:             FLYSArtifact artifact,
raimund@2113:             String       hash,
raimund@2113:             CallContext  context,
raimund@2113:             List<Facet>  facets,
raimund@2113:             Object       old)
raimund@2113:         {
raimund@2113:             logger.debug("ChartState.computeAdvance");
raimund@2113: 
raimund@2113: 
raimund@2113:             return null;
raimund@2113:         }
raimund@2113: 
raimund@2113: 
raimund@2113:         @Override
raimund@2113:         protected Element[] createItems(
raimund@2113:             XMLUtils.ElementCreator cr,
raimund@2113:             Artifact    artifact,
raimund@2113:             String      name,
raimund@2113:             CallContext context)
raimund@2113:         {
raimund@2113:             CallMeta meta   = context.getMeta();
raimund@2113:             Element[] charts = new Element[CHARTS.length];
raimund@2113: 
raimund@2113:             int i = 0;
raimund@2113: 
raimund@2113:             for (String chart: CHARTS) {
raimund@2113:                 charts[i++] = createItem(
raimund@2113:                     cr, new String[] {
raimund@2113:                         Resources.getMsg(meta, chart, chart),
raimund@2113:                         chart
raimund@2113:                     });
raimund@2113:             }
raimund@2113: 
raimund@2113:             return charts;
raimund@2113:         }
raimund@2113:     }
raimund@2113: }
sascha@3083: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :