ingo@927: package de.intevation.flys.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: ingo@2013: import de.intevation.artifacts.Artifact; ingo@1622: import de.intevation.artifacts.CallContext; ingo@1622: ingo@1622: import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator; ingo@1622: ingo@2013: import de.intevation.flys.model.DGM; ingo@2013: import de.intevation.flys.model.River; ingo@2013: ingo@1743: import de.intevation.flys.artifacts.FLYSArtifact; ingo@2013: import de.intevation.flys.utils.FLYSUtils; ingo@1622: ingo@1622: ingo@927: /** ingo@927: * @author Ingo Weinzierl ingo@927: */ ingo@927: public class DGMSelect extends DefaultState { ingo@927: ingo@1622: private static final Logger logger = 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( ingo@1743: FLYSArtifact 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) { ingo@1622: logger.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) { ingo@1622: logger.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: { ingo@2013: FLYSArtifact flys = (FLYSArtifact) 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: ingo@2013: double l = dgm.getLower().doubleValue(); ingo@2013: double u = dgm.getUpper().doubleValue(); ingo@2013: ingo@2013: double[] range = FLYSUtils.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: ingo@2013: River selectedRiver = FLYSUtils.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: * ingo@2013: * @param flys The FLYSArtifact that knows the ID of a DGM. ingo@2013: * ingo@2013: * @throws IllegalArgumentException If the FLYSArtifact doesn't know the ID ingo@2013: * of a DGM. ingo@2013: * ingo@2013: * @return the DGM specified by FLYSArtifact's parameters. ingo@2013: */ ingo@2013: public static DGM getDGM(FLYSArtifact 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: ingo@2013: logger.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 :