ingo@2709: package de.intevation.flys.artifacts.states; ingo@2709: ingo@2712: import java.util.ArrayList; ingo@2712: import java.util.List; ingo@2712: ingo@2709: import org.apache.log4j.Logger; ingo@2709: ingo@2709: import de.intevation.artifacts.Artifact; ingo@2709: import de.intevation.artifacts.CallContext; ingo@2709: ingo@2709: import de.intevation.artifacts.common.model.KVP; ingo@2709: ingo@2712: import de.intevation.flys.model.BedHeightEpoch; ingo@2712: import de.intevation.flys.model.BedHeightSingle; ingo@2712: import de.intevation.flys.model.River; ingo@2712: ingo@2712: import de.intevation.flys.artifacts.FLYSArtifact; ingo@2712: import de.intevation.flys.utils.FLYSUtils; ingo@2712: ingo@2709: ingo@2709: public class SoundingsSelect extends MultiStringArrayState { ingo@2709: ingo@2709: public static final String SOUNDINGS = "soundings"; ingo@2709: ingo@2712: public static final String PREFIX_SINGLE = "single-"; ingo@2712: ingo@2712: public static final String PREFIX_EPOCH = "epoch-"; ingo@2712: ingo@2709: ingo@2709: private static final Logger logger = 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: ingo@2709: ingo@2709: @Override ingo@2709: protected KVP[] getOptions( ingo@2709: Artifact artifact, raimund@3722: String parameterName, raimund@3722: CallContext context ingo@2709: ) ingo@2709: throws IllegalArgumentException ingo@2709: { ingo@2709: logger.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: ingo@2712: River river = FLYSUtils.getRiver((FLYSArtifact) artifact); ingo@2716: double lo = ((FLYSArtifact) artifact).getDataAsDouble("ld_from"); ingo@2716: double hi = ((FLYSArtifact) artifact).getDataAsDouble("ld_to"); ingo@2716: ingo@2716: double kmLo = Math.min(lo, hi); ingo@2716: double kmHi = Math.max(lo, hi); ingo@2709: ingo@2712: List> kvp = new ArrayList>(); ingo@2709: ingo@2716: appendSingles(river, kmLo, kmHi, kvp); ingo@2716: appendEpochs(river, kmLo, kmHi, kvp); ingo@2712: 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, ingo@2716: List> kvp ingo@2716: ) { ingo@2712: List singles = ingo@2716: BedHeightSingle.getBedHeightSingles(river, kmLo, kmHi); ingo@3782: ingo@2712: if (singles != null) { ingo@3782: int size = singles.size(); ingo@3782: ingo@3782: logger.debug("Found " + size + " singles."); ingo@3782: 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: ingo@2712: kvp.add(new KVP(id, value)); ingo@2712: } ingo@2709: } ingo@2712: } ingo@2709: ingo@2712: ingo@2716: protected void appendEpochs( ingo@2716: River river, ingo@2716: double kmLo, ingo@2716: double kmHi, ingo@2716: List> kvp ingo@2716: ) { ingo@2712: List epochs = ingo@2716: BedHeightEpoch.getBedHeightEpochs(river, kmLo, kmHi); ingo@2712: ingo@2712: if (epochs != null) { ingo@3782: int size = epochs.size(); ingo@3782: ingo@3782: logger.debug("Found " + size + " epochs."); ingo@3782: ingo@3782: for (int i = 0; i < size; i++) { ingo@2712: BedHeightEpoch e = epochs.get(i); ingo@2712: ingo@2712: String id = PREFIX_EPOCH + e.getId(); ingo@2712: String value = e.getDescription(); ingo@2712: ingo@2712: kvp.add(new KVP(id, value)); ingo@2712: } ingo@2712: } ingo@2709: } ingo@2709: ingo@2709: ingo@2709: @Override 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: else if (value.indexOf(PREFIX_EPOCH) >= 0) { ingo@2712: return getLabelForEpoch(cc, value); ingo@2712: } 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) { ingo@2712: logger.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@2712: protected String getLabelForEpoch(CallContext cc, String value) { ingo@2712: String id = value.replace(PREFIX_EPOCH, ""); ingo@2712: try { ingo@2712: BedHeightEpoch e = BedHeightEpoch.getBedHeightEpochById( ingo@2712: Integer.parseInt(id)); ingo@2712: ingo@2712: if (e != null) { ingo@2712: return e.getDescription(); ingo@2712: } ingo@2712: else { ingo@2712: return "no value for '" + id + "'"; ingo@2712: } ingo@2712: } ingo@2712: catch (NumberFormatException nfe) { ingo@2712: logger.warn("Could not parse id from string '" + id + "'", nfe); ingo@2712: } ingo@2712: ingo@2712: return "n.A."; ingo@2709: } ingo@2709: ingo@2709: 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 :