ingo@137: package de.intevation.flys.artifacts.states; ingo@137: ingo@322: import java.util.Map; ingo@322: ingo@137: import org.apache.log4j.Logger; ingo@137: ingo@137: import org.w3c.dom.Element; ingo@137: ingo@313: import de.intevation.artifacts.Artifact; ingo@137: import de.intevation.artifacts.CallContext; ingo@137: ingo@137: import de.intevation.artifacts.common.utils.XMLUtils; ingo@137: ingo@137: import de.intevation.artifactdatabase.ProtocolUtils; ingo@137: import de.intevation.artifactdatabase.data.StateData; ingo@137: ingo@319: import de.intevation.flys.model.Gauge; ingo@319: import de.intevation.flys.model.River; ingo@319: import de.intevation.flys.model.Wst; ingo@319: ingo@319: import de.intevation.flys.artifacts.FLYSArtifact; ingo@319: import de.intevation.flys.artifacts.model.WstFactory; ingo@137: import de.intevation.flys.artifacts.resources.Resources; ingo@137: ingo@137: /** ingo@137: * @author Ingo Weinzierl ingo@137: */ ingo@322: public class WQSelect extends RangeState { ingo@137: ingo@137: /** The logger used in this class.*/ ingo@137: private static Logger logger = Logger.getLogger(WQSelect.class); ingo@137: ingo@137: ingo@137: /** The default step width for Qs.*/ ingo@137: public static final String DEFAULT_STEP_Q = "50"; ingo@137: ingo@137: /** The default step width for Qs.*/ ingo@137: public static final String DEFAULT_STEP_W = "30"; ingo@137: ingo@322: /** The name of the 'mode' field. */ ingo@322: public static final String WQ_MODE = "wq_mode"; ingo@322: ingo@322: /** The name of the 'from' field. */ ingo@322: public static final String WQ_FROM = "wq_from"; ingo@322: ingo@322: /** The name of the 'to' field. */ ingo@322: public static final String WQ_TO = "wq_to"; ingo@322: ingo@322: /** The name of the 'step' field. */ ingo@322: public static final String WQ_STEP = "wq_step"; ingo@322: ingo@137: /** ingo@137: * The default constructor that initializes an empty State object. ingo@137: */ ingo@137: public WQSelect() { ingo@137: } ingo@137: ingo@137: protected Element createData( ingo@137: XMLUtils.ElementCreator cr, ingo@313: Artifact artifact, ingo@137: StateData data, ingo@137: CallContext context) ingo@137: { ingo@137: Element select = ProtocolUtils.createArtNode( ingo@137: cr, "select", null, null); ingo@137: ingo@137: cr.addAttr(select, "name", data.getName(), true); ingo@137: ingo@137: Element label = ProtocolUtils.createArtNode( ingo@137: cr, "label", null, null); ingo@137: ingo@137: Element choices = ProtocolUtils.createArtNode( ingo@137: cr, "choices", null, null); ingo@137: ingo@137: label.setTextContent(Resources.getMsg( ingo@137: context.getMeta(), ingo@137: data.getName(), ingo@137: data.getName())); ingo@137: ingo@137: select.appendChild(label); ingo@137: ingo@137: return select; ingo@137: } ingo@137: ingo@137: ingo@137: protected Element[] createItems( ingo@137: XMLUtils.ElementCreator cr, ingo@313: Artifact artifact, ingo@137: String name, ingo@137: CallContext context) ingo@137: { ingo@137: // TODO Insert correct min/max values! ingo@319: double[] minmaxW = determineMinMaxW(artifact); ingo@319: double[] minmaxQ = determineMinMaxQ(artifact); ingo@137: ingo@137: if (name.equals("wq_from")) { ingo@319: Element minW = createItem( ingo@319: cr, new String[] {"minW", new Double(minmaxW[0]).toString()}); ingo@319: Element minQ = createItem( ingo@319: cr, new String[] {"minQ", new Double(minmaxQ[0]).toString()}); ingo@137: return new Element[] { minW, minQ }; ingo@137: } ingo@137: else if (name.equals("wq_to")) { ingo@319: Element maxW = createItem( ingo@319: cr, new String[] {"maxW", new Double(minmaxW[1]).toString()}); ingo@319: Element maxQ = createItem( ingo@319: cr, new String[] {"maxQ", new Double(minmaxQ[1]).toString()}); ingo@137: return new Element[] { maxW, maxQ }; ingo@137: } ingo@137: else { ingo@137: Element stepW = createItem(cr, new String[] {"stepW", DEFAULT_STEP_W}); ingo@137: Element stepQ = createItem(cr, new String[] {"stepQ", DEFAULT_STEP_Q}); ingo@137: return new Element[] { stepW, stepQ }; ingo@137: } ingo@137: } ingo@137: ingo@137: ingo@137: protected Element createItem(XMLUtils.ElementCreator cr, Object obj) { ingo@137: Element item = ProtocolUtils.createArtNode(cr, "item", null, null); ingo@137: Element label = ProtocolUtils.createArtNode(cr, "label", null, null); ingo@137: Element value = ProtocolUtils.createArtNode(cr, "value", null, null); ingo@137: ingo@137: String[] arr = (String[]) obj; ingo@137: ingo@137: label.setTextContent(arr[0]); ingo@137: value.setTextContent(arr[1]); ingo@137: ingo@137: item.appendChild(label); ingo@137: item.appendChild(value); ingo@137: ingo@137: return item; ingo@137: } ingo@137: ingo@137: ingo@137: protected String getUIProvider() { ingo@137: return "wq_panel"; ingo@137: } ingo@319: ingo@319: ingo@319: /** ingo@319: * Determines the min and max W value for the current gauge. If no min and ingo@319: * max values could be determined, this method will return ingo@319: * [Double.MIN_VALUE, Double.MAX_VALUE]. ingo@319: * ingo@319: * @param artifact The FLYSArtifact. ingo@319: * ingo@319: * @return the min and max W values for the current gauge. ingo@319: */ ingo@319: protected double[] determineMinMaxW(Artifact artifact) { ingo@319: logger.debug("WQSelect.determineCurrentGauge"); ingo@319: ingo@319: Gauge gauge = ((FLYSArtifact) artifact).getGauge(); ingo@320: double[] minmaxW = gauge != null ? gauge.determineMinMaxW() : null; ingo@319: ingo@319: double minW = minmaxW != null ? minmaxW[0] : Double.MIN_VALUE; ingo@319: double maxW = minmaxW != null ? minmaxW[1] : Double.MAX_VALUE; ingo@319: ingo@319: return new double[] { minW, maxW }; ingo@319: } ingo@319: ingo@319: ingo@319: /** ingo@319: * Determines the min and max Q value for the current gauge. If no min and ingo@319: * max values could be determined, this method will return ingo@319: * [Double.MIN_VALUE, Double.MAX_VALUE]. ingo@319: * ingo@319: * @param artifact The FLYSArtifact. ingo@319: * ingo@319: * @return the min and max Q values for the current gauge. ingo@319: */ ingo@319: protected double[] determineMinMaxQ(Artifact artifact) { ingo@319: logger.debug("WQSelect.determineMinMaxQ"); ingo@319: ingo@319: FLYSArtifact flysArtifact = (FLYSArtifact) artifact; ingo@319: ingo@319: River river = flysArtifact.getRiver(); ingo@319: Gauge gauge = flysArtifact.getGauge(); ingo@319: Wst wst = WstFactory.getWst(river); ingo@319: ingo@320: double[] minmaxQ = gauge != null ingo@320: ? wst.determineMinMaxQ(gauge.getRange()) ingo@320: : null; ingo@319: ingo@319: double minQ = minmaxQ != null ? minmaxQ[0] : Double.MIN_VALUE; ingo@319: double maxQ = minmaxQ != null ? minmaxQ[1] : Double.MAX_VALUE; ingo@319: ingo@319: return new double[] { minQ, maxQ }; ingo@319: } ingo@322: ingo@322: ingo@322: @Override ingo@322: public boolean validate(Artifact artifact, CallContext context) ingo@322: throws IllegalArgumentException ingo@322: { ingo@322: logger.debug("WQSelect.validate"); ingo@322: ingo@322: Map data = getData(); ingo@322: ingo@322: String mode = (String) data.get(WQ_MODE).getValue(); ingo@322: logger.debug("WQ Mode: " + mode); ingo@322: ingo@322: String fromStr = (String) data.get(WQ_FROM).getValue(); ingo@322: String toStr = (String) data.get(WQ_TO).getValue(); ingo@322: String stepStr = (String) data.get(WQ_STEP).getValue(); ingo@322: ingo@352: if (fromStr == null || toStr == null || stepStr == null) { ingo@352: throw new IllegalArgumentException("error_empty_state"); ingo@352: } ingo@352: ingo@322: double from = Double.parseDouble(fromStr); ingo@322: double to = Double.parseDouble(toStr); ingo@322: double step = Double.parseDouble(stepStr); ingo@322: ingo@322: try { ingo@322: if (mode != null && mode.trim().toLowerCase().equals("w")) { ingo@322: return validateW(artifact, context, from, to, step); ingo@322: } ingo@322: else if (mode != null && mode.trim().toLowerCase().equals("q")) { ingo@322: return validateQ(artifact, context, from, to, step); ingo@322: } ingo@322: else { ingo@322: throw new IllegalArgumentException("error_feed_invalid_wq_mode"); ingo@322: } ingo@322: } ingo@322: catch (NumberFormatException nfe) { ingo@322: throw new IllegalArgumentException("error_feed_number_format"); ingo@322: } ingo@322: } ingo@322: ingo@322: ingo@322: /** ingo@322: * Validates the inserted W values. ingo@322: * ingo@322: * @param artifact The owner artifact. ingo@322: * @param context The CallContext ingo@322: * @param from The lower value of the W range. ingo@322: * @param to The upper value of the W range. ingo@322: * @param step The step width. ingo@322: * ingo@322: * @return true, if everything was fine, otherwise an exception is thrown. ingo@322: */ ingo@322: protected boolean validateW( ingo@322: Artifact artifact, ingo@322: CallContext context, ingo@322: double from, ingo@322: double to, ingo@322: double step) ingo@322: throws IllegalArgumentException ingo@322: { ingo@322: logger.debug("WQSelect.validateW"); ingo@322: ingo@322: double[] minmaxW = determineMinMaxW(artifact); ingo@322: ingo@322: return validateRange(minmaxW[0], minmaxW[1], from, to, step); ingo@322: } ingo@322: ingo@322: ingo@322: /** ingo@322: * Validates the inserted Q values. ingo@322: * ingo@322: * @param artifact The owner artifact. ingo@322: * @param context The CallContext ingo@322: * @param from The lower value of the Q range. ingo@322: * @param to The upper value of the Q range. ingo@322: * @param step The step width. ingo@322: * ingo@322: * @return true, if everything was fine, otherwise an exception is thrown. ingo@322: */ ingo@322: protected boolean validateQ( ingo@322: Artifact artifact, ingo@322: CallContext context, ingo@322: double from, ingo@322: double to, ingo@322: double step) ingo@322: throws IllegalArgumentException ingo@322: { ingo@322: logger.debug("WQSelect.validateQ"); ingo@322: ingo@322: double[] minmaxQ = determineMinMaxQ(artifact); ingo@322: ingo@322: return validateRange(minmaxQ[0], minmaxQ[1], from, to, step); ingo@322: } ingo@137: } ingo@137: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :