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@927: ingo@1622: import java.io.File; ingo@1622: ingo@1622: import org.w3c.dom.Element; ingo@1622: ingo@1622: import org.apache.log4j.Logger; ingo@1622: teichmann@5831: import org.dive4elements.artifacts.Artifact; teichmann@5831: import org.dive4elements.artifacts.CallContext; ingo@1622: teichmann@5831: import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator; ingo@2013: teichmann@5831: import org.dive4elements.river.model.DGM; teichmann@5831: import org.dive4elements.river.model.River; teichmann@5831: teichmann@5867: import org.dive4elements.river.artifacts.D4EArtifact; teichmann@5865: import org.dive4elements.river.utils.RiverUtils; ingo@1622: ingo@1622: ingo@927: /** ingo@927: * @author Ingo Weinzierl ingo@927: */ ingo@927: public class DGMSelect extends DefaultState { ingo@927: teichmann@8202: private static final Logger log = Logger.getLogger(DGMSelect.class); ingo@1622: ingo@2013: public static final String ERR_EMPTY = "error_no_dgm_selected"; ingo@2013: public static final String ERR_INVALID_DGM = "error_invalid_dgm_selected"; ingo@2013: public static final String ERR_BAD_DGM_RANGE = "error_bad_dgm_range"; ingo@2013: public static final String ERR_BAD_DGM_RIVER = "error_bad_dgm_river"; ingo@2013: ingo@1622: ingo@927: @Override ingo@927: protected String getUIProvider() { ingo@1172: return "dgm_datacage_panel"; ingo@927: } ingo@1622: ingo@1622: ingo@1622: @Override ingo@1622: protected Element createStaticData( teichmann@5867: D4EArtifact flys, ingo@1622: ElementCreator creator, ingo@1622: CallContext cc, ingo@1622: String name, ingo@1622: String value, ingo@1622: String type ingo@1622: ) { ingo@1622: Element dataElement = creator.create("data"); ingo@1622: creator.addAttr(dataElement, "name", name, true); ingo@1622: creator.addAttr(dataElement, "type", type, true); ingo@1622: ingo@1622: Element itemElement = creator.create("item"); ingo@1622: creator.addAttr(itemElement, "value", value, true); ingo@1622: ingo@1622: creator.addAttr(itemElement, "label", getLabel(cc, value), true); ingo@1622: dataElement.appendChild(itemElement); ingo@1622: ingo@1622: return dataElement; ingo@1622: } ingo@1622: ingo@1622: ingo@1622: public static String getLabel(CallContext cc, String value) { teichmann@8202: log.debug("Create label for value: " + value); ingo@1622: ingo@1622: try { ingo@1622: DGM dgm = DGM.getDGM(Integer.parseInt(value)); ingo@1622: ingo@1622: File file = new File(dgm.getPath()); ingo@1622: return file.getName(); ingo@1622: } ingo@1622: catch (NumberFormatException nfe) { teichmann@8202: log.warn("Cannot parse int value: '" + value + "'"); ingo@1622: } ingo@1622: ingo@1622: return ""; ingo@1622: } ingo@2013: ingo@2013: ingo@2013: @Override ingo@2013: public boolean validate(Artifact artifact) ingo@2013: throws IllegalArgumentException ingo@2013: { teichmann@5867: D4EArtifact flys = (D4EArtifact) artifact; ingo@2013: ingo@2013: DGM dgm = getDGM(flys); ingo@2013: ingo@2013: if (dgm == null) { ingo@2013: throw new IllegalArgumentException(ERR_INVALID_DGM); ingo@2013: } ingo@2013: rrenkert@5225: double l = dgm.getRange().getA().doubleValue(); rrenkert@5225: double u = dgm.getRange().getB().doubleValue(); ingo@2013: teichmann@5865: double[] range = RiverUtils.getKmFromTo(flys); ingo@2013: ingo@2013: if (range[0] < l || range[0] > u || range[1] < l || range[1] > u) { ingo@2013: throw new IllegalArgumentException(ERR_BAD_DGM_RANGE); ingo@2013: } ingo@2013: teichmann@5865: River selectedRiver = RiverUtils.getRiver(flys); ingo@2013: River dgmRiver = dgm.getRiver(); ingo@2013: ingo@2013: if (selectedRiver != dgmRiver) { ingo@2013: throw new IllegalArgumentException(ERR_BAD_DGM_RIVER); ingo@2013: } ingo@2013: ingo@2013: return true; ingo@2013: } ingo@2013: ingo@2013: ingo@2013: /** ingo@2013: * Returns the DGM specified in the parameters of flys. ingo@2013: * teichmann@5867: * @param flys The D4EArtifact that knows the ID of a DGM. ingo@2013: * teichmann@5867: * @throws IllegalArgumentException If the D4EArtifact doesn't know the ID ingo@2013: * of a DGM. ingo@2013: * teichmann@5867: * @return the DGM specified by D4EArtifact's parameters. ingo@2013: */ teichmann@5867: public static DGM getDGM(D4EArtifact flys) ingo@2013: throws IllegalArgumentException ingo@2013: { ingo@2013: try { ingo@2013: Integer dgmId = flys.getDataAsInteger("dgm"); ingo@2013: if (dgmId == null) { ingo@2013: throw new IllegalArgumentException(ERR_EMPTY); ingo@2013: } ingo@2013: teichmann@8202: log.debug("Found selected dgm: '" + dgmId + "'"); ingo@2013: ingo@2013: return DGM.getDGM(dgmId); ingo@2013: } ingo@2013: catch (NumberFormatException nfe) { ingo@2013: throw new IllegalArgumentException(ERR_INVALID_DGM); ingo@2013: } ingo@2013: } ingo@927: } ingo@927: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :