Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/states/DGMSelect.java @ 5838:5aa05a7a34b7
Rename modules to more fitting names.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 15:23:37 +0200 |
parents | flys-artifacts/src/main/java/org/dive4elements/river/artifacts/states/DGMSelect.java@bd047b71ab37 |
children | 4897a58c8746 |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.artifacts.states; | |
2 | |
3 import java.io.File; | |
4 | |
5 import org.w3c.dom.Element; | |
6 | |
7 import org.apache.log4j.Logger; | |
8 | |
9 import org.dive4elements.artifacts.Artifact; | |
10 import org.dive4elements.artifacts.CallContext; | |
11 | |
12 import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator; | |
13 | |
14 import org.dive4elements.river.model.DGM; | |
15 import org.dive4elements.river.model.River; | |
16 | |
17 import org.dive4elements.river.artifacts.FLYSArtifact; | |
18 import org.dive4elements.river.utils.FLYSUtils; | |
19 | |
20 | |
21 /** | |
22 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
23 */ | |
24 public class DGMSelect extends DefaultState { | |
25 | |
26 private static final Logger logger = Logger.getLogger(DGMSelect.class); | |
27 | |
28 public static final String ERR_EMPTY = "error_no_dgm_selected"; | |
29 public static final String ERR_INVALID_DGM = "error_invalid_dgm_selected"; | |
30 public static final String ERR_BAD_DGM_RANGE = "error_bad_dgm_range"; | |
31 public static final String ERR_BAD_DGM_RIVER = "error_bad_dgm_river"; | |
32 | |
33 | |
34 @Override | |
35 protected String getUIProvider() { | |
36 return "dgm_datacage_panel"; | |
37 } | |
38 | |
39 | |
40 @Override | |
41 protected Element createStaticData( | |
42 FLYSArtifact flys, | |
43 ElementCreator creator, | |
44 CallContext cc, | |
45 String name, | |
46 String value, | |
47 String type | |
48 ) { | |
49 Element dataElement = creator.create("data"); | |
50 creator.addAttr(dataElement, "name", name, true); | |
51 creator.addAttr(dataElement, "type", type, true); | |
52 | |
53 Element itemElement = creator.create("item"); | |
54 creator.addAttr(itemElement, "value", value, true); | |
55 | |
56 creator.addAttr(itemElement, "label", getLabel(cc, value), true); | |
57 dataElement.appendChild(itemElement); | |
58 | |
59 return dataElement; | |
60 } | |
61 | |
62 | |
63 public static String getLabel(CallContext cc, String value) { | |
64 logger.debug("Create label for value: " + value); | |
65 | |
66 try { | |
67 DGM dgm = DGM.getDGM(Integer.parseInt(value)); | |
68 | |
69 File file = new File(dgm.getPath()); | |
70 return file.getName(); | |
71 } | |
72 catch (NumberFormatException nfe) { | |
73 logger.warn("Cannot parse int value: '" + value + "'"); | |
74 } | |
75 | |
76 return ""; | |
77 } | |
78 | |
79 | |
80 @Override | |
81 public boolean validate(Artifact artifact) | |
82 throws IllegalArgumentException | |
83 { | |
84 FLYSArtifact flys = (FLYSArtifact) artifact; | |
85 | |
86 DGM dgm = getDGM(flys); | |
87 | |
88 if (dgm == null) { | |
89 throw new IllegalArgumentException(ERR_INVALID_DGM); | |
90 } | |
91 | |
92 double l = dgm.getRange().getA().doubleValue(); | |
93 double u = dgm.getRange().getB().doubleValue(); | |
94 | |
95 double[] range = FLYSUtils.getKmFromTo(flys); | |
96 | |
97 if (range[0] < l || range[0] > u || range[1] < l || range[1] > u) { | |
98 throw new IllegalArgumentException(ERR_BAD_DGM_RANGE); | |
99 } | |
100 | |
101 River selectedRiver = FLYSUtils.getRiver(flys); | |
102 River dgmRiver = dgm.getRiver(); | |
103 | |
104 if (selectedRiver != dgmRiver) { | |
105 throw new IllegalArgumentException(ERR_BAD_DGM_RIVER); | |
106 } | |
107 | |
108 return true; | |
109 } | |
110 | |
111 | |
112 /** | |
113 * Returns the DGM specified in the parameters of <i>flys</i>. | |
114 * | |
115 * @param flys The FLYSArtifact that knows the ID of a DGM. | |
116 * | |
117 * @throws IllegalArgumentException If the FLYSArtifact doesn't know the ID | |
118 * of a DGM. | |
119 * | |
120 * @return the DGM specified by FLYSArtifact's parameters. | |
121 */ | |
122 public static DGM getDGM(FLYSArtifact flys) | |
123 throws IllegalArgumentException | |
124 { | |
125 try { | |
126 Integer dgmId = flys.getDataAsInteger("dgm"); | |
127 if (dgmId == null) { | |
128 throw new IllegalArgumentException(ERR_EMPTY); | |
129 } | |
130 | |
131 logger.debug("Found selected dgm: '" + dgmId + "'"); | |
132 | |
133 return DGM.getDGM(dgmId); | |
134 } | |
135 catch (NumberFormatException nfe) { | |
136 throw new IllegalArgumentException(ERR_INVALID_DGM); | |
137 } | |
138 } | |
139 } | |
140 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |