Revision 1118 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dynobject/impl/DefaultDynField.java

View differences:

DefaultDynField.java
10 10
 *
11 11
 * This program is distributed in the hope that it will be useful,
12 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14
 * GNU General Public License for more details.
15 15
 *
16 16
 * You should have received a copy of the GNU General Public License
17 17
 * along with this program; if not, write to the Free Software
18 18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
19
 * MA 02110-1301, USA.
20 20
 *
21 21
 * For any additional information, do not hesitate to contact us
22 22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
......
26 26
import java.io.File;
27 27
import java.net.URI;
28 28
import java.net.URL;
29
import java.util.Collection;
29 30
import java.util.Date;
30 31
import java.util.Iterator;
31 32
import java.util.List;
32 33
import java.util.Map;
33 34
import java.util.Set;
35
import org.apache.commons.lang3.StringUtils;
34 36

  
35 37
import org.gvsig.tools.ToolsLocator;
36 38
import org.gvsig.tools.dataTypes.CoercionException;
37 39
import org.gvsig.tools.dataTypes.DataType;
38 40
import org.gvsig.tools.dataTypes.DataTypes;
39 41
import org.gvsig.tools.dataTypes.DataTypesManager;
40
import org.gvsig.tools.dynobject.DynClass;
41 42
import org.gvsig.tools.dynobject.DynField;
42 43
import org.gvsig.tools.dynobject.DynField_v2;
43 44
import org.gvsig.tools.dynobject.DynObject;
44
import org.gvsig.tools.dynobject.DynObjectException;
45
import org.gvsig.tools.dynobject.DynObjectManager;
46 45
import org.gvsig.tools.dynobject.DynObjectValueItem;
47 46
import org.gvsig.tools.dynobject.DynStruct;
48 47
import org.gvsig.tools.dynobject.Tags;
......
55 54
import org.slf4j.LoggerFactory;
56 55

  
57 56
public class DefaultDynField implements DynField_v2 {
58
	private String name;
59
	private String description;
60 57

  
61
	private DataType dataType;
62
	private String subtype;
58
    public static final Logger log = LoggerFactory.getLogger(DefaultDynField.class);
63 59

  
64
	private Object defaultValue;
60
    private String name;
61
    private String description;
65 62

  
66
	private int order;
67
	private boolean hidden;
68
	private String groupName;
69
	private DynObjectValueItem[] availableValues;
70
	private Object minValue;
71
	private Object maxValue;
72
	private boolean mandatory;
73
	private boolean persistent;
74
	private Class theClass;
75
	//private DynField elementsType;
76
	private int elementsType;
77
	
78
	private boolean validateElements;
79
	private boolean isReadOnly;
80
	private Class theClassOfItems = null;
81
	private String label = null;
82
	private DynStruct structWhenTypeIsDynObject = null;
83
	private Tags tags = new DefaultTags();
84
	
85
	public static final Logger log = LoggerFactory.getLogger(DefaultDynField.class);
86
        
63
    private ValueType type;
64

  
65
    private String subtype;
66

  
67
    private Object defaultValue;
68

  
69
    private int order;
70
    private boolean hidden;
71
    private String groupName;
72
    private DynObjectValueItem[] availableValues;
73
    private Object minValue;
74
    private Object maxValue;
75
    private boolean mandatory;
76
    private boolean persistent;
77
    private String label = null;
78

  
79
    private boolean validateElements;
80
    private boolean isReadOnly;
81
    private Tags tags = new DefaultTags();
82

  
83
    private ValueType itemsType;
84
    private boolean validateItems;
85

  
87 86
//        Para implementacion futura, como minimo para implementar
88 87
//        un copy entre dynobject y saber cuando parar.
89 88
//        Los valores deberan ser algo asi como:
......
92 91
//        - Colaboracion, 1:1, no se copian
93 92
//        Y solo tendra efecto/sentido cuando el field sea de tipo
94 93
//        DynObject, y tal vez lista de DynObject
95
        private int relationType; 
96
        
97
	public void check() throws ListBaseException {
98
		ListBaseException exceptions = null;
94
    private int relationType;
99 95

  
100
		if (name == null) {
101
			exceptions = CheckDynFieldListException.add(exceptions, name,
102
					"name", name);
103
		}
104
		switch(this.getType()){
105
		case DataTypes.DYNOBJECT:
106
			if(this.structWhenTypeIsDynObject == null){
107
				CheckDynFieldListException.add(exceptions, name,
108
						"structWhenTypeIsDynObject", this.structWhenTypeIsDynObject);
109
			}
110
			break;
111
		case DataTypes.LIST:
112
			if( this.elementsType == DataTypes.DYNOBJECT ) {
113
				if(this.structWhenTypeIsDynObject == null){
114
					try{
115
						int n = 1/0;
116
					}catch(Exception e){
117
						log.warn("El campo "+ name+ " est? declarado como DynObject y su estructura no est? inicializada", e);
118
					}
119
					/*
120
					 * Si entra en este IF deberia petar.
121
					 * Se mantiene este c?digo por intentar mantener compatibilidad con lo anterior
122
					 * pero es un c?digo DEPRECATED
123
					 * 11-07-2014
124
					 */
125
					if( !this.subtype.equalsIgnoreCase("DynObject") ) {
126
						DynObjectManager mgr = ToolsLocator.getDynObjectManager();
127
						this.structWhenTypeIsDynObject = (DynStruct) mgr.createDynClassName(this.subtype);
128
						this.subtype = "DynObject";
129
					}
130
				}	
131
			}
132
			break;
133
		default:
134
				
135
		}
136
		if (exceptions != null) {
137
			throw exceptions;
138
		}
139
	}
96
    public DefaultDynField(String name, int dataType) {
97
        this(name, // field name
98
                dataType, // data type
99
                null, // default value
100
                true, // persistent
101
                false // mandatory
102
        );
103
    }
140 104

  
141
	public static class CheckDynFieldListException extends ListBaseException {
105
    protected DefaultDynField(String name, int dataType, Object defaultValue,
106
            boolean persistent, boolean mandatory) {
107
        DataTypesManager datamanager = ToolsLocator.getDataTypesManager();
142 108

  
143
		public static class CheckDynFieldException extends DynObjectException {
109
        if ( StringUtils.isBlank(name) ) {
110
            throw new IllegalArgumentException("name can't be null");
111
        }
112
        this.name = name;
113
        this.type = new ValueType(dataType);
114
        this.subtype = this.getSubtype();
144 115

  
145
			/**
146
			 * 
147
			 */
148
			private static final long serialVersionUID = 2486744641818117262L;
116
        this.defaultValue = defaultValue;
117
        this.persistent = persistent;
118
        this.mandatory = mandatory;
119
        this.validateElements = false;
120
        this.groupName = null;
121
        this.order = 0;
122
        this.hidden = false;
123
        this.availableValues = null;
149 124

  
150
			public CheckDynFieldException(String attrname, Object attrvalue) {
151
				super("Wrong value %(value) for attribute %(name).",
152
						"Wrong_value_XvalueX_for_attribute_XnameX",
153
						serialVersionUID);
154
			}
155
		}
125
        this.itemsType = new ValueType(DataTypes.UNKNOWN);
126
        this.validateItems = false;
127
    }
156 128

  
157
		/**
158
		 * 
159
		 */
160
		private static final long serialVersionUID = 1L;
129
    protected ValueType getValueType() {
130
        return type;
131
    }
161 132

  
162
		public CheckDynFieldListException(String name) {
163
			super("Inconsistent field %(name) definition.",
164
					"_Inconsistent_field_XnameX_definition", serialVersionUID);
165
			if (name == null) {
166
				name = "[unknow]";
167
			}
168
			setValue("name", name);
169
		}
133
    public void check() throws ListBaseException {
134
    }
170 135

  
171
		public static ListBaseException add(ListBaseException exceptions,
172
				String name, String attrname, Object attrvalue) {
173
			if (exceptions == null) {
174
				exceptions = new CheckDynFieldListException(name);
175
			}
176
			exceptions.add(new CheckDynFieldException(attrname, attrvalue));
177
			return exceptions;
178
		}
179
	}
136
    public String toString() {
137
        StringBuffer buffer = new StringBuffer();
180 138

  
181
	public DefaultDynField(String name) {
182
		this(name, // field name
183
				DataTypes.STRING, // data type
184
				null, // default value
185
				true, // persistent
186
				false // mandatory
187
		);
188
	}
139
        buffer.append("DynField").append("[").append(this.hashCode())
140
                .append("]").append("( ").append("name='").append(this.name)
141
                .append("', ").append("description='").append(this.description)
142
                .append("', ").append("type='").append(this.type)
143
                .append("', ").append("subType='").append(this.subtype)
144
                .append("', ").append("mandatory='").append(this.isMandatory())
145
                .append("', ").append("defaultValue='")
146
                .append(this.getDefaultValue()).append("', ")
147
                .append("minValue='").append(this.minValue).append("', ")
148
                .append("maxValue='").append(this.maxValue).append("', ")
149
                .append("persistent='").append(this.isPersistent())
150
                .append(" )");
151
        return buffer.toString();
152
    }
189 153

  
190
	public DefaultDynField(String name, int dataType) {
191
		this(name, // field name
192
				dataType, // data type
193
				null, // default value
194
				true, // persistent
195
				false // mandatory
196
		);
197
	}
154
    public String getName() {
155
        return name;
156
    }
198 157

  
199
	public DefaultDynField(String name, int dataType, Object defaultValue,
200
			boolean persistent, boolean mandatory) {
201
		DataTypesManager datamanager = ToolsLocator.getDataTypesManager();
158
    public DynField setName(String name) {
159
        if ( StringUtils.isBlank(name) ) {
160
            throw new IllegalArgumentException("name can't be null");
161
        }
162
        this.name = name;
163
        return this;
164
    }
202 165

  
203
		this.name = name;
204
                if( dataType==DataTypes.UNKNOWN ) {
205
                    this.dataType = null;
206
                } else {
207
                    this.dataType = datamanager.get(dataType);
208
                    this.subtype = this.dataType.getSubtype();
209
                }
210
		this.defaultValue = defaultValue;
211
		this.persistent = persistent;
212
		this.mandatory = mandatory;
213
		this.theClass = null;
214
		this.validateElements = false;
215
		this.groupName = null;
216
		this.order = 0;
217
		this.hidden = false;
218
		this.availableValues = null;
219
	}
166
    public DynField setDescription(String description) {
167
        this.description = description;
168
        return this;
169
    }
220 170

  
221
	public String toString() {
222
		StringBuffer buffer = new StringBuffer();
171
    public String getDescription() {
172
        return (description == null) ? getLabel() : description;
173
    }
223 174

  
224
		buffer.append("DynField").append("[").append(this.hashCode())
225
				.append("]").append("( ").append("name='").append(this.name)
226
				.append("', ").append("description='").append(this.description)
227
				.append("', ").append("type='").append(this.dataType.getName())
228
				.append("', ").append("subType='").append(this.subtype)
229
				.append("', ").append("mandatory='").append(this.isMandatory())
230
				.append("', ").append("defaultValue='")
231
				.append(this.getDefaultValue()).append("', ")
232
				.append("dataType=[").append(this.dataType).append("], ")
233
				.append("minValue='").append(this.minValue).append("', ")
234
				.append("maxValue='").append(this.maxValue).append("', ")
235
				.append("persistent='").append(this.isPersistent())
236
				.append(" )");
237
		return buffer.toString();
238
	}
175
    public DynField setLabel(String label) {
176
        this.label = label;
177
        return this;
178
    }
239 179

  
240
	public String getName() {
241
		return name;
242
	}
243
        
244
        public DynField setName(String name) {
245
            this.name = name;
246
            return this;
247
        }
180
    public String getLabel() {
181
        return (label == null) ? getName() : label;
182
    }
248 183

  
249
	public DynField setDescription(String description) {
250
		this.description = description;
251
		return this;
252
	}
184
    public DynField setType(int dataType) {
185
        this.type.setType(dataType);
186
        return this;
187
    }
253 188

  
254
	public String getDescription() {
255
		return (description == null) ? getLabel() : description;
256
	}
189
    public DynField setType(DataType dataType) {
190
        this.type.setType(dataType);
191
        return this;
192
    }
257 193

  
258
	public DynField setLabel(String label) {
259
		this.label = label;
260
		return this;
261
	}
194
    public int getType() {
195
        return this.type.getType();
196
    }
262 197

  
263
	public String getLabel() {
264
		return (label == null) ? getName() : label;
265
	}
266
	
267
	public DynField setType(int dataType) {
268
		DataTypesManager datamanager = ToolsLocator.getDataTypesManager();
269
		return setType(datamanager.get(dataType));
270
	}
198
    public DataType getDataType() {
199
        return this.type.getDataType();
200
    }
271 201

  
272
	public DynField setType(DataType dataType) {
273
		this.dataType = dataType;
274
		this.theClass = this.dataType.getDefaultClass();
275
		this.subtype = this.dataType.getSubtype();
276
		return this;
277
	}
202
    public DynField setSubtype(String subtype) {
203
        this.subtype = subtype;
204
        return this;
205
    }
278 206

  
279
	public int getType() {
280
            if( dataType==null ) {
281
                return DataTypes.UNKNOWN;
282
            }
283
            return dataType.getType();
284
	}
207
    public String getSubtype() {
208
        return subtype;
209
    }
285 210

  
286
	public DataType getDataType() {
287
		return this.dataType;
288
	}
211
    public DynField setDefaultDynValue(Object defaultValue) {
212
        this.defaultValue = defaultValue;
213
        return this;
214
    }
289 215

  
290
	public DynField setSubtype(String subtype) {
291
		this.subtype = subtype;
292
		/*
293
		if (subtype != null && this.dataType.getType() == DataTypes.LIST
294
				&& this.structWhenTypeIsDynObject != null ) {
295
			if (ToolsLocator.getDynObjectManager().get(subtype) == null) {
296
				throw new IllegalArgumentException("DynClass '" + subtype
297
						+ "' does not exist.");
298
			}
299
		} else if (subtype != null
300
				&& this.dataType.getType() == DataTypes.DYNOBJECT) {
301
			if (ToolsLocator.getDynObjectManager().get(subtype) == null) {
302
				throw new IllegalArgumentException("DynClass '" + subtype
303
						+ "' does not exist.");
304
			}
305
		}
306
		*/
307
		return this;
308
	}
216
    public Object getDefaultValue() {
217
        return defaultValue;
218
    }
309 219

  
310
	public String getSubtype() {
311
		return subtype;
312
	}
220
    public DynField setAvailableValues(DynObjectValueItem[] availableValues) {
221
        if ( availableValues == null || availableValues.length == 0 ) {
222
            this.availableValues = null;
223
        } else {
224
            this.availableValues = availableValues;
225
        }
226
        return this;
227
    }
313 228

  
314
	public DynField setDefaultDynValue(Object defaultValue) {
315
		this.defaultValue = defaultValue;
316
		return this;
317
	}
229
    public DynField setAvailableValues(List availableValues) {
230
        if ( availableValues == null ) {
231
            this.availableValues = null;
232
        } else if ( availableValues.isEmpty() ) {
233
            this.availableValues = null;
234
        } else {
235
            this.availableValues = (DynObjectValueItem[]) availableValues
236
                    .toArray(new DynObjectValueItem[availableValues.size()]);
237
        }
238
        return this;
239
    }
318 240

  
319
	public Object getDefaultValue() {
320
		return defaultValue;
321
	}
241
    public DynObjectValueItem[] getAvailableValues() {
242
        return availableValues;
243
    }
322 244

  
323
	public DynField setStructWhenTypeIsDynObject(DynStruct dynStruct) {
324
		if( dynStruct==null ) {
325
			this.structWhenTypeIsDynObject = null;
326
			this.theClassOfItems = null;
327
			//this.subtype = null;
328
			this.elementsType = DataTypes.UNKNOWN; 
329
		} else {
330
			this.structWhenTypeIsDynObject = dynStruct;
331
			this.theClassOfItems = DynObject.class;
332
			//this.subtype = "DynObject";
333
			//this.elementsType = DataTypes.DYNOBJECT; 
334
		}
335
		return this;
336
	}
337
	
338
	public DynStruct getStructWhenTypeIsDynObject() {
339
		return this.structWhenTypeIsDynObject;
340
	}
341
	
342
	public DynField setAvailableValues(DynObjectValueItem[] availableValues) {
343
		if (availableValues == null || availableValues.length == 0) {
344
			this.availableValues = null;
345
		} else {
346
			this.availableValues = availableValues;
347
		}
348
		return this;
349
	}
245
    public DynField setMinValue(Object minValue) {
246
        try {
247
            this.minValue = this.coerce(minValue);
248
        } catch (CoercionException e) {
249
            IllegalArgumentException ex = new IllegalArgumentException(e.getLocalizedMessage());
250
            ex.initCause(e);
251
            throw ex;
252
        }
253
        return this;
254
    }
350 255

  
351
	public DynField setAvailableValues(List availableValues) {
352
		if (availableValues == null) {
353
			this.availableValues = null;
354
		} else if (availableValues.isEmpty()) {
355
			this.availableValues = null;
356
		} else {
357
			this.availableValues = (DynObjectValueItem[]) availableValues
358
					.toArray(new DynObjectValueItem[availableValues.size()]);
359
		}
360
		return this;
361
	}
256
    public Object getMinValue() {
257
        return minValue;
258
    }
362 259

  
363
	public DynObjectValueItem[] getAvailableValues() {
364
		return availableValues;
365
	}
260
    public DynField setMaxValue(Object maxValue) {
261
        try {
262
            this.maxValue = this.coerce(maxValue);
263
        } catch (CoercionException e) {
264
            IllegalArgumentException ex = new IllegalArgumentException(e.getLocalizedMessage());
265
            ex.initCause(e);
266
            throw ex;
267
        }
268
        return this;
269
    }
366 270

  
367
	public DynField setMinValue(Object minValue) {
368
		try {
369
			this.minValue = this.coerce(minValue);
370
		} catch (CoercionException e) {
371
			IllegalArgumentException ex = new IllegalArgumentException(e.getLocalizedMessage());
372
			ex.initCause(e);
373
			throw ex;
374
		}
375
		return this;
376
	}
271
    public Object getMaxValue() {
272
        return maxValue;
273
    }
377 274

  
378
	public Object getMinValue() {
379
		return minValue;
380
	}
275
    public boolean isMandatory() {
276
        return this.mandatory;
277
    }
381 278

  
382
	public DynField setMaxValue(Object maxValue) {
383
		try {
384
			this.maxValue = this.coerce(maxValue);
385
		} catch (CoercionException e) {
386
			IllegalArgumentException ex = new IllegalArgumentException(e.getLocalizedMessage());
387
			ex.initCause(e);
388
			throw ex;
389
		}
390
		return this;
391
	}
279
    public boolean isPersistent() {
280
        return this.persistent;
281
    }
392 282

  
393
	public Object getMaxValue() {
394
		return maxValue;
395
	}
283
    public DynField setMandatory(boolean mandatory) {
284
        this.mandatory = mandatory;
285
        return this;
286
    }
396 287

  
397
	public boolean isMandatory() {
398
		return this.mandatory;
399
	}
288
    public DynField setPersistent(boolean persistent) {
289
        this.persistent = persistent;
290
        return this;
291
    }
400 292

  
401
	public boolean isPersistent() {
402
		return this.persistent;
403
	}
293
    public DynField setTheTypeOfAvailableValues(int type) {
294
        return this; // FIXME: this method is @deprecated
295
    }
404 296

  
405
	public DynField setMandatory(boolean mandatory) {
406
		this.mandatory = mandatory;
407
		return this;
408
	}
297
    public int getTheTypeOfAvailableValues() {
298
        return 1; // FIXME: this method is @deprecated
299
    }
409 300

  
410
	public DynField setPersistent(boolean persistent) {
411
		this.persistent = persistent;
412
		return this;
413
	}
301
    public boolean equals(Object obj) {
302
        if ( this == obj ) {
303
            return true;
304
        }
305
        if ( obj instanceof DynField ) {
306
            // FIXME: No esta claro que esto sea correcto.
307
            return name.equals(((DynField) obj).getName());
308
        }
309
        return false;
310
    }
414 311

  
415
	public DynField setTheTypeOfAvailableValues(int type) {
416
		return this; // FIXME: this method is @deprecated
417
	}
312
    public Class getClassOfValue() {
313
        return this.type.getClassOfValue();
314
    }
418 315

  
419
	public int getTheTypeOfAvailableValues() {
420
		return 1; // FIXME: this method is @deprecated
421
	}
316
    public DynField setClassOfValue(Class theClass) {
317
        this.type.setClassOfValue(theClass);
318
        return this;
319
    }
422 320

  
423
	public boolean equals(Object obj) {
424
		if (this == obj) {
425
			return true;
426
		}
427
		if (obj instanceof DynField) {
428
			// FIXME: No esta claro que esto sea correcto.
429
			return name.equals(((DynField) obj).getName());
430
		}
431
		return false;
432
	}
321
    public DynField setClassOfValue(String theClassName) {
322
        this.type.setClassOfValue(theClassName);
323
        return this;
324
    }
433 325

  
434
	public Class getClassOfValue() {
435
		return theClass;
436
	}
326
    public boolean isContainer() {
327
        if( type.getDataType()==null ) {
328
            return false;
329
        }
330
        return type.getDataType().isContainer();
331
    }
437 332

  
438
	public DynField setValidateElements(boolean validate) {
439
		if (!this.dataType.isContainer()) {
440
			throw new DynFieldIsNotAContainerException(this.name);
441
		}
442
		this.validateElements = validate;
443
		return this;
444
	}
333
    public void validate(Object value) throws DynFieldValidateException {
334
        Comparable v;
335
        if ( value == null ) {
336
            if ( this.mandatory ) {
337
                throw new DynFieldRequiredValueException(this, value);
338
            }
339
            return;
340
        }
445 341

  
446
	public boolean getValidateElements() {
447
		return this.validateElements;
448
	}
342
        switch (this.type.getType()) {
343
        case DataTypes.BOOLEAN:
344
            if ( !(value instanceof Boolean) ) {
345
                throw new DynFieldValidateException(value, this);
346
            }
347
            break;
449 348

  
450
	public DynField setClassOfValue(Class theClass)
451
			throws DynFieldIsNotAContainerException {
452
		this.theClass = theClass;
453
		return this;
454
	}
349
        case DataTypes.DOUBLE:
350
            if ( !(value instanceof Double) ) {
351
                throw new DynFieldValidateException(value, this);
352
            }
353
            break;
455 354

  
355
        case DataTypes.FLOAT:
356
            if ( !(value instanceof Float) ) {
357
                throw new DynFieldValidateException(value, this);
358
            }
359
            break;
456 360

  
361
        case DataTypes.BYTE:
362
            if ( !(value instanceof Byte) ) {
363
                throw new DynFieldValidateException(value, this);
364
            }
365
            break;
457 366

  
458
	
459
	public boolean isContainer() {
460
		return this.dataType.isContainer();
461
	}
367
        case DataTypes.INT:
368
            if ( !(value instanceof Integer) ) {
369
                throw new DynFieldValidateException(value, this);
370
            }
371
            break;
462 372

  
463
	public void validate(Object value) throws DynFieldValidateException {
464
		Comparable v;
465
		if (value == null) {
466
			if (this.mandatory) {
467
				throw new DynFieldRequiredValueException(this, value);
468
			}
469
			return;
470
		}
373
        case DataTypes.LONG:
374
            if ( !(value instanceof Long) ) {
375
                throw new DynFieldValidateException(value, this);
376
            }
377
            break;
471 378

  
472
		switch (this.dataType.getType()) {
473
		case DataTypes.BOOLEAN:
474
			if (!(value instanceof Boolean)) {
475
				throw new DynFieldValidateException(value, this);
476
			}
477
			break;
379
        case DataTypes.STRING:
380
            if ( !(value instanceof String) ) {
381
                throw new DynFieldValidateException(value, this);
382
            }
383
            break;
478 384

  
479
		case DataTypes.DOUBLE:
480
			if (!(value instanceof Double)) {
481
				throw new DynFieldValidateException(value, this);
482
			}
483
			break;
385
        case DataTypes.CHAR:
386
            if ( !(value instanceof String) ) {
387
                throw new DynFieldValidateException(value, this);
388
            }
389
            if ( ((String) value).length() > 1 ) {
390
                throw new DynFieldValidateException(value, this);
391
            }
392
            break;
484 393

  
485
		case DataTypes.FLOAT:
486
			if (!(value instanceof Float)) {
487
				throw new DynFieldValidateException(value, this);
488
			}
489
			break;
394
        case DataTypes.DATE:
395
            if ( !(value instanceof Date) ) {
396
                throw new DynFieldValidateException(value, this);
397
            }
398
            break;
490 399

  
491
		case DataTypes.BYTE:
492
			if (!(value instanceof Byte)) {
493
				throw new DynFieldValidateException(value, this);
494
			}
495
			break;
496
			
497
		case DataTypes.INT:
498
			if (!(value instanceof Integer)) {
499
				throw new DynFieldValidateException(value, this);
500
			}
501
			break;
400
        case DataTypes.TIMESTAMP:
401
            if ( !(value instanceof Date) ) {
402
                throw new DynFieldValidateException(value, this);
403
            }
404
            break;
502 405

  
503
		case DataTypes.LONG:
504
			if (!(value instanceof Long)) {
505
				throw new DynFieldValidateException(value, this);
506
			}
507
			break;
406
        case DataTypes.TIME:
407
            if ( !(value instanceof Date) ) {
408
                throw new DynFieldValidateException(value, this);
409
            }
410
            break;
508 411

  
509
		case DataTypes.STRING:
510
			if (!(value instanceof String)) {
511
				throw new DynFieldValidateException(value, this);
512
			}
513
			break;
412
        case DataTypes.FILE:
413
            if ( !(value instanceof File) ) {
414
                throw new DynFieldValidateException(value, this);
415
            }
416
            break;
417
        case DataTypes.FOLDER:
418
            if ( !(value instanceof File) ) {
419
                throw new DynFieldValidateException(value, this);
420
            }
421
            break;
422
        case DataTypes.URI:
423
            if ( !(value instanceof URI) ) {
424
                throw new DynFieldValidateException(value, this);
425
            }
426
            break;
427
        case DataTypes.URL:
428
            if ( !(value instanceof URL) ) {
429
                throw new DynFieldValidateException(value, this);
430
            }
431
            break;
514 432

  
515
		case DataTypes.CHAR:
516
			if (!(value instanceof String)) {
517
				throw new DynFieldValidateException(value, this);
518
			}
519
			if( ((String)value).length()>1 ) {
520
				throw new DynFieldValidateException(value, this);
521
			}
522
			break;
433
        case DataTypes.ARRAY:
434
            // TODO: falta verificar que es un array del tipo que toca.
435
            break;
523 436

  
524
		case DataTypes.DATE:
525
			if (!(value instanceof Date)) {
526
				throw new DynFieldValidateException(value, this);
527
			}
528
			break;
437
        case DataTypes.OBJECT:
438
            if ( this.type.getClassOfValue() != null ) {
439
                if ( !this.type.getClassOfValue().isInstance(value) ) {
440
                    throw new DynFieldValidateException(value, this);
441
                }
442
            }
443
            break;
529 444

  
530
		case DataTypes.TIMESTAMP:
531
			if (!(value instanceof Date)) {
532
				throw new DynFieldValidateException(value, this);
533
			}
534
			break;
445
        case DataTypes.MAP:
446
            if ( !(value instanceof Map) ) {
447
                throw new DynFieldValidateException(value, this);
448
            }
449
            validateCollection(value);
450
            break;
535 451

  
536
		case DataTypes.TIME:
537
			if (!(value instanceof Date)) {
538
				throw new DynFieldValidateException(value, this);
539
			}
540
			break;
452
        case DataTypes.SET:
453
            if ( !(value instanceof Set) ) {
454
                throw new DynFieldValidateException(value, this);
455
            }
456
            validateCollection(value);
457
            break;
458
        
459
        case DataTypes.LIST:
460
            if ( !(value instanceof List) ) {
461
                throw new DynFieldValidateException(value, this);
462
            }
463
            validateCollection(value);
464
            break;
541 465

  
542
		case DataTypes.LIST:
543
			if (!(value instanceof List)) {
544
				throw new DynFieldValidateException(value, this);
545
			}
546
			/*
547
			if (this.validateElements && this.elementsType != null) {
548
				Iterator it = ((List) value).iterator();
549
				while (it.hasNext()) {
550
					this.elementsType.validate(it.next());
551
				}
552
			}
553
			*/
554
			break;
466
        default:
467
            throw new DynFieldValidateException(value, this);
468
        }
555 469

  
556
		case DataTypes.MAP:
557
			if (!(value instanceof Map)) {
558
				throw new DynFieldValidateException(value, this);
559
			}
560
			break;
561
		case DataTypes.FILE:
562
			if (!(value instanceof File)) {
563
				throw new DynFieldValidateException(value, this);
564
			}
565
			break;
566
		case DataTypes.FOLDER:
567
			if (!(value instanceof File)) {
568
				throw new DynFieldValidateException(value, this);
569
			}
570
			break;
571
		case DataTypes.URI:
572
			if (!(value instanceof URI)) {
573
				throw new DynFieldValidateException(value, this);
574
			}
575
			break;
576
		case DataTypes.URL:
577
			if (!(value instanceof URL)) {
578
				throw new DynFieldValidateException(value, this);
579
			}
580
			break;
581
		case DataTypes.SET:
582
			if (!(value instanceof Set)) {
583
				throw new DynFieldValidateException(value, this);
584
			}
585
			break;
470
        if ( this.getAvailableValues() != null ) {
471
            if ( !(value instanceof Comparable) ) {
472
                throw new DynFieldValidateException(value, this);
473
            }
474
            v = (Comparable) value;
475
            boolean ok = false;
476
            for ( int i = 0; i < this.availableValues.length; i++ ) {
477
                if ( v.compareTo(this.availableValues[i].getValue()) == 0 ) {
478
                    ok = true;
479
                    break;
480
                }
481
            }
482
            if ( !ok ) {
483
                throw new DynFieldValidateException(value, this);
484
            }
485
        } else if ( this.getMaxValue() != null && this.getMinValue() != null ) {
486
            if ( !(value instanceof Comparable) ) {
487
                throw new DynFieldValidateException(value, this);
488
            }
489
            v = (Comparable) value;
490
            if ( v.compareTo(this.minValue) < 0
491
                    || v.compareTo(this.maxValue) > 0 ) {
492
                throw new DynFieldValidateException(value, this);
493
            }
494
        }
495
    }
586 496

  
587
		case DataTypes.DYNOBJECT:
588
			if (!(value instanceof DynObject)) {
589
				throw new DynFieldValidateException(value, this);
590
			}
591
			DynStruct dynClass = this.getStructWhenTypeIsDynObject();
592
			if (dynClass == null || !dynClass.isInstance((DynObject) value)) {
593
				throw new DynFieldValidateException(value, this);
594
			}
595
			try {
596
				dynClass.validate((DynObject) value);
597
			} catch (DynObjectValidateException e) {
598
				throw new DynFieldValidateException(value, this, e);
599
			}
600
			break;
497
    private void validateCollection(Object value) throws ValidateItemException {
498
        if ( this.validateItems ) {
499
            DynStruct dynClass = this.itemsType.getDynClassOfValue();
500
            if ( dynClass != null ) {
501
                int index = 0;
502
                Iterator it = ((Collection) value).iterator();
503
                while ( it.hasNext() ) {
504
                    try {
505
                        dynClass.validate((DynObject) it.next());
506
                        index++;
507
                    } catch (DynObjectValidateException ex) {
508
                        throw new ValidateItemException(ex, index);
509
                    }
510
                }
511
            }
512
        }
601 513

  
602
		case DataTypes.ARRAY:
603
			// TODO: falta verificar que es un array del tipo que toca.
604
			break;
514
    }
605 515

  
606
		default:
607
			if (this.dataType.isObject()) {
608
				if (this.theClass != null) {
609
					if (!this.theClass.isInstance(value)) {
610
						throw new DynFieldValidateException(value, this);
611
					}
612
				}
613
			} else {
614
				if( this.dataType.getClass()==null && ! this.dataType.getClass().isInstance(value) ) {
615
					throw new DynFieldValidateException(value, this);
616
				}
617
			}
618
		}
516
    private static class ValidateItemException extends DynFieldValidateException {
619 517

  
620
		if (this.getAvailableValues() != null) {
621
			if (!(value instanceof Comparable)) {
622
				throw new DynFieldValidateException(value, this);
623
			}
624
			v = (Comparable) value;
625
			boolean ok = false;
626
			for (int i = 0; i < this.availableValues.length; i++) {
627
				if (v.compareTo(this.availableValues[i].getValue()) == 0) {
628
					ok = true;
629
					break;
630
				}
631
			}
632
			if (!ok) {
633
				throw new DynFieldValidateException(value, this);
634
			}
635
		} else if (this.getMaxValue() != null && this.getMinValue() != null) {
636
			if (!(value instanceof Comparable)) {
637
				throw new DynFieldValidateException(value, this);
638
			}
639
			v = (Comparable) value;
640
			if (v.compareTo(this.minValue) < 0
641
					|| v.compareTo(this.maxValue) > 0) {
642
				throw new DynFieldValidateException(value, this);
643
			}
644
		}
645
		//
646
		// This shouldn't be necessary since any assignment passes through the
647
		// coerce function anyway
648
		//
649
		// //if all the above is correct, then we should check that coercing is
650
		// possible
651
		// try {
652
		// coerce(value);
653
		// } catch (CoercionException e) {
654
		// throw new DynFieldValidateException(value, this);
655
		// }
518
        private static final long serialVersionUID = 9011437364983996567L;
656 519

  
657
	}
520
        ValidateItemException(Throwable cause, int index) {
521
            super(
522
                    "Can't validate item %(index) of the collection.",
523
                    cause,
524
                    "_Cant_validate_item_%(index)_of_the_collection",
525
                    serialVersionUID
526
            );
527
            setValue("index", Integer.valueOf(index));
528
        }
529
    }
658 530

  
659
	public Object coerce(Object value) throws CoercionException {
660
		if (value == null) {
661
			return value; // O debe devolver this.defaultValue
662
		}
663
		return this.dataType.coerce(value);
664
	}
531
    public Object coerce(Object value) throws CoercionException {
532
        if ( value == null ) {
533
            return value; // O debe devolver this.defaultValue
534
        }
535
        return this.type.getDataType().coerce(value);
536
    }
665 537

  
666
	public String getGroup() {
667
		return this.groupName;
668
	}
538
    public String getGroup() {
539
        return this.groupName;
540
    }
669 541

  
670
	public DynField setGroup(String groupName) {
671
		this.groupName = groupName;
672
		return this;
673
	}
542
    public DynField setGroup(String groupName) {
543
        this.groupName = groupName;
544
        return this;
545
    }
674 546

  
675
	public int getOder() {
676
		return this.order;
677
	}
547
    public int getOder() {
548
        return this.order;
549
    }
678 550

  
679
	public DynField setOrder(int order) {
680
		this.order = order;
681
		return this;
682
	}
551
    public DynField setOrder(int order) {
552
        this.order = order;
553
        return this;
554
    }
683 555

  
684
	public boolean isHidden() {
685
		return this.hidden;
686
	}
556
    public boolean isHidden() {
557
        return this.hidden;
558
    }
687 559

  
688
	public DynField setHidden(boolean hidden) {
689
		this.hidden = hidden;
690
		return this;
691
	}
560
    public DynField setHidden(boolean hidden) {
561
        this.hidden = hidden;
562
        return this;
563
    }
692 564

  
693
	public boolean isReadOnly() {
694
		return this.isReadOnly;
695
	}
565
    public boolean isReadOnly() {
566
        return this.isReadOnly;
567
    }
696 568

  
697
	public DynField setReadOnly(boolean isReadOnly) {
698
		this.isReadOnly = isReadOnly;
699
		return this;
700
	}
569
    public DynField setReadOnly(boolean isReadOnly) {
570
        this.isReadOnly = isReadOnly;
571
        return this;
572
    }
701 573

  
702
	public Class getClassOfItems() {
703
		return this.theClassOfItems;
704
	}
574
    public DynField setDefaultFieldValue(Object defaultValue) {
575
        try {
576
            this.defaultValue = this.coerce(defaultValue);
577
        } catch (CoercionException e) {
578
            IllegalArgumentException ex = new IllegalArgumentException(e.getLocalizedMessage());
579
            ex.initCause(e);
580
            throw ex;
581
        }
582
        return this;
583
    }
705 584

  
706
	public DynField setDefaultFieldValue(Object defaultValue) {
707
		try {
708
			this.defaultValue = this.coerce(defaultValue);
709
		} catch (CoercionException e) {
710
			IllegalArgumentException ex = new IllegalArgumentException(e.getLocalizedMessage());
711
			ex.initCause(e);
712
			throw ex;
713
		}
714
		return this;
715
	}
585
    public Tags getTags() {
586
        return tags;
587
    }
716 588

  
717
	public DynField setClassOfItems(Class theClass)
718
			throws DynFieldIsNotAContainerException {
719
		// Getter allows null values
720
		if (theClass == null) {
721
			return this;
722
		}
723
		if (!this.dataType.isContainer()) {
724
			throw new DynFieldIsNotAContainerException(this.name);
725
		}
726
		this.theClassOfItems = theClass;
727
		return this;
728
	}
729
	
730
	public Tags getTags() {
731
		return tags;
732
	}
733
	
734
	
735
	public DynField setElementsType(int type) {
736
		this.elementsType = type;
737
		return this;
738
	}
589
    public String getClassNameOfValue() {
590
        return this.type.getClassNameOfValue();
591
    }
739 592

  
740
	public DynField setElementsType(DynStruct type)
741
			throws DynFieldIsNotAContainerException {
742
		this.setStructWhenTypeIsDynObject(type);
743
		return this;
744
	}
593
    public DynField setClassOfValue(DynStruct dynStruct) {
594
        this.type.setClassOfValue(dynStruct);
595
        return this;
596
    }
745 597

  
746
	public DynField getElementsType() {
747
		return this;
748
	}
598
    public DynStruct getDynClassOfValue() {
599
        return this.type.getDynClassOfValue();
600
    }
749 601

  
750 602
    public int getRelationType() {
751 603
        return this.relationType;
......
756 608
        return this;
757 609
    }
758 610

  
759
        
760
}
611
    public DynField setElementsType(int type) {
612
        this.setTypeOfItems(type);
613
        return this;
614
    }
615

  
616
    public DynField setElementsType(DynStruct type) {
617
        this.setClassOfItems(type);
618
        return this;
619
    }
620

  
621
    public DynField getElementsType() {
622
        throw new UnsupportedOperationException("This operation is not suported nevermore.");
623
    }
624

  
625
    public DynField setClassOfItems(DynStruct dynStrct) {
626
        if ( !this.isContainer() ) {
627
            throw new IllegalStateException("Can't assign validateElements in non container.");
628
        }
629
        this.itemsType.setClassOfValue(dynStrct);
630
        return this;
631
    }
632

  
633
    public DynField setClassOfItems(String theClassNameOfValue) {
634
        if ( !this.isContainer() ) {
635
            throw new IllegalStateException("Can't assign validateElements in non container.");
636
        }
637
        this.itemsType.setClassOfValue(theClassNameOfValue);
638
        return this;
639
    }
640

  
641
    public String getClassNameOfItems() {
642
        if ( !this.isContainer() ) {
643
            throw new IllegalStateException("Can't assign validateElements in non container.");
644
        }
645
        return this.itemsType.getClassNameOfValue();
646
    }
647

  
648
    public DynStruct getDynClassOfItems() {
649
        if ( !this.isContainer() ) {
650
            throw new IllegalStateException("Can't assign validateElements in non container.");
651
        }
652
        return this.itemsType.getDynClassOfValue();
653
    }
