gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dynobject / impl / DefaultDynField.java @ 1860
History | View | Annotate | Download (27.6 KB)
1 | 802 | cordinyana | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
5 | *
|
||
6 | 1405 | jjdelcerro | * This program is free software; you can redistribute it and/or modify it under
|
7 | * the terms of the GNU General Public License as published by the Free Software
|
||
8 | * Foundation; either version 2 of the License, or (at your option) any later
|
||
9 | * version.
|
||
10 | 802 | cordinyana | *
|
11 | 1405 | jjdelcerro | * This program is distributed in the hope that it will be useful, but WITHOUT
|
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||
13 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||
14 | * details.
|
||
15 | 802 | cordinyana | *
|
16 | 1405 | jjdelcerro | * You should have received a copy of the GNU General Public License along with
|
17 | * this program; if not, write to the Free Software Foundation, Inc., 51
|
||
18 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
19 | 802 | cordinyana | *
|
20 | 1405 | jjdelcerro | * For any additional information, do not hesitate to contact us at info AT
|
21 | * gvsig.com, or visit our website www.gvsig.com.
|
||
22 | 802 | cordinyana | */
|
23 | 18 | cordinyana | package org.gvsig.tools.dynobject.impl; |
24 | |||
25 | 141 | cmartin | import java.io.File; |
26 | 251 | jjdelcerro | import java.net.URI; |
27 | import java.net.URL; |
||
28 | 1118 | jjdelcerro | import java.util.Collection; |
29 | 73 | jjdelcerro | import java.util.Date; |
30 | import java.util.Iterator; |
||
31 | import java.util.List; |
||
32 | import java.util.Map; |
||
33 | import java.util.Set; |
||
34 | 1118 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
35 | 73 | jjdelcerro | |
36 | import org.gvsig.tools.ToolsLocator; |
||
37 | 112 | jjdelcerro | import org.gvsig.tools.dataTypes.CoercionException; |
38 | 217 | jjdelcerro | import org.gvsig.tools.dataTypes.DataType; |
39 | 18 | cordinyana | import org.gvsig.tools.dataTypes.DataTypes; |
40 | 112 | jjdelcerro | import org.gvsig.tools.dataTypes.DataTypesManager; |
41 | 18 | cordinyana | import org.gvsig.tools.dynobject.DynField; |
42 | 1031 | jbadia | import org.gvsig.tools.dynobject.DynField_v2; |
43 | 1405 | jjdelcerro | import org.gvsig.tools.dynobject.DynMethod; |
44 | 73 | jjdelcerro | import org.gvsig.tools.dynobject.DynObject; |
45 | 18 | cordinyana | import org.gvsig.tools.dynobject.DynObjectValueItem; |
46 | 73 | jjdelcerro | import org.gvsig.tools.dynobject.DynStruct; |
47 | 1033 | jbadia | import org.gvsig.tools.dynobject.Tags; |
48 | 73 | jjdelcerro | import org.gvsig.tools.dynobject.exception.DynFieldIsNotAContainerException; |
49 | 256 | cmartin | import org.gvsig.tools.dynobject.exception.DynFieldRequiredValueException; |
50 | 73 | jjdelcerro | import org.gvsig.tools.dynobject.exception.DynFieldValidateException; |
51 | 1405 | jjdelcerro | import org.gvsig.tools.dynobject.exception.DynMethodException; |
52 | 73 | jjdelcerro | import org.gvsig.tools.dynobject.exception.DynObjectValidateException; |
53 | 146 | jjdelcerro | import org.gvsig.tools.exception.ListBaseException; |
54 | 1117 | jbadia | import org.slf4j.Logger; |
55 | import org.slf4j.LoggerFactory; |
||
56 | 18 | cordinyana | |
57 | 1335 | jjdelcerro | @SuppressWarnings("EqualsAndHashcode") |
58 | 1031 | jbadia | public class DefaultDynField implements DynField_v2 { |
59 | 18 | cordinyana | |
60 | 1118 | jjdelcerro | public static final Logger log = LoggerFactory.getLogger(DefaultDynField.class); |
61 | 18 | cordinyana | |
62 | 1118 | jjdelcerro | private String name; |
63 | private String description; |
||
64 | 18 | cordinyana | |
65 | 1405 | jjdelcerro | private ValueType valueType;
|
66 | 1118 | jjdelcerro | |
67 | private String subtype; |
||
68 | |||
69 | private Object defaultValue; |
||
70 | |||
71 | private int order; |
||
72 | private boolean hidden; |
||
73 | private String groupName; |
||
74 | private DynObjectValueItem[] availableValues; |
||
75 | 1405 | jjdelcerro | private DynMethod availableValuesMethod = null; |
76 | |||
77 | 1118 | jjdelcerro | private Object minValue; |
78 | private Object maxValue; |
||
79 | private boolean mandatory; |
||
80 | private boolean persistent; |
||
81 | private String label = null; |
||
82 | |||
83 | private boolean isReadOnly; |
||
84 | 1405 | jjdelcerro | private Tags tags = new DefaultTags(); |
85 | 1118 | jjdelcerro | |
86 | private ValueType itemsType;
|
||
87 | private boolean validateItems; |
||
88 | |||
89 | 1085 | jjdelcerro | // Para implementacion futura, como minimo para implementar
|
90 | // un copy entre dynobject y saber cuando parar.
|
||
91 | // Los valores deberan ser algo asi como:
|
||
92 | 1305 | jjdelcerro | // - Identidad/Identity, 1:1 y se copian.
|
93 | // - Composicion/Composition, 1:N y se copian.
|
||
94 | // - Agregacion/Aggregate, 1:N, no se copian
|
||
95 | // - Colaboracion/Collaboration, 1:1, no se copian
|
||
96 | 1085 | jjdelcerro | // Y solo tendra efecto/sentido cuando el field sea de tipo
|
97 | // DynObject, y tal vez lista de DynObject
|
||
98 | 1118 | jjdelcerro | private int relationType; |
99 | 1405 | jjdelcerro | private DynMethod calculate = null; |
100 | 627 | cmartin | |
101 | 1118 | jjdelcerro | public DefaultDynField(String name, int dataType) { |
102 | this(name, // field name |
||
103 | dataType, // data type
|
||
104 | null, // default value |
||
105 | true, // persistent |
||
106 | false // mandatory |
||
107 | ); |
||
108 | } |
||
109 | 627 | cmartin | |
110 | 1335 | jjdelcerro | @SuppressWarnings("OverridableMethodCallInConstructor") |
111 | 1118 | jjdelcerro | protected DefaultDynField(String name, int dataType, Object defaultValue, |
112 | boolean persistent, boolean mandatory) { |
||
113 | DataTypesManager datamanager = ToolsLocator.getDataTypesManager(); |
||
114 | 627 | cmartin | |
115 | 1405 | jjdelcerro | if (StringUtils.isBlank(name)) {
|
116 | 1118 | jjdelcerro | throw new IllegalArgumentException("name can't be null"); |
117 | } |
||
118 | this.name = name;
|
||
119 | 1405 | jjdelcerro | this.valueType = new ValueType(dataType); |
120 | 1118 | jjdelcerro | this.subtype = this.getSubtype(); |
121 | 627 | cmartin | |
122 | 1118 | jjdelcerro | this.defaultValue = defaultValue;
|
123 | this.persistent = persistent;
|
||
124 | this.mandatory = mandatory;
|
||
125 | this.groupName = null; |
||
126 | this.order = 0; |
||
127 | this.hidden = false; |
||
128 | this.availableValues = null; |
||
129 | 146 | jjdelcerro | |
130 | 1118 | jjdelcerro | this.itemsType = new ValueType(DataTypes.UNKNOWN); |
131 | this.validateItems = false; |
||
132 | 1306 | jjdelcerro | this.relationType = RELATION_TYPE_NONE;
|
133 | 1118 | jjdelcerro | } |
134 | 627 | cmartin | |
135 | 1405 | jjdelcerro | @Override
|
136 | public void copyFrom(DynField other) { |
||
137 | this.name = other.getName();
|
||
138 | this.description = other.getDescription();
|
||
139 | this.subtype = other.getSubtype();
|
||
140 | this.defaultValue = other.getDefaultValue();
|
||
141 | this.order = other.getOder();
|
||
142 | this.hidden = other.isHidden();
|
||
143 | this.groupName = other.getGroup();
|
||
144 | this.minValue = other.getMinValue();
|
||
145 | this.maxValue = other.getMaxValue();
|
||
146 | this.mandatory = other.isMandatory();
|
||
147 | this.persistent = other.isMandatory();
|
||
148 | this.isReadOnly = other.isReadOnly();
|
||
149 | this.tags = new DefaultTags(); |
||
150 | |||
151 | if (other instanceof DynField_v2) { |
||
152 | DynField_v2 other2 = (DynField_v2) other; |
||
153 | this.tags.add(other2.getTags());
|
||
154 | this.label = other2.getLabel();
|
||
155 | this.validateItems = other2.getValidateElements();
|
||
156 | |||
157 | if (other2.isCalculated()) {
|
||
158 | this.setCalculateMethod(other2.getCalculateMethod());
|
||
159 | } |
||
160 | |||
161 | if( other2.isAvailableValuesCalculated() ) {
|
||
162 | this.availableValuesMethod = other2.getAvailableValuesMethod();
|
||
163 | } else {
|
||
164 | this.availableValues = other.getAvailableValues().clone();
|
||
165 | } |
||
166 | |||
167 | this.setType(other.getDataType());
|
||
168 | this.setClassOfValue(other2.getClassOfValue());
|
||
169 | this.setClassOfValue(other2.getDynClassOfValue());
|
||
170 | |||
171 | this.setTypeOfItems(other2.getTypeOfItems());
|
||
172 | this.setClassOfItems(other2.getClassOfItems());
|
||
173 | this.setClassOfItems(other2.getDynClassOfItems());
|
||
174 | } |
||
175 | |||
176 | } |
||
177 | |||
178 | 1118 | jjdelcerro | protected ValueType getValueType() {
|
179 | 1405 | jjdelcerro | return valueType;
|
180 | 1118 | jjdelcerro | } |
181 | 146 | jjdelcerro | |
182 | 1405 | jjdelcerro | @Override
|
183 | public DynField setCalculateMethod(DynMethod method) {
|
||
184 | this.calculate = method;
|
||
185 | this.persistent = false; |
||
186 | return this; |
||
187 | } |
||
188 | |||
189 | public DynMethod getCalculateMethod() {
|
||
190 | return this.calculate; |
||
191 | } |
||
192 | |||
193 | @Override
|
||
194 | public boolean isCalculated() { |
||
195 | return this.calculate != null; |
||
196 | } |
||
197 | |||
198 | @Override
|
||
199 | public Object getCalculatedValue(DynObject self) { |
||
200 | try {
|
||
201 | return this.calculate.invoke(self, new Object[]{this}); |
||
202 | } catch (DynMethodException ex) {
|
||
203 | throw new RuntimeException(ex); |
||
204 | } |
||
205 | } |
||
206 | |||
207 | 1118 | jjdelcerro | public void check() throws ListBaseException { |
208 | } |
||
209 | 627 | cmartin | |
210 | 1305 | jjdelcerro | @Override
|
211 | 1118 | jjdelcerro | public String toString() { |
212 | 1305 | jjdelcerro | StringBuilder buffer = new StringBuilder(); |
213 | 627 | cmartin | |
214 | 1118 | jjdelcerro | buffer.append("DynField").append("[").append(this.hashCode()) |
215 | .append("]").append("( ").append("name='").append(this.name) |
||
216 | .append("', ").append("description='").append(this.description) |
||
217 | 1405 | jjdelcerro | .append("', ").append("type='").append(this.valueType) |
218 | 1118 | jjdelcerro | .append("', ").append("subType='").append(this.subtype) |
219 | .append("', ").append("mandatory='").append(this.isMandatory()) |
||
220 | .append("', ").append("defaultValue='") |
||
221 | .append(this.getDefaultValue()).append("', ") |
||
222 | .append("minValue='").append(this.minValue).append("', ") |
||
223 | .append("maxValue='").append(this.maxValue).append("', ") |
||
224 | .append("persistent='").append(this.isPersistent()) |
||
225 | .append(" )");
|
||
226 | return buffer.toString();
|
||
227 | } |
||
228 | 627 | cmartin | |
229 | 1305 | jjdelcerro | @Override
|
230 | 1118 | jjdelcerro | public String getName() { |
231 | return name;
|
||
232 | } |
||
233 | 130 | jjdelcerro | |
234 | 1118 | jjdelcerro | public DynField setName(String name) { |
235 | 1405 | jjdelcerro | if (StringUtils.isBlank(name)) {
|
236 | 1118 | jjdelcerro | throw new IllegalArgumentException("name can't be null"); |
237 | } |
||
238 | this.name = name;
|
||
239 | return this; |
||
240 | } |
||
241 | 217 | jjdelcerro | |
242 | 1305 | jjdelcerro | @Override
|
243 | 1118 | jjdelcerro | public DynField setDescription(String description) { |
244 | this.description = description;
|
||
245 | return this; |
||
246 | } |
||
247 | 18 | cordinyana | |
248 | 1305 | jjdelcerro | @Override
|
249 | 1118 | jjdelcerro | public String getDescription() { |
250 | return (description == null) ? getLabel() : description; |
||
251 | } |
||
252 | 73 | jjdelcerro | |
253 | 1305 | jjdelcerro | @Override
|
254 | 1118 | jjdelcerro | public DynField setLabel(String label) { |
255 | this.label = label;
|
||
256 | return this; |
||
257 | } |
||
258 | 73 | jjdelcerro | |
259 | 1305 | jjdelcerro | @Override
|
260 | 1118 | jjdelcerro | public String getLabel() { |
261 | return (label == null) ? getName() : label; |
||
262 | } |
||
263 | 18 | cordinyana | |
264 | 1305 | jjdelcerro | @Override
|
265 | 1118 | jjdelcerro | public DynField setType(int dataType) { |
266 | 1405 | jjdelcerro | this.valueType.setType(dataType);
|
267 | 1118 | jjdelcerro | return this; |
268 | } |
||
269 | 18 | cordinyana | |
270 | 1305 | jjdelcerro | @Override
|
271 | 1118 | jjdelcerro | public DynField setType(DataType dataType) {
|
272 | 1405 | jjdelcerro | this.valueType.setType(dataType);
|
273 | 1118 | jjdelcerro | return this; |
274 | } |
||
275 | 18 | cordinyana | |
276 | 1305 | jjdelcerro | @Override
|
277 | 1118 | jjdelcerro | public int getType() { |
278 | 1405 | jjdelcerro | return this.valueType.getType(); |
279 | 1118 | jjdelcerro | } |
280 | 842 | jjdelcerro | |
281 | 1305 | jjdelcerro | @Override
|
282 | 1118 | jjdelcerro | public DataType getDataType() {
|
283 | 1405 | jjdelcerro | return this.valueType.getDataType(); |
284 | 1118 | jjdelcerro | } |
285 | 220 | jjdelcerro | |
286 | 1305 | jjdelcerro | @Override
|
287 | 1118 | jjdelcerro | public DynField setSubtype(String subtype) { |
288 | this.subtype = subtype;
|
||
289 | return this; |
||
290 | } |
||
291 | 18 | cordinyana | |
292 | 1305 | jjdelcerro | @Override
|
293 | 1118 | jjdelcerro | public String getSubtype() { |
294 | return subtype;
|
||
295 | } |
||
296 | 18 | cordinyana | |
297 | 1305 | jjdelcerro | @Override
|
298 | 1118 | jjdelcerro | public DynField setDefaultDynValue(Object defaultValue) { |
299 | this.defaultValue = defaultValue;
|
||
300 | return this; |
||
301 | } |
||
302 | 627 | cmartin | |
303 | 1305 | jjdelcerro | @Override
|
304 | 1118 | jjdelcerro | public Object getDefaultValue() { |
305 | return defaultValue;
|
||
306 | } |
||
307 | 73 | jjdelcerro | |
308 | 1305 | jjdelcerro | @Override
|
309 | 1405 | jjdelcerro | public boolean isAvailableValuesCalculated() { |
310 | return this.availableValuesMethod != null; |
||
311 | } |
||
312 | |||
313 | @Override
|
||
314 | 1118 | jjdelcerro | public DynField setAvailableValues(DynObjectValueItem[] availableValues) { |
315 | 1405 | jjdelcerro | if (availableValues == null || availableValues.length == 0) { |
316 | 1118 | jjdelcerro | this.availableValues = null; |
317 | } else {
|
||
318 | this.availableValues = availableValues;
|
||
319 | } |
||
320 | return this; |
||
321 | } |
||
322 | 73 | jjdelcerro | |
323 | 1305 | jjdelcerro | @Override
|
324 | 1118 | jjdelcerro | public DynField setAvailableValues(List availableValues) { |
325 | 1405 | jjdelcerro | if (availableValues == null) { |
326 | 1118 | jjdelcerro | this.availableValues = null; |
327 | 1405 | jjdelcerro | } else if (availableValues.isEmpty()) { |
328 | 1118 | jjdelcerro | this.availableValues = null; |
329 | } else {
|
||
330 | this.availableValues = (DynObjectValueItem[]) availableValues |
||
331 | .toArray(new DynObjectValueItem[availableValues.size()]);
|
||
332 | } |
||
333 | return this; |
||
334 | } |
||
335 | 1405 | jjdelcerro | |
336 | 1335 | jjdelcerro | @Override
|
337 | 1405 | jjdelcerro | public DynField setAvailableValues(DynMethod computeAvailableValues) {
|
338 | this.availableValuesMethod = computeAvailableValues;
|
||
339 | return this; |
||
340 | 1335 | jjdelcerro | } |
341 | 18 | cordinyana | |
342 | 1305 | jjdelcerro | @Override
|
343 | 1405 | jjdelcerro | public DynMethod getAvailableValuesMethod() {
|
344 | return this.availableValuesMethod; |
||
345 | 1118 | jjdelcerro | } |
346 | 1405 | jjdelcerro | |
347 | 1335 | jjdelcerro | |
348 | @Override
|
||
349 | 1405 | jjdelcerro | public DynObjectValueItem[] getAvailableValues() { |
350 | return this.getAvailableValues(null); |
||
351 | } |
||
352 | |||
353 | @Override
|
||
354 | public DynObjectValueItem[] getAvailableValues(DynObject self) { |
||
355 | if (this.availableValuesMethod != null) { |
||
356 | DynObjectValueItem[] values;
|
||
357 | try {
|
||
358 | values = (DynObjectValueItem[]) this.availableValuesMethod.invoke(self, new Object[]{this}); |
||
359 | } catch (DynMethodException ex) {
|
||
360 | return this.availableValues; |
||
361 | } |
||
362 | if (values != null) { |
||
363 | 1335 | jjdelcerro | return values;
|
364 | } |
||
365 | } |
||
366 | return this.availableValues; |
||
367 | } |
||
368 | 18 | cordinyana | |
369 | 1305 | jjdelcerro | @Override
|
370 | 1118 | jjdelcerro | public DynField setMinValue(Object minValue) { |
371 | try {
|
||
372 | this.minValue = this.coerce(minValue); |
||
373 | } catch (CoercionException e) {
|
||
374 | 1405 | jjdelcerro | throw new IllegalArgumentException(); |
375 | 1118 | jjdelcerro | } |
376 | return this; |
||
377 | } |
||
378 | 18 | cordinyana | |
379 | 1305 | jjdelcerro | @Override
|
380 | 1118 | jjdelcerro | public Object getMinValue() { |
381 | return minValue;
|
||
382 | } |
||
383 | 109 | jjdelcerro | |
384 | 1305 | jjdelcerro | @Override
|
385 | 1118 | jjdelcerro | public DynField setMaxValue(Object maxValue) { |
386 | try {
|
||
387 | this.maxValue = this.coerce(maxValue); |
||
388 | } catch (CoercionException e) {
|
||
389 | 1405 | jjdelcerro | throw new IllegalArgumentException(e); |
390 | 1118 | jjdelcerro | } |
391 | return this; |
||
392 | } |
||
393 | 18 | cordinyana | |
394 | 1305 | jjdelcerro | @Override
|
395 | 1118 | jjdelcerro | public Object getMaxValue() { |
396 | return maxValue;
|
||
397 | } |
||
398 | 18 | cordinyana | |
399 | 1305 | jjdelcerro | @Override
|
400 | 1118 | jjdelcerro | public boolean isMandatory() { |
401 | return this.mandatory; |
||
402 | } |
||
403 | 18 | cordinyana | |
404 | 1305 | jjdelcerro | @Override
|
405 | 1118 | jjdelcerro | public boolean isPersistent() { |
406 | 1405 | jjdelcerro | if (this.isCalculated()) { |
407 | return false; |
||
408 | } |
||
409 | 1118 | jjdelcerro | return this.persistent; |
410 | } |
||
411 | 18 | cordinyana | |
412 | 1305 | jjdelcerro | @Override
|
413 | 1118 | jjdelcerro | public DynField setMandatory(boolean mandatory) { |
414 | this.mandatory = mandatory;
|
||
415 | return this; |
||
416 | } |
||
417 | 18 | cordinyana | |
418 | 1305 | jjdelcerro | @Override
|
419 | 1118 | jjdelcerro | public DynField setPersistent(boolean persistent) { |
420 | 1405 | jjdelcerro | if (this.isCalculated()) { |
421 | persistent = false;
|
||
422 | } |
||
423 | 1118 | jjdelcerro | this.persistent = persistent;
|
424 | return this; |
||
425 | } |
||
426 | 18 | cordinyana | |
427 | 1305 | jjdelcerro | @Override
|
428 | 1118 | jjdelcerro | public DynField setTheTypeOfAvailableValues(int type) { |
429 | return this; // FIXME: this method is @deprecated |
||
430 | } |
||
431 | 18 | cordinyana | |
432 | 1305 | jjdelcerro | @Override
|
433 | 1118 | jjdelcerro | public int getTheTypeOfAvailableValues() { |
434 | return 1; // FIXME: this method is @deprecated |
||
435 | } |
||
436 | 18 | cordinyana | |
437 | 1335 | jjdelcerro | /**
|
438 | *
|
||
439 | * @param obj
|
||
440 | * @return
|
||
441 | */
|
||
442 | 1305 | jjdelcerro | @Override
|
443 | 1118 | jjdelcerro | public boolean equals(Object obj) { |
444 | 1405 | jjdelcerro | if (this == obj) { |
445 | 1118 | jjdelcerro | return true; |
446 | } |
||
447 | 1405 | jjdelcerro | if (obj instanceof DynField) { |
448 | 1118 | jjdelcerro | // FIXME: No esta claro que esto sea correcto.
|
449 | return name.equals(((DynField) obj).getName());
|
||
450 | } |
||
451 | return false; |
||
452 | } |
||
453 | 18 | cordinyana | |
454 | 1305 | jjdelcerro | @Override
|
455 | 1118 | jjdelcerro | public Class getClassOfValue() { |
456 | 1405 | jjdelcerro | return this.valueType.getClassOfValue(); |
457 | 1118 | jjdelcerro | } |
458 | 18 | cordinyana | |
459 | 1305 | jjdelcerro | @Override
|
460 | 1118 | jjdelcerro | public DynField setClassOfValue(Class theClass) { |
461 | 1405 | jjdelcerro | this.valueType.setClassOfValue(theClass);
|
462 | 1118 | jjdelcerro | return this; |
463 | } |
||
464 | 18 | cordinyana | |
465 | 1305 | jjdelcerro | @Override
|
466 | 1118 | jjdelcerro | public DynField setClassOfValue(String theClassName) { |
467 | 1405 | jjdelcerro | this.valueType.setClassOfValue(theClassName);
|
468 | 1118 | jjdelcerro | return this; |
469 | } |
||
470 | 112 | jjdelcerro | |
471 | 1305 | jjdelcerro | @Override
|
472 | 1118 | jjdelcerro | public boolean isContainer() { |
473 | 1405 | jjdelcerro | if (valueType.getDataType() == null) { |
474 | 1118 | jjdelcerro | return false; |
475 | } |
||
476 | 1405 | jjdelcerro | return valueType.getDataType().isContainer();
|
477 | 1118 | jjdelcerro | } |
478 | 73 | jjdelcerro | |
479 | 1305 | jjdelcerro | @Override
|
480 | 1118 | jjdelcerro | public void validate(Object value) throws DynFieldValidateException { |
481 | Comparable v;
|
||
482 | 1405 | jjdelcerro | if (value == null) { |
483 | if (this.mandatory) { |
||
484 | 1118 | jjdelcerro | throw new DynFieldRequiredValueException(this, value); |
485 | } |
||
486 | return;
|
||
487 | } |
||
488 | 130 | jjdelcerro | |
489 | 1405 | jjdelcerro | switch (this.valueType.getType()) { |
490 | case DataTypes.BOOLEAN:
|
||
491 | if (!(value instanceof Boolean)) { |
||
492 | throw new DynFieldValidateException(value, this); |
||
493 | } |
||
494 | break;
|
||
495 | 130 | jjdelcerro | |
496 | 1405 | jjdelcerro | case DataTypes.DOUBLE:
|
497 | if (!(value instanceof Double)) { |
||
498 | throw new DynFieldValidateException(value, this); |
||
499 | } |
||
500 | break;
|
||
501 | 73 | jjdelcerro | |
502 | 1405 | jjdelcerro | case DataTypes.FLOAT:
|
503 | if (!(value instanceof Float)) { |
||
504 | throw new DynFieldValidateException(value, this); |
||
505 | } |
||
506 | break;
|
||
507 | 130 | jjdelcerro | |
508 | 1405 | jjdelcerro | case DataTypes.BYTE:
|
509 | if (!(value instanceof Byte)) { |
||
510 | throw new DynFieldValidateException(value, this); |
||
511 | } |
||
512 | break;
|
||
513 | 130 | jjdelcerro | |
514 | 1405 | jjdelcerro | case DataTypes.INT:
|
515 | if (!(value instanceof Integer)) { |
||
516 | throw new DynFieldValidateException(value, this); |
||
517 | } |
||
518 | break;
|
||
519 | 130 | jjdelcerro | |
520 | 1405 | jjdelcerro | case DataTypes.LONG:
|
521 | if (!(value instanceof Long)) { |
||
522 | throw new DynFieldValidateException(value, this); |
||
523 | } |
||
524 | break;
|
||
525 | 130 | jjdelcerro | |
526 | 1405 | jjdelcerro | case DataTypes.STRING:
|
527 | if (!(value instanceof String)) { |
||
528 | throw new DynFieldValidateException(value, this); |
||
529 | } |
||
530 | break;
|
||
531 | 130 | jjdelcerro | |
532 | 1405 | jjdelcerro | case DataTypes.CHAR:
|
533 | if (!(value instanceof String)) { |
||
534 | throw new DynFieldValidateException(value, this); |
||
535 | } |
||
536 | if (((String) value).length() > 1) { |
||
537 | throw new DynFieldValidateException(value, this); |
||
538 | } |
||
539 | break;
|
||
540 | 130 | jjdelcerro | |
541 | 1405 | jjdelcerro | case DataTypes.DATE:
|
542 | if (!(value instanceof Date)) { |
||
543 | throw new DynFieldValidateException(value, this); |
||
544 | } |
||
545 | break;
|
||
546 | 130 | jjdelcerro | |
547 | 1405 | jjdelcerro | case DataTypes.TIMESTAMP:
|
548 | if (!(value instanceof Date)) { |
||
549 | throw new DynFieldValidateException(value, this); |
||
550 | } |
||
551 | break;
|
||
552 | 130 | jjdelcerro | |
553 | 1405 | jjdelcerro | case DataTypes.TIME:
|
554 | if (!(value instanceof Date)) { |
||
555 | throw new DynFieldValidateException(value, this); |
||
556 | } |
||
557 | break;
|
||
558 | 130 | jjdelcerro | |
559 | 1405 | jjdelcerro | case DataTypes.FILE:
|
560 | if (!(value instanceof File)) { |
||
561 | throw new DynFieldValidateException(value, this); |
||
562 | } |
||
563 | break;
|
||
564 | case DataTypes.FOLDER:
|
||
565 | if (!(value instanceof File)) { |
||
566 | throw new DynFieldValidateException(value, this); |
||
567 | } |
||
568 | break;
|
||
569 | case DataTypes.URI:
|
||
570 | if (!(value instanceof URI)) { |
||
571 | throw new DynFieldValidateException(value, this); |
||
572 | } |
||
573 | break;
|
||
574 | case DataTypes.URL:
|
||
575 | if (!(value instanceof URL)) { |
||
576 | throw new DynFieldValidateException(value, this); |
||
577 | } |
||
578 | break;
|
||
579 | 73 | jjdelcerro | |
580 | 1405 | jjdelcerro | case DataTypes.ARRAY:
|
581 | // TODO: falta verificar que es un array del tipo que toca.
|
||
582 | break;
|
||
583 | 914 | jjdelcerro | |
584 | 1405 | jjdelcerro | case DataTypes.OBJECT:
|
585 | if (this.valueType.getClassOfValue() != null) { |
||
586 | if (!this.valueType.getClassOfValue().isInstance(value)) { |
||
587 | throw new DynFieldValidateException(value, this); |
||
588 | } |
||
589 | } |
||
590 | break;
|
||
591 | |||
592 | case DataTypes.MAP:
|
||
593 | if (!(value instanceof Map)) { |
||
594 | 1118 | jjdelcerro | throw new DynFieldValidateException(value, this); |
595 | } |
||
596 | 1405 | jjdelcerro | validateCollection(value); |
597 | break;
|
||
598 | 73 | jjdelcerro | |
599 | 1405 | jjdelcerro | case DataTypes.SET:
|
600 | if (!(value instanceof Set)) { |
||
601 | throw new DynFieldValidateException(value, this); |
||
602 | } |
||
603 | validateCollection(value); |
||
604 | break;
|
||
605 | 914 | jjdelcerro | |
606 | 1405 | jjdelcerro | case DataTypes.LIST:
|
607 | if (!(value instanceof List)) { |
||
608 | throw new DynFieldValidateException(value, this); |
||
609 | } |
||
610 | validateCollection(value); |
||
611 | break;
|
||
612 | 1119 | jjdelcerro | |
613 | 1405 | jjdelcerro | case DataTypes.DYNOBJECT:
|
614 | if (!(value instanceof DynObject)) { |
||
615 | throw new DynFieldValidateException(value, this); |
||
616 | } |
||
617 | if (!this.getDynClassOfValue().isInstance((DynObject) value)) { |
||
618 | throw new DynFieldValidateException(value, this); |
||
619 | } |
||
620 | try {
|
||
621 | this.getDynClassOfValue().validate((DynObject) value);
|
||
622 | } catch (DynObjectValidateException e) {
|
||
623 | throw new DynFieldValidateException(value, this, e); |
||
624 | } |
||
625 | break;
|
||
626 | 914 | jjdelcerro | |
627 | 1405 | jjdelcerro | default:
|
628 | if (this.valueType.getDataType().isObject()) { |
||
629 | if (this.valueType.getClassOfValue() != null) { |
||
630 | if (!this.valueType.getClassOfValue().isInstance(value)) { |
||
631 | throw new DynFieldValidateException(value, this); |
||
632 | } |
||
633 | 1119 | jjdelcerro | } |
634 | } |
||
635 | 1118 | jjdelcerro | } |
636 | 259 | jjdelcerro | |
637 | 1405 | jjdelcerro | if (this.getAvailableValues() != null) { |
638 | if (!(value instanceof Comparable)) { |
||
639 | 1118 | jjdelcerro | throw new DynFieldValidateException(value, this); |
640 | } |
||
641 | v = (Comparable) value;
|
||
642 | boolean ok = false; |
||
643 | 1305 | jjdelcerro | for (DynObjectValueItem availableValue : this.availableValues) { |
644 | if (v.compareTo(availableValue.getValue()) == 0) { |
||
645 | 1118 | jjdelcerro | ok = true;
|
646 | break;
|
||
647 | } |
||
648 | } |
||
649 | 1405 | jjdelcerro | if (!ok) {
|
650 | 1118 | jjdelcerro | throw new DynFieldValidateException(value, this); |
651 | } |
||
652 | 1405 | jjdelcerro | } else if (this.getMaxValue() != null && this.getMinValue() != null) { |
653 | if (!(value instanceof Comparable)) { |
||
654 | 1118 | jjdelcerro | throw new DynFieldValidateException(value, this); |
655 | } |
||
656 | v = (Comparable) value;
|
||
657 | 1405 | jjdelcerro | if (v.compareTo(this.minValue) < 0 |
658 | || v.compareTo(this.maxValue) > 0) { |
||
659 | 1118 | jjdelcerro | throw new DynFieldValidateException(value, this); |
660 | } |
||
661 | } |
||
662 | } |
||
663 | 130 | jjdelcerro | |
664 | 1118 | jjdelcerro | private void validateCollection(Object value) throws ValidateItemException { |
665 | 1405 | jjdelcerro | if (this.validateItems) { |
666 | 1118 | jjdelcerro | DynStruct dynClass = this.itemsType.getDynClassOfValue();
|
667 | 1405 | jjdelcerro | if (dynClass != null) { |
668 | 1118 | jjdelcerro | int index = 0; |
669 | Iterator it = ((Collection) value).iterator(); |
||
670 | 1405 | jjdelcerro | while (it.hasNext()) {
|
671 | 1118 | jjdelcerro | try {
|
672 | dynClass.validate((DynObject) it.next()); |
||
673 | index++; |
||
674 | } catch (DynObjectValidateException ex) {
|
||
675 | throw new ValidateItemException(ex, index); |
||
676 | } |
||
677 | } |
||
678 | } |
||
679 | } |
||
680 | 130 | jjdelcerro | |
681 | 1118 | jjdelcerro | } |
682 | 130 | jjdelcerro | |
683 | 1118 | jjdelcerro | private static class ValidateItemException extends DynFieldValidateException { |
684 | 73 | jjdelcerro | |
685 | 1118 | jjdelcerro | private static final long serialVersionUID = 9011437364983996567L; |
686 | 627 | cmartin | |
687 | 1118 | jjdelcerro | ValidateItemException(Throwable cause, int index) { |
688 | super(
|
||
689 | "Can't validate item %(index) of the collection.",
|
||
690 | cause, |
||
691 | "_Cant_validate_item_%(index)_of_the_collection",
|
||
692 | serialVersionUID |
||
693 | ); |
||
694 | 1335 | jjdelcerro | setValue("index", index);
|
695 | 1118 | jjdelcerro | } |
696 | } |
||
697 | 73 | jjdelcerro | |
698 | 1305 | jjdelcerro | @Override
|
699 | 1118 | jjdelcerro | public Object coerce(Object value) throws CoercionException { |
700 | 1405 | jjdelcerro | if (value == null) { |
701 | 1118 | jjdelcerro | return value; // O debe devolver this.defaultValue |
702 | } |
||
703 | 1119 | jjdelcerro | try {
|
704 | 1405 | jjdelcerro | return this.valueType.getDataType().coerce(value); |
705 | } catch (Exception ex) { |
||
706 | 1119 | jjdelcerro | throw new RuntimeException(ex); |
707 | } |
||
708 | 1118 | jjdelcerro | } |
709 | 73 | jjdelcerro | |
710 | 1305 | jjdelcerro | @Override
|
711 | 1118 | jjdelcerro | public String getGroup() { |
712 | return this.groupName; |
||
713 | } |
||
714 | 109 | jjdelcerro | |
715 | 1305 | jjdelcerro | @Override
|
716 | 1118 | jjdelcerro | public DynField setGroup(String groupName) { |
717 | this.groupName = groupName;
|
||
718 | return this; |
||
719 | } |
||
720 | 109 | jjdelcerro | |
721 | 1305 | jjdelcerro | @Override
|
722 | 1118 | jjdelcerro | public int getOder() { |
723 | return this.order; |
||
724 | } |
||
725 | 109 | jjdelcerro | |
726 | 1305 | jjdelcerro | @Override
|
727 | 1118 | jjdelcerro | public DynField setOrder(int order) { |
728 | this.order = order;
|
||
729 | return this; |
||
730 | } |
||
731 | 109 | jjdelcerro | |
732 | 1305 | jjdelcerro | @Override
|
733 | 1118 | jjdelcerro | public boolean isHidden() { |
734 | return this.hidden; |
||
735 | } |
||
736 | 120 | jjdelcerro | |
737 | 1305 | jjdelcerro | @Override
|
738 | 1118 | jjdelcerro | public DynField setHidden(boolean hidden) { |
739 | this.hidden = hidden;
|
||
740 | return this; |
||
741 | } |
||
742 | 627 | cmartin | |
743 | 1305 | jjdelcerro | @Override
|
744 | 1118 | jjdelcerro | public boolean isReadOnly() { |
745 | return this.isReadOnly; |
||
746 | } |
||
747 | 141 | cmartin | |
748 | 1305 | jjdelcerro | @Override
|
749 | 1118 | jjdelcerro | public DynField setReadOnly(boolean isReadOnly) { |
750 | this.isReadOnly = isReadOnly;
|
||
751 | return this; |
||
752 | } |
||
753 | 141 | cmartin | |
754 | 1305 | jjdelcerro | @Override
|
755 | 1118 | jjdelcerro | public DynField setDefaultFieldValue(Object defaultValue) { |
756 | try {
|
||
757 | this.defaultValue = this.coerce(defaultValue); |
||
758 | } catch (CoercionException e) {
|
||
759 | 1305 | jjdelcerro | throw new IllegalArgumentException(e); |
760 | 1118 | jjdelcerro | } |
761 | return this; |
||
762 | } |
||
763 | 141 | cmartin | |
764 | 1305 | jjdelcerro | @Override
|
765 | 1118 | jjdelcerro | public Tags getTags() {
|
766 | return tags;
|
||
767 | } |
||
768 | 212 | jjdelcerro | |
769 | 1305 | jjdelcerro | @Override
|
770 | 1118 | jjdelcerro | public String getClassNameOfValue() { |
771 | 1405 | jjdelcerro | return this.valueType.getClassNameOfValue(); |
772 | 1118 | jjdelcerro | } |
773 | 212 | jjdelcerro | |
774 | 1305 | jjdelcerro | @Override
|
775 | 1118 | jjdelcerro | public DynField setClassOfValue(DynStruct dynStruct) {
|
776 | 1405 | jjdelcerro | this.valueType.setClassOfValue(dynStruct);
|
777 | 1118 | jjdelcerro | return this; |
778 | } |
||
779 | 1031 | jbadia | |
780 | 1305 | jjdelcerro | @Override
|
781 | 1118 | jjdelcerro | public DynStruct getDynClassOfValue() {
|
782 | 1405 | jjdelcerro | return this.valueType.getDynClassOfValue(); |
783 | 1118 | jjdelcerro | } |
784 | 1085 | jjdelcerro | |
785 | 1305 | jjdelcerro | @Override
|
786 | 1085 | jjdelcerro | public int getRelationType() { |
787 | return this.relationType; |
||
788 | } |
||
789 | |||
790 | 1305 | jjdelcerro | @Override
|
791 | 1085 | jjdelcerro | public DynField setRelationType(int relationType) { |
792 | this.relationType = relationType;
|
||
793 | return this; |
||
794 | } |
||
795 | |||
796 | 1305 | jjdelcerro | @Override
|
797 | 1118 | jjdelcerro | public DynField setElementsType(int type) { |
798 | this.setTypeOfItems(type);
|
||
799 | return this; |
||
800 | } |
||
801 | |||
802 | 1305 | jjdelcerro | @Override
|
803 | 1118 | jjdelcerro | public DynField setElementsType(DynStruct type) {
|
804 | this.setClassOfItems(type);
|
||
805 | return this; |
||
806 | } |
||
807 | |||
808 | 1305 | jjdelcerro | @Override
|
809 | 1118 | jjdelcerro | public DynField getElementsType() {
|
810 | throw new UnsupportedOperationException("This operation is not suported nevermore."); |
||
811 | } |
||
812 | |||
813 | 1305 | jjdelcerro | @Override
|
814 | 1118 | jjdelcerro | public DynField setClassOfItems(DynStruct dynStrct) {
|
815 | 1860 | jjdelcerro | if (dynStrct!=null && !this.isContainer()) { |
816 | 1405 | jjdelcerro | throw new IllegalStateException("Can't assign classOfItems in non container."); |
817 | 1118 | jjdelcerro | } |
818 | this.itemsType.setClassOfValue(dynStrct);
|
||
819 | return this; |
||
820 | } |
||
821 | |||
822 | 1305 | jjdelcerro | @Override
|
823 | 1118 | jjdelcerro | public DynField setClassOfItems(String theClassNameOfValue) { |
824 | 1860 | jjdelcerro | if (theClassNameOfValue!=null && !this.isContainer()) { |
825 | 1405 | jjdelcerro | throw new IllegalStateException("Can't assign classOfItems in non container."); |
826 | 1118 | jjdelcerro | } |
827 | this.itemsType.setClassOfValue(theClassNameOfValue);
|
||
828 | return this; |
||
829 | } |
||
830 | |||
831 | 1305 | jjdelcerro | @Override
|
832 | 1118 | jjdelcerro | public String getClassNameOfItems() { |
833 | 1860 | jjdelcerro | if( this.itemsType==null || |
834 | this.itemsType.getClassNameOfValue()==null ) { |
||
835 | return null; |
||
836 | } |
||
837 | 1405 | jjdelcerro | if (!this.isContainer()) { |
838 | throw new IllegalStateException("Can't get classNameOfItems in non container."); |
||
839 | 1118 | jjdelcerro | } |
840 | return this.itemsType.getClassNameOfValue(); |
||
841 | } |
||
842 | |||
843 | 1305 | jjdelcerro | @Override
|
844 | 1118 | jjdelcerro | public DynStruct getDynClassOfItems() {
|
845 | 1860 | jjdelcerro | if( this.itemsType==null || |
846 | this.itemsType.getDynClassOfValue()==null ) { |
||
847 | return null; |
||
848 | } |
||
849 | 1405 | jjdelcerro | if (!this.isContainer()) { |
850 | throw new IllegalStateException("Can't get dynClassOfItems in non container."); |
||
851 | 1118 | jjdelcerro | } |
852 | return this.itemsType.getDynClassOfValue(); |
||
853 | } |
||
854 | |||
855 | 1305 | jjdelcerro | @Override
|
856 | 1118 | jjdelcerro | public DynField setClassOfItems(Class theClass) |
857 | throws DynFieldIsNotAContainerException {
|
||
858 | 1860 | jjdelcerro | if (theClass!=null && !this.isContainer()) { |
859 | 1405 | jjdelcerro | throw new IllegalStateException("Can't assign classNameOfItems in non container."); |
860 | 1118 | jjdelcerro | } |
861 | this.itemsType.setClassOfValue(theClass);
|
||
862 | return this; |
||
863 | } |
||
864 | |||
865 | 1305 | jjdelcerro | @Override
|
866 | 1118 | jjdelcerro | public Class getClassOfItems() { |
867 | 1860 | jjdelcerro | if( this.itemsType==null || |
868 | this.itemsType.getClassOfValue()==null ) { |
||
869 | return null; |
||
870 | } |
||
871 | 1405 | jjdelcerro | if (!this.isContainer()) { |
872 | throw new IllegalStateException("Can't get classOfItems in non container."); |
||
873 | 1118 | jjdelcerro | } |
874 | return this.itemsType.getClassOfValue(); |
||
875 | } |
||
876 | |||
877 | 1305 | jjdelcerro | @Override
|
878 | 1118 | jjdelcerro | public DynField setTypeOfItems(int type) { |
879 | 1405 | jjdelcerro | if (!this.isContainer()) { |
880 | throw new IllegalStateException("Can't assign typeOfItems in non container."); |
||
881 | 1118 | jjdelcerro | } |
882 | this.itemsType.setType(type);
|
||
883 | return this; |
||
884 | } |
||
885 | |||
886 | 1305 | jjdelcerro | @Override
|
887 | 1118 | jjdelcerro | public int getTypeOfItems() { |
888 | 1405 | jjdelcerro | if (!this.isContainer()) { |
889 | throw new IllegalStateException("Can't get typeOfItems in non container."); |
||
890 | 1118 | jjdelcerro | } |
891 | return this.itemsType.getType(); |
||
892 | } |
||
893 | |||
894 | 1405 | jjdelcerro | @Override
|
895 | 1118 | jjdelcerro | public DynField setValidateElements(boolean validate) { |
896 | 1405 | jjdelcerro | if (!this.isContainer()) { |
897 | 1118 | jjdelcerro | throw new IllegalStateException("Can't assign validateElements in non container."); |
898 | } |
||
899 | this.validateItems = validate;
|
||
900 | return this; |
||
901 | } |
||
902 | |||
903 | 1405 | jjdelcerro | @Override
|
904 | 1118 | jjdelcerro | public boolean getValidateElements() { |
905 | 1405 | jjdelcerro | if (!this.isContainer()) { |
906 | throw new IllegalStateException("Can't get validateElements in non container."); |
||
907 | 1118 | jjdelcerro | } |
908 | return this.validateItems; |
||
909 | } |
||
910 | |||
911 | 1405 | jjdelcerro | @Override
|
912 | public Object clone() throws CloneNotSupportedException { |
||
913 | DefaultDynField other = (DefaultDynField) super.clone();
|
||
914 | other.tags = (Tags) this.tags.clone();
|
||
915 | other.valueType = (ValueType) this.valueType.clone();
|
||
916 | other.itemsType = (ValueType) this.itemsType.clone();
|
||
917 | if( this.availableValues==null ) { |
||
918 | other.availableValues = null;
|
||
919 | } else {
|
||
920 | other.availableValues = this.availableValues.clone();
|
||
921 | } |
||
922 | other.calculate = (DynMethod) this.calculate;
|
||
923 | return other;
|
||
924 | } |
||
925 | |||
926 | 1118 | jjdelcerro | } |