Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultFeatureType.java @ 34270

History | View | Annotate | Download (16 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
        private String id;
40
        protected boolean hasOID;
41
        protected boolean allowAtomaticValues;
42
        protected FeatureAttributeDescriptor[] pk = null;
43

    
44
        private List srsList = null;
45

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

    
55
        protected DefaultFeatureType() {
56
                this("default");
57
        }
58

    
59
        protected DefaultFeatureType(DefaultFeatureType other) {
60
                initialize(other, true);
61
        }
62

    
63
        protected DefaultFeatureType(DefaultFeatureType other,
64
                        boolean copyAttributes) {
65
                initialize(other, copyAttributes);
66
        }
67

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

    
86
        protected void intitalizeAddAttibute(DefaultFeatureAttributeDescriptor attr) {
87
                super.add(attr.getCopy());
88
        }
89

    
90
        public String getId() {
91
                return this.id;
92
        }
93

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

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

    
118
        public FeatureAttributeDescriptor getAttributeDescriptor(int index) {
119
                return (FeatureAttributeDescriptor) super.get(index);
120
        }
121

    
122
        public FeatureType getCopy() {
123
                return new DefaultFeatureType(this);
124
        }
125

    
126
        public int getDefaultGeometryAttributeIndex() {
127
                return this.defaultGeometryAttributeIndex;
128
        }
129

    
130
        public String getDefaultGeometryAttributeName() {
131
                return this.defaultGeometryAttributeName;
132
        }
133

    
134
        public EditableFeatureType getEditable() {
135
                return new DefaultEditableFeatureType(this);
136
        }
137

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

    
150
        public FeatureRules getRules() {
151
                return this.rules;
152
        }
153

    
154
        public boolean hasEvaluators() {
155
                return this.hasEvaluators;
156
        }
157

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

    
189
        public IProjection getDefaultSRS() {
190
                if (this.getDefaultGeometryAttributeIndex() < 0) {
191
                        return null;
192
                }
193
                return this.getAttributeDescriptor(
194
                                this.getDefaultGeometryAttributeIndex()).getSRS();
195
        }
196

    
197
        public void validateFeature(Feature feature, int mode) {
198
                if (Feature.UPDATE == mode){
199
                        ((DefaultFeatureRules)getRules()).validate(feature);
200
                }
201
        }
202

    
203
        public FeatureType getSubtype(String[] names) throws DataException {
204
                return new SubtypeFeatureType(this, names);
205
        }
206

    
207
        public boolean isSubtypeOf(FeatureType featureType) {
208
                return false;
209
        }
210

    
211

    
212

    
213
        class SubtypeFeatureType extends DefaultFeatureType {
214
                /**
215
                 *
216
                 */
217
                private static final long serialVersionUID = 6913732960073922540L;
218
                WeakReference parent;
219

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

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

    
252
                        this.defaultGeometryAttributeIndex = this
253
                                        .getIndex(this.defaultGeometryAttributeName);
254
                        if (this.defaultGeometryAttributeIndex < 0) {
255
                                this.defaultGeometryAttributeName = null;
256
                        }
257
                        this.parent = new WeakReference(parent);
258
                }
259

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

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

    
273
                public EditableFeatureType getEditable() {
274
                        throw new UnsupportedOperationException();
275
                }
276
        }
277

    
278
        public class SubtypeFeatureTypeNameException extends DataException {
279

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

    
287
                public SubtypeFeatureTypeNameException(String name, String type) {
288
                        super(MESSAGE_FORMAT, MESSAGE_KEY, serialVersionUID);
289
                        setValue("name", name);
290
                        setValue("type", type);
291
                }
292
        }
293

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

    
313
        public Iterator iterator() {
314
                return getIterator(super.iterator());
315
        }
316

    
317
        protected Iterator getIterator(Iterator iter) {
318
                return new DelegatedIterator(iter);
319
        }
320

    
321
        protected class DelegatedIterator implements Iterator {
322

    
323
                protected Iterator iterator;
324

    
325
                public DelegatedIterator(Iterator iter) {
326
                        this.iterator = iter;
327
                }
328

    
329
                public boolean hasNext() {
330
                        return iterator.hasNext();
331
                }
332

    
333
                public Object next() {
334
                        return iterator.next();
335
                }
336

    
337
                public void remove() {
338
                        throw new UnsupportedOperationException();
339
                }
340

    
341
        }
342

    
343
        public boolean allowAutomaticValues() {
344
                return this.allowAtomaticValues;
345
        }
346

    
347
        public FeatureAttributeDescriptor[] getAttributeDescriptors() {
348
                return (FeatureAttributeDescriptor[]) super
349
                                .toArray(new FeatureAttributeDescriptor[super.size()]);
350
        }
351

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

    
369
        public FeatureAttributeDescriptor getDefaultGeometryAttribute() {
370
                if (this.defaultGeometryAttributeIndex < 0) {
371
                        return null;
372
                }
373
                return (FeatureAttributeDescriptor) super
374
                                .get(this.defaultGeometryAttributeIndex);
375
        }
376

    
377

    
378

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

    
405
                if (defaultGeometryAttributeName != otherType.defaultGeometryAttributeName) {
406
                        if (defaultGeometryAttributeName == null) {
407
                                return false;
408
                        }
409
                        return defaultGeometryAttributeName
410
                                        .equals(otherType.defaultGeometryAttributeName);
411

    
412
                }
413
                return true;
