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 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* 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 |
*
|
11 |
* 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 |
*
|
16 |
* 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 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.tools.dynobject.impl; |
24 |
|
25 |
import java.io.File; |
26 |
import java.net.URI; |
27 |
import java.net.URL; |
28 |
import java.util.Collection; |
29 |
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 |
import org.apache.commons.lang3.StringUtils; |
35 |
|
36 |
import org.gvsig.tools.ToolsLocator; |
37 |
import org.gvsig.tools.dataTypes.CoercionException; |
38 |
import org.gvsig.tools.dataTypes.DataType; |
39 |
import org.gvsig.tools.dataTypes.DataTypes; |
40 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
41 |
import org.gvsig.tools.dynobject.DynField; |
42 |
import org.gvsig.tools.dynobject.DynField_v2; |
43 |
import org.gvsig.tools.dynobject.DynMethod; |
44 |
import org.gvsig.tools.dynobject.DynObject; |
45 |
import org.gvsig.tools.dynobject.DynObjectValueItem; |
46 |
import org.gvsig.tools.dynobject.DynStruct; |
47 |
import org.gvsig.tools.dynobject.Tags; |
48 |
import org.gvsig.tools.dynobject.exception.DynFieldIsNotAContainerException; |
49 |
import org.gvsig.tools.dynobject.exception.DynFieldRequiredValueException; |
50 |
import org.gvsig.tools.dynobject.exception.DynFieldValidateException; |
51 |
import org.gvsig.tools.dynobject.exception.DynMethodException; |
52 |
import org.gvsig.tools.dynobject.exception.DynObjectValidateException; |
53 |
import org.gvsig.tools.exception.ListBaseException; |
54 |
import org.gvsig.tools.i18n.I18nManager; |
55 |
import org.slf4j.Logger; |
56 |
import org.slf4j.LoggerFactory; |
57 |
|
58 |
@SuppressWarnings("EqualsAndHashcode") |
59 |
public class DefaultDynField implements DynField_v2 { |
60 |
|
61 |
public static final Logger log = LoggerFactory.getLogger(DefaultDynField.class); |
62 |
|
63 |
private String name; |
64 |
private String description; |
65 |
|
66 |
private ValueType valueType;
|
67 |
|
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 |
private DynMethod availableValuesMethod = null; |
77 |
|
78 |
private Object minValue; |
79 |
private Object maxValue; |
80 |
private boolean mandatory; |
81 |
private boolean persistent; |
82 |
private String label = null; |
83 |
private String shortLabel = null; |
84 |
|
85 |
private boolean isReadOnly; |
86 |
private Tags tags = new DefaultTags(); |
87 |
|
88 |
private ValueType itemsType;
|
89 |
private boolean validateItems; |
90 |
|
91 |
// 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 |
// - 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 |
// Y solo tendra efecto/sentido cuando el field sea de tipo
|
99 |
// DynObject, y tal vez lista de DynObject
|
100 |
private int relationType; |
101 |
private DynMethod calculate = null; |
102 |
|
103 |
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 |
|
112 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
113 |
protected DefaultDynField(String name, int dataType, Object defaultValue, |
114 |
boolean persistent, boolean mandatory) { |
115 |
DataTypesManager datamanager = ToolsLocator.getDataTypesManager(); |
116 |
|
117 |
if (StringUtils.isBlank(name)) {
|
118 |
throw new IllegalArgumentException("name can't be null"); |
119 |
} |
120 |
this.name = name;
|
121 |
this.valueType = new ValueType(dataType); |
122 |
this.subtype = this.getSubtype(); |
123 |
|
124 |
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 |
|
132 |
this.itemsType = new ValueType(DataTypes.UNKNOWN); |
133 |
this.validateItems = false; |
134 |
this.relationType = RELATION_TYPE_NONE;
|
135 |
} |
136 |
|
137 |
@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 |
DynObjectValueItem[] x = other.getAvailableValues();
|
167 |
if( x != null ) { |
168 |
this.availableValues = x.clone();
|
169 |
} |
170 |
} |
171 |
|
172 |
this.setType(other.getDataType());
|
173 |
this.setClassOfValue(other2.getClassOfValue());
|
174 |
this.setClassOfValue(other2.getDynClassOfValue());
|
175 |
|
176 |
this.itemsType.setType(other2.getTypeOfItems());
|
177 |
this.itemsType.setClassOfValue(other2.getClassOfItems());
|
178 |
this.itemsType.setClassOfValue(other2.getDynClassOfItems());
|
179 |
} |
180 |
|
181 |
} |
182 |
|
183 |
protected ValueType getValueType() {
|
184 |
return valueType;
|
185 |
} |
186 |
|
187 |
@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 |
public void check() throws ListBaseException { |
213 |
} |
214 |
|
215 |
@Override
|
216 |
public String toString() { |
217 |
StringBuilder buffer = new StringBuilder(); |
218 |
|
219 |
buffer.append("DynField").append("[").append(this.hashCode()) |
220 |
.append("]").append("( ").append("name='").append(this.name) |
221 |
.append("', ").append("description='").append(this.description) |
222 |
.append("', ").append("type='").append(this.valueType) |
223 |
.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 |
|
234 |
@Override
|
235 |
public String getName() { |
236 |
return name;
|
237 |
} |
238 |
|
239 |
public DynField setName(String name) { |
240 |
if (StringUtils.isBlank(name)) {
|
241 |
throw new IllegalArgumentException("name can't be null"); |
242 |
} |
243 |
this.name = name;
|
244 |
return this; |
245 |
} |
246 |
|
247 |
@Override
|
248 |
public DynField setDescription(String description) { |
249 |
this.description = description;
|
250 |
return this; |
251 |
} |
252 |
|
253 |
@Override
|
254 |
public String getDescription() { |
255 |
return (description == null) ? getLabel() : description; |
256 |
} |
257 |
|
258 |
@Override
|
259 |
public DynField setLabel(String label) { |
260 |
this.label = label;
|
261 |
return this; |
262 |
} |
263 |
|
264 |
@Override
|
265 |
public String getLabel() { |
266 |
return StringUtils.isBlank(label) ? getName() : label;
|
267 |
} |
268 |
|
269 |
@Override
|
270 |
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 |
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 |
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 |
public DynField setType(int dataType) { |
300 |
this.valueType.setType(dataType);
|
301 |
return this; |
302 |
} |
303 |
|
304 |
@Override
|
305 |
public DynField setType(DataType dataType) {
|
306 |
this.valueType.setType(dataType);
|
307 |
return this; |
308 |
} |
309 |
|
310 |
@Override
|
311 |
public int getType() { |
312 |
return this.valueType.getType(); |
313 |
} |
314 |
|
315 |
@Override
|
316 |
public DataType getDataType() {
|
317 |
return this.valueType.getDataType(); |
318 |
} |
319 |
|
320 |
@Override
|
321 |
public DynField setSubtype(String subtype) { |
322 |
this.subtype = subtype;
|
323 |
return this; |
324 |
} |
325 |
|
326 |
@Override
|
327 |
public String getSubtype() { |
328 |
return subtype;
|
329 |
} |
330 |
|
331 |
@Override
|
332 |
public DynField setDefaultDynValue(Object defaultValue) { |
333 |
this.defaultValue = defaultValue;
|
334 |
return this; |
335 |
} |
336 |
|
337 |
@Override
|
338 |
public Object getDefaultValue() { |
339 |
return defaultValue;
|
340 |
} |
341 |
|
342 |
@Override
|
343 |
public boolean isAvailableValuesCalculated() { |
344 |
return this.availableValuesMethod != null; |
345 |
} |
346 |
|
347 |
@Override
|
348 |
public DynField setAvailableValues(DynObjectValueItem[] availableValues) { |
349 |
if (availableValues == null || availableValues.length == 0) { |
350 |
this.availableValues = null; |
351 |
} else {
|
352 |
this.availableValues = availableValues;
|
353 |
} |
354 |
return this; |
355 |
} |
356 |
|
357 |
@Override
|
358 |
public DynField setAvailableValues(List availableValues) { |
359 |
if (availableValues == null) { |
360 |
this.availableValues = null; |
361 |
} else if (availableValues.isEmpty()) { |
362 |
this.availableValues = null; |
363 |
} else {
|
364 |
this.availableValues = (DynObjectValueItem[]) availableValues |
365 |
.toArray(new DynObjectValueItem[availableValues.size()]);
|
366 |
} |
367 |
return this; |
368 |
} |
369 |
|
370 |
@Override
|
371 |
public DynField setAvailableValues(DynMethod computeAvailableValues) {
|
372 |
this.availableValuesMethod = computeAvailableValues;
|
373 |
return this; |
374 |
} |
375 |
|
376 |
@Override
|
377 |
public DynMethod getAvailableValuesMethod() {
|
378 |
return this.availableValuesMethod; |
379 |
} |
380 |
|
381 |
|
382 |
@Override
|
383 |
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 |
return values;
|
398 |
} |
399 |
} |
400 |
return this.availableValues; |
401 |
} |
402 |
|
403 |
@Override
|
404 |
public DynField setMinValue(Object minValue) { |
405 |
try {
|
406 |
this.minValue = this.coerce(minValue); |
407 |
} catch (CoercionException e) {
|
408 |
throw new IllegalArgumentException(); |
409 |
} |
410 |
return this; |
411 |
} |
412 |
|
413 |
@Override
|
414 |
public Object getMinValue() { |
415 |
return minValue;
|
416 |
} |
417 |
|
418 |
@Override
|
419 |
public DynField setMaxValue(Object maxValue) { |
420 |
try {
|
421 |
this.maxValue = this.coerce(maxValue); |
422 |
} catch (CoercionException e) {
|
423 |
throw new IllegalArgumentException(e); |
424 |
} |
425 |
return this; |
426 |
} |
427 |
|
428 |
@Override
|
429 |
public Object getMaxValue() { |
430 |
return maxValue;
|
431 |
} |
432 |
|
433 |
@Override
|
434 |
public boolean isMandatory() { |
435 |
return this.mandatory; |
436 |
} |
437 |
|
438 |
@Override
|
439 |
public boolean isPersistent() { |
440 |
if (this.isCalculated()) { |
441 |
return false; |
442 |
} |
443 |
return this.persistent; |
444 |
} |
445 |
|
446 |
@Override
|
447 |
public DynField setMandatory(boolean mandatory) { |
448 |
this.mandatory = mandatory;
|
449 |
return this; |
450 |
} |
451 |
|
452 |
@Override
|
453 |
public DynField setPersistent(boolean persistent) { |
454 |
if (this.isCalculated()) { |
455 |
persistent = false;
|
456 |
} |
457 |
this.persistent = persistent;
|
458 |
return this; |
459 |
} |
460 |
|
461 |
@Override
|
462 |
public DynField setTheTypeOfAvailableValues(int type) { |
463 |
return this; // FIXME: this method is @deprecated |
464 |
} |
465 |
|
466 |
@Override
|
467 |
public int getTheTypeOfAvailableValues() { |
468 |
return 1; // FIXME: this method is @deprecated |
469 |
} |
470 |
|
471 |
/**
|
472 |
*
|
473 |
* @param obj
|
474 |
* @return
|
475 |
*/
|
476 |
@Override
|
477 |
public boolean equals(Object obj) { |
478 |
if (this == obj) { |
479 |
return true; |
480 |
} |
481 |
if (obj instanceof DynField) { |
482 |
// FIXME: No esta claro que esto sea correcto.
|
483 |
return name.equals(((DynField) obj).getName());
|
484 |
} |
485 |
return false; |
486 |
} |
487 |
|
488 |
@Override
|
489 |
public Class getClassOfValue() { |
490 |
return this.valueType.getClassOfValue(); |
491 |
} |
492 |
|
493 |
@Override
|
494 |
public DynField setClassOfValue(Class theClass) { |
495 |
this.valueType.setClassOfValue(theClass);
|
496 |
return this; |
497 |
} |
498 |
|
499 |
@Override
|
500 |
public DynField setClassOfValue(String theClassName) { |
501 |
this.valueType.setClassOfValue(theClassName);
|
502 |
return this; |
503 |
} |
504 |
|
505 |
@Override
|
506 |
public boolean isContainer() { |
507 |
if (valueType.getDataType() == null) { |
508 |
return false; |
509 |
} |
510 |
return valueType.getDataType().isContainer();
|
511 |
} |
512 |
|
513 |
@Override
|
514 |
public void validate(Object value) throws DynFieldValidateException { |
515 |
Comparable v;
|
516 |
if (value == null) { |
517 |
if (this.mandatory) { |
518 |
throw new DynFieldRequiredValueException(this, value); |
519 |
} |
520 |
return;
|
521 |
} |
522 |
|
523 |
switch (this.valueType.getType()) { |
524 |
case DataTypes.BOOLEAN:
|
525 |
if (!(value instanceof Boolean)) { |
526 |
throw new DynFieldValidateException(value, this); |
527 |
} |
528 |
break;
|
529 |
|
530 |
case DataTypes.DOUBLE:
|
531 |
if (!(value instanceof Double)) { |
532 |
throw new DynFieldValidateException(value, this); |
533 |
} |
534 |
break;
|
535 |
|
536 |
case DataTypes.FLOAT:
|
537 |
if (!(value instanceof Float)) { |
538 |
throw new DynFieldValidateException(value, this); |
539 |
} |
540 |
break;
|
541 |
|
542 |
case DataTypes.BYTE:
|
543 |
if (!(value instanceof Byte)) { |
544 |
throw new DynFieldValidateException(value, this); |
545 |
} |
546 |
break;
|
547 |
|
548 |
case DataTypes.INT:
|
549 |
if (!(value instanceof Integer)) { |
550 |
throw new DynFieldValidateException(value, this); |
551 |
} |
552 |
break;
|
553 |
|
554 |
case DataTypes.LONG:
|
555 |
if (!(value instanceof Long)) { |
556 |
throw new DynFieldValidateException(value, this); |
557 |
} |
558 |
break;
|
559 |
|
560 |
case DataTypes.STRING:
|
561 |
if (!(value instanceof String)) { |
562 |
throw new DynFieldValidateException(value, this); |
563 |
} |
564 |
break;
|
565 |
|
566 |
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 |
|
575 |
case DataTypes.DATE:
|
576 |
if (!(value instanceof Date)) { |
577 |
throw new DynFieldValidateException(value, this); |
578 |
} |
579 |
break;
|
580 |
|
581 |
case DataTypes.TIMESTAMP:
|
582 |
if (!(value instanceof Date)) { |
583 |
throw new DynFieldValidateException(value, this); |
584 |
} |
585 |
break;
|
586 |
|
587 |
case DataTypes.TIME:
|
588 |
if (!(value instanceof Date)) { |
589 |
throw new DynFieldValidateException(value, this); |
590 |
} |
591 |
break;
|
592 |
|
593 |
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 |
|
614 |
case DataTypes.ARRAY:
|
615 |
// TODO: falta verificar que es un array del tipo que toca.
|
616 |
break;
|
617 |
|
618 |
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 |
throw new DynFieldValidateException(value, this); |
629 |
} |
630 |
validateCollection(value); |
631 |
break;
|
632 |
|
633 |
case DataTypes.SET:
|
634 |
if (!(value instanceof Set)) { |
635 |
throw new DynFieldValidateException(value, this); |
636 |
} |
637 |
validateCollection(value); |
638 |
break;
|
639 |
|
640 |
case DataTypes.LIST:
|
641 |
if (!(value instanceof List)) { |
642 |
throw new DynFieldValidateException(value, this); |
643 |
} |
644 |
validateCollection(value); |
645 |
break;
|
646 |
|
647 |
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 |
|
661 |
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 |
} |
668 |
} |
669 |
} |
670 |
|
671 |
if (this.getAvailableValues() != null) { |
672 |
if (!(value instanceof Comparable)) { |
673 |
throw new DynFieldValidateException(value, this); |
674 |
} |
675 |
v = (Comparable) value;
|
676 |
boolean ok = false; |
677 |
for (DynObjectValueItem availableValue : this.availableValues) { |
678 |
if (v.compareTo(availableValue.getValue()) == 0) { |
679 |
ok = true;
|
680 |
break;
|
681 |
} |
682 |
} |
683 |
if (!ok) {
|
684 |
throw new DynFieldValidateException(value, this); |
685 |
} |
686 |
} else if (this.getMaxValue() != null && this.getMinValue() != null) { |
687 |
if (!(value instanceof Comparable)) { |
688 |
throw new DynFieldValidateException(value, this); |
689 |
} |
690 |
v = (Comparable) value;
|
691 |
if (v.compareTo(this.minValue) < 0 |
692 |
|| v.compareTo(this.maxValue) > 0) { |
693 |
throw new DynFieldValidateException(value, this); |
694 |
} |
695 |
} |
696 |
} |
697 |
|
698 |
private void validateCollection(Object value) throws ValidateItemException { |
699 |
if (this.validateItems) { |
700 |
DynStruct dynClass = this.itemsType.getDynClassOfValue();
|
701 |
if (dynClass != null) { |
702 |
int index = 0; |
703 |
Iterator it = ((Collection) value).iterator(); |
704 |
while (it.hasNext()) {
|
705 |
try {
|
706 |
dynClass.validate((DynObject) it.next()); |
707 |
index++; |
708 |
} catch (DynObjectValidateException ex) {
|
709 |
throw new ValidateItemException(ex, index); |
710 |
} |
711 |
} |
712 |
} |
713 |
} |
714 |
|
715 |
} |
716 |
|
717 |
private static class ValidateItemException extends DynFieldValidateException { |
718 |
|
719 |
private static final long serialVersionUID = 9011437364983996567L; |
720 |
|
721 |
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 |
setValue("index", index);
|
729 |
} |
730 |
} |
731 |
|
732 |
@Override
|
733 |
public Object coerce(Object value) throws CoercionException { |
734 |
if (value == null) { |
735 |
return value; // O debe devolver this.defaultValue |
736 |
} |
737 |
try {
|
738 |
return this.valueType.getDataType().coerce(value); |
739 |
} catch (Exception ex) { |
740 |
throw new RuntimeException(ex); |
741 |
} |
742 |
} |
743 |
|
744 |
@Override
|
745 |
public String getGroup() { |
746 |
return this.groupName; |
747 |
} |
748 |
|
749 |
@Override
|
750 |
public DynField setGroup(String groupName) { |
751 |
this.groupName = groupName;
|
752 |
return this; |
753 |
} |
754 |
|
755 |
@Override
|
756 |
public int getOder() { |
757 |
return this.order; |
758 |
} |
759 |
|
760 |
@Override
|
761 |
public DynField setOrder(int order) { |
762 |
this.order = order;
|
763 |
return this; |
764 |
} |
765 |
|
766 |
@Override
|
767 |
public boolean isHidden() { |
768 |
return this.hidden; |
769 |
} |
770 |
|
771 |
@Override
|
772 |
public DynField setHidden(boolean hidden) { |
773 |
this.hidden = hidden;
|
774 |
return this; |
775 |
} |
776 |
|
777 |
@Override
|
778 |
public boolean isReadOnly() { |
779 |
return this.isReadOnly; |
780 |
} |
781 |
|
782 |
@Override
|
783 |
public DynField setReadOnly(boolean isReadOnly) { |
784 |
this.isReadOnly = isReadOnly;
|
785 |
return this; |
786 |
} |
787 |
|
788 |
@Override
|
789 |
public DynField setDefaultFieldValue(Object defaultValue) { |
790 |
try {
|
791 |
this.defaultValue = this.coerce(defaultValue); |
792 |
} catch (CoercionException e) {
|
793 |
throw new IllegalArgumentException(e); |
794 |
} |
795 |
return this; |
796 |
} |
797 |
|
798 |
@Override
|
799 |
public Tags getTags() {
|
800 |
return tags;
|
801 |
} |
802 |
|
803 |
@Override
|
804 |
public String getClassNameOfValue() { |
805 |
return this.valueType.getClassNameOfValue(); |
806 |
} |
807 |
|
808 |
@Override
|
809 |
public DynField setClassOfValue(DynStruct dynStruct) {
|
810 |
this.valueType.setClassOfValue(dynStruct);
|
811 |
return this; |
812 |
} |
813 |
|
814 |
@Override
|
815 |
public DynStruct getDynClassOfValue() {
|
816 |
return this.valueType.getDynClassOfValue(); |
817 |
} |
818 |
|
819 |
@Override
|
820 |
public int getRelationType() { |
821 |
return this.relationType; |
822 |
} |
823 |
|
824 |
@Override
|
825 |
public DynField setRelationType(int relationType) { |
826 |
this.relationType = relationType;
|
827 |
return this; |
828 |
} |
829 |
|
830 |
@Override
|
831 |
public DynField setElementsType(int type) { |
832 |
this.setTypeOfItems(type);
|
833 |
return this; |
834 |
} |
835 |
|
836 |
@Override
|
837 |
public DynField setElementsType(DynStruct type) {
|
838 |
this.setClassOfItems(type);
|
839 |
return this; |
840 |
} |
841 |
|
842 |
@Override
|
843 |
public DynField getElementsType() {
|
844 |
throw new UnsupportedOperationException("This operation is not suported nevermore."); |
845 |
} |
846 |
|
847 |
@Override
|
848 |
public DynField setClassOfItems(DynStruct dynStrct) {
|
849 |
if (dynStrct!=null && !this.isContainer()) { |
850 |
throw new IllegalStateException("Can't assign classOfItems in non container."); |
851 |
} |
852 |
this.itemsType.setClassOfValue(dynStrct);
|
853 |
return this; |
854 |
} |
855 |
|
856 |
@Override
|
857 |
public DynField setClassOfItems(String theClassNameOfValue) { |
858 |
if (theClassNameOfValue!=null && !this.isContainer()) { |
859 |
throw new IllegalStateException("Can't assign classOfItems in non container."); |
860 |
} |
861 |
this.itemsType.setClassOfValue(theClassNameOfValue);
|
862 |
return this; |
863 |
} |
864 |
|
865 |
@Override
|
866 |
public String getClassNameOfItems() { |
867 |
if( this.itemsType==null || |
868 |
this.itemsType.getClassNameOfValue()==null ) { |
869 |
return null; |
870 |
} |
871 |
if (!this.isContainer()) { |
872 |
throw new IllegalStateException("Can't get classNameOfItems in non container."); |
873 |
} |
874 |
return this.itemsType.getClassNameOfValue(); |
875 |
} |
876 |
|
877 |
@Override
|
878 |
public DynStruct getDynClassOfItems() {
|
879 |
if( this.itemsType==null || |
880 |
this.itemsType.getDynClassOfValue()==null ) { |
881 |
return null; |
882 |
} |
883 |
if (!this.isContainer()) { |
884 |
throw new IllegalStateException("Can't get dynClassOfItems in non container."); |
885 |
} |
886 |
return this.itemsType.getDynClassOfValue(); |
887 |
} |
888 |
|
889 |
@Override
|
890 |
public DynField setClassOfItems(Class theClass) |
891 |
throws DynFieldIsNotAContainerException {
|
892 |
if (theClass!=null && !this.isContainer()) { |
893 |
throw new IllegalStateException("Can't assign classNameOfItems in non container."); |
894 |
} |
895 |
this.itemsType.setClassOfValue(theClass);
|
896 |
return this; |
897 |
} |
898 |
|
899 |
@Override
|
900 |
public Class getClassOfItems() { |
901 |
if( this.itemsType==null || |
902 |
this.itemsType.getClassOfValue()==null ) { |
903 |
return null; |
904 |
} |
905 |
if (!this.isContainer()) { |
906 |
throw new IllegalStateException("Can't get classOfItems in non container."); |
907 |
} |
908 |
return this.itemsType.getClassOfValue(); |
909 |
} |
910 |
|
911 |
@Override
|
912 |
public DynField setTypeOfItems(int type) { |
913 |
if (!this.isContainer()) { |
914 |
throw new IllegalStateException("Can't assign typeOfItems in non container."); |
915 |
} |
916 |
this.itemsType.setType(type);
|
917 |
return this; |
918 |
} |
919 |
|
920 |
@Override
|
921 |
public int getTypeOfItems() { |
922 |
if (!this.isContainer()) { |
923 |
throw new IllegalStateException("Can't get typeOfItems in non container."); |
924 |
} |
925 |
return this.itemsType.getType(); |
926 |
} |
927 |
|
928 |
@Override
|
929 |
public DynField setValidateElements(boolean validate) { |
930 |
if (!this.isContainer()) { |
931 |
throw new IllegalStateException("Can't assign validateElements in non container."); |
932 |
} |
933 |
this.validateItems = validate;
|
934 |
return this; |
935 |
} |
936 |
|
937 |
@Override
|
938 |
public boolean getValidateElements() { |
939 |
if (!this.isContainer()) { |
940 |
throw new IllegalStateException("Can't get validateElements in non container."); |
941 |
} |
942 |
return this.validateItems; |
943 |
} |
944 |
|
945 |
@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 |
} |