ingo@127: package de.intevation.flys.artifacts.states;
ingo@127:
ingo@323: import java.util.Map;
ingo@323:
ingo@127: import org.apache.log4j.Logger;
ingo@127:
ingo@127: import org.w3c.dom.Element;
ingo@127:
ingo@313: import de.intevation.artifacts.Artifact;
ingo@127: import de.intevation.artifacts.CallContext;
ingo@141: import de.intevation.artifacts.CallMeta;
ingo@127:
ingo@127: import de.intevation.artifacts.common.utils.XMLUtils;
ingo@127:
ingo@127: import de.intevation.artifactdatabase.ProtocolUtils;
ingo@323: import de.intevation.artifactdatabase.data.StateData;
ingo@127:
ingo@141: import de.intevation.flys.artifacts.resources.Resources;
ingo@141:
ingo@127: /**
ingo@127: * @author Ingo Weinzierl
ingo@127: */
ingo@127: public class CalculationSelect extends DefaultState {
ingo@127:
ingo@127: /** The logger that is used in this class.*/
ingo@127: private static Logger logger = Logger.getLogger(CalculationSelect.class);
ingo@127:
ingo@127:
ingo@141: /** Constant value for the reference line calculation.*/
ingo@157: public static final String CALCULATION_SURFACE_CURVE =
ingo@157: "calc.surface.curve";
ingo@141:
ingo@141: /** Constant value for the differences calculation.*/
ingo@157: public static final String CALCULATION_DURATION_CURVE =
ingo@157: "calc.duration.curve";
ingo@141:
ingo@141: /** Constant value for the flood map calculation.*/
ingo@141: public static final String CALCULATION_FLOOD_MAP =
ingo@141: "calc.flood.map";
ingo@141:
ingo@141: /** Constant value for the profile calculation.*/
ingo@157: public static final String CALCULATION_DISCHARGE_LONGITUDINAL_CURVE =
ingo@157: "calc.discharge.longitudinal.section";
ingo@141:
ingo@141: /** Constant value for the state discharge curve calculation.*/
ingo@157: public static final String CALCULATION_DISCHARGE_CURVE =
ingo@157: "calc.discharge.curve";
ingo@141:
ingo@141: /** An array that holds all available calculation modes.*/
ingo@141: public static final String[] CALCULATIONS = {
ingo@157: CALCULATION_SURFACE_CURVE,
ingo@141: CALCULATION_FLOOD_MAP,
ingo@157: CALCULATION_DISCHARGE_CURVE,
ingo@157: CALCULATION_DURATION_CURVE,
ingo@157: CALCULATION_DISCHARGE_LONGITUDINAL_CURVE };
ingo@141:
ingo@141:
ingo@323: /** Error message that is thrown if no mode has been chosen.*/
ingo@323: public static final String ERROR_NO_CALCULATION_MODE =
ingo@323: "error_feed_no_calculation_mode";
ingo@323:
ingo@323: /** Error message that is thrown if an invalid calculation mode has been
ingo@323: * chosen.*/
ingo@323: public static final String ERROR_INVALID_CALCULATION_MODE =
ingo@323: "error_feed_invalid_calculation_mode";
ingo@323:
ingo@323:
ingo@127: protected Element[] createItems(
ingo@127: XMLUtils.ElementCreator cr,
ingo@313: Artifact artifact,
ingo@127: String name,
ingo@127: CallContext context)
ingo@127: {
ingo@141: CallMeta meta = context.getMeta();
ingo@141: Element[] calcs = new Element[CALCULATIONS.length];
ingo@127:
ingo@141: int i = 0;
ingo@141:
ingo@141: for (String calc: CALCULATIONS) {
ingo@141: calcs[i++] = createItem(
ingo@141: cr, new String[] {
ingo@141: Resources.getMsg(meta, calc, calc),
ingo@141: calc
ingo@141: });
ingo@141: }
ingo@141:
ingo@141: return calcs;
ingo@127: }
ingo@127:
ingo@127:
ingo@127: protected Element createItem(XMLUtils.ElementCreator cr, Object obj) {
ingo@127: Element item = ProtocolUtils.createArtNode(cr, "item", null, null);
ingo@127: Element label = ProtocolUtils.createArtNode(cr, "label", null, null);
ingo@127: Element value = ProtocolUtils.createArtNode(cr, "value", null, null);
ingo@127:
ingo@127: String[] arr = (String[]) obj;
ingo@127:
ingo@127: label.setTextContent(arr[0]);
ingo@127: value.setTextContent(arr[1]);
ingo@127:
ingo@127: item.appendChild(label);
ingo@127: item.appendChild(value);
ingo@127:
ingo@127: return item;
ingo@127: }
ingo@323:
ingo@323:
ingo@323: @Override
ingo@323: public boolean validate(Artifact artifact, CallContext context)
ingo@323: throws IllegalArgumentException
ingo@323: {
ingo@323: logger.debug("CalculationSelect.validate");
ingo@323:
ingo@323: Map data = getData();
ingo@323:
ingo@323: String calc = (String) data.get("calculation_mode").getValue();
ingo@323:
ingo@323: if (calc == null) {
ingo@323: throw new IllegalArgumentException(ERROR_NO_CALCULATION_MODE);
ingo@323: }
ingo@323:
ingo@323: calc = calc.trim().toLowerCase();
ingo@323:
ingo@323: for (String mode: CALCULATIONS) {
ingo@323: if (mode.equals(calc)) {
ingo@323: return true;
ingo@323: }
ingo@323: }
ingo@323:
ingo@323: throw new IllegalArgumentException(ERROR_INVALID_CALCULATION_MODE);
ingo@323: }
ingo@127: }
ingo@127: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :