ingo@399: package de.intevation.flys.artifacts.states; ingo@399: 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@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@399: ingo@399: import de.intevation.flys.artifacts.FLYSArtifact; 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@399: } ingo@399: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :