Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dynobject / impl / DefaultDynField.java @ 146

History | View | Annotate | Download (13.2 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

    
47
        public void check() throws ListBaseException {
48
                ListBaseException exceptions = null;
49
                DataTypesManager manager = ToolsLocator.getDataTypesManager();
50
                
51
                if( name == null ) {
52
                        exceptions = CheckDynFieldListException.add(exceptions, name, "name", name);
53
                }
54
                if( !manager.isValidType(dataType) ) {
55
                        exceptions = CheckDynFieldListException.add(exceptions, name, "dataType", new Integer(dataType));
56
                }
57
                if( exceptions!= null ) {
58
                        throw exceptions;
59
                }
60
        }
61
        
62
        public static class CheckDynFieldListException extends ListBaseException {  
63
        
64
                public static class CheckDynFieldException extends DynObjectException {
65
                        
66
                        /**
67
                         * 
68
                         */
69
                        private static final long serialVersionUID = 2486744641818117262L;
70

    
71
                        public CheckDynFieldException(String attrname, Object attrvalue) {
72
                                super(
73
                                                "Wrong value %(value) for attribute %(name).",
74
                                                "Wrong_value_XvalueX_for_attribute_XnameX",
75
                                                serialVersionUID
76
                                );
77
                        }
78
                }
79
                
80
                /**
81
                 * 
82
                 */
83
                private static final long serialVersionUID = 1L;
84

    
85
                public CheckDynFieldListException(String name) {
86
                        super(
87
                                "Inconsistent field %(name) definition.",
88
                                "_Inconsistent_field_XnameX_definition",
89
                                serialVersionUID
90
                        );
91
                        if( name == null ) {
92
                                name = "[unknow]";
93
                        }
94
                        setValue("name",name);
95
                }
96
                
97
                public static ListBaseException add(ListBaseException exceptions, String name, String attrname, Object attrvalue) {
98
                        if( exceptions == null ) {
99
                                exceptions = new CheckDynFieldListException(name);
100
                        }
101
                        exceptions.add( new CheckDynFieldException(attrname, attrvalue) );
102
                        return exceptions;
103
                }
104
        }
105
        
106
        public DefaultDynField(String name) {
107
                this(name, // field name
108
                                DataTypes.STRING, // data type
109
                                null, // default value
110
                                true, // persistent
111
                                false // mandatory
112
                );
113
        }
114
        
115
        private DefaultDynField(String name, int dataType) {
116
                this(name, // field name
117
                                dataType, // data type
118
                                null, // default value
119
                                true, // persistent
120
                                false // mandatory
121
                );
122
        }
123

    
124
        public DefaultDynField(String name, int dataType, Object defaultValue,
125
                        boolean persistent, boolean mandatory) {
126
                this.name = name;
127
                this.dataType = dataType;
128
                this.defaultValue = defaultValue;
129
                this.persistent = persistent;
130
                this.mandatory = mandatory;
131
                this.theClass = null;
132
                this.validateElements = false;
133
                this.subtype = null;
134
                this.groupName = null;
135
                this.order = 0;
136
                this.hidden = false;
137
                this.availableValues = null;
138
        }
139

    
140
        public String toString() {
141
                StringBuffer buffer = new StringBuffer();
142

    
143
                buffer.append("DynField").append("[").append(this.hashCode()).append(
144
                                "]").append("( ").append("name='").append(this.name).append(
145
                                "', ").append("description='").append(this.description).append(
146
                                "', ").append("dataType='").append(this.dataType).append(", ")
147
                                .append("minValue='").append(this.minValue).append(", ")
148
                                .append("maxValue='").append(this.maxValue).append(", ")
149
                                .append("mandatory='").append(this.isMandatory()).append(", ")
150
                                .append("persistent='").append(this.isPersistent())
151
                                .append(", ").append("defaultValue='").append(
152
                                                this.getDefaultValue()).append(" )");
153
                return buffer.toString();
154
        }
155

    
156
        public String getName() {
157
                return name;
158
        }
159

    
160
        public DynField setDescription(String description) {
161
                this.description = description;
162
                return this;
163
        }
164

    
165
        public String getDescription() {
166
                return (description == null) ? getName() : description;
167
        }
168

    
169
        public DynField setType(int dataType) {
170
                DataTypesManager manager = ToolsLocator.getDataTypesManager();
171
                this.dataType = dataType;
172
                this.theClass = manager.getDefaultClass(dataType);
173
                this.subtype = manager.getSubtype(dataType);
174
                return this;
175
        }
176

    
177
        public int getType() {
178
                return dataType;
179
        }
180

    
181
        public DynField setSubtype(String subtype) {
182
                this.subtype = subtype;
183
                if( subtype!=null && this.dataType == DataTypes.DYNOBJECT ) {
184
                        if( ToolsLocator.getDynObjectManager().get(subtype) == null ) {
185
                                throw new IllegalArgumentException("DynClass '"+subtype+"' does not exist.");
186
                        }
187
                }
188
                return this;
189
        }
190

    
191
        public String getSubtype() {
192
                return subtype;
193
        }
194

    
195
        public DynField setDefaultDynValue(Object defaultValue) {
196
                this.defaultValue = defaultValue;
197
                return this;
198
        }
199

    
200
        public Object getDefaultValue() {
201
                return defaultValue;
202
        }
203

    
204
        public DynField setAvailableValues(DynObjectValueItem[] availableValues) {
205
                if( availableValues.length == 0 ) {
206
                        this.availableValues = null;
207
                } else {
208
                        this.availableValues = availableValues;
209
                }
210
                return this;
211
        }
212

    
213
        public DynField setAvailableValues(List availableValues) {
214
                if( availableValues == null ) {
215
                        this.availableValues = null;
216
                } else if( availableValues.isEmpty() ) {
217
                        this.availableValues = null;
218
                } else {
219
                        this.availableValues = (DynObjectValueItem[]) availableValues
220
                                        .toArray(new DynObjectValueItem[availableValues.size()]);
221
                }
222
                return this;
223
        }
224

    
225
        public DynObjectValueItem[] getAvailableValues() {
226
                return availableValues;
227
        }
228

    
229
        public DynField setMinValue(Object minValue) {
230
                this.minValue = minValue;
231
                return this;
232
        }
233

    
234
        public Object getMinValue() {
235
                return minValue;
236
        }
237

    
238
        public DynField setMaxValue(Object maxValue) {
239
                this.maxValue = maxValue;
240
                return this;
241
        }
242

    
243
        public Object getMaxValue() {
244
                return maxValue;
245
        }
246

    
247
        public boolean isMandatory() {
248
                return this.mandatory;
249
        }
250

    
251
        public boolean isPersistent() {
252
                return this.persistent;
253
        }
254

    
255
        public DynField setMandatory(boolean mandatory) {
256
                this.mandatory = mandatory;
257
                return this;
258
        }
259

    
260
        public DynField setPersistent(boolean persistent) {
261
                this.persistent = persistent;
262
                return this;
263
        }
264

    
265
        public DynField setTheTypeOfAvailableValues(int type) {
266
                return this; // FIXME: this method is @deprecated
267
        }
268

    
269
        public int getTheTypeOfAvailableValues() {
270
                return 1; // FIXME: this method is @deprecated
271
        }
272

    
273
        public boolean equals(Object obj) {
274
                if (this == obj) {
275
                        return true;
276
                }
277
                if (obj instanceof DynField) {
278
                        // FIXME: No esta claro que esto sea correcto.
279
                        return name.equals(((DynField) obj).getName());
280
                }
281
                return false;
282
        }
283

    
284
        public Class getClassOfValue() {
285
                return theClass;
286
        }
287

    
288
        public DynField setValidateElements(boolean validate) {
289
                switch (this.dataType) {
290
                case DataTypes.ARRAY:
291
                case DataTypes.LIST:
292
                case DataTypes.MAP:
293
                case DataTypes.SET:
294
                        break;
295
                default:
296
                        throw new DynFieldIsNotAContainerException(this.name);
297
                }
298
                this.validateElements = validate;
299
                return this;
300
        }
301

    
302
        public boolean getValidateElements() {
303
                return this.validateElements;
304
        }
305

    
306
        public DynField setClassOfValue(Class theClass)
307
                        throws DynFieldIsNotAContainerException {
308
                switch (this.dataType) {
309
                case DataTypes.OBJECT:
310
                case DataTypes.ARRAY:
311
                case DataTypes.LIST:
312
                case DataTypes.MAP:
313
                case DataTypes.SET:
314
                        break;
315
                default:
316
                        throw new DynFieldIsNotAContainerException(this.name);
317
                }
318
                this.theClass = theClass;
319
                return this;
320
        }
321

    
322
        public DynField setElementsType(int type)
323
                        throws DynFieldIsNotAContainerException {
324
                if (!isContainer()) {
325
                        throw new DynFieldIsNotAContainerException(this.name);
326
                }
327
                this.elementsType = new DefaultDynField(this.name + "-"
328
                                + ToolsLocator.getDataTypesManager().getTypeName(this.dataType)
329
                                + "-item",type);
330
                return this;
331
        }
332

    
333

    
334
        public DynField setElementsType(DynStruct type)
335
                        throws DynFieldIsNotAContainerException {
336
                this.setElementsType(DataTypes.DYNOBJECT).getElementsType().setSubtype(type.getFullName());
337
                return this;
338
        }
339

    
340
        private boolean isContainer(){
341
            return ToolsLocator.getDataTypesManager().isContainer(this.dataType);
342
        }
343

    
344
        public DynField getElementsType() {
345
                return this.elementsType;
346
        }
347

    
348
        public void validate(Object value) throws DynFieldValidateException {
349
                Comparable v;
350
                if (value == null) {
351
                        if (this.mandatory) {
352
                                throw new DynFieldValidateException(value, this);
353
                        }
354
                        return;
355
                }
356

    
357
                switch (this.dataType) {
358
                case DataTypes.BOOLEAN:
359
                        if (!(value instanceof Boolean)) {
360
                                throw new DynFieldValidateException(value, this);
361
                        }
362
                        break;
363

    
364
                case DataTypes.DOUBLE:
365
                        if (!(value instanceof Double)) {
366
                                throw new DynFieldValidateException(value, this);
367
                        }
368
                        break;
369

    
370
                case DataTypes.FLOAT:
371
                        if (!(value instanceof Float)) {
372
                                throw new DynFieldValidateException(value, this);
373
                        }
374
                        break;
375

    
376
                case DataTypes.INT:
377
                        if (!(value instanceof Integer)) {
378
                                throw new DynFieldValidateException(value, this);
379
                        }
380
                        break;
381

    
382
                case DataTypes.LONG:
383
                        if (!(value instanceof Long)) {
384
                                throw new DynFieldValidateException(value, this);
385
                        }
386
                        break;
387

    
388
                case DataTypes.STRING:
389
                        if (!(value instanceof String)) {
390
                                throw new DynFieldValidateException(value, this);
391
                        }
392
                        break;
393

    
394
                case DataTypes.DATE:
395
                        if (!(value instanceof Date)) {
396
                                throw new DynFieldValidateException(value, this);
397
                        }
398
                        break;
399

    
400
                case DataTypes.LIST:
401
                        if (!(value instanceof List)) {
402
                                throw new DynFieldValidateException(value, this);
403
                        }
404
                        if (this.validateElements && this.elementsType != null) {
405
                                Iterator it = ((List) value).iterator();
406
                                while (it.hasNext()) {
407
                                        this.elementsType.validate(it.next());
408
                                }
409
                        }
410
                        break;
411

    
412
                case DataTypes.MAP:
413
                        if (!(value instanceof Map)) {
414
                                throw new DynFieldValidateException(value, this);
415
                        }
416
                        break;
417
                case DataTypes.FILE:
418
                    if (!(value instanceof File)) {
419
                        throw new DynFieldValidateException(value, this);
420
                    }
421
                    break;
422
                case DataTypes.FOLDER:
423
                    if (!(value instanceof File)) {
424
                        throw new DynFieldValidateException(value, this);
425
                    }
426
                    break;
427
                case DataTypes.SET:
428
                        if (!(value instanceof Set)) {
429
                                throw new DynFieldValidateException(value, this);
430
                        }
431
                        break;
432

    
433
                case DataTypes.OBJECT:
434
                        if (this.theClass != null) {
435
                                if (!this.theClass.isInstance(value)) {
436
                                        throw new DynFieldValidateException(value, this);
437
                                }
438
                        }
439
                        break;
440

    
441
                case DataTypes.DYNOBJECT:
442
                        if (!(value instanceof DynObject)) {
443
                                throw new DynFieldValidateException(value, this);
444
                        }
445
                        DynClass dynClass = ToolsLocator.getDynObjectManager().get(
446
                                        this.getSubtype());
447
                        if ( dynClass==null || !dynClass.isInstance((DynObject) value)) {
448
                                throw new DynFieldValidateException(value, this);
449
                        }
450
                        try {
451
                                dynClass.validate((DynObject) value);
452
                        } catch (DynObjectValidateException e) {
453
                                throw new DynFieldValidateException(value, this, e);
454
                        }
455
                        break;
456

    
457
                case DataTypes.ARRAY:
458
                        // TODO: falta verificar que es un array del tipo que toca.
459
                        break;
460

    
461
                default:
462
                        throw new DynFieldValidateException(value, this);
463
                }
464

    
465
                if (this.mandatory && value == null) {
466
                        throw new DynFieldValidateException(value, this);
467
                }
468

    
469
                if (this.getAvailableValues() != null) {
470
                        if (!(value instanceof Comparable)) {
471
                                throw new DynFieldValidateException(value, this);
472
                        }
473
                        v = (Comparable) value;
474
                        boolean ok = false;
475
                        for (int i = 0; i < this.availableValues.length; i++) {
476
                                if (v.compareTo(this.availableValues[i].getValue()) == 0) {
477
                                        ok = true;
478
                                        break;
479
                                }
480
                        }
481
                        if (!ok) {
482
                                throw new DynFieldValidateException(value, this);
483
                        }
484
                } else if (this.getMaxValue() != null && this.getMinValue() != null) {
485
                        if (!(value instanceof Comparable)) {
486
                                throw new DynFieldValidateException(value, this);
487
                        }
488
                        v = (Comparable) value;
489
                        if (v.compareTo(this.minValue) < 0
490
                                        || v.compareTo(this.maxValue) > 0) {
491
                                throw new DynFieldValidateException(value, this);
492
                        }
493
                }
494
        }
495

    
496
        public Object coerce(Object value) throws CoercionException {
497
                if (value == null) {
498
                        return value; // O debe retorna this.defaultValue
499
                }
500
                DataTypesManager manager = ToolsLocator.getDataTypesManager();
501
                return manager.coerce(this.dataType, value);
502
        }
503

    
504

    
505
        public String getGroup() {
506
                return this.groupName;
507
        }
508

    
509
        public DynField setGroup(String groupName) {
510
                this.groupName = groupName;
511
                return this;
512
        }
513

    
514
        public int getOder() {
515
                return this.order;
516
        }
517

    
518
        public DynField setOrder(int order) {
519
                this.order = order;
520
                return this;
521
        }
522

    
523
        public boolean isHidden() {
524
                return this.hidden;
525
        }
526

    
527
        public DynField setHidden(boolean hidden) {
528
                this.hidden = hidden;
529
                return this;
530
        }
531
        
532
        public boolean isReadOnly() {
533
            return this.isReadOnly;
534
        }
535

    
536
        
537
        public DynField setReadOnly(boolean isReadOnly) {
538
            this.isReadOnly = isReadOnly;
539
            return this;
540
        }
541

    
542

    
543
}