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