comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/MinMaxState.java @ 3468:f37e7e8907cb

merged flys-artifacts/2.8.1
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:39 +0200
parents ee5310134463
children
comparison
equal deleted inserted replaced
3387:5ffad8bde8ad 3468:f37e7e8907cb
1 package de.intevation.flys.artifacts.states;
2
3 import org.w3c.dom.Element;
4
5 import org.apache.log4j.Logger;
6
7 import de.intevation.artifacts.Artifact;
8 import de.intevation.artifacts.CallContext;
9
10 import de.intevation.artifacts.common.ArtifactNamespaceContext;
11 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
12
13 import de.intevation.artifactdatabase.ProtocolUtils;
14
15 import de.intevation.flys.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