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 @ 40559

History | View | Annotate | Download (17.6 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.dal.feature.impl;
25

    
26
import java.lang.ref.WeakReference;
27
import java.util.ArrayList;
28
import java.util.Collections;
29
import java.util.Iterator;
30
import java.util.List;
31

    
32
import org.cresques.cts.IProjection;
33

    
34
import org.gvsig.fmap.dal.DataTypes;
35
import org.gvsig.fmap.dal.exception.DataException;
36
import org.gvsig.fmap.dal.feature.EditableFeatureType;
37
import org.gvsig.fmap.dal.feature.Feature;
38
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
39
import org.gvsig.fmap.dal.feature.FeatureRules;
40
import org.gvsig.fmap.dal.feature.FeatureType;
41
import org.gvsig.tools.dynobject.DynClass;
42
import org.gvsig.tools.dynobject.DynField;
43
import org.gvsig.tools.dynobject.DynMethod;
44
import org.gvsig.tools.dynobject.DynObject;
45
import org.gvsig.tools.dynobject.DynObjectValueItem;
46
import org.gvsig.tools.dynobject.DynStruct;
47
import org.gvsig.tools.dynobject.exception.DynMethodException;
48
import org.gvsig.tools.dynobject.exception.DynObjectValidateException;
49

    
50
public class DefaultFeatureType extends ArrayList implements FeatureType,
51
                DynClass {
52

    
53
        /**
54
         *
55
         */
56
        private static final long serialVersionUID = -7988721447349282215L;
57

    
58
        private DefaultFeatureRules rules;
59
        private boolean hasEvaluators;
60
        protected String defaultGeometryAttributeName;
61
        protected int defaultGeometryAttributeIndex;
62
        protected int defaultTimeAttributeIndex;
63
        private String id;
64
        protected boolean hasOID;
65
        protected boolean allowAtomaticValues;
66
        protected FeatureAttributeDescriptor[] pk = null;
67

    
68
        private List srsList = null; 
69

    
70
        protected DefaultFeatureType(String id) {
71
                this.id = id;
72
                this.rules = new DefaultFeatureRules();
73
                this.hasEvaluators = false;
74
                this.defaultGeometryAttributeName = null;
75
                this.defaultGeometryAttributeIndex = -1;
76
                this.defaultTimeAttributeIndex = -1;
77
                this.allowAtomaticValues = false;
78
        }
79

    
80
        protected DefaultFeatureType() {
81
                this("default");
82
        }
83

    
84
        protected DefaultFeatureType(DefaultFeatureType other) {
85
                initialize(other, true);
86
        }
87

    
88
        protected DefaultFeatureType(DefaultFeatureType other,
89
                        boolean copyAttributes) {
90
                initialize(other, copyAttributes);
91
        }
92

    
93
        protected void initialize(DefaultFeatureType other, boolean copyAttributes) {
94
                this.id = other.getId();
95
                if (copyAttributes) {
96
                        Iterator iter = other.iterator();
97
                        DefaultFeatureAttributeDescriptor attr;
98
                        while (iter.hasNext()) {
99
                                attr = (DefaultFeatureAttributeDescriptor) iter.next();
100
                                this.intitalizeAddAttibute(attr);
101
                        }
102
                }
103
                this.defaultGeometryAttributeName = other.defaultGeometryAttributeName;
104
                this.hasEvaluators = other.hasEvaluators;
105
                this.rules = (DefaultFeatureRules) other.rules.getCopy();
106
                this.defaultGeometryAttributeIndex = other.defaultGeometryAttributeIndex;
107
                this.defaultTimeAttributeIndex = other.defaultTimeAttributeIndex;
108
                this.hasOID = other.hasOID;
109
                this.id = other.id; // XXX ???? copiar o no esto????
110
        }
111

    
112
        protected void intitalizeAddAttibute(DefaultFeatureAttributeDescriptor attr) {
113
                super.add(attr.getCopy());
114
        }
115

    
116
        public String getId() {
117
                return this.id;
118
        }
119

    
120
        public Object get(String name) {
121
                FeatureAttributeDescriptor attr;
122
                Iterator iter = this.iterator();
123
                while (iter.hasNext()) {
124
                        attr = (FeatureAttributeDescriptor) iter.next();
125
                        if (attr.getName().equalsIgnoreCase(name)) {
126
                                return attr;
127
                        }
128
                }
129
                return null;
130
        }
131

    
132
        public FeatureAttributeDescriptor getAttributeDescriptor(String name) {
133
                FeatureAttributeDescriptor attr;
134
                Iterator iter = this.iterator();
135
                while (iter.hasNext()) {
136
                        attr = (FeatureAttributeDescriptor) iter.next();
137
                        if (attr.getName().equalsIgnoreCase(name)) {
138
                                return attr;
139
                        }
140
                }
141
                return null;
142
        }
143

    
144
        public FeatureAttributeDescriptor getAttributeDescriptor(int index) {
145
                return (FeatureAttributeDescriptor) super.get(index);
146
        }
147

    
148
        public FeatureType getCopy() {
149
                return new DefaultFeatureType(this);
150
        }
151

    
152
        public int getDefaultGeometryAttributeIndex() {
153
                return this.defaultGeometryAttributeIndex;
154
        }
155

    
156
        public String getDefaultGeometryAttributeName() {
157
                return this.defaultGeometryAttributeName;
158
        }
159

    
160
        public EditableFeatureType getEditable() {
161
                return new DefaultEditableFeatureType(this);
162
        }
163

    
164
        public int getIndex(String name) {
165
                FeatureAttributeDescriptor attr;
166
                Iterator iter = this.iterator();
167
                while (iter.hasNext()) {
168
                        attr = (FeatureAttributeDescriptor) iter.next();
169
                        if (attr.getName().equalsIgnoreCase(name)) {
170
                                return attr.getIndex();
171
                        }
172
                }
173
                return -1;
174
        }
175

    
176
        public FeatureRules getRules() {
177
                return this.rules;
178
        }
179

    
180
        public boolean hasEvaluators() {
181
                return this.hasEvaluators;
182
        }
183

    
184
        public List getSRSs() {
185
                if (this.srsList == null) {
186
                        ArrayList tmp = new ArrayList();
187
                        Iterator iter = iterator();
188
                        Iterator tmpIter;
189
                        boolean allreadyHave;
190
                        IProjection tmpSRS;
191
                        FeatureAttributeDescriptor attr;
192
                        while (iter.hasNext()){
193
                                attr = (FeatureAttributeDescriptor) iter.next();
194
                                if (attr.getDataType().getType() == DataTypes.GEOMETRY
195
                                                && attr.getSRS() != null) {
196
                                        allreadyHave = false;
197
                                        tmpIter = tmp.iterator();
198
                                        while (tmpIter.hasNext()) {
199
                                                tmpSRS = (IProjection) tmpIter.next();
200
                                                if (tmpSRS.getAbrev().equals(attr.getSRS().getAbrev())) {
201
                                                        allreadyHave = true;
202
                                                        break;
203
                                                }
204
                                        }
205
                                        if (!allreadyHave) {
206
                                                tmp.add(attr.getSRS());
207
                                        }
208
                                }
209
                        }
210
                        this.srsList = Collections.unmodifiableList(tmp);
211
                }
212
                return this.srsList;
213
        }
214

    
215
        public IProjection getDefaultSRS() {
216
                if (this.getDefaultGeometryAttributeIndex() < 0) {
217
                        return null;
218
                }
219
                return this.getAttributeDescriptor(
220
                                this.getDefaultGeometryAttributeIndex()).getSRS();
221
        }
222

    
223
        public void validateFeature(Feature feature, int mode) {
224
                if (Feature.UPDATE == mode){
225
                        ((DefaultFeatureRules)getRules()).validate(feature);
226
                }
227
        }
228

    
229
        public FeatureType getSubtype(String[] names) throws DataException {
230
                return new SubtypeFeatureType(this, names);
231
        }
232

    
233
        public boolean isSubtypeOf(FeatureType featureType) {
234
                return false;
235
        }
236

    
237

    
238

    
239
        class SubtypeFeatureType extends DefaultFeatureType {
240
                /**
241
                 *
242
                 */
243
                private static final long serialVersionUID = 6913732960073922540L;
244
                WeakReference parent;
245

    
246
                SubtypeFeatureType(DefaultFeatureType parent, String[] names)
247
                                throws DataException {
248
                        super(parent, false);
249
                        DefaultFeatureAttributeDescriptor attrcopy;
250
                        DefaultFeatureAttributeDescriptor attr;
251
                        // Copy attributes
252
                        for (int i = 0; i < names.length; i++) {
253
                                attr = (DefaultFeatureAttributeDescriptor) parent
254
                                                .getAttributeDescriptor(names[i]);
255
                                if (attr == null) {
256
                                        throw new SubtypeFeatureTypeNameException(names[i], parent
257
                                                        .getId());
258
                                }
259
                                attrcopy = new DefaultFeatureAttributeDescriptor(attr);
260
                                this.add(attrcopy);
261
                                attrcopy.index = i;
262
                        }
263

    
264
                        // Add missing pk fiels if any
265
                        if (!parent.hasOID()) {
266
                                Iterator iter = parent.iterator();
267
                                while (iter.hasNext()) {
268
                                        attr = (DefaultFeatureAttributeDescriptor) iter.next();
269
                                        if (attr.isPrimaryKey()
270
                                                        && this.getIndex(attr.getName()) < 0) {
271
                                                attrcopy = new DefaultFeatureAttributeDescriptor(attr);
272
                                                this.add(attrcopy);
273
                                                attrcopy.index = this.size() - 1;
274
                                        }
275
                                }
276
                        }
277

    
278
                        this.defaultGeometryAttributeIndex = this
279
                                        .getIndex(this.defaultGeometryAttributeName);
280
                        if (this.defaultGeometryAttributeIndex < 0) {
281
                                this.defaultGeometryAttributeName = null;
282
                        }
283
                        this.parent = new WeakReference(parent);
284
                }
285

    
286
                public FeatureType getSubtype(String[] names) throws DataException {
287
                        return new SubtypeFeatureType((DefaultFeatureType) this.parent
288
                                        .get(), names);
289
                }
290

    
291
                public boolean isSubtypeOf(FeatureType featureType) {
292
                        if (featureType == null) {
293
                                return false;
294
                        }
295
                        FeatureType parent = (FeatureType) this.parent.get();
296
                        return featureType.equals(parent);
297
                }
298

    
299
                public EditableFeatureType getEditable() {
300
                        throw new UnsupportedOperationException();
301
                }
302
        }
303

    
304
        public class SubtypeFeatureTypeNameException extends DataException {
305

    
306
                /**
307
                 *
308
                 */
309
                private static final long serialVersionUID = -4414242486723260101L;
310
                private final static String MESSAGE_FORMAT = "Attribute name '%(name)s' not found in type (%(type)s).";
311
                private final static String MESSAGE_KEY = "_SubtypeFeatureTypeNameException";
312

    
313
                public SubtypeFeatureTypeNameException(String name, String type) {
314
                        super(MESSAGE_FORMAT, MESSAGE_KEY, serialVersionUID);
315
                        setValue("name", name);
316
                        setValue("type", type);
317
                }
318
        }
319

    
320
        public boolean hasOID() {
321
                return hasOID;
322
        }
323
        public String toString(){
324
                StringBuffer s = new StringBuffer();
325
                s.append(this.getId());
326
                s.append(":[");
327
                String attName;
328
                for (int i = 0; i < size(); i++) {
329
                        attName =((FeatureAttributeDescriptor)get(i)).getName().toString();
330
                        s.append(attName);
331
                        if (i < size() - 1) {
332
                                s.append(',');
333
                        }
334
                }
335
                s.append(']');
336
                return s.toString();
337
        }
338

    
339
        public Iterator iterator() {
340
                return getIterator(super.iterator());
341
        }
342

    
343
        protected Iterator getIterator(Iterator iter) {
344
                return new DelegatedIterator(iter);
345
        }
346

    
347
        protected class DelegatedIterator implements Iterator {
348

    
349
                protected Iterator iterator;
350

    
351
                public DelegatedIterator(Iterator iter) {
352
                        this.iterator = iter;
353
                }
354

    
355
                public boolean hasNext() {
356
                        return iterator.hasNext();
357
                }
358

    
359
                public Object next() {
360
                        return iterator.next();
361
                }
362

    
363
                public void remove() {
364
                        throw new UnsupportedOperationException();
365
                }
366

    
367
        }
368

    
369
        public boolean allowAutomaticValues() {
370
                return this.allowAtomaticValues;
371
        }
372

    
373
        public FeatureAttributeDescriptor[] getAttributeDescriptors() {
374
                return (FeatureAttributeDescriptor[]) super
375
                                .toArray(new FeatureAttributeDescriptor[super.size()]);
376
        }
377

    
378
        public FeatureAttributeDescriptor[] getPrimaryKey() {
379
                if (pk == null) {
380
                        List pkList = new ArrayList();
381
                        Iterator iter = super.iterator();
382
                        FeatureAttributeDescriptor attr;
383
                        while (iter.hasNext()){
384
                                attr = (FeatureAttributeDescriptor) iter.next();
385
                                if (attr.isPrimaryKey()){
386
                                        pkList.add(attr);
387
                                }
388
                        }
389
                        pk = (FeatureAttributeDescriptor[]) pkList
390
                                        .toArray(new FeatureAttributeDescriptor[pkList.size()]);
391
                }
392
                return pk;
393
        }
394

    
395
        public FeatureAttributeDescriptor getDefaultGeometryAttribute() {
396
                if (this.defaultGeometryAttributeIndex < 0) {
397
                        return null;
398
                }
399
                return (FeatureAttributeDescriptor) super
400
                                .get(this.defaultGeometryAttributeIndex);
401
        }
402

    
403

    
404

    
405
        public boolean equals(Object other) {
406
                if (this == other) {
407
                        return true;
408
                }
409
                if (!(other instanceof DefaultFeatureType)) {
410
                        return false;
411
                }
412
                DefaultFeatureType otherType = (DefaultFeatureType) other;
413
                if (!this.id.equals(otherType.id)) {
414
                        return false;
415
                }
416
                if (this.size() != otherType.size()) {
417
                        return false;
418
                }
419
                FeatureAttributeDescriptor attr,attrOther;
420
                Iterator iter,iterOther;
421
                iter = this.iterator();
422
                iterOther = otherType.iterator();
423
                while (iter.hasNext()) {
424
                        attr = (FeatureAttributeDescriptor) iter.next();
425
                        attrOther = (FeatureAttributeDescriptor) iterOther.next();
426
                        if (!attr.equals(attrOther)) {
427
                                return false;
428
                        }
429
                }
430

    
431
                if (defaultGeometryAttributeName != otherType.defaultGeometryAttributeName) {
432
                        if (defaultGeometryAttributeName == null) {
433
                                return false;
434
                        }
435
                        return defaultGeometryAttributeName
436
                                        .equals(otherType.defaultGeometryAttributeName);
437

    
438
                }
439
                return true;
440

    
441
        }
442

    
443
        /**
444
         * Start of DynClass interface implementation
445
         * READONLY
446
         */
447

    
448
        public DynField addDynField(String name) {
449
                throw new UnsupportedOperationException();
450
        }
451

    
452
        public DynField getDeclaredDynField(String name) {
453
                return (DynField) getAttributeDescriptor(name);
454
        }
455

    
456
        public DynField[] getDeclaredDynFields() {
457
                return (DynField[]) getAttributeDescriptors();
458
        }
459

    
460
        public String getDescription() {
461
                return null;
462
        }
463

    
464
        public DynField getDynField(String name) {
465
                return (DynField) getAttributeDescriptor(name);
466
        }
467

    
468
        public DynField[] getDynFields() {
469
                return (DynField[]) getAttributeDescriptors();
470
        }
471

    
472
        public String getName() {
473
                return this.id;
474
        }
475

    
476
        public void removeDynField(String name) {
477
                throw new UnsupportedOperationException();
478

    
479
        }
480

    
481
        public void addDynMethod(DynMethod dynMethod) {
482
                throw new UnsupportedOperationException();
483

    
484
        }
485

    
486
        public void extend(DynClass dynClass) {
487
                throw new UnsupportedOperationException();
488

    
489
        }
490

    
491
        public void extend(String dynClassName) {
492
                throw new UnsupportedOperationException();
493

    
494
        }
495

    
496
        public void extend(String namespace, String dynClassName) {
497
                throw new UnsupportedOperationException();
498

    
499
        }
500

    
501
        public DynMethod getDeclaredDynMethod(String name)
502
                        throws DynMethodException {
503
                return null;
504
        }
505

    
506
        public DynMethod[] getDeclaredDynMethods() throws DynMethodException {
507
                return null;
508
        }
509

    
510
        public DynMethod getDynMethod(String name) throws DynMethodException {
511
                return null;
512
        }
513

    
514
        public DynMethod getDynMethod(int code) throws DynMethodException {
515
                return null;
516
        }
517

    
518
        public DynMethod[] getDynMethods() throws DynMethodException {
519
                return null;
520
        }
521

    
522
        public DynClass[] getSuperDynClasses() {
523
                return null;
524
        }
525

    
526
        public boolean isInstance(DynObject dynObject) {
527
                if (dynObject.getDynClass().getName() == getName()) {
528
                        return true;
529
                }
530
                return false;
531
        }
532

    
533
        public DynObject newInstance() {
534

    
535
                throw new UnsupportedOperationException();
536
        }
537

    
538
        public void removeDynMethod(String name) {
539
                throw new UnsupportedOperationException();
540

    
541
        }
542

    
543
        public DynField addDynFieldChoice(String name, int type,
544
                        Object defaultValue, DynObjectValueItem[] values,
545
                        boolean mandatory, boolean persistent) {
546
                throw new UnsupportedOperationException();
547
        }
548

    
549
        public DynField addDynFieldRange(String name, int type,
550
                        Object defaultValue, Object min, Object max, boolean mandatory,
551
                        boolean persistent) {
552
                throw new UnsupportedOperationException();
553
        }
554

    
555
        public DynField addDynFieldSingle(String name, int type,
556
                        Object defaultValue, boolean mandatory, boolean persistent) {
557
                throw new UnsupportedOperationException();
558
        }
559

    
560
        public void validate(DynObject object) throws DynObjectValidateException {
561
                //FIXME: not sure it's the correct code
562
                if (object instanceof Feature) {
563
                        Feature fea = (Feature) object;
564
                        if (fea.getType().equals(this)) {
565
                                return;
566
                        }
567
                }
568
                throw new DynObjectValidateException(this.id);
569
        }
570

    
571
        public DynField addDynFieldLong(String name) {
572
                throw new UnsupportedOperationException();
573
        }
574

    
575
        public DynField addDynFieldChoice(String name, int type,
576
                        Object defaultValue, DynObjectValueItem[] values) {
577
                throw new UnsupportedOperationException();
578
        }
579

    
580
        public DynField addDynFieldRange(String name, int type,
581
                        Object defaultValue, Object min, Object max) {
582
                throw new UnsupportedOperationException();
583
        }
584

    
585
        public DynField addDynFieldSingle(String name, int type, Object defaultValue) {
586
                throw new UnsupportedOperationException();
587
        }
588

    
589
        public DynField addDynFieldString(String name) {
590
                throw new UnsupportedOperationException();
591
        }
592
        
593
        public DynField addDynFieldInt(String name) {
594
                throw new UnsupportedOperationException();
595
        }
596
        
597
        public DynField addDynFieldDouble(String name) {
598
                throw new UnsupportedOperationException();
599
        }
600
        
601
        public DynField addDynFieldFloat(String name) {
602
                throw new UnsupportedOperationException();
603
        }
604

    
605
        public DynField addDynFieldBoolean(String name) {
606
                throw new UnsupportedOperationException();
607
        }
608

    
609
        public DynField addDynFieldList(String name) {
610
                throw new UnsupportedOperationException();
611
        }
612

    
613
        public DynField addDynFieldMap(String name) {
614
                throw new UnsupportedOperationException();
615
        }
616

    
617
        public DynField addDynFieldObject(String name) {
618
                throw new UnsupportedOperationException();
619
        }
620

    
621
        public DynField addDynFieldSet(String name) {
622
                throw new UnsupportedOperationException();
623
        }
624

    
625
        public DynField addDynFieldArray(String name) {
626
                throw new UnsupportedOperationException();
627
        }
628

    
629
        public DynField addDynFieldDate(String name) {
630
                throw new UnsupportedOperationException();
631
        }
632

    
633
        public void extend(DynStruct struct) {
634
                throw new UnsupportedOperationException();
635
        }
636

    
637
        public String getFullName() {
638
        // TODO: usar el DynClassName
639
                return this.id;
640
        }
641

    
642
        public String getNamespace() {
643
                return "DALFeature";
644
        }
645

    
646
        public DynStruct[] getSuperDynStructs() {
647
                return null;
648
        }
649

    
650
        public void setDescription(String description) {
651
                throw new UnsupportedOperationException();
652
        }
653

    
654
        public void setNamespace(String namespace) {
655
                throw new UnsupportedOperationException();
656
        }
657

    
658
        public DynField addDynFieldFile(String name) {
659
                throw new UnsupportedOperationException();
660
        }
661

    
662
        public DynField addDynFieldFolder(String name) {
663
                throw new UnsupportedOperationException();
664
        }
665

    
666
        public DynField addDynFieldURL(String name) {
667
                throw new UnsupportedOperationException();
668
        }
669

    
670
        public DynField addDynFieldURI(String name) {
671
                throw new UnsupportedOperationException();
672
        }
673

    
674
    public boolean isExtendable(DynStruct dynStruct) {
675
        return false;
676
    }
677

    
678
        public void extend(DynStruct[] structs) {
679
                // TODO Auto-generated method stub
680
                
681
        }
682

    
683
        public void remove(DynStruct superDynStruct) {
684
                // TODO Auto-generated method stub
685
                
686
        }
687

    
688
        public void removeAll(DynStruct[] superDynStruct) {
689
                // TODO Auto-generated method stub
690
                
691
        }
692

    
693
        public FeatureAttributeDescriptor getDefaultTimeAttribute() {
694
                if (this.defaultTimeAttributeIndex < 0) {
695
                        return null;
696
                }
697
                return (FeatureAttributeDescriptor) super
698
                                .get(this.defaultTimeAttributeIndex);
699
        }
700
}