gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dynobject / impl / DefaultDynField.java @ 212
History | View | Annotate | Download (13.5 KB)
1 |
package org.gvsig.tools.dynobject.impl; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.util.Date; |
5 |
import java.util.Iterator; |
6 |
import java.util.List; |
7 |
import java.util.Map; |
8 |
import java.util.Set; |
9 |
|
10 |
import org.gvsig.tools.ToolsLocator; |
11 |
import org.gvsig.tools.dataTypes.CoercionException; |
12 |
import org.gvsig.tools.dataTypes.DataTypes; |
13 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
14 |
import org.gvsig.tools.dynobject.DynClass; |
15 |
import org.gvsig.tools.dynobject.DynField; |
16 |
import org.gvsig.tools.dynobject.DynObject; |
17 |
import org.gvsig.tools.dynobject.DynObjectException; |
18 |
import org.gvsig.tools.dynobject.DynObjectValueItem; |
19 |
import org.gvsig.tools.dynobject.DynStruct; |
20 |
import org.gvsig.tools.dynobject.exception.DynFieldIsNotAContainerException; |
21 |
import org.gvsig.tools.dynobject.exception.DynFieldValidateException; |
22 |
import org.gvsig.tools.dynobject.exception.DynObjectValidateException; |
23 |
import org.gvsig.tools.exception.ListBaseException; |
24 |
|
25 |
public class DefaultDynField implements DynField { |
26 |
private String name; |
27 |
private String description; |
28 |
|
29 |
private int dataType; |
30 |
private String subtype; |
31 |
|
32 |
private Object defaultValue; |
33 |
|
34 |
private int order; |
35 |
private boolean hidden; |
36 |
private String groupName; |
37 |
private DynObjectValueItem[] availableValues; |
38 |
private Object minValue; |
39 |
private Object maxValue; |
40 |
private boolean mandatory; |
41 |
private boolean persistent; |
42 |
private Class theClass; |
43 |
private DynField elementsType;
|
44 |
private boolean validateElements; |
45 |
private boolean isReadOnly; |
46 |
private Class theClassOfItems=null; |
47 |
|
48 |
public void check() throws ListBaseException { |
49 |
ListBaseException exceptions = null;
|
50 |
DataTypesManager manager = ToolsLocator.getDataTypesManager(); |
51 |
|
52 |
if( name == null ) { |
53 |
exceptions = CheckDynFieldListException.add(exceptions, name, "name", name);
|
54 |
} |
55 |
if( !manager.isValidType(dataType) ) {
|
56 |
exceptions = CheckDynFieldListException.add(exceptions, name, "dataType", new Integer(dataType)); |
57 |
} |
58 |
if( exceptions!= null ) { |
59 |
throw exceptions;
|
60 |
} |
61 |
} |
62 |
|
63 |
public static class CheckDynFieldListException extends ListBaseException { |
64 |
|
65 |
public static class CheckDynFieldException extends DynObjectException { |
66 |
|
67 |
/**
|
68 |
*
|
69 |
*/
|
70 |
private static final long serialVersionUID = 2486744641818117262L; |
71 |
|
72 |
public CheckDynFieldException(String attrname, Object attrvalue) { |
73 |
super(
|
74 |
"Wrong value %(value) for attribute %(name).",
|
75 |
"Wrong_value_XvalueX_for_attribute_XnameX",
|
76 |
serialVersionUID |
77 |
); |
78 |
} |
79 |
} |
80 |
|
81 |
/**
|
82 |
*
|
83 |
*/
|
84 |
private static final long serialVersionUID = 1L; |
85 |
|
86 |
public CheckDynFieldListException(String name) { |
87 |
super(
|
88 |
"Inconsistent field %(name) definition.",
|
89 |
"_Inconsistent_field_XnameX_definition",
|
90 |
serialVersionUID |
91 |
); |
92 |
if( name == null ) { |
93 |
name = "[unknow]";
|
94 |
} |
95 |
setValue("name",name);
|
96 |
} |
97 |
|
98 |
public static ListBaseException add(ListBaseException exceptions, String name, String attrname, Object attrvalue) { |
99 |
if( exceptions == null ) { |
100 |
exceptions = new CheckDynFieldListException(name);
|
101 |
} |
102 |
exceptions.add( new CheckDynFieldException(attrname, attrvalue) );
|
103 |
return exceptions;
|
104 |
} |
105 |
} |
106 |
|
107 |
public DefaultDynField(String name) { |
108 |
this(name, // field name |
109 |
DataTypes.STRING, // data type
|
110 |
null, // default value |
111 |
true, // persistent |
112 |
false // mandatory |
113 |
); |
114 |
} |
115 |
|
116 |
private DefaultDynField(String name, int dataType) { |
117 |
this(name, // field name |
118 |
dataType, // data type
|
119 |
null, // default value |
120 |
true, // persistent |
121 |
false // mandatory |
122 |
); |
123 |
} |
124 |
|
125 |
public DefaultDynField(String name, int dataType, Object defaultValue, |
126 |
boolean persistent, boolean mandatory) { |
127 |
this.name = name;
|
128 |
this.dataType = dataType;
|
129 |
this.defaultValue = defaultValue;
|
130 |
this.persistent = persistent;
|
131 |
this.mandatory = mandatory;
|
132 |
this.theClass = null; |
133 |
this.validateElements = false; |
134 |
this.subtype = null; |
135 |
this.groupName = null; |
136 |
this.order = 0; |
137 |
this.hidden = false; |
138 |
this.availableValues = null; |
139 |
} |
140 |
|
141 |
public String toString() { |
142 |
StringBuffer buffer = new StringBuffer(); |
143 |
|
144 |
buffer.append("DynField").append("[").append(this.hashCode()).append( |
145 |
"]").append("( ").append("name='").append(this.name).append( |
146 |
"', ").append("description='").append(this.description).append( |
147 |
"', ").append("dataType='").append(this.dataType).append(", ") |
148 |
.append("minValue='").append(this.minValue).append(", ") |
149 |
.append("maxValue='").append(this.maxValue).append(", ") |
150 |
.append("mandatory='").append(this.isMandatory()).append(", ") |
151 |
.append("persistent='").append(this.isPersistent()) |
152 |
.append(", ").append("defaultValue='").append( |
153 |
this.getDefaultValue()).append(" )"); |
154 |
return buffer.toString();
|
155 |
} |
156 |
|
157 |
public String getName() { |
158 |
return name;
|
159 |
} |
160 |
|
161 |
public DynField setDescription(String description) { |
162 |
this.description = description;
|
163 |
return this; |
164 |
} |
165 |
|
166 |
public String getDescription() { |
167 |
return (description == null) ? getName() : description; |
168 |
} |
169 |
|
170 |
public DynField setType(int dataType) { |
171 |
DataTypesManager manager = ToolsLocator.getDataTypesManager(); |
172 |
this.dataType = dataType;
|
173 |
this.theClass = manager.getDefaultClass(dataType);
|
174 |
this.subtype = manager.getSubtype(dataType);
|
175 |
return this; |
176 |
} |
177 |
|
178 |
public int getType() { |
179 |
return dataType;
|
180 |
} |
181 |
|
182 |
public DynField setSubtype(String subtype) { |
183 |
this.subtype = subtype;
|
184 |
if( subtype!=null && this.dataType == DataTypes.DYNOBJECT ) { |
185 |
if( ToolsLocator.getDynObjectManager().get(subtype) == null ) { |
186 |
throw new IllegalArgumentException("DynClass '"+subtype+"' does not exist."); |
187 |
} |
188 |
} |
189 |
return this; |
190 |
} |
191 |
|
192 |
public String getSubtype() { |
193 |
return subtype;
|
194 |
} |
195 |
|
196 |
public DynField setDefaultDynValue(Object defaultValue) { |
197 |
this.defaultValue = defaultValue;
|
198 |
return this; |
199 |
} |
200 |
|
201 |
public Object getDefaultValue() { |
202 |
return defaultValue;
|
203 |
} |
204 |
|
205 |
public DynField setAvailableValues(DynObjectValueItem[] availableValues) { |
206 |
if( availableValues.length == 0 ) { |
207 |
this.availableValues = null; |
208 |
} else {
|
209 |
this.availableValues = availableValues;
|
210 |
} |
211 |
return this; |
212 |
} |
213 |
|
214 |
public DynField setAvailableValues(List availableValues) { |
215 |
if( availableValues == null ) { |
216 |
this.availableValues = null; |
217 |
} else if( availableValues.isEmpty() ) { |
218 |
this.availableValues = null; |
219 |
} else {
|
220 |
this.availableValues = (DynObjectValueItem[]) availableValues |
221 |
.toArray(new DynObjectValueItem[availableValues.size()]);
|
222 |
} |
223 |
return this; |
224 |
} |
225 |
|
226 |
public DynObjectValueItem[] getAvailableValues() { |
227 |
return availableValues;
|
228 |
} |
229 |
|
230 |
public DynField setMinValue(Object minValue) { |
231 |
this.minValue = minValue;
|
232 |
return this; |
233 |
} |
234 |
|
235 |
public Object getMinValue() { |
236 |
return minValue;
|
237 |
} |
238 |
|
239 |
public DynField setMaxValue(Object maxValue) { |
240 |
this.maxValue = maxValue;
|
241 |
return this; |
242 |
} |
243 |
|
244 |
public Object getMaxValue() { |
245 |
return maxValue;
|
246 |
} |
247 |
|
248 |
public boolean isMandatory() { |
249 |
return this.mandatory; |
250 |
} |
251 |
|
252 |
public boolean isPersistent() { |
253 |
return this.persistent; |
254 |
} |
255 |
|
256 |
public DynField setMandatory(boolean mandatory) { |
257 |
this.mandatory = mandatory;
|
258 |
return this; |
259 |
} |
260 |
|
261 |
public DynField setPersistent(boolean persistent) { |
262 |
this.persistent = persistent;
|
263 |
return this; |
264 |
} |
265 |
|
266 |
public DynField setTheTypeOfAvailableValues(int type) { |
267 |
return this; // FIXME: this method is @deprecated |
268 |
} |
269 |
|
270 |
public int getTheTypeOfAvailableValues() { |
271 |
return 1; // FIXME: this method is @deprecated |
272 |
} |
273 |
|
274 |
public boolean equals(Object obj) { |
275 |
if (this == obj) { |
276 |
return true; |
277 |
} |
278 |
if (obj instanceof DynField) { |
279 |
// FIXME: No esta claro que esto sea correcto.
|
280 |
return name.equals(((DynField) obj).getName());
|
281 |
} |
282 |
return false; |
283 |
} |
284 |
|
285 |
public Class getClassOfValue() { |
286 |
return theClass;
|
287 |
} |
288 |
|
289 |
public DynField setValidateElements(boolean validate) { |
290 |
switch (this.dataType) { |
291 |
case DataTypes.ARRAY:
|
292 |
case DataTypes.LIST:
|
293 |
case DataTypes.MAP:
|
294 |
case DataTypes.SET:
|
295 |
break;
|
296 |
default:
|
297 |
throw new DynFieldIsNotAContainerException(this.name); |
298 |
} |
299 |
this.validateElements = validate;
|
300 |
return this; |
301 |
} |
302 |
|
303 |
public boolean getValidateElements() { |
304 |
return this.validateElements; |
305 |
} |
306 |
|
307 |
public DynField setClassOfValue(Class theClass) |
308 |
throws DynFieldIsNotAContainerException {
|
309 |
this.theClass = theClass;
|
310 |
return this; |
311 |
} |
312 |
|
313 |
public DynField setElementsType(int type) |
314 |
throws DynFieldIsNotAContainerException {
|
315 |
if (!isContainer()) {
|
316 |
throw new DynFieldIsNotAContainerException(this.name); |
317 |
} |
318 |
this.elementsType = new DefaultDynField(this.name + "-" |
319 |
+ ToolsLocator.getDataTypesManager().getTypeName(this.dataType)
|
320 |
+ "-item",type);
|
321 |
return this; |
322 |
} |
323 |
|
324 |
|
325 |
public DynField setElementsType(DynStruct type)
|
326 |
throws DynFieldIsNotAContainerException {
|
327 |
this.setElementsType(DataTypes.DYNOBJECT).getElementsType().setSubtype(type.getFullName());
|
328 |
return this; |
329 |
} |
330 |
|
331 |
public boolean isContainer(){ |
332 |
return ToolsLocator.getDataTypesManager().isContainer(this.dataType); |
333 |
} |
334 |
|
335 |
public DynField getElementsType() {
|
336 |
return this.elementsType; |
337 |
} |
338 |
|
339 |
public void validate(Object value) throws DynFieldValidateException { |
340 |
Comparable v;
|
341 |
if (value == null) { |
342 |
if (this.mandatory) { |
343 |
throw new DynFieldValidateException(value, this); |
344 |
} |
345 |
return;
|
346 |
} |
347 |
|
348 |
switch (this.dataType) { |
349 |
case DataTypes.BOOLEAN:
|
350 |
if (!(value instanceof Boolean)) { |
351 |
throw new DynFieldValidateException(value, this); |
352 |
} |
353 |
break;
|
354 |
|
355 |
case DataTypes.DOUBLE:
|
356 |
if (!(value instanceof Double)) { |
357 |
throw new DynFieldValidateException(value, this); |
358 |
} |
359 |
break;
|
360 |
|
361 |
case DataTypes.FLOAT:
|
362 |
if (!(value instanceof Float)) { |
363 |
throw new DynFieldValidateException(value, this); |
364 |
} |
365 |
break;
|
366 |
|
367 |
case DataTypes.INT:
|
368 |
if (!(value instanceof Integer)) { |
369 |
throw new DynFieldValidateException(value, this); |
370 |
} |
371 |
break;
|
372 |
|
373 |
case DataTypes.LONG:
|
374 |
if (!(value instanceof Long)) { |
375 |
throw new DynFieldValidateException(value, this); |
376 |
} |
377 |
break;
|
378 |
|
379 |
case DataTypes.STRING:
|
380 |
if (!(value instanceof String)) { |
381 |
throw new DynFieldValidateException(value, this); |
382 |
} |
383 |
break;
|
384 |
|
385 |
case DataTypes.DATE:
|
386 |
if (!(value instanceof Date)) { |
387 |
throw new DynFieldValidateException(value, this); |
388 |
} |
389 |
break;
|
390 |
|
391 |
case DataTypes.LIST:
|
392 |
if (!(value instanceof List)) { |
393 |
throw new DynFieldValidateException(value, this); |
394 |
} |
395 |
if (this.validateElements && this.elementsType != null) { |
396 |
Iterator it = ((List) value).iterator(); |
397 |
while (it.hasNext()) {
|
398 |
this.elementsType.validate(it.next());
|
399 |
} |
400 |
} |
401 |
break;
|
402 |
|
403 |
case DataTypes.MAP:
|
404 |
if (!(value instanceof Map)) { |
405 |
throw new DynFieldValidateException(value, this); |
406 |
} |
407 |
break;
|
408 |
case DataTypes.FILE:
|
409 |
if (!(value instanceof File)) { |
410 |
throw new DynFieldValidateException(value, this); |
411 |
} |
412 |
break;
|
413 |
case DataTypes.FOLDER:
|
414 |
if (!(value instanceof File)) { |
415 |
throw new DynFieldValidateException(value, this); |
416 |
} |
417 |
break;
|
418 |
case DataTypes.SET:
|
419 |
if (!(value instanceof Set)) { |
420 |
throw new DynFieldValidateException(value, this); |
421 |
} |
422 |
break;
|
423 |
|
424 |
case DataTypes.OBJECT:
|
425 |
if (this.theClass != null) { |
426 |
if (!this.theClass.isInstance(value)) { |
427 |
throw new DynFieldValidateException(value, this); |
428 |
} |
429 |
} |
430 |
break;
|
431 |
|
432 |
case DataTypes.DYNOBJECT:
|
433 |
if (!(value instanceof DynObject)) { |
434 |
throw new DynFieldValidateException(value, this); |
435 |
} |
436 |
DynClass dynClass = ToolsLocator.getDynObjectManager().get( |
437 |
this.getSubtype());
|
438 |
if ( dynClass==null || !dynClass.isInstance((DynObject) value)) { |
439 |
throw new DynFieldValidateException(value, this); |
440 |
} |
441 |
try {
|
442 |
dynClass.validate((DynObject) value); |
443 |
} catch (DynObjectValidateException e) {
|
444 |
throw new DynFieldValidateException(value, this, e); |
445 |
} |
446 |
break;
|
447 |
|
448 |
case DataTypes.ARRAY:
|
449 |
// TODO: falta verificar que es un array del tipo que toca.
|
450 |
break;
|
451 |
|
452 |
default:
|
453 |
throw new DynFieldValidateException(value, this); |
454 |
} |
455 |
|
456 |
if (this.mandatory && value == null) { |
457 |
throw new DynFieldValidateException(value, this); |
458 |
} |
459 |
|
460 |
if (this.getAvailableValues() != null) { |
461 |
if (!(value instanceof Comparable)) { |
462 |
throw new DynFieldValidateException(value, this); |
463 |
} |
464 |
v = (Comparable) value;
|
465 |
boolean ok = false; |
466 |
for (int i = 0; i < this.availableValues.length; i++) { |
467 |
if (v.compareTo(this.availableValues[i].getValue()) == 0) { |
468 |
ok = true;
|
469 |
break;
|
470 |
} |
471 |
} |
472 |
if (!ok) {
|
473 |
throw new DynFieldValidateException(value, this); |
474 |
} |
475 |
} else if (this.getMaxValue() != null && this.getMinValue() != null) { |
476 |
if (!(value instanceof Comparable)) { |
477 |
throw new DynFieldValidateException(value, this); |
478 |
} |
479 |
v = (Comparable) value;
|
480 |
if (v.compareTo(this.minValue) < 0 |
481 |
|| v.compareTo(this.maxValue) > 0) { |
482 |
throw new DynFieldValidateException(value, this); |
483 |
} |
484 |
} |
485 |
} |
486 |
|
487 |
public Object coerce(Object value) throws CoercionException { |
488 |
if (value == null) { |
489 |
return value; // O debe retorna this.defaultValue |
490 |
} |
491 |
DataTypesManager manager = ToolsLocator.getDataTypesManager(); |
492 |
return manager.coerce(this.dataType, value); |
493 |
} |
494 |
|
495 |
|
496 |
public String getGroup() { |
497 |
return this.groupName; |
498 |
} |
499 |
|
500 |
public DynField setGroup(String groupName) { |
501 |
this.groupName = groupName;
|
502 |
return this; |
503 |
} |
504 |
|
505 |
public int getOder() { |
506 |
return this.order; |
507 |
} |
508 |
|
509 |
public DynField setOrder(int order) { |
510 |
this.order = order;
|
511 |
return this; |
512 |
} |
513 |
|
514 |
public boolean isHidden() { |
515 |
return this.hidden; |
516 |
} |
517 |
|
518 |
public DynField setHidden(boolean hidden) { |
519 |
this.hidden = hidden;
|
520 |
return this; |
521 |
} |
522 |
|
523 |
public boolean isReadOnly() { |
524 |
return this.isReadOnly; |
525 |
} |
526 |
|
527 |
|
528 |
public DynField setReadOnly(boolean isReadOnly) { |
529 |
this.isReadOnly = isReadOnly;
|
530 |
return this; |
531 |
} |
532 |
|
533 |
public Class getClassOfItems() { |
534 |
return this.theClassOfItems; |
535 |
} |
536 |
|
537 |
public DynField setDefaultValue(Object defaultValue) { |
538 |
this.defaultValue = defaultValue;
|
539 |
return this; |
540 |
} |
541 |
|
542 |
public DynField setClassOfItems(Class theClass) |
543 |
throws DynFieldIsNotAContainerException {
|
544 |
switch (this.dataType) { |
545 |
case DataTypes.ARRAY:
|
546 |
case DataTypes.LIST:
|
547 |
case DataTypes.MAP:
|
548 |
case DataTypes.SET:
|
549 |
break;
|
550 |
default:
|
551 |
throw new DynFieldIsNotAContainerException(this.name); |
552 |
} |
553 |
this.theClassOfItems = theClass;
|
554 |
return this; |
555 |
} |
556 |
|
557 |
|
558 |
} |