654

  
655
    public DynField setClassOfItems(Class theClass)
656
            throws DynFieldIsNotAContainerException {
657
        if ( !this.isContainer() ) {
658
            throw new IllegalStateException("Can't assign validateElements in non container.");
659
        }
660
        this.itemsType.setClassOfValue(theClass);
661
        return this;
662
    }
663

  
664
    public Class getClassOfItems() {
665
        if ( !this.isContainer() ) {
666
            throw new IllegalStateException("Can't assign validateElements in non container.");
667
        }
668
        return this.itemsType.getClassOfValue();
669
    }
670

  
671
    public DynField setTypeOfItems(int type) {
672
        if ( !this.isContainer() ) {
673
            throw new IllegalStateException("Can't assign validateElements in non container.");
674
        }
675
        this.itemsType.setType(type);
676
        return this;
677
    }
678

  
679
    public int getTypeOfItems() {
680
        if ( !this.isContainer() ) {
681
            throw new IllegalStateException("Can't assign validateElements in non container.");
682
        }
683
        return this.itemsType.getType();
684
    }
685

  
686
    public DynField setValidateElements(boolean validate) {
687
        if ( !this.isContainer() ) {
688
            throw new IllegalStateException("Can't assign validateElements in non container.");
689
        }
690
        this.validateItems = validate;
691
        return this;
692
    }
693

  
694
    public boolean getValidateElements() {
695
        if ( !this.isContainer() ) {
696
            throw new IllegalStateException("Can't assign validateElements in non container.");
697
        }
698
        return this.validateItems;
699
    }
700

  
701
}

Also available in: Unified diff