comparison artifacts/src/main/java/org/dive4elements/river/artifacts/states/MinMaxState.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/MinMaxState.java@bd047b71ab37
children 4897a58c8746
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.artifacts.states;
2
3 import org.w3c.dom.Element;
4
5 import org.apache.log4j.Logger;
6
7 import org.dive4elements.artifacts.Artifact;
8 import org.dive4elements.artifacts.CallContext;
9
10 import org.dive4elements.artifacts.common.ArtifactNamespaceContext;
11 import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator;
12
13 import org.dive4elements.artifactdatabase.ProtocolUtils;
14
15 import org.dive4elements.river.artifacts.FLYSArtifact;
16
17
18 /**
19 * State that holds minimun and maximum (for validation).
20 *
21 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
22 */
23 public abstract class MinMaxState extends DefaultState {
24
25 private static final Logger logger = Logger.getLogger(MinMaxState.class);
26
27 @Override
28 protected void appendItems(
29 Artifact artifact,
30 ElementCreator creator,
31 String name,
32 CallContext context,
33 Element select
34 ) {
35 FLYSArtifact flys = (FLYSArtifact) artifact;
36
37 select.setAttributeNS(
38 ArtifactNamespaceContext.NAMESPACE_URI,
39 "art:type",
40 getType());
41
42 String[] defMinMax = getDefaults(artifact, name);
43
44 Element min = ProtocolUtils.createArtNode(
45 creator,
46 "min",
47 new String[] { "value", "default" },
48 new String[] { String.valueOf(getLower(flys)), defMinMax[0] });
49
50 Element max = ProtocolUtils.createArtNode(
51 creator,
52 "max",
53 new String[] { "value", "default" },
54 new String[] { String.valueOf(getUpper(flys)), defMinMax[1] });
55
56 select.appendChild(min);
57 select.appendChild(max);
58 }
59
60
61 /**
62 * @param cc
63 * @param name
64 * @param value
65 * @param type
66 *
67 * @return
68 */
69 @Override
70 protected String getLabelFor(
71 CallContext cc,
72 String name,
73 String value,
74 String type
75 ) {
76 if (type.indexOf("range") > 0) {
77 String[] minmax = extractRangeAsString(value);
78
79 if (minmax != null) {
80 return minmax[0] + " - " + minmax[1];
81 }
82 }
83
84 return super.getLabelFor(cc, name, value, type);
85 }
86
87
88 /**
89 * Returns a string array with [min,max] from <i>rawValue</i>.
90 * <i>rawValue</i> should be a string like "1999;2001".
91 *
92 * @param rawValue A string with min and max separated by a ';'.
93 *
94 * @return the min and max as string array ([min,max]).
95 */
96 protected String[] extractRangeAsString(String rawValue) {
97 return rawValue.split(";");
98 }
99
100
101 /**
102 * This method returns the default values for min and max. If the static
103 * field DefaultState.USE_DEFAULTS is set, the minimum and maximum inserted
104 * by the user is returned as string. Otherwise, the absolute minimum and
105 * maximum are returned.
106 *
107 * @param artifact The FLYSArtifact.
108 * @param name The name of the parameter.
109 *
110 * @return a string array [min,max] that contains the minimum and maximum
111 * values for the parameter <i>name</i>.
112 */
113 protected String[] getDefaults(Artifact artifact, String name) {
114 if (DefaultState.USE_DEFAULTS) {
115 String[] tmp = getMinMaxByParameter(artifact, name);
116
117 return tmp != null ? tmp : getMinMaxDefaults(artifact, name);
118 }
119 else {
120 return getMinMaxDefaults(artifact, name);
121 }
122 }
123
124
125 /**
126 * Returns a string array with minimum and maximum inserted by the user as
127 * [min,max].
128 *
129 * @param artifact The FLYSArtifact that stores the parameter.
130 * @param name The name of the parameter for raw min/max value string.
131 *
132 * @return a string array [min,max].
133 */
134 protected String[] getMinMaxByParameter(Artifact artifact, String name) {
135 FLYSArtifact flys = (FLYSArtifact) artifact;
136 String rawValue = flys.getDataAsString(name);
137
138 if (rawValue == null) {
139 logger.debug("No value for '" + rawValue + "' existing.");
140 return null;
141 }
142
143 logger.debug("Raw value for '" + name + "' = " + rawValue);
144
145 return extractRangeAsString(rawValue);
146 }
147
148
149 /**
150 * Returns a string array with absolute minimum and maximum as [min,max].
151 *
152 * @param artifact The FLYSArtifact (not used in this implementation).
153 * @param name The parameter name (not used in this implementation).
154 *
155 * @return a string array [min,max].
156 */
157 protected String[] getMinMaxDefaults(Artifact artifact, String name) {
158 FLYSArtifact flys = (FLYSArtifact) artifact;
159
160 Object lower = getLower(flys);
161 Object upper = getUpper(flys);
162
163 return new String[] { String.valueOf(lower), String.valueOf(upper) };
164 }
165
166
167 protected abstract Object getLower(FLYSArtifact flys);
168
169 protected abstract Object getUpper(FLYSArtifact flys);
170
171 protected abstract String getType();
172 }
173 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org