Revision 130 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
12 12
import org.gvsig.tools.dataTypes.DataTypesManager;
13 13
import org.gvsig.tools.dynobject.DynClass;
14 14
import org.gvsig.tools.dynobject.DynField;
15
import org.gvsig.tools.dynobject.DynMethod;
16 15
import org.gvsig.tools.dynobject.DynObject;
17 16
import org.gvsig.tools.dynobject.DynObjectValueItem;
18 17
import org.gvsig.tools.dynobject.DynStruct;
19 18
import org.gvsig.tools.dynobject.exception.DynFieldIsNotAContainerException;
20 19
import org.gvsig.tools.dynobject.exception.DynFieldValidateException;
21
import org.gvsig.tools.dynobject.exception.DynMethodException;
22 20
import org.gvsig.tools.dynobject.exception.DynObjectValidateException;
23 21

  
24 22
public class DefaultDynField implements DynField {
......
27 25

  
28 26
	private int dataType;
29 27
	private String subtype;
30
	
31 28

  
32 29
	private Object defaultValue;
33 30

  
34 31
	private int order;
35 32
	private boolean hidden;
36
	private String groupName; 
33
	private String groupName;
37 34
	private DynObjectValueItem[] availableValues;
38 35
	private Object minValue;
39 36
	private Object maxValue;
40 37
	private boolean mandatory;
41 38
	private boolean persistent;
42 39
	private Class theClass;
43
	private DynStruct elementsType;
44
	private boolean validateElements; 
40
	private DynField elementsType;
41
	private boolean validateElements;
45 42

  
46 43
	public DefaultDynField(String name) {
47 44
		this(name, // field name
48
				DataTypes.UNKNOWN, // data type
45
				DataTypes.STRING, // data type
49 46
				null, // default value
50 47
				true, // persistent
51 48
				false // mandatory
52 49
		);
53 50
	}
54
	
51

  
55 52
	public DefaultDynField(String name, int dataType, Object defaultValue,
56 53
			boolean persistent, boolean mandatory) {
57 54
		this.name = name;
......
67 64
		this.hidden = false;
68 65
	}
69 66

  
67
	public String toString() {
68
		StringBuffer buffer = new StringBuffer();
70 69

  
71
    public String toString() {
72
    	StringBuffer buffer = new StringBuffer();
73
    	
74
    	buffer.append("DynField").append("[").append(this.hashCode()).append("]").append("( ")
75
    		.append("name='").append(this.name).append("', ")
76
    		.append("description='").append(this.description).append("', ")
77
    		.append("dataType='").append(this.dataType).append(", ")
78
    		.append("minValue='").append(this.minValue).append(", ")
79
    		.append("maxValue='").append(this.maxValue).append(", ")
80
    		.append("mandatory='").append(this.isMandatory()).append(", ")
81
    		.append("persistent='").append(this.isPersistent()).append(", ")
82
    		.append("defaultValue='").append(this.getDefaultValue())
83
    		.append(" )");
84
    	return buffer.toString();
85
    }
70
		buffer.append("DynField").append("[").append(this.hashCode()).append(
71
				"]").append("( ").append("name='").append(this.name).append(
72
				"', ").append("description='").append(this.description).append(
73
				"', ").append("dataType='").append(this.dataType).append(", ")
74
				.append("minValue='").append(this.minValue).append(", ")
75
				.append("maxValue='").append(this.maxValue).append(", ")
76
				.append("mandatory='").append(this.isMandatory()).append(", ")
77
				.append("persistent='").append(this.isPersistent())
78
				.append(", ").append("defaultValue='").append(
79
						this.getDefaultValue()).append(" )");
80
		return buffer.toString();
81
	}
86 82

  
87

  
88 83
	public String getName() {
89 84
		return name;
90 85
	}
......
112 107

  
113 108
	public DynField setSubtype(String subtype) {
114 109
		this.subtype = subtype;
110
		if( subtype!=null && this.dataType == DataTypes.DYNOBJECT ) {
111
			if( ToolsLocator.getDynObjectManager().get(subtype) == null ) {
112
				throw new IllegalArgumentException("DynClass '"+subtype+"' does not exist.");
113
			}
114
		}
115 115
		return this;
116 116
	}
117 117

  
......
134 134
	}
135 135

  
136 136
	public DynField setAvailableValues(List availableValues) {
137
		this.availableValues = (DynObjectValueItem[]) availableValues.toArray(new DynObjectValueItem[availableValues.size()]) ;
137
		this.availableValues = (DynObjectValueItem[]) availableValues
138
				.toArray(new DynObjectValueItem[availableValues.size()]);
138 139
		return this;
139 140
	}
140 141

  
......
179 180
	}
180 181

  
181 182
	public DynField setTheTypeOfAvailableValues(int type) {
182
		return this;  // FIXME: this method is @deprecated
183
		return this; // FIXME: this method is @deprecated
183 184
	}
184 185

  
185 186
	public int getTheTypeOfAvailableValues() {
186 187
		return 1; // FIXME: this method is @deprecated
187 188
	}
188 189

  
190
	public boolean equals(Object obj) {
191
		if (this == obj) {
192
			return true;
193
		}
194
		if (obj instanceof DynField) {
195
			// FIXME: No esta claro que esto sea correcto.
196
			return name.equals(((DynField) obj).getName());
197
		}
198
		return false;
199
	}
189 200

  
190
    public boolean equals(Object obj) {
191
        if (this == obj) {
192
            return true;
193
        }
194
        if (obj instanceof DynField) {
195
        	// FIXME: No esta claro que esto sea correcto.
196
            return name.equals(((DynField) obj).getName());
197
        }
198
        return false;
199
    }
200

  
201

  
202

  
203 201
	public Class getClassOfValue() {
204 202
		return theClass;
205 203
	}
206 204

  
207 205
	public DynField setValidateElements(boolean validate) {
208
		switch(this.dataType) {
206
		switch (this.dataType) {
209 207
		case DataTypes.ARRAY:
210 208
		case DataTypes.LIST:
211 209
		case DataTypes.MAP:
......
217 215
		this.validateElements = validate;
218 216
		return this;
219 217
	}
220
	
218

  
221 219
	public boolean getValidateElements() {
222 220
		return this.validateElements;
223 221
	}
224
	
225
	public DynField setClassOfValue(Class theClass) throws DynFieldIsNotAContainerException{
226
		switch(this.dataType) {
222

  
223
	public DynField setClassOfValue(Class theClass)
224
			throws DynFieldIsNotAContainerException {
225
		switch (this.dataType) {
227 226
		case DataTypes.OBJECT:
228 227
		case DataTypes.ARRAY:
229 228
		case DataTypes.LIST:
......
237 236
		return this;
238 237
	}
239 238

  
240
	public DynField setElementsType(DynStruct dynClass)  throws DynFieldIsNotAContainerException{
241
		switch(this.dataType) {
242
		case DataTypes.OBJECT:
243
		case DataTypes.ARRAY:
244
		case DataTypes.LIST:
245
		case DataTypes.MAP:
246
		case DataTypes.SET:
247
			break;
248
		default:
239
	public DynField setElementsType(int type)
240
			throws DynFieldIsNotAContainerException {
241
		if (!ToolsLocator.getDataTypesManager().isContainer(this.dataType)) {
249 242
			throw new DynFieldIsNotAContainerException(this.name);
250 243
		}
251
		this.elementsType = dynClass;
244
		this.elementsType = new DefaultDynField(this.name + "-"
245
				+ ToolsLocator.getDataTypesManager().getTypeName(this.dataType)
246
				+ "-item");
252 247
		return this;
253 248
	}
254
	
255
	public DynField addElementsType()  throws DynFieldIsNotAContainerException{
256
		switch(this.dataType) {
257
		case DataTypes.ARRAY:
258
		case DataTypes.LIST:
259
		case DataTypes.MAP:
260
		case DataTypes.SET:
261
			break;
262
		default:
263
			throw new DynFieldIsNotAContainerException(this.name);
264
		}
265
		DynField dynField = new DefaultDynField(this.name+"-"+ToolsLocator.getDataTypesManager().getTypeName(this.dataType)+"-item");
266
		this.elementsType = new ElementTypeWrapper(this.name, this.description, dynField);
267
		return dynField;
249

  
250

  
251
	public DynField setElementsType(DynStruct type)
252
			throws DynFieldIsNotAContainerException {
253
		this.setElementsType(DataTypes.DYNOBJECT).getElementsType().setSubtype(type.getFullName());
254
		return this;
268 255
	}
269
	
270
	public DynStruct getElementsType() {
256

  
257

  
258
	public DynField getElementsType() {
271 259
		return this.elementsType;
272 260
	}
273
	
261

  
274 262
	public void validate(Object value) throws DynFieldValidateException {
275 263
		Comparable v;
276
		if( value == null ) {
277
			if( this.mandatory ) {
278
				throw new DynFieldValidateException(value,this);
264
		if (value == null) {
265
			if (this.mandatory) {
266
				throw new DynFieldValidateException(value, this);
279 267
			}
280 268
			return;
281 269
		}
282
		
283
		switch( this.dataType ) {
270

  
271
		switch (this.dataType) {
284 272
		case DataTypes.BOOLEAN:
285
			if( ! (value instanceof Boolean) ) {
286
				throw new DynFieldValidateException(value,this);
273
			if (!(value instanceof Boolean)) {
274
				throw new DynFieldValidateException(value, this);
287 275
			}
288 276
			break;
289
			
277

  
290 278
		case DataTypes.DOUBLE:
291
			if( ! (value instanceof Double) ) {
292
				throw new DynFieldValidateException(value,this);
279
			if (!(value instanceof Double)) {
280
				throw new DynFieldValidateException(value, this);
293 281
			}
294 282
			break;
295
			
283

  
296 284
		case DataTypes.FLOAT:
297
			if( ! (value instanceof Float) ) {
298
				throw new DynFieldValidateException(value,this);
285
			if (!(value instanceof Float)) {
286
				throw new DynFieldValidateException(value, this);
299 287
			}
300 288
			break;
301
			
289

  
302 290
		case DataTypes.INT:
303
			if( ! (value instanceof Integer) ) {
304
				throw new DynFieldValidateException(value,this);
291
			if (!(value instanceof Integer)) {
292
				throw new DynFieldValidateException(value, this);
305 293
			}
306 294
			break;
307
		
295

  
308 296
		case DataTypes.LONG:
309
			if( ! (value instanceof Long) ) {
310
				throw new DynFieldValidateException(value,this);
297
			if (!(value instanceof Long)) {
298
				throw new DynFieldValidateException(value, this);
311 299
			}
312 300
			break;
313
		
301

  
314 302
		case DataTypes.STRING:
315
			if( ! (value instanceof String) ) {
316
				throw new DynFieldValidateException(value,this);
303
			if (!(value instanceof String)) {
304
				throw new DynFieldValidateException(value, this);
317 305
			}
318 306
			break;
319 307

  
320 308
		case DataTypes.DATE:
321
			if( ! (value instanceof Date) ) {
322
				throw new DynFieldValidateException(value,this);
309
			if (!(value instanceof Date)) {
310
				throw new DynFieldValidateException(value, this);
323 311
			}
324 312
			break;
325 313

  
326 314
		case DataTypes.LIST:
327
			if( ! (value instanceof List) ) {
328
				throw new DynFieldValidateException(value,this);
315
			if (!(value instanceof List)) {
316
				throw new DynFieldValidateException(value, this);
329 317
			}
330
			if ( this.validateElements && this.elementsType != null) {
331
				if( this.elementsType instanceof ElementTypeWrapper) {
332
					DynField field = ((ElementTypeWrapper)this.elementsType).getDynField();
333
					Iterator it = ((List)value).iterator();
334
					while( it.hasNext() ) {
335
						field.validate( it.next() );
336
					}
337
				} else {
338
					DynStruct dynClass = this.elementsType;
339
					Iterator it = ((List)value).iterator();
340
					while( it.hasNext() ) {
341
						try {
342
							Object obj = it.next();
343
							dynClass.validate((DynObject) obj);
344
						} catch (DynObjectValidateException e) {
345
							throw new DynFieldValidateException(value,this,e);
346
						}
347
					}
348
					
318
			if (this.validateElements && this.elementsType != null) {
319
				Iterator it = ((List) value).iterator();
320
				while (it.hasNext()) {
321
					this.elementsType.validate(it.next());
349 322
				}
350 323
			}
351 324
			break;
352
			
325

  
353 326
		case DataTypes.MAP:
354
			if( ! (value instanceof Map) ) {
355
				throw new DynFieldValidateException(value,this);
327
			if (!(value instanceof Map)) {
328
				throw new DynFieldValidateException(value, this);
356 329
			}
357 330
			break;
358 331
		case DataTypes.SET:
359
			if( ! (value instanceof Set) ) {
360
				throw new DynFieldValidateException(value,this);
332
			if (!(value instanceof Set)) {
333
				throw new DynFieldValidateException(value, this);
361 334
			}
362 335
			break;
363
		
336

  
364 337
		case DataTypes.OBJECT:
365
			if( this.theClass != null ) {
366
				if(! this.theClass.isInstance(value)) {
367
					throw new DynFieldValidateException(value,this);
338
			if (this.theClass != null) {
339
				if (!this.theClass.isInstance(value)) {
340
					throw new DynFieldValidateException(value, this);
368 341
				}
369
			} else if( this.elementsType!= null ) {
370
				if( ! (value instanceof DynObject) ) {
371
					throw new DynFieldValidateException(value,this);
372
				}
373
				if( !this.elementsType.getFullName().equals(((DynObject)value).getDynClass().getFullName()) )  {
374
					throw new DynFieldValidateException(value,this);
375
				}
376
				try {
377
					((DynObject)value).getDynClass().validate((DynObject)value);
378
				} catch (DynObjectValidateException e) {
379
					throw new DynFieldValidateException(value,this, e);
380
				}
381 342
			}
382 343
			break;
383
			
344

  
345
		case DataTypes.DYNOBJECT:
346
			if (!(value instanceof DynObject)) {
347
				throw new DynFieldValidateException(value, this);
348
			}
349
			DynClass dynClass = ToolsLocator.getDynObjectManager().get(
350
					this.getSubtype());
351
			if ( dynClass==null || !dynClass.isInstance((DynObject) value)) {
352
				throw new DynFieldValidateException(value, this);
353
			}
354
			try {
355
				dynClass.validate((DynObject) value);
356
			} catch (DynObjectValidateException e) {
357
				throw new DynFieldValidateException(value, this, e);
358
			}
359
			break;
360

  
384 361
		case DataTypes.ARRAY:
385 362
			// TODO: falta verificar que es un array del tipo que toca.
386 363
			break;
387
		
364

  
388 365
		default:
389
			throw new DynFieldValidateException(value,this);
366
			throw new DynFieldValidateException(value, this);
390 367
		}
391 368

  
392
		if( this.mandatory && value==null ) {
393
			throw new DynFieldValidateException(value,this);
369
		if (this.mandatory && value == null) {
370
			throw new DynFieldValidateException(value, this);
394 371
		}
395 372

  
396
		if( this.getAvailableValues() != null ) {
397
			if( !(value instanceof Comparable) ) {
398
				throw new DynFieldValidateException(value,this);
373
		if (this.getAvailableValues() != null) {
374
			if (!(value instanceof Comparable)) {
375
				throw new DynFieldValidateException(value, this);
399 376
			}
400
			v = (Comparable)value;
377
			v = (Comparable) value;
401 378
			boolean ok = false;
402
			for( int i=0; i<this.availableValues.length; i++ ) {
403
				if( v.compareTo(this.availableValues[i].getValue())==0) {
379
			for (int i = 0; i < this.availableValues.length; i++) {
380
				if (v.compareTo(this.availableValues[i].getValue()) == 0) {
404 381
					ok = true;
405 382
					break;
406 383
				}
407 384
			}
408
			if( !ok ) {
409
				throw new DynFieldValidateException(value,this);
385
			if (!ok) {
386
				throw new DynFieldValidateException(value, this);
410 387
			}
411
		} else if( this.getMaxValue()!=null && this.getMinValue()!=null ) {
412
			if( !(value instanceof Comparable) ) {
413
				throw new DynFieldValidateException(value,this);
388
		} else if (this.getMaxValue() != null && this.getMinValue() != null) {
389
			if (!(value instanceof Comparable)) {
390
				throw new DynFieldValidateException(value, this);
414 391
			}
415
			v = (Comparable)value;
416
			if( v.compareTo(this.minValue)<0 || v.compareTo(this.maxValue)>0 ) {
417
				throw new DynFieldValidateException(value,this);
392
			v = (Comparable) value;
393
			if (v.compareTo(this.minValue) < 0
394
					|| v.compareTo(this.maxValue) > 0) {
395
				throw new DynFieldValidateException(value, this);
418 396
			}
419 397
		}
420 398
	}
421 399

  
422 400
	public Object coerce(Object value) throws CoercionException {
423
		if( value == null ) {
424
			return value; // O debe retorna this.defaultValue 
425
		} 
401
		if (value == null) {
402
			return value; // O debe retorna this.defaultValue
403
		}
426 404
		DataTypesManager manager = ToolsLocator.getDataTypesManager();
427 405
		return manager.coerce(this.dataType, value);
428 406
	}
429 407

  
430 408

  
431
	
432
	class ElementTypeWrapper implements DynClass {
433

  
434
	    private DynField[] declaredFields;
435
		private String name;
436
		private String description;
437
		
438
		ElementTypeWrapper(String name, String description, DynField dynField) {
439
			this.declaredFields =  new DynField[] { dynField };
440
			this.name = name;
441
			this.description = description;
442
		}
443

  
444
		public DynField getDynField() {
445
			return this.declaredFields[0];
446
		}
447
		
448
		public DynField getDeclaredDynField(String name) {
449
			if( this.name.equals(name) ) {
450
				return this.declaredFields[0];
451
			}
452
			return null;
453
		}
454

  
455
		public DynField[] getDeclaredDynFields() {
456
			return this.declaredFields;
457
		}
458

  
459
		public String getDescription() {
460
			return this.description;
461
		}
462

  
463
		public DynField getDynField(String name) {
464
			if( this.name.equals(name) ) {
465
				return this.declaredFields[0];
466
			}
467
			return null;
468
		}
469

  
470
		public DynField[] getDynFields() {
471
			return this.declaredFields;
472
		}
473

  
474
		public String getFullName() {
475
			return this.name;
476
		}
477

  
478
		public String getName() {
479
			return this.name;
480
		}
481

  
482
		public String getNamespace() {
483
			return null;
484
		}
485

  
486
		public DynClass[] getSuperDynClasses() {
487
			return null;
488
		}
489

  
490
		public boolean isInstance(DynObject dynObject) {
491
			return false;
492
		}
493

  
494
		public DynObject newInstance() {
495
			return null;
496
		}
497

  
498
		public void addDynMethod(DynMethod dynMethod) {
499
			throw new UnsupportedOperationException();
500
		}
501

  
502
		public void extend(String dynClassName) {
503
			throw new UnsupportedOperationException();
504
		}
505

  
506
		public void extend(String namespace, String dynClassName) {
507
			throw new UnsupportedOperationException();
508
		}
509

  
510
		public DynMethod getDeclaredDynMethod(String name)
511
				throws DynMethodException {
512
			throw new UnsupportedOperationException();
513
		}
514

  
515
		public DynMethod[] getDeclaredDynMethods() throws DynMethodException {
516
			throw new UnsupportedOperationException();
517
		}
518

  
519
		public DynMethod getDynMethod(String name) throws DynMethodException {
520
			throw new UnsupportedOperationException();
521
		}
522

  
523
		public DynMethod getDynMethod(int code) throws DynMethodException {
524
			throw new UnsupportedOperationException();
525
		}
526

  
527
		public DynMethod[] getDynMethods() throws DynMethodException {
528
			throw new UnsupportedOperationException();
529
		}
530

  
531
		public void removeDynMethod(String name) {
532
			throw new UnsupportedOperationException();
533
		}
534

  
535
		public DynField addDynField(String name) {
536
			throw new UnsupportedOperationException();
537
		}
538

  
539
		public DynField addDynFieldArray(String name) {
540
			throw new UnsupportedOperationException();
541
		}
542

  
543
		public DynField addDynFieldBoolean(String name) {
544
			throw new UnsupportedOperationException();
545
		}
546

  
547
		public DynField addDynFieldChoice(String name, int type,
548
				Object defaultValue, DynObjectValueItem[] values,
549
				boolean mandatory, boolean persistent) {
550
			throw new UnsupportedOperationException();
551
		}
552

  
553
		public DynField addDynFieldChoice(String name, int type,
554
				Object defaultValue, DynObjectValueItem[] values) {
555
			throw new UnsupportedOperationException();
556
		}
557

  
558
		public DynField addDynFieldDouble(String name) {
559
			throw new UnsupportedOperationException();
560
		}
561

  
562
		public DynField addDynFieldFloat(String name) {
563
			throw new UnsupportedOperationException();
564
		}
565

  
566
		public DynField addDynFieldInt(String name) {
567
			throw new UnsupportedOperationException();
568
		}
569

  
570
		public DynField addDynFieldList(String name) {
571
			throw new UnsupportedOperationException();
572
		}
573

  
574
		public DynField addDynFieldLong(String name) {
575
			throw new UnsupportedOperationException();
576
		}
577

  
578
		public DynField addDynFieldMap(String name) {
579
			throw new UnsupportedOperationException();
580
		}
581

  
582
		public DynField addDynFieldObject(String name) {
583
			throw new UnsupportedOperationException();
584
		}
585

  
586
		public DynField addDynFieldRange(String name, int type,
587
				Object defaultValue, Object min, Object max, boolean mandatory,
588
				boolean persistent) {
589
			throw new UnsupportedOperationException();
590
		}
591

  
592
		public DynField addDynFieldRange(String name, int type,
593
				Object defaultValue, Object min, Object max) {
594
			throw new UnsupportedOperationException();
595
		}
596

  
597
		public DynField addDynFieldSet(String name) {
598
			throw new UnsupportedOperationException();
599
		}
600

  
601
		public DynField addDynFieldSingle(String name, int type,
602
				Object defaultValue, boolean mandatory, boolean persistent) {
603
			throw new UnsupportedOperationException();
604
		}
605

  
606
		public DynField addDynFieldSingle(String name, int type,
607
				Object defaultValue) {
608
			throw new UnsupportedOperationException();
609
		}
610

  
611
		public DynField addDynFieldString(String name) {
612
			throw new UnsupportedOperationException();
613
		}
614

  
615
		public void removeDynField(String name) {
616
			throw new UnsupportedOperationException();
617
		}
618

  
619
		public void validate(DynObject object)
620
				throws DynObjectValidateException {
621
			throw new UnsupportedOperationException();
622
		}
623

  
624
		public DynField addDynFieldDate(String name) {
625
			throw new UnsupportedOperationException();
626
		}
627

  
628
		public void extend(DynStruct struct) {
629
			throw new UnsupportedOperationException();
630
		}
631

  
632
		public DynStruct[] getSuperDynStructs() {
633
			throw new UnsupportedOperationException();
634
		}
635

  
636
		public void setDescription(String description) {
637
			throw new UnsupportedOperationException();
638
		}
639

  
640
		public void setNamespace(String namespace) {
641
			throw new UnsupportedOperationException();
642
		}
643
	}
644

  
645

  
646

  
647 409
	public String getGroup() {
648 410
		return this.groupName;
649 411
	}
......
670 432
		this.hidden = hidden;
671 433
		return this;
672 434
	}
673

  
674 435
}

Also available in: Unified diff