ingo@2696: package de.intevation.flys.artifacts.states;
ingo@2696:
ingo@2696: import java.util.List;
ingo@2696:
ingo@2696: import org.apache.log4j.Logger;
ingo@2696:
ingo@2696: import de.intevation.artifacts.Artifact;
ingo@2705: import de.intevation.artifacts.CallContext;
ingo@2696:
ingo@2696: import de.intevation.artifacts.common.model.KVP;
ingo@2696:
ingo@2696: import de.intevation.flys.model.DischargeZone;
ingo@2696: import de.intevation.flys.model.River;
ingo@2696:
ingo@2696: import de.intevation.flys.artifacts.FLYSArtifact;
ingo@2696: import de.intevation.flys.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 :