ingo@399: package de.intevation.flys.artifacts.states;
ingo@399:
ingo@417: import java.util.ArrayList;
ingo@399: import java.util.List;
sascha@636: import java.util.Comparator;
sascha@636: import java.util.Collections;
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:
felix@3445: import de.intevation.flys.artifacts.FLYSArtifact;
sascha@1055:
ingo@417: import de.intevation.flys.artifacts.model.RangeWithValues;
ingo@417: import de.intevation.flys.artifacts.model.WstFactory;
ingo@1095: import de.intevation.flys.utils.FLYSUtils;
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:
raimund@2556: public static final String FIELD_WQ_MODE = "wq_isq";
ingo@401:
ingo@401: public static final String FIELD_WQ_VALUES = "wq_values";
ingo@401:
sascha@636: public static final class GaugeOrder implements Comparator {
sascha@636: private int order;
sascha@636:
sascha@636: public GaugeOrder(boolean up) {
sascha@642: order = up ? 1 : -1;
sascha@636: }
sascha@636:
sascha@636: public int compare(Gauge a, Gauge b) {
sascha@636: return order * a.getRange().getA().compareTo(b.getRange().getA());
sascha@636: }
sascha@636: } // class GaugeOrder
sascha@636:
sascha@636: public static final GaugeOrder GAUGE_UP = new GaugeOrder(true);
sascha@636: public static final GaugeOrder GAUGE_DOWN = new GaugeOrder(false);
ingo@401:
sascha@660: public WQAdapted() {
sascha@660: }
sascha@660:
felix@3445:
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: */
sascha@660: @Override
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:
felix@3445: FLYSArtifact flysArtifact = (FLYSArtifact) artifact;
ingo@399:
ingo@1095: double[] dist = FLYSUtils.getKmRange(flysArtifact);
felix@1103: River river = FLYSUtils.getRiver(flysArtifact);
ingo@681: Wst wst = WstFactory.getWst(river);
felix@3445: List gauges = FLYSUtils.getGauges(flysArtifact);
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:
sascha@636: double rangeFrom = dist[0];
sascha@636: double rangeTo = dist[1];
sascha@636:
ingo@399: int idx = 0;
ingo@399:
sascha@636: if (rangeFrom < rangeTo) {
sascha@636: Collections.sort(gauges, GAUGE_UP);
sascha@636: for (Gauge gauge: gauges) {
sascha@636: Range range = gauge.getRange();
sascha@636: double lower = range.getA().doubleValue();
sascha@636: double upper = range.getB().doubleValue();
ingo@399:
sascha@636: double from = lower < rangeFrom ? rangeFrom : lower;
sascha@636: double to = upper > rangeTo ? rangeTo : upper;
ingo@399:
ingo@681: double[] mmQ = determineMinMaxQ(gauge, wst);
ingo@681: double[] mmW = gauge.determineMinMaxW();
ingo@681:
sascha@636: elements[idx++] = createItem(
ingo@681: cr, new String[] { from + ";" + to, ""}, mmQ, mmW);
sascha@636: }
sascha@636: }
sascha@636: else {
sascha@636: Collections.sort(gauges, GAUGE_DOWN);
sascha@642: rangeFrom = dist[1];
sascha@642: rangeTo = dist[0];
sascha@636: for (Gauge gauge: gauges) {
sascha@636: Range range = gauge.getRange();
sascha@636: double lower = range.getA().doubleValue();
sascha@636: double upper = range.getB().doubleValue();
ingo@399:
sascha@642: double from = lower < rangeFrom ? rangeFrom : lower;
sascha@642: double to = upper > rangeTo ? rangeTo : upper;
sascha@636:
ingo@724: double[] mmQ = determineMinMaxQ(gauge, wst);
ingo@724: double[] mmW = gauge.determineMinMaxW();
ingo@724:
sascha@636: elements[idx++] = createItem(
ingo@724: cr, new String[] { to + ";" + from, ""}, mmQ, mmW);
sascha@636: }
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@681: return createItem(cr, obj, null, null);
ingo@681: }
ingo@681:
ingo@681:
ingo@681: protected Element createItem(
ingo@681: XMLUtils.ElementCreator cr,
ingo@681: Object obj,
ingo@681: double[] q,
ingo@681: double[] w)
ingo@681: {
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@681: if (q != null) {
ingo@681: Element qRange = createRangeElement(cr, q, "Q");
ingo@681: item.appendChild(qRange);
ingo@681: }
ingo@681:
ingo@681: if (w != null) {
ingo@681: Element wRange = createRangeElement(cr, w, "W");
ingo@681: item.appendChild(wRange);
ingo@681: }
ingo@681:
ingo@399: return item;
ingo@399: }
ingo@399:
ingo@399:
ingo@681: protected Element createRangeElement(
ingo@681: XMLUtils.ElementCreator cr,
ingo@681: double[] mm,
ingo@681: String type)
ingo@681: {
ingo@681: Element range = ProtocolUtils.createArtNode(
ingo@681: cr, "range",
ingo@681: new String[] {"type"},
ingo@681: new String[] {type});
ingo@681:
ingo@681: Element min = ProtocolUtils.createArtNode(cr, "min", null, null);
ingo@681: min.setTextContent(String.valueOf(mm[0]));
ingo@681:
ingo@681: Element max = ProtocolUtils.createArtNode(cr, "max", null, null);
ingo@681: max.setTextContent(String.valueOf(mm[1]));
ingo@681:
ingo@681: range.appendChild(min);
ingo@681: range.appendChild(max);
ingo@681:
ingo@681: return range;
ingo@681: }
ingo@681:
ingo@681:
ingo@681: /**
ingo@681: * Determines the min and max Q value for the given gauge. If no min and
ingo@681: * max values could be determined, this method will return
ingo@681: * [Double.MIN_VALUE, Double.MAX_VALUE].
ingo@681: *
ingo@681: * @param gauge
ingo@681: * @param wst
ingo@681: *
ingo@681: * @return the min and max Q values for the given gauge.
ingo@681: */
ingo@681: protected double[] determineMinMaxQ(Gauge gauge, Wst wst) {
ingo@681: logger.debug("WQAdapted.determineMinMaxQ");
ingo@681:
ingo@681: double[] minmaxQ = gauge != null
ingo@681: ? wst.determineMinMaxQ(gauge.getRange())
ingo@681: : null;
ingo@681:
ingo@681: double minQ = minmaxQ != null ? minmaxQ[0] : Double.MIN_VALUE;
ingo@681: double maxQ = minmaxQ != null ? minmaxQ[1] : Double.MAX_VALUE;
ingo@681:
ingo@681: return new double[] { minQ, maxQ };
ingo@681: }
ingo@681:
ingo@681:
ingo@399: @Override
ingo@399: protected String getUIProvider() {
ingo@399: return "wq_panel_adapted";
ingo@399: }
ingo@417:
ingo@417:
sascha@660: @Override
sascha@1050: public boolean validate(Artifact artifact)
ingo@417: throws IllegalArgumentException
ingo@417: {
ingo@417: logger.debug("WQAdapted.validate");
ingo@417:
felix@3445: 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;
raimund@2552: boolean isQ = mode != null
raimund@2552: ? Boolean.valueOf(mode)
raimund@2552: : false;
ingo@417:
raimund@2552: if (!isQ) {
sascha@1050: return validateW(artifact);
ingo@417: }
raimund@2552: else if (isQ) {
sascha@1050: return validateQ(artifact);
ingo@417: }
ingo@417: else {
ingo@417: throw new IllegalArgumentException("error_feed_no_wq_mode_selected");
ingo@417: }
ingo@417: }
ingo@417:
ingo@417:
sascha@1050: protected boolean validateW(Artifact artifact)
ingo@417: throws IllegalArgumentException
ingo@417: {
ingo@417: logger.debug("WQAdapted.validateW");
felix@3445: 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:
felix@3445: List gauges = FLYSUtils.getGauges((FLYSArtifact) artifact);
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:
sascha@1050: protected boolean validateQ(Artifact artifact)
ingo@417: throws IllegalArgumentException
ingo@417: {
ingo@417: logger.debug("WQAdapted.validateQ");
felix@3445: 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:
felix@3445: List gauges = FLYSUtils.getGauges(flys);
felix@1103: River river = FLYSUtils.getRiver(flys);
felix@1103: 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:
sascha@3452: return 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 :