teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5994: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.artifacts.states; ingo@2709: rrenkert@7823: import java.text.NumberFormat; teichmann@8070: import java.util.Collections; ingo@2712: import java.util.List; ingo@2712: ingo@2709: import org.apache.log4j.Logger; teichmann@5831: import org.dive4elements.artifacts.Artifact; teichmann@5831: import org.dive4elements.artifacts.CallContext; teichmann@5831: import org.dive4elements.artifacts.common.model.KVP; rrenkert@7823: import org.dive4elements.artifacts.common.utils.XMLUtils; rrenkert@7823: import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator; teichmann@5831: import org.dive4elements.river.model.BedHeightSingle; teichmann@5831: import org.dive4elements.river.model.River; teichmann@5867: import org.dive4elements.river.artifacts.D4EArtifact; rrenkert@7823: import org.dive4elements.river.artifacts.resources.Resources; rrenkert@7823: import org.dive4elements.river.utils.Formatter; teichmann@5865: import org.dive4elements.river.utils.RiverUtils; rrenkert@7823: import org.w3c.dom.Element; ingo@2712: ingo@2709: rrenkert@7823: public class SoundingsSelect extends DefaultState { ingo@2709: felix@3931: public static final String SOUNDINGS = "soundings"; ingo@2709: ingo@2712: public static final String PREFIX_SINGLE = "single-"; ingo@2712: teichmann@8202: /** Private log. */ teichmann@8202: private static final Logger log = Logger.getLogger(SoundingsSelect.class); ingo@2709: ingo@2709: ingo@2709: @Override ingo@2709: public String getUIProvider() { ingo@2709: return "parameter-matrix"; ingo@2709: } ingo@2709: rrenkert@7823: @Override rrenkert@7823: protected void appendItems( rrenkert@7823: Artifact artifact, rrenkert@7823: ElementCreator creator, rrenkert@7823: String name, rrenkert@7823: CallContext context, rrenkert@7823: Element select rrenkert@7823: ) { rrenkert@7823: try { rrenkert@7823: creator.addAttr(select, "type", "multiattribute", true); ingo@2709: teichmann@8070: appendMeta(creator, select); teichmann@8070: rrenkert@7823: getOptions(artifact, name, context, creator, select); rrenkert@7823: } rrenkert@7823: catch (IllegalArgumentException iae) { teichmann@8202: log.warn("Illegal argument", iae); rrenkert@7823: } rrenkert@7823: } rrenkert@7823: teichmann@8070: private static Element order(ElementCreator creator, String name, String order) { teichmann@8070: Element element = creator.create(name); teichmann@8070: creator.addAttr(element, "order", order, false); teichmann@8070: return element; teichmann@8070: } teichmann@8070: teichmann@8070: private void appendMeta(ElementCreator creator, Element select) { teichmann@8070: teichmann@8070: Element meta = creator.create("meta"); teichmann@8070: teichmann@8070: meta.appendChild(order(creator, "year", "0")); teichmann@8070: meta.appendChild(order(creator, "value", "1")); teichmann@8070: meta.appendChild(order(creator, "analyzed_range", "2")); teichmann@8070: meta.appendChild(order(creator, "label", "3")); teichmann@8070: meta.appendChild(order(creator, "minfo_type", "4")); teichmann@8070: teichmann@8070: select.appendChild(meta); teichmann@8070: teichmann@8070: } teichmann@8070: ingo@2709: protected KVP[] getOptions( ingo@2709: Artifact artifact, ingo@3649: String parameterName, rrenkert@7823: CallContext context, rrenkert@7823: ElementCreator creator, rrenkert@7823: Element select ingo@2709: ) ingo@2709: throws IllegalArgumentException ingo@2709: { teichmann@8202: log.debug("Get options for parameter: '" + parameterName + "'"); ingo@2709: ingo@2709: if (!testParameterName(parameterName)) { ingo@2709: throw new IllegalArgumentException( ingo@2709: "Invalid parameter for state: '" + parameterName + "'"); ingo@2709: } ingo@2709: teichmann@5867: River river = RiverUtils.getRiver((D4EArtifact) artifact); teichmann@5867: double lo = ((D4EArtifact) artifact).getDataAsDouble("ld_from"); teichmann@5867: double hi = ((D4EArtifact) artifact).getDataAsDouble("ld_to"); ingo@2716: ingo@2716: double kmLo = Math.min(lo, hi); ingo@2716: double kmHi = Math.max(lo, hi); ingo@2709: rrenkert@7823: appendSingles(river, kmLo, kmHi, creator, select, context); ingo@2712: teichmann@8070: List> kvp = Collections.>emptyList(); sascha@3452: return kvp.toArray(new KVP[kvp.size()]); ingo@2712: } ingo@2712: ingo@2712: ingo@2716: protected void appendSingles( ingo@2716: River river, ingo@2716: double kmLo, ingo@2716: double kmHi, rrenkert@7823: ElementCreator creator, rrenkert@7823: Element select, rrenkert@7823: CallContext context ingo@2716: ) { ingo@2712: List singles = ingo@2716: BedHeightSingle.getBedHeightSingles(river, kmLo, kmHi); ingo@3785: ingo@2712: if (singles != null) { ingo@3782: int size = singles.size(); ingo@3785: teichmann@8202: log.debug("Found " + size + " singles."); ingo@3785: rrenkert@7823: NumberFormat nf = Formatter.getCalculationKm(context.getMeta()); ingo@3782: for (int i = 0; i < size; i++) { ingo@2712: BedHeightSingle s = singles.get(i); ingo@2712: ingo@2712: String id = PREFIX_SINGLE + s.getId(); ingo@2712: String value = s.getDescription(); ingo@2712: rrenkert@7833: Integer year = s.getYear(); rrenkert@7823: Element item = creator.create("item"); rrenkert@7823: creator.addAttr(item, "label", value, true); rrenkert@7823: creator.addAttr(item, "value", id, true); rrenkert@7875: creator.addAttr(item, "analyzed_range", rrenkert@7823: nf.format(s.getRange().getA()) + rrenkert@7823: " - " + rrenkert@7823: nf.format(s.getRange().getB())); rrenkert@7875: creator.addAttr(item, "year", rrenkert@7875: year != null ? s.getYear().toString() : ""); rrenkert@7875: creator.addAttr(item, "minfo_type", s.getType().getName()); rrenkert@7823: select.appendChild(item); ingo@2712: } ingo@2709: } teichmann@8202: if (log.isDebugEnabled()) { teichmann@8202: log.debug("appended singles"); teichmann@8202: log.debug(XMLUtils.toString(select)); teichmann@8070: } ingo@2712: } ingo@2709: rrenkert@7823: @Override rrenkert@7823: protected Element createStaticData( rrenkert@7823: D4EArtifact flys, rrenkert@7823: ElementCreator creator, rrenkert@7823: CallContext cc, rrenkert@7823: String name, rrenkert@7823: String value, rrenkert@7823: String type rrenkert@7823: ) { rrenkert@7823: Element data = creator.create("data"); rrenkert@7823: creator.addAttr(data, "name", name, true); rrenkert@7823: creator.addAttr(data, "type", type, true); rrenkert@7823: creator.addAttr(data, "label", rrenkert@7823: Resources.getMsg(cc.getMeta(), name, name), true); ingo@2709: rrenkert@7823: String[] values = value.split(";"); rrenkert@7823: rrenkert@7823: for (String val: values) { rrenkert@7823: Element item = creator.create("item"); rrenkert@7823: creator.addAttr(item, "value", val, true); rrenkert@7823: creator.addAttr(item, "label", getLabelFor(cc, name, val), true); rrenkert@7823: rrenkert@7823: data.appendChild(item); rrenkert@7823: } rrenkert@7823: rrenkert@7823: return data; rrenkert@7823: } rrenkert@7823: ingo@2709: protected String getLabelFor( ingo@2709: CallContext cc, ingo@2709: String parameterName, ingo@2709: String value ingo@2709: ) throws IllegalArgumentException ingo@2709: { ingo@2709: if (!testParameterName(parameterName)) { ingo@2709: throw new IllegalArgumentException( ingo@2709: "Invalid parameter for state: '" + parameterName + "'"); ingo@2709: } ingo@2709: ingo@2712: if (value.indexOf(PREFIX_SINGLE) >= 0) { ingo@2712: return getLabelForSingle(cc, value); ingo@2712: } ingo@2712: return value; ingo@2712: } ingo@2712: ingo@2712: ingo@2712: protected String getLabelForSingle(CallContext cc, String value) { ingo@2712: String id = value.replace(PREFIX_SINGLE, ""); ingo@2712: try { ingo@2712: BedHeightSingle s = BedHeightSingle.getBedHeightSingleById( ingo@2712: Integer.parseInt(id)); ingo@2712: ingo@2712: if (s != null) { ingo@2712: return s.getDescription(); ingo@2712: } ingo@2712: else { ingo@2712: return "no value for '" + id + "'"; ingo@2712: } ingo@2712: } ingo@2712: catch (NumberFormatException nfe) { teichmann@8202: log.warn("Could not parse id from string '" + id + "'", nfe); ingo@2712: } ingo@2712: ingo@2712: return "n.A."; ingo@2712: } ingo@2712: ingo@2712: ingo@2709: /** ingo@2709: * This method might be used to test, if a parameter name is handled by this ingo@2709: * state. ingo@2709: * ingo@2709: * @param parameterName The name of a parameter. ingo@2709: * ingo@2709: * @return true, if parameterName is one of MAIN_CHANNEL or ingo@2709: * TOTAL_CHANNEL. Otherwise false. ingo@2709: */ ingo@2709: protected boolean testParameterName(String parameterName) { ingo@2709: if (parameterName == null || parameterName.length() == 0) { ingo@2709: return false; ingo@2709: } ingo@2709: else if (parameterName.equals(SOUNDINGS)) { ingo@2709: return true; ingo@2709: } ingo@2709: else { ingo@2709: return false; ingo@2709: } ingo@2709: } ingo@2709: } sascha@3083: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :