ingo@399: package de.intevation.flys.artifacts.states; ingo@399: ingo@417: import java.util.ArrayList; ingo@399: import java.util.List; ingo@399: ingo@399: import org.apache.log4j.Logger; ingo@399: ingo@399: import org.w3c.dom.Element; ingo@399: ingo@399: import de.intevation.artifacts.Artifact; ingo@399: import de.intevation.artifacts.CallContext; ingo@399: ingo@399: import de.intevation.artifactdatabase.ProtocolUtils; ingo@417: import de.intevation.artifactdatabase.data.StateData; ingo@399: ingo@399: import de.intevation.artifacts.common.utils.XMLUtils; ingo@399: ingo@399: import de.intevation.flys.model.Gauge; ingo@399: import de.intevation.flys.model.Range; ingo@417: import de.intevation.flys.model.River; ingo@417: import de.intevation.flys.model.Wst; ingo@399: ingo@399: import de.intevation.flys.artifacts.FLYSArtifact; ingo@417: import de.intevation.flys.artifacts.model.RangeWithValues; ingo@417: import de.intevation.flys.artifacts.model.WstFactory; ingo@399: ingo@399: ingo@399: /** ingo@399: * @author Ingo Weinzierl ingo@399: */ ingo@399: public class WQAdapted extends DefaultState { ingo@399: ingo@399: /** The logger used in this state.*/ ingo@399: private static Logger logger = Logger.getLogger(WQAdapted.class); ingo@399: ingo@399: ingo@401: public static final String FIELD_WQ_MODE = "wq_mode"; ingo@401: ingo@401: public static final String FIELD_WQ_VALUES = "wq_values"; ingo@401: ingo@401: ingo@399: /** ingo@399: * This method creates one element for each gauge of the selected river that ingo@399: * is intersected by the given kilometer range. Each element is a tuple of ingo@399: * (from;to) where from is the lower bounds of the gauge or the lower ingo@399: * kilometer range. to is the upper bounds of the gauge or the upper ingo@399: * kilometer range. ingo@399: * ingo@399: * @param cr The ElementCreator. ingo@399: * @param artifact The FLYS artifact. ingo@399: * @param name The name of the data item. ingo@399: * @param context The CallContext. ingo@399: * ingo@399: * @return a list of elements that consist of tuples of the intersected ingo@399: * gauges of the selected river. ingo@399: */ ingo@399: protected Element[] createItems( ingo@399: XMLUtils.ElementCreator cr, ingo@399: Artifact artifact, ingo@399: String name, ingo@399: CallContext context) ingo@399: { ingo@399: logger.debug("WQAdapted.createItems"); ingo@399: ingo@401: if (name != null && name.equals(FIELD_WQ_MODE)) { ingo@401: return createModeItems(cr, artifact, name, context); ingo@401: } ingo@401: else if (name != null && name.equals(FIELD_WQ_VALUES)) { ingo@401: return createValueItems(cr, artifact, name, context); ingo@401: } ingo@401: else { ingo@401: logger.warn("Unknown data object: " + name); ingo@401: return null; ingo@401: } ingo@401: } ingo@401: ingo@401: ingo@401: protected Element[] createModeItems( ingo@401: XMLUtils.ElementCreator cr, ingo@401: Artifact artifact, ingo@401: String name, ingo@401: CallContext context) ingo@401: { ingo@401: logger.debug("WQAdapted.createModeItems"); ingo@401: ingo@401: Element w = createItem(cr, new String[] { "w", "W" }); ingo@401: Element q = createItem(cr, new String[] { "q", "Q" }); ingo@401: ingo@401: return new Element[] { w, q }; ingo@401: } ingo@401: ingo@401: ingo@401: protected Element[] createValueItems( ingo@401: XMLUtils.ElementCreator cr, ingo@401: Artifact artifact, ingo@401: String name, ingo@401: CallContext context) ingo@401: { ingo@401: logger.debug("WQAdapted.createValueItems"); ingo@401: ingo@399: FLYSArtifact flysArtifact = (FLYSArtifact) artifact; ingo@399: ingo@399: double[] dist = flysArtifact.getDistance(); ingo@399: List gauges = flysArtifact.getGauges(); ingo@399: ingo@399: int num = gauges != null ? gauges.size() : 0; ingo@399: ingo@399: if (num == 0) { ingo@399: logger.warn("Selected distance matches no gauges."); ingo@399: return null; ingo@399: } ingo@399: ingo@399: Element[] elements = new Element[num]; ingo@399: ingo@399: int idx = 0; ingo@399: ingo@399: for (Gauge gauge: gauges) { ingo@399: Range range = gauge.getRange(); ingo@399: double lower = range.getA().doubleValue(); ingo@399: double upper = range.getB().doubleValue(); ingo@399: ingo@399: double from = dist[0] < lower ? lower : dist[0]; ingo@399: double to = dist[1] > upper ? upper : dist[1]; ingo@399: ingo@399: String key = Double.toString(from) + ";" + Double.toString(to); ingo@399: ingo@399: elements[idx++] = createItem(cr, new String[] {key, ""}); ingo@399: } ingo@399: ingo@399: return elements; ingo@399: } ingo@399: ingo@399: ingo@399: protected Element createItem(XMLUtils.ElementCreator cr, Object obj) { ingo@399: Element item = ProtocolUtils.createArtNode(cr, "item", null, null); ingo@399: Element label = ProtocolUtils.createArtNode(cr, "label", null, null); ingo@399: Element value = ProtocolUtils.createArtNode(cr, "value", null, null); ingo@399: ingo@399: String[] arr = (String[]) obj; ingo@399: ingo@399: label.setTextContent(arr[0]); ingo@399: value.setTextContent(arr[1]); ingo@399: ingo@399: item.appendChild(label); ingo@399: item.appendChild(value); ingo@399: ingo@399: return item; ingo@399: } ingo@399: ingo@399: ingo@399: @Override ingo@399: protected String getUIProvider() { ingo@399: return "wq_panel_adapted"; ingo@399: } ingo@417: ingo@417: ingo@417: public boolean validate(Artifact artifact, CallContext context) ingo@417: throws IllegalArgumentException ingo@417: { ingo@417: logger.debug("WQAdapted.validate"); ingo@417: ingo@624: FLYSArtifact flys = (FLYSArtifact) artifact; ingo@624: StateData data = getData(flys, FIELD_WQ_MODE); ingo@417: ingo@624: String mode = data != null ? (String) data.getValue() : null; ingo@417: ingo@417: if (mode != null && mode.equals("W")) { ingo@417: return validateW(artifact, context); ingo@417: } ingo@417: else if (mode != null && mode.equals("Q")) { ingo@417: return validateQ(artifact, context); ingo@417: } ingo@417: else { ingo@417: throw new IllegalArgumentException("error_feed_no_wq_mode_selected"); ingo@417: } ingo@417: } ingo@417: ingo@417: ingo@417: protected boolean validateW(Artifact artifact, CallContext context) ingo@417: throws IllegalArgumentException ingo@417: { ingo@417: logger.debug("WQAdapted.validateW"); ingo@624: FLYSArtifact flys = (FLYSArtifact) artifact; ingo@417: ingo@624: RangeWithValues[] rwvs = extractInput(getData(flys, "wq_values")); ingo@624: ingo@624: if (rwvs == null) { ingo@624: throw new IllegalArgumentException("error_missing_wq_data"); ingo@624: } ingo@624: ingo@417: List gauges = ((FLYSArtifact) artifact).getGauges(); ingo@417: ingo@417: for (Gauge gauge: gauges) { ingo@417: Range range = gauge.getRange(); ingo@417: double lower = range.getA().doubleValue(); ingo@417: double upper = range.getB().doubleValue(); ingo@417: ingo@417: for (RangeWithValues rwv: rwvs) { ingo@417: if (lower <= rwv.getLower() && upper >= rwv.getUpper()) { ingo@417: compareWsWithGauge(gauge, rwv.getValues()); ingo@417: } ingo@417: } ingo@417: } ingo@417: ingo@417: return true; ingo@417: } ingo@417: ingo@417: ingo@417: protected boolean validateQ(Artifact artifact, CallContext context) ingo@417: throws IllegalArgumentException ingo@417: { ingo@417: logger.debug("WQAdapted.validateQ"); ingo@624: FLYSArtifact flys = (FLYSArtifact) artifact; ingo@417: ingo@624: RangeWithValues[] rwvs = extractInput(getData(flys, "wq_values")); ingo@624: ingo@624: if (rwvs == null) { ingo@624: throw new IllegalArgumentException("error_missing_wq_data"); ingo@624: } ingo@624: ingo@417: List gauges = ((FLYSArtifact) artifact).getGauges(); ingo@417: River river = ((FLYSArtifact) artifact).getRiver(); ingo@417: Wst wst = WstFactory.getWst(river); ingo@417: ingo@417: for (Gauge gauge: gauges) { ingo@417: Range range = gauge.getRange(); ingo@417: double lower = range.getA().doubleValue(); ingo@417: double upper = range.getB().doubleValue(); ingo@417: ingo@417: for (RangeWithValues rwv: rwvs) { ingo@417: if (lower <= rwv.getLower() && upper >= rwv.getUpper()) { ingo@417: compareQsWithGauge(wst, gauge, rwv.getValues()); ingo@417: } ingo@417: } ingo@417: } ingo@417: ingo@417: return true; ingo@417: } ingo@417: ingo@417: ingo@417: protected boolean compareQsWithGauge(Wst wst, Gauge gauge, double[] qs) ingo@417: throws IllegalArgumentException ingo@417: { ingo@417: double[] minmax = gauge != null ingo@417: ? wst.determineMinMaxQ(gauge.getRange()) ingo@417: : null; ingo@417: ingo@417: if (minmax == null) { ingo@417: logger.warn("Could not determine min/max Q of gauge."); ingo@417: return true; ingo@417: } ingo@417: ingo@417: if (logger.isDebugEnabled()) { ingo@417: logger.debug("Validate Qs with:"); ingo@417: logger.debug("-- Gauge: " + gauge.getName()); ingo@417: logger.debug("-- Gauge min: " + minmax[0]); ingo@417: logger.debug("-- Gauge max: " + minmax[1]); ingo@417: } ingo@417: ingo@417: for (double q: qs) { ingo@417: if (q < minmax[0] || q > minmax[1]) { ingo@417: throw new IllegalArgumentException( ingo@417: "error_feed_q_values_invalid"); ingo@417: } ingo@417: } ingo@417: ingo@417: return true; ingo@417: } ingo@417: ingo@417: ingo@417: protected boolean compareWsWithGauge(Gauge gauge, double[] ws) ingo@417: throws IllegalArgumentException ingo@417: { ingo@417: double[] minmax = gauge != null ingo@417: ? gauge.determineMinMaxW() ingo@417: : null; ingo@417: ingo@417: if (minmax == null) { ingo@417: logger.warn("Could not determine min/max W of gauge."); ingo@417: return true; ingo@417: } ingo@417: ingo@417: if (logger.isDebugEnabled()) { ingo@417: logger.debug("Validate Ws with:"); ingo@417: logger.debug("-- Gauge: " + gauge.getName()); ingo@417: logger.debug("-- Gauge min: " + minmax[0]); ingo@417: logger.debug("-- Gauge max: " + minmax[1]); ingo@417: } ingo@417: ingo@417: for (double w: ws) { ingo@417: if (w < minmax[0] || w > minmax[1]) { ingo@417: throw new IllegalArgumentException( ingo@417: "error_feed_w_values_invalid"); ingo@417: } ingo@417: } ingo@417: ingo@417: return true; ingo@417: } ingo@417: ingo@417: ingo@417: protected RangeWithValues[] extractInput(StateData data) { ingo@624: if (data == null) { ingo@624: return null; ingo@624: } ingo@624: ingo@417: String dataString = (String) data.getValue(); ingo@417: String[] ranges = dataString.split(":"); ingo@417: ingo@417: List rwv = new ArrayList(); ingo@417: ingo@417: for (String range: ranges) { ingo@417: String[] parts = range.split(";"); ingo@417: ingo@417: double lower = Double.parseDouble(parts[0]); ingo@417: double upper = Double.parseDouble(parts[1]); ingo@417: ingo@417: String[] values = parts[2].split(","); ingo@417: ingo@417: int num = values.length; ingo@417: double[] res = new double[num]; ingo@417: ingo@417: for (int i = 0; i < num; i++) { ingo@417: try { ingo@417: res[i] = Double.parseDouble(values[i]); ingo@417: } ingo@417: catch (NumberFormatException nfe) { ingo@417: logger.warn(nfe, nfe); ingo@417: } ingo@417: } ingo@417: ingo@417: rwv.add(new RangeWithValues(lower, upper, res)); ingo@417: } ingo@417: ingo@417: return (RangeWithValues[]) rwv.toArray(new RangeWithValues[rwv.size()]); ingo@417: } ingo@399: } ingo@399: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :