ingo@2693: package de.intevation.flys.artifacts.states; ingo@2693: ingo@2693: import org.apache.log4j.Logger; ingo@2693: ingo@2693: import org.w3c.dom.Element; ingo@2693: ingo@2693: import de.intevation.artifacts.Artifact; ingo@2693: import de.intevation.artifacts.CallContext; ingo@2693: import de.intevation.artifacts.CallMeta; ingo@2693: ingo@2693: import de.intevation.artifacts.common.utils.XMLUtils; ingo@2693: ingo@2693: import de.intevation.artifactdatabase.data.StateData; ingo@2693: ingo@2693: import de.intevation.flys.artifacts.FLYSArtifact; ingo@2693: import de.intevation.flys.artifacts.resources.Resources; ingo@2693: ingo@2693: /** ingo@2693: * @author Ingo Weinzierl ingo@2693: */ ingo@2693: public class CalculationSelectMinfo extends DefaultState { ingo@2693: ingo@2693: /** The logger that is used in this class. */ ingo@2693: private static Logger logger = Logger.getLogger(CalculationSelectMinfo.class); ingo@2693: ingo@2693: ingo@2693: public static final String FIELD_MODE = "calculation_mode"; ingo@2693: ingo@2693: public static final String CALC_BED_MIDDLE = "calc.bed.middle"; ingo@2693: public static final String CALC_BED_DIFF = "calc.bed.diff"; ingo@2693: public static final String CALC_BED_QUALITY = "calc.bed.quality"; ingo@2693: public static final String CALC_SEDIMENT_LOAD = "calc.sediment.load"; ingo@2693: public static final String CALC_FLOW_VELOCITY = "calc.flow.velocity"; ingo@2693: public static final String CALC_SQ_RELATION = "calc.sq.relation"; ingo@2693: ingo@2693: /** An array that holds all available calculation modes. */ ingo@2693: public static final String[] CALCULATIONS = { ingo@2693: CALC_BED_MIDDLE, ingo@2693: CALC_BED_DIFF, ingo@2693: CALC_BED_QUALITY, ingo@2693: CALC_SEDIMENT_LOAD, ingo@2693: CALC_FLOW_VELOCITY, ingo@2693: CALC_SQ_RELATION ingo@2693: }; ingo@2693: ingo@2693: ingo@2693: /** Error message that is thrown if no mode has been chosen. */ ingo@2693: public static final String ERROR_NO_CALCULATION_MODE = ingo@2693: "error_feed_no_calculation_mode"; ingo@2693: ingo@2693: /** Error message that is thrown if an invalid calculation mode has been ingo@2693: * chosen. */ ingo@2693: public static final String ERROR_INVALID_CALCULATION_MODE = ingo@2693: "error_feed_invalid_calculation_mode"; ingo@2693: ingo@2693: ingo@2693: public CalculationSelectMinfo() { ingo@2693: } ingo@2693: ingo@2693: ingo@2693: @Override ingo@2693: protected Element[] createItems( ingo@2693: XMLUtils.ElementCreator cr, ingo@2693: Artifact artifact, ingo@2693: String name, ingo@2693: CallContext context) ingo@2693: { ingo@2693: CallMeta meta = context.getMeta(); ingo@2693: Element[] calcs = new Element[CALCULATIONS.length]; ingo@2693: ingo@2693: for (int i = 0; i < CALCULATIONS.length; ++i) { ingo@2693: String calc = CALCULATIONS[i]; ingo@2693: calcs[i] = createItem( ingo@2693: cr, new String[] { ingo@2693: Resources.getMsg(meta, calc, calc), ingo@2693: calc ingo@2693: }); ingo@2693: } ingo@2693: ingo@2693: return calcs; ingo@2693: } ingo@2693: ingo@2693: ingo@2693: @Override ingo@2693: public boolean validate(Artifact artifact) ingo@2693: throws IllegalArgumentException ingo@2693: { ingo@2693: logger.debug("CalculationSelect.validate"); ingo@2693: FLYSArtifact flys = (FLYSArtifact) artifact; ingo@2693: ingo@2693: StateData data = getData(flys, FIELD_MODE); ingo@2693: String calc = (data != null) ? (String) data.getValue() : null; ingo@2693: ingo@2693: if (calc == null) { ingo@2693: throw new IllegalArgumentException(ERROR_NO_CALCULATION_MODE); ingo@2693: } ingo@2693: ingo@2693: calc = calc.trim().toLowerCase(); ingo@2693: ingo@2693: for (String mode: CALCULATIONS) { ingo@2693: if (mode.equals(calc)) { ingo@2693: return true; ingo@2693: } ingo@2693: } ingo@2693: ingo@2693: throw new IllegalArgumentException(ERROR_INVALID_CALCULATION_MODE); ingo@2693: } ingo@2693: } ingo@2693: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :