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

History | View | Annotate | Download (17.8 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
        protected 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
        private String internalID = null;
68

    
69
        private List srsList = null; 
70

    
71
        protected DefaultFeatureType(String id) {
72
                this.internalID = Integer.toHexString((int) (Math.random()*100000)).toUpperCase();
73
                this.id = id;
74
                this.rules = new DefaultFeatureRules();
75
                this.hasEvaluators = false;
76
                this.defaultGeometryAttributeName = null;
77
                this.defaultGeometryAttributeIndex = -1;
78
                this.defaultTimeAttributeIndex = -1;
79
                this.allowAtomaticValues = false;
80
        }
81

    
82
        protected DefaultFeatureType() {
83
                this("default");
84
        }
85

    
86
        protected DefaultFeatureType(DefaultFeatureType other) {
87
                this("default");
88
                initialize(other, true);
89
        }
90

    
91
        protected DefaultFeatureType(DefaultFeatureType other,
92
                        boolean copyAttributes) {
93
                this("default");
94
                initialize(other, copyAttributes);
95
        }
96

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

    
116
        protected void intitalizeAddAttibute(DefaultFeatureAttributeDescriptor attr) {
117
                super.add(attr.getCopy());
118
        }
119

    
120
        public String getId() {
121
                return this.id;
122
        }
123

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

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

    
148
        public FeatureAttributeDescriptor getAttributeDescriptor(int index) {
149
                return (FeatureAttributeDescriptor) super.get(index);
150
        }
151

    
152
        public FeatureType getCopy() {
153
                return new DefaultFeatureType(this);
154
        }
155

    
156
        public int getDefaultGeometryAttributeIndex() {
157
                return this.defaultGeometryAttributeIndex;
158
        }
159

    
160
        public String getDefaultGeometryAttributeName() {
161
                return this.defaultGeometryAttributeName;
162
        }
163

    
164
        public EditableFeatureType getEditable() {
165
                return new DefaultEditableFeatureType(this);
166
        }
167

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

    
180
        public FeatureRules getRules() {
181
                return this.rules;
182
        }
183

    
184
        public boolean hasEvaluators() {
185
                return this.hasEvaluators;
186
        }
187

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

    
219
        public IProjection getDefaultSRS() {
220
                if (this.getDefaultGeometryAttributeIndex() < 0) {
221
                        return null;
222
                }
223
                return this.getAttributeDescriptor(
224
                                this.getDefaultGeometryAttributeIndex()).getSRS();
225
        }
226

    
227
        public void validateFeature(Feature feature, int mode) {
228
                if (Feature.UPDATE == mode){
229
                        ((DefaultFeatureRules)getRules()).validate(feature);
230
                }
231
        }
232

    
233
        public FeatureType getSubtype(String[] names) throws DataException {
234
                return new SubtypeFeatureType(this, names);
235
        }
236

    
237
        public boolean isSubtypeOf(FeatureType featureType) {
238
                return false;
239
        }
240

    
241

    
242

    
243
        class SubtypeFeatureType extends DefaultFeatureType {
244
                /**
245
                 *
246
                 */
247
                private static final long serialVersionUID = 6913732960073922540L;
248
                WeakReference parent;
249

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

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

    
282
                        this.defaultGeometryAttributeIndex = this
283
                                        .getIndex(this.defaultGeometryAttributeName);
284
                        if (this.defaultGeometryAttributeIndex < 0) {
285
                                this.defaultGeometryAttributeName = null;
286
                        }
287
                        this.parent = new WeakReference(parent);
288
                }
289

    
290
                public FeatureType getSubtype(String[] names) throws DataException {
291
                        return new SubtypeFeatureType((DefaultFeatureType) this.parent
292
                                        .get(), names);
293
                }
294

    
295
                public boolean isSubtypeOf(FeatureType featureType) {
296
                        if (featureType == null) {
297
                                return false;
298
                        }
299
                        FeatureType parent = (FeatureType) this.parent.get();
300
                        return featureType.equals(parent);
301
                }
302

    
303
                public EditableFeatureType getEditable() {
304
                        throw new UnsupportedOperationException();
305
                }
306
        }
307

    
308
        public class SubtypeFeatureTypeNameException extends DataException {
309

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

    
317
                public SubtypeFeatureTypeNameException(String name, String type) {
318
                        super(MESSAGE_FORMAT, MESSAGE_KEY, serialVersionUID);
319
                        setValue("name", name);
320
                        setValue("type", type);
321
                }
322
        }
323

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

    
343
        public Iterator iterator() {
344
                return getIterator(super.iterator());
345
        }
346

    
347
        protected Iterator getIterator(Iterator iter) {
348
                return new DelegatedIterator(iter);
349
        }
350

    
351
        protected class DelegatedIterator implements Iterator {
352

    
353
                protected Iterator iterator;
354

    
355
                public DelegatedIterator(Iterator iter) {
356
                        this.iterator = iter;
357
                }
358

    
359
                public boolean hasNext() {
360
                        return iterator.hasNext();
361
                }
362

    
363
                public Object next() {
364
                        return iterator.next();
365
                }
366

    
367
                public void remove() {
368
                        throw new UnsupportedOperationException();
369
                }
370

    
371
        }
372

    
373
        public boolean allowAutomaticValues() {
374
                return this.allowAtomaticValues;
375
        }
376

    
377
        public FeatureAttributeDescriptor[] getAttributeDescriptors() {
378
                return (FeatureAttributeDescriptor[]) super
379
                                .toArray(new FeatureAttributeDescriptor[super.size()]);
380
        }
381

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

    
399
        public FeatureAttributeDescriptor getDefaultGeometryAttribute() {
400
                if (this.defaultGeometryAttributeIndex < 0) {
401
                        return null;
402
                }
403
                return (FeatureAttributeDescriptor) super
404
                                .get(this.defaultGeometryAttributeIndex);
405
        }
406

    
407

    
408

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

    
435
                if (defaultGeometryAttributeName != otherType.defaultGeometryAttributeName) {
436
                        if (defaultGeometryAttributeName == null) {
437
                                return false;
438
                        }
439
                        return defaultGeometryAttributeName
440
                                        .equals(otherType.defaultGeometryAttributeName);
441

    
442
                }
443
                return true;
444

    
445
        }
446

    
447
        /**
448
         * Start of DynClass interface implementation
449
         * READONLY
450
         */
451

    
452
        public DynField addDynField(String name) {
453
                throw new UnsupportedOperationException();
454
        }
455

    
456
        public DynField getDeclaredDynField(String name) {
457
                return (DynField) getAttributeDescriptor(name);
458
        }
459

    
460
        public DynField[] getDeclaredDynFields() {
461
                return (DynField[]) getAttributeDescriptors();
462
        }
463

    
464
        public String getDescription() {
465
                return null;
466
        }
467

    
468
        public DynField getDynField(String name) {
469
                return (DynField) getAttributeDescriptor(name);
470
        }
471

    
472
        public DynField[] getDynFields() {
473
                return (DynField[]) getAttributeDescriptors();
474
        }
475

    
476
        public String getName() {
477
                return this.id + "_" + internalID;
478
        }
479

    
480
        public void removeDynField(String name) {
481
                throw new UnsupportedOperationException();
482

    
483
        }
484

    
485
        public void addDynMethod(DynMethod dynMethod) {
486
                throw new UnsupportedOperationException();
487

    
488
        }
489

    
490
        public void extend(DynClass dynClass) {
491
                throw new UnsupportedOperationException();
492

    
493
        }
494

    
495
        public void extend(String dynClassName) {
496
                throw new UnsupportedOperationException();
497

    
498
        }
499

    
500
        public void extend(String namespace, String dynClassName) {
501
                throw new UnsupportedOperationException();
502

    
503
        }
504

    
505
        public DynMethod getDeclaredDynMethod(String name)
506
                        throws DynMethodException {
507
                return null;
508
        }
509

    
510
        public DynMethod[] getDeclaredDynMethods() throws DynMethodException {
511
                return null;
512
        }
513

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

    
518
        public DynMethod getDynMethod(int code) throws DynMethodException {
519
                return null;
520
        }
521

    
522
        public DynMethod[] getDynMethods() throws DynMethodException {
523
                return null;
524
        }
525

    
526
        public DynClass[] getSuperDynClasses() {
527
                return null;
528
        }
529

    
530
        public boolean isInstance(DynObject dynObject) {
531
                if (dynObject.getDynClass().getName() == getName()) {
532
                        return true;
533
                }
534
                return false;
535
        }
536

    
537
        public DynObject newInstance() {
538

    
539
                throw new UnsupportedOperationException();
540
        }
541

    
542
        public void removeDynMethod(String name) {
543
                throw new UnsupportedOperationException();
544

    
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 addDynFieldRange(String name, int type,
554
                        Object defaultValue, Object min, Object max, boolean mandatory,
555
                        boolean persistent) {
556
                throw new UnsupportedOperationException();
557
        }
558

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

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

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

    
579
        public DynField addDynFieldChoice(String name, int type,
580
                        Object defaultValue, DynObjectValueItem[] values) {
581
                throw new UnsupportedOperationException();
582
        }
583

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

    
589
        public DynField addDynFieldSingle(String name, int type, Object defaultValue) {
590
                throw new UnsupportedOperationException();
591
        }
592

    
593
        public DynField addDynFieldString(String name) {
594
                throw new UnsupportedOperationException();
595
        }
596
        
597
        public DynField addDynFieldInt(String name) {
598
                throw new UnsupportedOperationException();
599
        }
600
        
601
        public DynField addDynFieldDouble(String name) {
602
                throw new UnsupportedOperationException();
603
        }
604
        
605
        public DynField addDynFieldFloat(String name) {
606
                throw new UnsupportedOperationException();
607
        }
608

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

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

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

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

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

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

    
633
        public DynField addDynFieldDate(String name) {
634
                throw new UnsupportedOperationException();
635
        }
636

    
637
        public void extend(DynStruct struct) {
638
                throw new UnsupportedOperationException();
639
        }
640

    
641
        public String getFullName() {
642
        // TODO: usar el DynClassName
643
                return this.id;
644
        }
645

    
646
        public String getNamespace() {
647
                return "DALFeature";
648
        }
649

    
650
        public DynStruct[] getSuperDynStructs() {
651
                return null;
652
        }
653

    
654
        public void setDescription(String description) {
655
                throw new UnsupportedOperationException();
656
        }
657

    
658
        public void setNamespace(String namespace) {
659
                throw new UnsupportedOperationException();
660
        }
661

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

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

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

    
674
        public DynField addDynFieldURI(String name) {
675
                throw new UnsupportedOperationException();
676
        }
677

    
678
    public boolean isExtendable(DynStruct dynStruct) {
679
        return false;
680
    }
681

    
682
        public void extend(DynStruct[] structs) {
683
                // TODO Auto-generated method stub
684
                
685
        }
686

    
687
        public void remove(DynStruct superDynStruct) {
688
                // TODO Auto-generated method stub
689
                
690
        }
691

    
692
        public void removeAll(DynStruct[] superDynStruct) {
693
                // TODO Auto-generated method stub
694
                
695
        }
696

    
697
        public FeatureAttributeDescriptor getDefaultTimeAttribute() {
698
                if (this.defaultTimeAttributeIndex < 0) {
699
                        return null;
700
                }
701
                return (FeatureAttributeDescriptor) super
702
                                .get(this.defaultTimeAttributeIndex);
703
        }
704
}