414

    
415
        }
416

    
417
        /**
418
         * Start of DynClass interface implementation
419
         * READONLY
420
         */
421

    
422
        public DynField addDynField(String name) {
423
                throw new UnsupportedOperationException();
424
        }
425

    
426
        public DynField getDeclaredDynField(String name) {
427
                return (DynField) getAttributeDescriptor(name);
428
        }
429

    
430
        public DynField[] getDeclaredDynFields() {
431
                return (DynField[]) getAttributeDescriptors();
432
        }
433

    
434
        public String getDescription() {
435
                return null;
436
        }
437

    
438
        public DynField getDynField(String name) {
439
                return (DynField) getAttributeDescriptor(name);
440
        }
441

    
442
        public DynField[] getDynFields() {
443
                return (DynField[]) getAttributeDescriptors();
444
        }
445

    
446
        public String getName() {
447
                return this.id;
448
        }
449

    
450
        public void removeDynField(String name) {
451
                throw new UnsupportedOperationException();
452

    
453
        }
454

    
455
        public void addDynMethod(DynMethod dynMethod) {
456
                throw new UnsupportedOperationException();
457

    
458
        }
459

    
460
        public void extend(DynClass dynClass) {
461
                throw new UnsupportedOperationException();
462

    
463
        }
464

    
465
        public void extend(String dynClassName) {
466
                throw new UnsupportedOperationException();
467

    
468
        }
469

    
470
        public void extend(String namespace, String dynClassName) {
471
                throw new UnsupportedOperationException();
472

    
473
        }
474

    
475
        public DynMethod getDeclaredDynMethod(String name)
476
                        throws DynMethodException {
477
                return null;
478
        }
479

    
480
        public DynMethod[] getDeclaredDynMethods() throws DynMethodException {
481
                return null;
482
        }
483

    
484
        public DynMethod getDynMethod(String name) throws DynMethodException {
485
                return null;
486
        }
487

    
488
        public DynMethod getDynMethod(int code) throws DynMethodException {
489
                return null;
490
        }
491

    
492
        public DynMethod[] getDynMethods() throws DynMethodException {
493
                return null;
494
        }
495

    
496
        public DynClass[] getSuperDynClasses() {
497
                return null;
498
        }
499

    
500
        public boolean isInstance(DynObject dynObject) {
501
                if (dynObject.getDynClass().getName() == getName()) {
502
                        return true;
503
                }
504
                return false;
505
        }
506

    
507
        public DynObject newInstance() {
508

    
509
                throw new UnsupportedOperationException();
510
        }
511

    
512
        public void removeDynMethod(String name) {
513
                throw new UnsupportedOperationException();
514

    
515
        }
516

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

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

    
529
        public DynField addDynFieldSingle(String name, int type,
530
                        Object defaultValue, boolean mandatory, boolean persistent) {
531
                throw new UnsupportedOperationException();
532
        }
533

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

    
545
        public DynField addDynFieldLong(String name) {
546
                throw new UnsupportedOperationException();
547
        }
548

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

    
554
        public DynField addDynFieldRange(String name, int type,
555
                        Object defaultValue, Object min, Object max) {
556
                throw new UnsupportedOperationException();
557
        }
558

    
559
        public DynField addDynFieldSingle(String name, int type, Object defaultValue) {
560
                throw new UnsupportedOperationException();
561
        }
562

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

    
579
        public DynField addDynFieldBoolean(String name) {
580
                throw new UnsupportedOperationException();
581
        }
582

    
583
        public DynField addDynFieldList(String name) {
584
                throw new UnsupportedOperationException();
585
        }
586

    
587
        public DynField addDynFieldMap(String name) {
588
                throw new UnsupportedOperationException();
589
        }
590

    
591
        public DynField addDynFieldObject(String name) {
592
                throw new UnsupportedOperationException();
593
        }
594

    
595
        public DynField addDynFieldSet(String name) {
596
                throw new UnsupportedOperationException();
597
        }
598

    
599
        public DynField addDynFieldArray(String name) {
600
                throw new UnsupportedOperationException();
601
        }
602

    
603
        public DynField addDynFieldDate(String name) {
604
                throw new UnsupportedOperationException();
605
        }
606

    
607
        public void extend(DynStruct struct) {
608
                throw new UnsupportedOperationException();
609
        }
610

    
611
        public String getFullName() {
612
        // TODO: usar el DynClassName
613
                return this.id;
614
        }
615

    
616
        public String getNamespace() {
617
                return "DALFeature";
618
        }
619

    
620
        public DynStruct[] getSuperDynStructs() {
621
                return null;
622
        }
623

    
624
        public void setDescription(String description) {
625
                throw new UnsupportedOperationException();
626
        }
627

    
628
        public void setNamespace(String namespace) {
629
                throw new UnsupportedOperationException();
630
        }
631

    
632
        public DynField addDynFieldFile(String name) {
633
                throw new UnsupportedOperationException();
634
        }
635

    
636
        public DynField addDynFieldFolder(String name) {
637
                throw new UnsupportedOperationException();
638
        }
639

    
640
        public DynField addDynFieldURL(String name) {
641
                throw new UnsupportedOperationException();
642
        }
643

    
644
        public DynField addDynFieldURI(String name) {
645
                throw new UnsupportedOperationException();
646
        }
647

    
648
    public boolean isExtendable(DynStruct dynStruct) {
649
        return false;
650
    }
651
}