gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dynobject / impl / DefaultDynField.java @ 2537
History | View | Annotate | Download (30.2 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 java.util.function.Supplier; |
35 |
import org.apache.commons.lang3.StringUtils; |
36 |
import org.gvsig.tools.ToolsLocator; |
37 |
import org.gvsig.tools.dataTypes.Coercion; |
38 |
import org.gvsig.tools.dataTypes.CoercionException; |
39 |
import org.gvsig.tools.dataTypes.DataType; |
40 |
import org.gvsig.tools.dataTypes.DataTypes; |
41 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
42 |
import org.gvsig.tools.dynobject.DynField; |
43 |
import org.gvsig.tools.dynobject.DynField_v2; |
44 |
import org.gvsig.tools.dynobject.DynMethod; |
45 |
import org.gvsig.tools.dynobject.DynObject; |
46 |
import org.gvsig.tools.dynobject.DynObjectValueItem; |
47 |
import org.gvsig.tools.dynobject.DynStruct; |
48 |
import org.gvsig.tools.dynobject.Tags; |
49 |
import org.gvsig.tools.dynobject.exception.DynFieldIsNotAContainerException; |
50 |
import org.gvsig.tools.dynobject.exception.DynFieldRequiredValueException; |
51 |
import org.gvsig.tools.dynobject.exception.DynFieldValidateException; |
52 |
import org.gvsig.tools.dynobject.exception.DynMethodException; |
53 |
import org.gvsig.tools.dynobject.exception.DynObjectValidateException; |
54 |
import org.gvsig.tools.exception.ListBaseException; |
55 |
import org.gvsig.tools.i18n.I18nManager; |
56 |
import org.gvsig.tools.util.GetItem; |
57 |
import org.gvsig.tools.util.Size; |
58 |
import org.slf4j.Logger; |
59 |
import org.slf4j.LoggerFactory; |
60 |
|
61 |
@SuppressWarnings("EqualsAndHashcode") |
62 |
public class DefaultDynField implements DynField_v2 { |
63 |
|
64 |
public static final Logger log = LoggerFactory.getLogger(DefaultDynField.class); |
65 |
|
66 |
private String name; |
67 |
private String description; |
68 |
|
69 |
private ValueType valueType;
|
70 |
|
71 |
private String subtype; |
72 |
|
73 |
private Object defaultValue; |
74 |
private Supplier defaultValueSupplier;
|
75 |
|
76 |
private int order; |
77 |
private boolean hidden; |
78 |
private String groupName; |
79 |
private DynObjectValueItem[] availableValues; |
80 |
private DynMethod availableValuesMethod = null; |
81 |
|
82 |
private Object minValue; |
83 |
private Object maxValue; |
84 |
private boolean mandatory; |
85 |
private boolean persistent; |
86 |
private String label = null; |
87 |
private String shortLabel = null; |
88 |
|
89 |
private boolean isReadOnly; |
90 |
private Tags tags = new DefaultTags(); |
91 |
|
92 |
private ValueType itemsType;
|
93 |
private boolean validateItems; |
94 |
|
95 |
private int relationType; |
96 |
private DynMethod calculate = null; |
97 |
|
98 |
public DefaultDynField(String name, int dataType) { |
99 |
this(name, // field name |
100 |
dataType, // data type
|
101 |
null, // default value |
102 |
true, // persistent |
103 |
false // mandatory |
104 |
); |
105 |
} |
106 |
|
107 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
108 |
protected DefaultDynField(String name, int dataType, Object defaultValue, |
109 |
boolean persistent, boolean mandatory) { |
110 |
DataTypesManager datamanager = ToolsLocator.getDataTypesManager(); |
111 |
|
112 |
if (StringUtils.isBlank(name)) {
|
113 |
throw new IllegalArgumentException("name can't be null"); |
114 |
} |
115 |
this.name = name;
|
116 |
this.valueType = new ValueType(dataType); |
117 |
this.subtype = this.getSubtype(); |
118 |
|
119 |
this.defaultValue = defaultValue;
|
120 |
this.persistent = persistent;
|
121 |
this.mandatory = mandatory;
|
122 |
this.groupName = null; |
123 |
this.order = 0; |
124 |
this.hidden = false; |
125 |
this.availableValues = null; |
126 |
|
127 |
this.itemsType = new ValueType(DataTypes.UNKNOWN); |
128 |
this.validateItems = false; |
129 |
this.relationType = RELATION_TYPE_NONE;
|
130 |
} |
131 |
|
132 |
@Override
|
133 |
public void copyFrom(DynField other) { |
134 |
try {
|
135 |
this.name = other.getName();
|
136 |
this.description = other.getDescription();
|
137 |
this.subtype = other.getSubtype();
|
138 |
this.defaultValue = other.getDefaultValue();
|
139 |
this.order = other.getOder();
|
140 |
this.hidden = other.isHidden();
|
141 |
this.groupName = other.getGroup();
|
142 |
this.minValue = other.getMinValue();
|
143 |
this.maxValue = other.getMaxValue();
|
144 |
this.mandatory = other.isMandatory();
|
145 |
this.persistent = other.isMandatory();
|
146 |
this.isReadOnly = other.isReadOnly();
|
147 |
this.tags = new DefaultTags(); |
148 |
|
149 |
if (other instanceof DynField_v2) { |
150 |
DynField_v2 other2 = (DynField_v2) other; |
151 |
this.tags.add(other2.getTags());
|
152 |
this.label = other2.getLabel();
|
153 |
this.validateItems = other2.getValidateElements();
|
154 |
this.defaultValueSupplier = other2.getDefaultValueSupplier();
|
155 |
|
156 |
if (other2.isCalculated()) {
|
157 |
this.setCalculateMethod(other2.getCalculateMethod());
|
158 |
} |
159 |
|
160 |
if( other2.isAvailableValuesCalculated() ) {
|
161 |
this.availableValuesMethod = other2.getAvailableValuesMethod();
|
162 |
} else {
|
163 |
DynObjectValueItem[] x = other.getAvailableValues();
|
164 |
if( x != null ) { |
165 |
this.availableValues = x.clone();
|
166 |
} |
167 |
} |
168 |
|
169 |
this.setType(other.getDataType());
|
170 |
this.setClassOfValue(other2.getClassOfValue());
|
171 |
this.setClassOfValue(other2.getDynClassOfValue());
|
172 |
|
173 |
this.itemsType.setType(other2.getTypeOfItems());
|
174 |
this.itemsType.setClassOfValue(other2.getClassOfItems());
|
175 |
this.itemsType.setClassOfValue(other2.getDynClassOfItems());
|
176 |
} |
177 |
} catch(Exception ex) { |
178 |
log.warn("Can't copy dynfield from '"+other.getName()+"'.", ex); |
179 |
throw ex;
|
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 |
if(defaultValueSupplier != null){ |
340 |
return defaultValueSupplier.get();
|
341 |
} |
342 |
return defaultValue;
|
343 |
} |
344 |
|
345 |
public Object getCoercedDefaultValue() { |
346 |
DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager(); |
347 |
int type = this.getType(); |
348 |
if (type == DataTypes.OBJECT) {
|
349 |
try {
|
350 |
Coercion coercion = dataTypesManager.getDataType(this.getClassOfValue()).getCoercion();
|
351 |
return coercion.coerce(this.getDefaultValue()); |
352 |
} catch (Exception ex1) { |
353 |
return this.getDefaultValue(); |
354 |
} |
355 |
} |
356 |
try {
|
357 |
Coercion coercion = dataTypesManager.getCoercion(this.getType());
|
358 |
return coercion.coerce(this.getDefaultValue()); |
359 |
} catch (Exception ex) { |
360 |
return this.getDefaultValue(); |
361 |
} |
362 |
} |
363 |
|
364 |
@Override
|
365 |
public boolean isAvailableValuesCalculated() { |
366 |
return this.availableValuesMethod != null; |
367 |
} |
368 |
|
369 |
@Override
|
370 |
public DynField setAvailableValues(DynObjectValueItem[] availableValues) { |
371 |
if (availableValues == null || availableValues.length == 0) { |
372 |
this.availableValues = null; |
373 |
} else {
|
374 |
this.availableValues = availableValues;
|
375 |
} |
376 |
return this; |
377 |
} |
378 |
|
379 |
@Override
|
380 |
public DynField setAvailableValues(List availableValues) { |
381 |
if (availableValues == null) { |
382 |
this.availableValues = null; |
383 |
} else if (availableValues.isEmpty()) { |
384 |
this.availableValues = null; |
385 |
} else {
|
386 |
this.availableValues = (DynObjectValueItem[]) availableValues |
387 |
.toArray(new DynObjectValueItem[availableValues.size()]);
|
388 |
} |
389 |
return this; |
390 |
} |
391 |
|
392 |
@Override
|
393 |
public DynField setAvailableValues(DynMethod computeAvailableValues) {
|
394 |
this.availableValuesMethod = computeAvailableValues;
|
395 |
return this; |
396 |
} |
397 |
|
398 |
@Override
|
399 |
public DynMethod getAvailableValuesMethod() {
|
400 |
return this.availableValuesMethod; |
401 |
} |
402 |
|
403 |
|
404 |
@Override
|
405 |
public DynObjectValueItem[] getAvailableValues() { |
406 |
return this.getAvailableValues(null); |
407 |
} |
408 |
|
409 |
@Override
|
410 |
public DynObjectValueItem[] getAvailableValues(DynObject context) { |
411 |
if (this.availableValuesMethod != null) { |
412 |
try {
|
413 |
Object values = this.availableValuesMethod.invoke(context, new Object[]{this}); |
414 |
if( values instanceof DynObjectValueItem[] ) { |
415 |
return (DynObjectValueItem[]) values; |
416 |
} |
417 |
if( values instanceof GetItem && values instanceof Size ) { |
418 |
DynObjectValueItem[] items = new DynObjectValueItem[((Size)values).size()]; |
419 |
for (int i = 0; i < items.length; i++) { |
420 |
items[i] = (DynObjectValueItem) ((GetItem)values).get(i); |
421 |
} |
422 |
return items;
|
423 |
} |
424 |
} catch (DynMethodException ex) {
|
425 |
return this.availableValues; |
426 |
} |
427 |
} |
428 |
return this.availableValues; |
429 |
} |
430 |
|
431 |
@Override
|
432 |
public DynField setMinValue(Object minValue) { |
433 |
try {
|
434 |
this.minValue = this.coerce(minValue); |
435 |
} catch (CoercionException e) {
|
436 |
throw new IllegalArgumentException(); |
437 |
} |
438 |
return this; |
439 |
} |
440 |
|
441 |
@Override
|
442 |
public Object getMinValue() { |
443 |
return minValue;
|
444 |
} |
445 |
|
446 |
@Override
|
447 |
public DynField setMaxValue(Object maxValue) { |
448 |
try {
|
449 |
this.maxValue = this.coerce(maxValue); |
450 |
} catch (CoercionException e) {
|
451 |
throw new IllegalArgumentException(e); |
452 |
} |
453 |
return this; |
454 |
} |
455 |
|
456 |
@Override
|
457 |
public Object getMaxValue() { |
458 |
return maxValue;
|
459 |
} |
460 |
|
461 |
@Override
|
462 |
public boolean isMandatory() { |
463 |
return this.mandatory; |
464 |
} |
465 |
|
466 |
@Override
|
467 |
public boolean isPersistent() { |
468 |
if (this.isCalculated()) { |
469 |
return false; |
470 |
} |
471 |
return this.persistent; |
472 |
} |
473 |
|
474 |
@Override
|
475 |
public DynField setMandatory(boolean mandatory) { |
476 |
this.mandatory = mandatory;
|
477 |
return this; |
478 |
} |
479 |
|
480 |
@Override
|
481 |
public DynField setPersistent(boolean persistent) { |
482 |
if (this.isCalculated()) { |
483 |
persistent = false;
|
484 |
} |
485 |
this.persistent = persistent;
|
486 |
return this; |
487 |
} |
488 |
|
489 |
@Override
|
490 |
public DynField setTheTypeOfAvailableValues(int type) { |
491 |
return this; // FIXME: this method is @deprecated |
492 |
} |
493 |
|
494 |
@Override
|
495 |
public int getTheTypeOfAvailableValues() { |
496 |
return 1; // FIXME: this method is @deprecated |
497 |
} |
498 |
|
499 |
/**
|
500 |
*
|
501 |
* @param obj
|
502 |
* @return
|
503 |
*/
|
504 |
@Override
|
505 |
public boolean equals(Object obj) { |
506 |
if (this == obj) { |
507 |
return true; |
508 |
} |
509 |
if (obj instanceof DynField) { |
510 |
// FIXME: No esta claro que esto sea correcto.
|
511 |
return name.equals(((DynField) obj).getName());
|
512 |
} |
513 |
return false; |
514 |
} |
515 |
|
516 |
@Override
|
517 |
public Class getClassOfValue() { |
518 |
return this.valueType.getClassOfValue(); |
519 |
} |
520 |
|
521 |
@Override
|
522 |
public DynField setClassOfValue(Class theClass) { |
523 |
this.valueType.setClassOfValue(theClass);
|
524 |
return this; |
525 |
} |
526 |
|
527 |
@Override
|
528 |
public DynField setClassOfValue(String theClassName) { |
529 |
this.valueType.setClassOfValue(theClassName);
|
530 |
return this; |
531 |
} |
532 |
|
533 |
@Override
|
534 |
public boolean isContainer() { |
535 |
if (valueType.getDataType() == null) { |
536 |
return false; |
537 |
} |
538 |
return valueType.getDataType().isContainer();
|
539 |
} |
540 |
|
541 |
@Override
|
542 |
public void validate(Object value) throws DynFieldValidateException { |
543 |
Comparable v;
|
544 |
if (value == null) { |
545 |
if (this.mandatory) { |
546 |
throw new DynFieldRequiredValueException(this, value); |
547 |
} |
548 |
return;
|
549 |
} |
550 |
|
551 |
switch (this.valueType.getType()) { |
552 |
case DataTypes.BOOLEAN:
|
553 |
if (!(value instanceof Boolean)) { |
554 |
throw new DynFieldValidateException(value, this); |
555 |
} |
556 |
break;
|
557 |
|
558 |
case DataTypes.DOUBLE:
|
559 |
if (!(value instanceof Double)) { |
560 |
throw new DynFieldValidateException(value, this); |
561 |
} |
562 |
break;
|
563 |
|
564 |
case DataTypes.FLOAT:
|
565 |
if (!(value instanceof Float)) { |
566 |
throw new DynFieldValidateException(value, this); |
567 |
} |
568 |
break;
|
569 |
|
570 |
case DataTypes.BYTE:
|
571 |
if (!(value instanceof Byte)) { |
572 |
throw new DynFieldValidateException(value, this); |
573 |
} |
574 |
break;
|
575 |
|
576 |
case DataTypes.INT:
|
577 |
if (!(value instanceof Integer)) { |
578 |
throw new DynFieldValidateException(value, this); |
579 |
} |
580 |
break;
|
581 |
|
582 |
case DataTypes.LONG:
|
583 |
if (!(value instanceof Long)) { |
584 |
throw new DynFieldValidateException(value, this); |
585 |
} |
586 |
break;
|
587 |
|
588 |
case DataTypes.STRING:
|
589 |
if (!(value instanceof String)) { |
590 |
throw new DynFieldValidateException(value, this); |
591 |
} |
592 |
break;
|
593 |
|
594 |
case DataTypes.CHAR:
|
595 |
if (!(value instanceof String)) { |
596 |
throw new DynFieldValidateException(value, this); |
597 |
} |
598 |
if (((String) value).length() > 1) { |
599 |
throw new DynFieldValidateException(value, this); |
600 |
} |
601 |
break;
|
602 |
|
603 |
case DataTypes.DATE:
|
604 |
if (!(value instanceof Date)) { |
605 |
throw new DynFieldValidateException(value, this); |
606 |
} |
607 |
break;
|
608 |
|
609 |
case DataTypes.TIMESTAMP:
|
610 |
if (!(value instanceof Date)) { |
611 |
throw new DynFieldValidateException(value, this); |
612 |
} |
613 |
break;
|
614 |
|
615 |
case DataTypes.TIME:
|
616 |
if (!(value instanceof Date)) { |
617 |
throw new DynFieldValidateException(value, this); |
618 |
} |
619 |
break;
|
620 |
|
621 |
case DataTypes.FILE:
|
622 |
if (!(value instanceof File)) { |
623 |
throw new DynFieldValidateException(value, this); |
624 |
} |
625 |
break;
|
626 |
case DataTypes.FOLDER:
|
627 |
if (!(value instanceof File)) { |
628 |
throw new DynFieldValidateException(value, this); |
629 |
} |
630 |
break;
|
631 |
case DataTypes.URI:
|
632 |
if (!(value instanceof URI)) { |
633 |
throw new DynFieldValidateException(value, this); |
634 |
} |
635 |
break;
|
636 |
case DataTypes.URL:
|
637 |
if (!(value instanceof URL)) { |
638 |
throw new DynFieldValidateException(value, this); |
639 |
} |
640 |
break;
|
641 |
|
642 |
case DataTypes.ARRAY:
|
643 |
// TODO: falta verificar que es un array del tipo que toca.
|
644 |
break;
|
645 |
|
646 |
case DataTypes.OBJECT:
|
647 |
if (this.valueType.getClassOfValue() != null) { |
648 |
if (!this.valueType.getClassOfValue().isInstance(value)) { |
649 |
throw new DynFieldValidateException(value, this); |
650 |
} |
651 |
} |
652 |
break;
|
653 |
|
654 |
case DataTypes.MAP:
|
655 |
if (!(value instanceof Map)) { |
656 |
throw new DynFieldValidateException(value, this); |
657 |
} |
658 |
validateCollection(value); |
659 |
break;
|
660 |
|
661 |
case DataTypes.SET:
|
662 |
if (!(value instanceof Set)) { |
663 |
throw new DynFieldValidateException(value, this); |
664 |
} |
665 |
validateCollection(value); |
666 |
break;
|
667 |
|
668 |
case DataTypes.LIST:
|
669 |
if (!(value instanceof List)) { |
670 |
throw new DynFieldValidateException(value, this); |
671 |
} |
672 |
validateCollection(value); |
673 |
break;
|
674 |
|
675 |
case DataTypes.DYNOBJECT:
|
676 |
if (!(value instanceof DynObject)) { |
677 |
throw new DynFieldValidateException(value, this); |
678 |
} |
679 |
if (!this.getDynClassOfValue().isInstance((DynObject) value)) { |
680 |
throw new DynFieldValidateException(value, this); |
681 |
} |
682 |
try {
|
683 |
this.getDynClassOfValue().validate((DynObject) value);
|
684 |
} catch (DynObjectValidateException e) {
|
685 |
throw new DynFieldValidateException(value, this, e); |
686 |
} |
687 |
break;
|
688 |
|
689 |
default:
|
690 |
if (this.valueType.getDataType().isObject()) { |
691 |
if (this.valueType.getClassOfValue() != null) { |
692 |
if (!this.valueType.getClassOfValue().isInstance(value)) { |
693 |
throw new DynFieldValidateException(value, this); |
694 |
} |
695 |
} |
696 |
} |
697 |
} |
698 |
|
699 |
if (this.getAvailableValues() != null) { |
700 |
if (!(value instanceof Comparable)) { |
701 |
throw new DynFieldValidateException(value, this); |
702 |
} |
703 |
v = (Comparable) value;
|
704 |
boolean ok = false; |
705 |
DynObjectValueItem[] theAvailableValues = this.getAvailableValues(); |
706 |
if( theAvailableValues!=null ) { |
707 |
for (DynObjectValueItem availableValue : theAvailableValues) {
|
708 |
if (v.compareTo(availableValue.getValue()) == 0) { |
709 |
ok = true;
|
710 |
break;
|
711 |
} |
712 |
} |
713 |
if (!ok) {
|
714 |
throw new DynFieldValidateException(value, this); |
715 |
} |
716 |
} |
717 |
} else if (this.getMaxValue() != null && this.getMinValue() != null) { |
718 |
if (!(value instanceof Comparable)) { |
719 |
throw new DynFieldValidateException(value, this); |
720 |
} |
721 |
v = (Comparable) value;
|
722 |
if (v.compareTo(this.minValue) < 0 |
723 |
|| v.compareTo(this.maxValue) > 0) { |
724 |
throw new DynFieldValidateException(value, this); |
725 |
} |
726 |
} |
727 |
} |
728 |
|
729 |
private void validateCollection(Object value) throws ValidateItemException { |
730 |
if (this.validateItems) { |
731 |
DynStruct dynClass = this.itemsType.getDynClassOfValue();
|
732 |
if (dynClass != null) { |
733 |
int index = 0; |
734 |
Iterator it = ((Collection) value).iterator(); |
735 |
while (it.hasNext()) {
|
736 |
try {
|
737 |
dynClass.validate((DynObject) it.next()); |
738 |
index++; |
739 |
} catch (DynObjectValidateException ex) {
|
740 |
throw new ValidateItemException(ex, index); |
741 |
} |
742 |
} |
743 |
} |
744 |
} |
745 |
|
746 |
} |
747 |
|
748 |
@Override
|
749 |
public Supplier getDefaultValueSupplier() {
|
750 |
return this.defaultValueSupplier; |
751 |
} |
752 |
|
753 |
@Override
|
754 |
public DynField setDefaultValueSupplier(Supplier supplier) {
|
755 |
this.defaultValueSupplier = supplier;
|
756 |
return this; |
757 |
} |
758 |
|
759 |
private static class ValidateItemException extends DynFieldValidateException { |
760 |
|
761 |
private static final long serialVersionUID = 9011437364983996567L; |
762 |
|
763 |
ValidateItemException(Throwable cause, int index) { |
764 |
super(
|
765 |
"Can't validate item %(index) of the collection.",
|
766 |
cause, |
767 |
"_Cant_validate_item_%(index)_of_the_collection",
|
768 |
serialVersionUID |
769 |
); |
770 |
setValue("index", index);
|
771 |
} |
772 |
} |
773 |
|
774 |
@Override
|
775 |
public Object coerce(Object value) throws CoercionException { |
776 |
if (value == null) { |
777 |
return value; // O debe devolver this.defaultValue |
778 |
} |
779 |
try {
|
780 |
return this.valueType.getDataType().coerce(value); |
781 |
} catch (Exception ex) { |
782 |
throw new RuntimeException(ex); |
783 |
} |
784 |
} |
785 |
|
786 |
@Override
|
787 |
public String getGroup() { |
788 |
return this.groupName; |
789 |
} |
790 |
|
791 |
@Override
|
792 |
public DynField setGroup(String groupName) { |
793 |
this.groupName = groupName;
|
794 |
return this; |
795 |
} |
796 |
|
797 |
@Override
|
798 |
public int getOder() { |
799 |
return this.order; |
800 |
} |
801 |
|
802 |
@Override
|
803 |
public DynField setOrder(int order) { |
804 |
this.order = order;
|
805 |
return this; |
806 |
} |
807 |
|
808 |
@Override
|
809 |
public boolean isHidden() { |
810 |
return this.hidden; |
811 |
} |
812 |
|
813 |
@Override
|
814 |
public DynField setHidden(boolean hidden) { |
815 |
this.hidden = hidden;
|
816 |
return this; |
817 |
} |
818 |
|
819 |
@Override
|
820 |
public boolean isReadOnly() { |
821 |
return this.isReadOnly; |
822 |
} |
823 |
|
824 |
@Override
|
825 |
public DynField setReadOnly(boolean isReadOnly) { |
826 |
this.isReadOnly = isReadOnly;
|
827 |
return this; |
828 |
} |
829 |
|
830 |
@Override
|
831 |
public DynField setDefaultFieldValue(Object defaultValue) { |
832 |
try {
|
833 |
this.defaultValue = this.coerce(defaultValue); |
834 |
} catch (CoercionException e) {
|
835 |
throw new IllegalArgumentException(e); |
836 |
} |
837 |
return this; |
838 |
} |
839 |
|
840 |
@Override
|
841 |
public Tags getTags() {
|
842 |
return tags;
|
843 |
} |
844 |
|
845 |
@Override
|
846 |
public String getClassNameOfValue() { |
847 |
return this.valueType.getClassNameOfValue(); |
848 |
} |
849 |
|
850 |
@Override
|
851 |
public DynField setClassOfValue(DynStruct dynStruct) {
|
852 |
this.valueType.setClassOfValue(dynStruct);
|
853 |
return this; |
854 |
} |
855 |
|
856 |
@Override
|
857 |
public DynStruct getDynClassOfValue() {
|
858 |
return this.valueType.getDynClassOfValue(); |
859 |
} |
860 |
|
861 |
@Override
|
862 |
public int getRelationType() { |
863 |
return this.relationType; |
864 |
} |
865 |
|
866 |
@Override
|
867 |
public DynField setRelationType(int relationType) { |
868 |
this.relationType = relationType;
|
869 |
return this; |
870 |
} |
871 |
|
872 |
@Override
|
873 |
public DynField setElementsType(int type) { |
874 |
this.setTypeOfItems(type);
|
875 |
return this; |
876 |
} |
877 |
|
878 |
@Override
|
879 |
public DynField setElementsType(DynStruct type) {
|
880 |
this.setClassOfItems(type);
|
881 |
return this; |
882 |
} |
883 |
|
884 |
@Override
|
885 |
public DynField getElementsType() {
|
886 |
throw new UnsupportedOperationException("This operation is not suported nevermore."); |
887 |
} |
888 |
|
889 |
@Override
|
890 |
public DynField setClassOfItems(DynStruct dynStrct) {
|
891 |
if (dynStrct!=null && !this.isContainer()) { |
892 |
throw new IllegalStateException("Can't assign classOfItems in non container."); |
893 |
} |
894 |
this.itemsType.setClassOfValue(dynStrct);
|
895 |
return this; |
896 |
} |
897 |
|
898 |
@Override
|
899 |
public DynField setClassOfItems(String theClassNameOfValue) { |
900 |
if (theClassNameOfValue!=null && !this.isContainer()) { |
901 |
throw new IllegalStateException("Can't assign classOfItems in non container."); |
902 |
} |
903 |
this.itemsType.setClassOfValue(theClassNameOfValue);
|
904 |
return this; |
905 |
} |
906 |
|
907 |
@Override
|
908 |
public String getClassNameOfItems() { |
909 |
if( this.itemsType==null || |
910 |
this.itemsType.getClassNameOfValue()==null ) { |
911 |
return null; |
912 |
} |
913 |
if (!this.isContainer()) { |
914 |
throw new IllegalStateException("Can't get classNameOfItems in non container."); |
915 |
} |
916 |
return this.itemsType.getClassNameOfValue(); |
917 |
} |
918 |
|
919 |
@Override
|
920 |
public DynStruct getDynClassOfItems() {
|
921 |
if( this.itemsType==null || |
922 |
this.itemsType.getDynClassOfValue()==null ) { |
923 |
return null; |
924 |
} |
925 |
if (!this.isContainer()) { |
926 |
throw new IllegalStateException("Can't get dynClassOfItems in non container."); |
927 |
} |
928 |
return this.itemsType.getDynClassOfValue(); |
929 |
} |
930 |
|
931 |
@Override
|
932 |
public DynField setClassOfItems(Class theClass) |
933 |
throws DynFieldIsNotAContainerException {
|
934 |
if (theClass!=null && !this.isContainer()) { |
935 |
throw new IllegalStateException("Can't assign classNameOfItems in non container."); |
936 |
} |
937 |
this.itemsType.setClassOfValue(theClass);
|
938 |
return this; |
939 |
} |
940 |
|
941 |
@Override
|
942 |
public Class getClassOfItems() { |
943 |
if( this.itemsType==null || |
944 |
this.itemsType.getClassOfValue()==null ) { |
945 |
return null; |
946 |
} |
947 |
if (!this.isContainer()) { |
948 |
throw new IllegalStateException("Can't get classOfItems in non container."); |
949 |
} |
950 |
return this.itemsType.getClassOfValue(); |
951 |
} |
952 |
|
953 |
@Override
|
954 |
public DynField setTypeOfItems(int type) { |
955 |
if (!this.isContainer()) { |
956 |
throw new IllegalStateException("Can't assign typeOfItems in non container."); |
957 |
} |
958 |
this.itemsType.setType(type);
|
959 |
return this; |
960 |
} |
961 |
|
962 |
@Override
|
963 |
public int getTypeOfItems() { |
964 |
if (!this.isContainer()) { |
965 |
throw new IllegalStateException("Can't get typeOfItems in non container."); |
966 |
} |
967 |
return this.itemsType.getType(); |
968 |
} |
969 |
|
970 |
@Override
|
971 |
public DynField setValidateElements(boolean validate) { |
972 |
if (!this.isContainer()) { |
973 |
throw new IllegalStateException("Can't assign validateElements in non container."); |
974 |
} |
975 |
this.validateItems = validate;
|
976 |
return this; |
977 |
} |
978 |
|
979 |
@Override
|
980 |
public boolean getValidateElements() { |
981 |
if (!this.isContainer()) { |
982 |
throw new IllegalStateException("Can't get validateElements in non container."); |
983 |
} |
984 |
return this.validateItems; |
985 |
} |
986 |
|
987 |
@Override
|
988 |
public Object clone() throws CloneNotSupportedException { |
989 |
DefaultDynField other = (DefaultDynField) super.clone();
|
990 |
other.tags = (Tags) this.tags.clone();
|
991 |
other.valueType = (ValueType) this.valueType.clone();
|
992 |
other.itemsType = (ValueType) this.itemsType.clone();
|
993 |
if( this.availableValues==null ) { |
994 |
other.availableValues = null;
|
995 |
} else {
|
996 |
other.availableValues = this.availableValues.clone();
|
997 |
} |
998 |
other.calculate = (DynMethod) this.calculate;
|
999 |
return other;
|
1000 |
} |
1001 |
|
1002 |
} |