teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5863: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5863: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.artifacts.states; ingo@2696: ingo@2696: import java.util.List; ingo@2696: ingo@2696: import org.apache.log4j.Logger; ingo@2696: teichmann@5831: import org.dive4elements.artifacts.Artifact; teichmann@5831: import org.dive4elements.artifacts.CallContext; ingo@2696: teichmann@5831: import org.dive4elements.artifacts.common.model.KVP; ingo@2696: teichmann@5831: import org.dive4elements.river.model.DischargeZone; teichmann@5831: import org.dive4elements.river.model.River; teichmann@5831: teichmann@5831: import org.dive4elements.river.artifacts.FLYSArtifact; teichmann@5831: import org.dive4elements.river.utils.FLYSUtils; ingo@2696: ingo@2696: ingo@2696: public class DischargeState extends MultiIntArrayState { ingo@2696: ingo@3759: public static final String MAIN_CHANNEL = "main_channel"; ingo@3759: public static final String TOTAL_CHANNEL = "total_channel"; ingo@2696: ingo@2696: ingo@2696: private static final Logger logger = Logger.getLogger(DischargeState.class); ingo@2696: ingo@2696: felix@3927: /** Let client display a matrix. */ ingo@2696: @Override ingo@2696: public String getUIProvider() { ingo@2696: return "parameter-matrix"; ingo@2696: } ingo@2696: ingo@2696: ingo@2696: /** ingo@2696: * This method fetches all DischargeZones for a given river (extracted from ingo@2696: * artifact) and returns a KVP[] where the key is the ID of the ingo@2696: * DischargeZone and the value is a string that consists of lower discharge ingo@2696: * and upper discharge. ingo@2696: * ingo@2696: * @param artifact Needs to be a FLYSArtifact that provides river ingo@2696: * information. ingo@2696: * @param parameterName The name of a parameter. ingo@2696: * ingo@2696: * @return a KVP[]. ingo@2696: */ ingo@2696: @Override ingo@2696: protected KVP[] getOptions( ingo@2696: Artifact artifact, ingo@2696: String parameterName ingo@2696: ) ingo@2696: throws IllegalArgumentException ingo@2696: { ingo@2696: if (!testParameterName(parameterName)) { ingo@2696: throw new IllegalArgumentException( ingo@2696: "Invalid parameter for state: '" + parameterName + "'"); ingo@2696: } ingo@2696: ingo@2696: List zones = getDischargeZones(artifact); ingo@2696: ingo@2696: KVP[] kvp = new KVP[zones.size()]; ingo@2696: sascha@3087: for (int i = 0, Z = zones.size(); i < Z; i++) { ingo@2696: DischargeZone zone = zones.get(i); ingo@2696: ingo@2696: String lower = zone.getLowerDischarge(); ingo@2696: String upper = zone.getUpperDischarge(); ingo@2696: ingo@2696: if (lower.equals(upper)) { ingo@2696: kvp[i] = new KVP(zone.getId(), lower); ingo@2696: } ingo@2696: else { ingo@2696: kvp[i] = new KVP(zone.getId(), lower + " - " + upper); ingo@2696: } ingo@2696: } ingo@2696: ingo@2696: return kvp; ingo@2696: } ingo@2696: ingo@2696: ingo@2705: @Override ingo@2705: protected String getLabelFor( ingo@2705: CallContext cc, ingo@2705: String parameterName, ingo@2705: int value ingo@2705: ) throws IllegalArgumentException ingo@2705: { ingo@2705: if (!testParameterName(parameterName)) { ingo@2705: throw new IllegalArgumentException( ingo@2705: "Invalid parameter for state: '" + parameterName + "'"); ingo@2705: } ingo@2705: ingo@2705: DischargeZone zone = DischargeZone.getDischargeZoneById(value); ingo@2705: ingo@2705: if (zone == null) { ingo@2705: throw new IllegalArgumentException( ingo@2705: "Invalid id for DischargeZone: '" + value + "'"); ingo@2705: } ingo@2705: ingo@2705: String lo = zone.getLowerDischarge(); ingo@2705: String hi = zone.getUpperDischarge(); ingo@2705: felix@3927: return hi != null && !lo.equals(hi) ingo@2705: ? lo + " - " + hi ingo@2705: : lo; ingo@2705: } ingo@2705: ingo@2705: ingo@2696: /** ingo@2696: * This method might be used to test, if a parameter name is handled by this ingo@2696: * state. ingo@2696: * ingo@2696: * @param parameterName The name of a parameter. ingo@2696: * ingo@2696: * @return true, if parameterName is one of MAIN_CHANNEL or ingo@2696: * TOTAL_CHANNEL. Otherwise false. ingo@2696: */ ingo@2696: protected boolean testParameterName(String parameterName) { ingo@2696: if (parameterName == null || parameterName.length() == 0) { ingo@2696: return false; ingo@2696: } ingo@2696: else if (parameterName.equals(MAIN_CHANNEL)) { ingo@2696: return true; ingo@2696: } ingo@2696: else if (parameterName.equals(TOTAL_CHANNEL)) { ingo@2696: return true; ingo@2696: } ingo@2696: else { ingo@2696: return false; ingo@2696: } ingo@2696: } ingo@2696: ingo@2696: ingo@2696: /** ingo@2696: * Returns all discharge zones for a given river. The river information is ingo@2696: * extracted from artifact using FLYSUtils.getRiver(). ingo@2696: * ingo@2696: * @param artifact Needs to be a FLYSArtifact that stores a rivername. ingo@2696: * ingo@2696: * @return a list of DischargeZones. ingo@2696: * ingo@2696: * @throws IllegalArgumentException if no river information is provided by ingo@2696: * artifact. ingo@2696: */ ingo@2696: protected List getDischargeZones(Artifact artifact) ingo@2696: throws IllegalArgumentException ingo@2696: { ingo@2696: River river = FLYSUtils.getRiver((FLYSArtifact) artifact); ingo@2696: ingo@2696: if (river == null) { ingo@2696: throw new IllegalArgumentException("No river found"); ingo@2696: } ingo@2696: ingo@2696: List zones = DischargeZone.getDischargeZones(river); ingo@2696: ingo@2696: logger.debug("Found " + zones.size() + " DischargeZones."); ingo@2696: ingo@2696: return zones; ingo@2696: } ingo@2696: } ingo@2696: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :