Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / feature / impl / DefaultFeatureType.java @ 40435

History | View | Annotate | Download (16.7 KB)

1
package org.gvsig.fmap.dal.feature.impl;
2

    
3
import java.lang.ref.WeakReference;
4
import java.util.ArrayList;
5
import java.util.Collections;
6
import java.util.Iterator;
7
import java.util.List;
8

    
9
import org.cresques.cts.IProjection;
10

    
11
import org.gvsig.fmap.dal.DataTypes;
12
import org.gvsig.fmap.dal.exception.DataException;
13
import org.gvsig.fmap.dal.feature.EditableFeatureType;
14
import org.gvsig.fmap.dal.feature.Feature;
15
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
16
import org.gvsig.fmap.dal.feature.FeatureRules;
17
import org.gvsig.fmap.dal.feature.FeatureType;
18
import org.gvsig.tools.dynobject.DynClass;
19
import org.gvsig.tools.dynobject.DynField;
20
import org.gvsig.tools.dynobject.DynMethod;
21
import org.gvsig.tools.dynobject.DynObject;
22
import org.gvsig.tools.dynobject.DynObjectValueItem;
23
import org.gvsig.tools.dynobject.DynStruct;
24
import org.gvsig.tools.dynobject.exception.DynMethodException;
25
import org.gvsig.tools.dynobject.exception.DynObjectValidateException;
26

    
27
public class DefaultFeatureType extends ArrayList implements FeatureType,
28
                DynClass {
29

    
30
        /**
31
         *
32
         */
33
        private static final long serialVersionUID = -7988721447349282215L;
34

    
35
        private DefaultFeatureRules rules;
36
        private boolean hasEvaluators;
37
        protected String defaultGeometryAttributeName;
38
        protected int defaultGeometryAttributeIndex;
39
        protected int defaultTimeAttributeIndex;
40
        private String id;
41
        protected boolean hasOID;
42
        protected boolean allowAtomaticValues;
43
        protected FeatureAttributeDescriptor[] pk = null;
44

    
45
        private List srsList = null; 
46

    
47
        protected DefaultFeatureType(String id) {
48
                this.id = id;
49
                this.rules = new DefaultFeatureRules();
50
                this.hasEvaluators = false;
51
                this.defaultGeometryAttributeName = null;
52
                this.defaultGeometryAttributeIndex = -1;
53
                this.defaultTimeAttributeIndex = -1;
54
                this.allowAtomaticValues = false;
55
        }
56

    
57
        protected DefaultFeatureType() {
58
                this("default");
59
        }
60

    
61
        protected DefaultFeatureType(DefaultFeatureType other) {
62
                initialize(other, true);
63
        }
64

    
65
        protected DefaultFeatureType(DefaultFeatureType other,
66
                        boolean copyAttributes) {
67
                initialize(other, copyAttributes);
68
        }
69

    
70
        protected void initialize(DefaultFeatureType other, boolean copyAttributes) {
71
                this.id = other.getId();
72
                if (copyAttributes) {
73
                        Iterator iter = other.iterator();
74
                        DefaultFeatureAttributeDescriptor attr;
75
                        while (iter.hasNext()) {
76
                                attr = (DefaultFeatureAttributeDescriptor) iter.next();
77
                                this.intitalizeAddAttibute(attr);
78
                        }
79
                }
80
                this.defaultGeometryAttributeName = other.defaultGeometryAttributeName;
81
                this.hasEvaluators = other.hasEvaluators;
82
                this.rules = (DefaultFeatureRules) other.rules.getCopy();
83
                this.defaultGeometryAttributeIndex = other.defaultGeometryAttributeIndex;
84
                this.defaultTimeAttributeIndex = other.defaultTimeAttributeIndex;
85
                this.hasOID = other.hasOID;
86
                this.id = other.id; // XXX ???? copiar o no esto????
87
        }
88

    
89
        protected void intitalizeAddAttibute(DefaultFeatureAttributeDescriptor attr) {
90
                super.add(attr.getCopy());
91
        }
92

    
93
        public String getId() {
94
                return this.id;
95
        }
96

    
97
        public Object get(String name) {
98
                FeatureAttributeDescriptor attr;
99
                Iterator iter = this.iterator();
100
                while (iter.hasNext()) {
101
                        attr = (FeatureAttributeDescriptor) iter.next();
102
                        if (attr.getName().equalsIgnoreCase(name)) {
103
                                return attr;
104
                        }
105
                }
106
                return null;
107
        }
108

    
109
        public FeatureAttributeDescriptor getAttributeDescriptor(String name) {
110
                FeatureAttributeDescriptor attr;
111
                Iterator iter = this.iterator();
112
                while (iter.hasNext()) {
113
                        attr = (FeatureAttributeDescriptor) iter.next();
114
                        if (attr.getName().equalsIgnoreCase(name)) {
115
                                return attr;
116
                        }
117
                }
118
                return null;
119
        }
120

    
121
        public FeatureAttributeDescriptor getAttributeDescriptor(int index) {
122
                return (FeatureAttributeDescriptor) super.get(index);
123
        }
124

    
125
        public FeatureType getCopy() {
126
                return new DefaultFeatureType(this);
127
        }
128

    
129
        public int getDefaultGeometryAttributeIndex() {
130
                return this.defaultGeometryAttributeIndex;
131
        }
132

    
133
        public String getDefaultGeometryAttributeName() {
134
                return this.defaultGeometryAttributeName;
135
        }
136

    
137
        public EditableFeatureType getEditable() {
138
                return new DefaultEditableFeatureType(this);
139
        }
140

    
141
        public int getIndex(String name) {
142
                FeatureAttributeDescriptor attr;
143
                Iterator iter = this.iterator();
144
                while (iter.hasNext()) {
145
                        attr = (FeatureAttributeDescriptor) iter.next();
146
                        if (attr.getName().equalsIgnoreCase(name)) {
147
                                return attr.getIndex();
148
                        }
149
                }
150
                return -1;
151
        }
152

    
153
        public FeatureRules getRules() {
154
                return this.rules;
155
        }
156

    
157
        public boolean hasEvaluators() {
158
                return this.hasEvaluators;
159
        }
160

    
161
        public List getSRSs() {
162
                if (this.srsList == null) {
163
                        ArrayList tmp = new ArrayList();
164
                        Iterator iter = iterator();
165
                        Iterator tmpIter;
166
                        boolean allreadyHave;
167
                        IProjection tmpSRS;
168
                        FeatureAttributeDescriptor attr;
169
                        while (iter.hasNext()){
170
                                attr = (FeatureAttributeDescriptor) iter.next();
171
                                if (attr.getDataType().getType() == DataTypes.GEOMETRY
172
                                                && attr.getSRS() != null) {
173
                                        allreadyHave = false;
174
                                        tmpIter = tmp.iterator();
175
                                        while (tmpIter.hasNext()) {
176
                                                tmpSRS = (IProjection) tmpIter.next();
177
                                                if (tmpSRS.getAbrev().equals(attr.getSRS().getAbrev())) {
178
                                                        allreadyHave = true;
179
                                                        break;
180
                                                }
181
                                        }
182
                                        if (!allreadyHave) {
183
                                                tmp.add(attr.getSRS());
184
                                        }
185
                                }
186
                        }
187
                        this.srsList = Collections.unmodifiableList(tmp);
188
                }
189
                return this.srsList;
190
        }
191

    
192
        public IProjection getDefaultSRS() {
193
                if (this.getDefaultGeometryAttributeIndex() < 0) {
194
                        return null;
195
                }
196
                return this.getAttributeDescriptor(
197
                                this.getDefaultGeometryAttributeIndex()).getSRS();
198
        }
199

    
200
        public void validateFeature(Feature feature, int mode) {
201
                if (Feature.UPDATE == mode){
202
                        ((DefaultFeatureRules)getRules()).validate(feature);
203
                }
204
        }
205

    
206
        public FeatureType getSubtype(String[] names) throws DataException {
207
                return new SubtypeFeatureType(this, names);
208
        }
209

    
210
        public boolean isSubtypeOf(FeatureType featureType) {
211
                return false;
212
        }
213

    
214

    
215

    
216
        class SubtypeFeatureType extends DefaultFeatureType {
217
                /**
218
                 *
219
                 */
220
                private static final long serialVersionUID = 6913732960073922540L;
221
                WeakReference parent;
222

    
223
                SubtypeFeatureType(DefaultFeatureType parent, String[] names)
224
                                throws DataException {
225
                        super(parent, false);
226
                        DefaultFeatureAttributeDescriptor attrcopy;
227
                        DefaultFeatureAttributeDescriptor attr;
228
                        // Copy attributes
229
                        for (int i = 0; i < names.length; i++) {
230
                                attr = (DefaultFeatureAttributeDescriptor) parent
231
                                                .getAttributeDescriptor(names[i]);
232
                                if (attr == null) {
233
                                        throw new SubtypeFeatureTypeNameException(names[i], parent
234
                                                        .getId());
235
                                }
236
                                attrcopy = new DefaultFeatureAttributeDescriptor(attr);
237
                                this.add(attrcopy);
238
                                attrcopy.index = i;
239
                        }
240

    
241
                        // Add missing pk fiels if any
242
                        if (!parent.hasOID()) {
243
                                Iterator iter = parent.iterator();
244
                                while (iter.hasNext()) {
245
                                        attr = (DefaultFeatureAttributeDescriptor) iter.next();
246
                                        if (attr.isPrimaryKey()
247
                                                        && this.getIndex(attr.getName()) < 0) {
248
                                                attrcopy = new DefaultFeatureAttributeDescriptor(attr);
249
                                                this.add(attrcopy);
250
                                                attrcopy.index = this.size() - 1;
251
                                        }
252
                                }
253
                        }
254

    
255
                        this.defaultGeometryAttributeIndex = this
256
                                        .getIndex(this.defaultGeometryAttributeName);
257
                        if (this.defaultGeometryAttributeIndex < 0) {
258
                                this.defaultGeometryAttributeName = null;
259
                        }
260
                        this.parent = new WeakReference(parent);
261
                }
262

    
263
                public FeatureType getSubtype(String[] names) throws DataException {
264
                        return new SubtypeFeatureType((DefaultFeatureType) this.parent
265
                                        .get(), names);
266
                }
267

    
268
                public boolean isSubtypeOf(FeatureType featureType) {
269
                        if (featureType == null) {
270
                                return false;
271
                        }
272
                        FeatureType parent = (FeatureType) this.parent.get();
273
                        return featureType.equals(parent);
274
                }
275

    
276
                public EditableFeatureType getEditable() {
277
                        throw new UnsupportedOperationException();
278
                }
279
        }
280

    
281
        public class SubtypeFeatureTypeNameException extends DataException {
282

    
283
                /**
284
                 *
285
                 */
286
                private static final long serialVersionUID = -4414242486723260101L;
287
                private final static String MESSAGE_FORMAT = "Attribute name '%(name)s' not found in type (%(type)s).";
288
                private final static String MESSAGE_KEY = "_SubtypeFeatureTypeNameException";
289

    
290
                public SubtypeFeatureTypeNameException(String name, String type) {
291
                        super(MESSAGE_FORMAT, MESSAGE_KEY, serialVersionUID);
292
                        setValue("name", name);
293
                        setValue("type", type);
294
                }
295
        }
296

    
297
        public boolean hasOID() {
298
                return hasOID;
299
        }
300
        public String toString(){
301
                StringBuffer s = new StringBuffer();
302
                s.append(this.getId());
303
                s.append(":[");
304
                String attName;
305
                for (int i = 0; i < size(); i++) {
306
                        attName =((FeatureAttributeDescriptor)get(i)).getName().toString();
307
                        s.append(attName);
308
                        if (i < size() - 1) {
309
                                s.append(',');
310
                        }
311
                }
312
                s.append(']');
313
                return s.toString();
314
        }
315

    
316
        public Iterator iterator() {
317
                return getIterator(super.iterator());
318
        }
319

    
320
        protected Iterator getIterator(Iterator iter) {
321
                return new DelegatedIterator(iter);
322
        }
323

    
324
        protected class DelegatedIterator implements Iterator {
325

    
326
                protected Iterator iterator;
327

    
328
                public DelegatedIterator(Iterator iter) {
329
                        this.iterator = iter;
330
                }
331

    
332
                public boolean hasNext() {
333
                        return iterator.hasNext();
334
                }
335

    
336
                public Object next() {
337
                        return iterator.next();
338
                }
339

    
340
                public void remove() {
341
                        throw new UnsupportedOperationException();
342
                }
343

    
344
        }
345

    
346
        public boolean allowAutomaticValues() {
347
                return this.allowAtomaticValues;
348
        }
349

    
350
        public FeatureAttributeDescriptor[] getAttributeDescriptors() {
351
                return (FeatureAttributeDescriptor[]) super
352
                                .toArray(new FeatureAttributeDescriptor[super.size()]);
353
        }
354

    
355
        public FeatureAttributeDescriptor[] getPrimaryKey() {
356
                if (pk == null) {
357
                        List pkList = new ArrayList();
358
                        Iterator iter = super.iterator();
359
                        FeatureAttributeDescriptor attr;
360
                        while (iter.hasNext()){
361
                                attr = (FeatureAttributeDescriptor) iter.next();
362
                                if (attr.isPrimaryKey()){
363
                                        pkList.add(attr);
364
                                }
365
                        }
366
                        pk = (FeatureAttributeDescriptor[]) pkList
367
                                        .toArray(new FeatureAttributeDescriptor[pkList.size()]);
368
                }
369
                return pk;
370
        }
371

    
372
        public FeatureAttributeDescriptor getDefaultGeometryAttribute() {
373
                if (this.defaultGeometryAttributeIndex < 0) {
374
                        return null;
375
                }
376
                return (FeatureAttributeDescriptor) super
377
                                .get(this.defaultGeometryAttributeIndex);
378
        }
379

    
380

    
381

    
382
        public boolean equals(Object other) {
383
                if (this == other) {
384
                        return true;
385
                }
386
                if (!(other instanceof DefaultFeatureType)) {
387
                        return false;
388
                }
389
                DefaultFeatureType otherType = (DefaultFeatureType) other;
390
                if (!this.id.equals(otherType.id)) {
391
                        return false;
392
                }
393
                if (this.size() != otherType.size()) {
394
                        return false;
395
                }
396
                FeatureAttributeDescriptor attr,attrOther;
397
                Iterator iter,iterOther;
398
                iter = this.iterator();
399
                iterOther = otherType.iterator();
400
                while (iter.hasNext()) {
401
                        attr = (FeatureAttributeDescriptor) iter.next();
402
                        attrOther = (FeatureAttributeDescriptor) iterOther.next();
403
                        if (!attr.equals(attrOther)) {
404
                                return false;
405
                        }
406
                }
407

    
408
                if (defaultGeometryAttributeName != otherType.defaultGeometryAttributeName) {
409
                        if (defaultGeometryAttributeName == null) {
410
                                return false;
411
                        }
412
                        return defaultGeometryAttributeName
413
                                        .equals(otherType.defaultGeometryAttributeName);
414

    
415
                }
416
                return true;
417

    
418
        }
419

    
420
        /**
421
         * Start of DynClass interface implementation
422
         * READONLY
423
         */
424

    
425
        public DynField addDynField(String name) {
426
                throw new UnsupportedOperationException();
427
        }
428

    
429
        public DynField getDeclaredDynField(String name) {
430
                return (DynField) getAttributeDescriptor(name);
431
        }
432

    
433
        public DynField[] getDeclaredDynFields() {
434
                return (DynField[]) getAttributeDescriptors();
435
        }
436

    
437
        public String getDescription() {
438
                return null;
439
        }
440

    
441
        public DynField getDynField(String name) {
442
                return (DynField) getAttributeDescriptor(name);
443
        }
444

    
445
        public DynField[] getDynFields() {
446
                return (DynField[]) getAttributeDescriptors();
447
        }
448

    
449
        public String getName() {
450
                return this.id;
451
        }
452

    
453
        public void removeDynField(String name) {
454
                throw new UnsupportedOperationException();
455

    
456
        }
457

    
458
        public void addDynMethod(DynMethod dynMethod) {
459
                throw new UnsupportedOperationException();
460

    
461
        }
462

    
463
        public void extend(DynClass dynClass) {
464
                throw new UnsupportedOperationException();
465

    
466
        }
467

    
468
        public void extend(String dynClassName) {
469
                throw new UnsupportedOperationException();
470

    
471
        }
472

    
473
        public void extend(String namespace, String dynClassName) {
474
                throw new UnsupportedOperationException();
475

    
476
        }
477

    
478
        public DynMethod getDeclaredDynMethod(String name)
479
                        throws DynMethodException {
480
                return null;
481
        }
482

    
483
        public DynMethod[] getDeclaredDynMethods() throws DynMethodException {
484
                return null;
485
        }
486

    
487
        public DynMethod getDynMethod(String name) throws DynMethodException {
488
                return null;
489
        }
490

    
491
        public DynMethod getDynMethod(int code) throws DynMethodException {
492
                return null;
493
        }
494

    
495
        public DynMethod[] getDynMethods() throws DynMethodException {
496
                return null;
497
        }
498

    
499
        public DynClass[] getSuperDynClasses() {
500
                return null;
501
        }
502

    
503
        public boolean isInstance(DynObject dynObject) {
504
                if (dynObject.getDynClass().getName() == getName()) {
505
                        return true;
506
                }
507
                return false;
508
        }
509

    
510
        public DynObject newInstance() {
511

    
512
                throw new UnsupportedOperationException();
513
        }
514

    
515
        public void removeDynMethod(String name) {
516
                throw new UnsupportedOperationException();
517

    
518
        }
519

    
520
        public DynField addDynFieldChoice(String name, int type,
521
                        Object defaultValue, DynObjectValueItem[] values,
522
                        boolean mandatory, boolean persistent) {
523
                throw new UnsupportedOperationException();
524
        }
525

    
526
        public DynField addDynFieldRange(String name, int type,
527
                        Object defaultValue, Object min, Object max, boolean mandatory,
528
                        boolean persistent) {
529
                throw new UnsupportedOperationException();
530
        }
531

    
532
        public DynField addDynFieldSingle(String name, int type,
533
                        Object defaultValue, boolean mandatory, boolean persistent) {
534
                throw new UnsupportedOperationException();
535
        }
536

    
537
        public void validate(DynObject object) throws DynObjectValidateException {
538
                //FIXME: not sure it's the correct code
539
                if (object instanceof Feature) {
540
                        Feature fea = (Feature) object;
541
                        if (fea.getType().equals(this)) {
542
                                return;
543
                        }
544
                }
545
                throw new DynObjectValidateException(this.id);
546
        }
547

    
548
        public DynField addDynFieldLong(String name) {
549
                throw new UnsupportedOperationException();
550
        }
551

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

    
557
        public DynField addDynFieldRange(String name, int type,
558
                        Object defaultValue, Object min, Object max) {
559
                throw new UnsupportedOperationException();
560
        }
561

    
562
        public DynField addDynFieldSingle(String name, int type, Object defaultValue) {
563
                throw new UnsupportedOperationException();
564
        }
565

    
566
        public DynField addDynFieldString(String name) {
567
                throw new UnsupportedOperationException();
568
        }
569
        
570
        public DynField addDynFieldInt(String name) {
571
                throw new UnsupportedOperationException();
572
        }
573
        
574
        public DynField addDynFieldDouble(String name) {
575
                throw new UnsupportedOperationException();
576
        }
577
        
578
        public DynField addDynFieldFloat(String name) {
579
                throw new UnsupportedOperationException();
580
        }
581

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

    
586
        public DynField addDynFieldList(String name) {
587
                throw new UnsupportedOperationException();
588
        }
589

    
590
        public DynField addDynFieldMap(String name) {
591
                throw new UnsupportedOperationException();
592
        }
593

    
594
        public DynField addDynFieldObject(String name) {
595
                throw new UnsupportedOperationException();
596
        }
597

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

    
602
        public DynField addDynFieldArray(String name) {
603
                throw new UnsupportedOperationException();
604
        }
605

    
606
        public DynField addDynFieldDate(String name) {
607
                throw new UnsupportedOperationException();
608
        }
609

    
610
        public void extend(DynStruct struct) {
611
                throw new UnsupportedOperationException();
612
        }
613

    
614
        public String getFullName() {
615
        // TODO: usar el DynClassName
616
                return this.id;
617
        }
618

    
619
        public String getNamespace() {
620
                return "DALFeature";
621
        }
622

    
623
        public DynStruct[] getSuperDynStructs() {
624
                return null;
625
        }
626

    
627
        public void setDescription(String description) {
628
                throw new UnsupportedOperationException();
629
        }
630

    
631
        public void setNamespace(String namespace) {
632
                throw new UnsupportedOperationException();
633
        }
634

    
635
        public DynField addDynFieldFile(String name) {
636
                throw new UnsupportedOperationException();
637
        }
638

    
639
        public DynField addDynFieldFolder(String name) {
640
                throw new UnsupportedOperationException();
641
        }
642

    
643
        public DynField addDynFieldURL(String name) {
644
                throw new UnsupportedOperationException();
645
        }
646

    
647
        public DynField addDynFieldURI(String name) {
648
                throw new UnsupportedOperationException();
649
        }
650

    
651
    public boolean isExtendable(DynStruct dynStruct) {
652
        return false;
653
    }
654

    
655
        public void extend(DynStruct[] structs) {
656
                // TODO Auto-generated method stub
657
                
658
        }
659

    
660
        public void remove(DynStruct superDynStruct) {
661
                // TODO Auto-generated method stub
662
                
663
        }
664

    
665
        public void removeAll(DynStruct[] superDynStruct) {
666
                // TODO Auto-generated method stub
667
                
668
        }
669

    
670
        public FeatureAttributeDescriptor getDefaultTimeAttribute() {
671
                if (this.defaultTimeAttributeIndex < 0) {
672
                        return null;
673
                }
674
                return (FeatureAttributeDescriptor) super
675
                                .get(this.defaultTimeAttributeIndex);
676
        }
677
}