Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultFeatureType.java @ 27700

History | View | Annotate | Download (7.67 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.Iterator;
6
import java.util.List;
7

    
8
import org.cresques.cts.IProjection;
9
import org.gvsig.fmap.dal.exception.DataException;
10
import org.gvsig.fmap.dal.feature.EditableFeatureType;
11
import org.gvsig.fmap.dal.feature.Feature;
12
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
13
import org.gvsig.fmap.dal.feature.FeatureRules;
14
import org.gvsig.fmap.dal.feature.FeatureType;
15
import org.gvsig.tools.exception.NotYetImplemented;
16

    
17
public class DefaultFeatureType extends ArrayList implements FeatureType {
18

    
19
        /**
20
         *
21
         */
22
        private static final long serialVersionUID = -7988721447349282215L;
23

    
24
        private DefaultFeatureRules rules;
25
        private boolean hasEvaluators;
26
        protected String defaultGeometryAttributeName;
27
        protected int defaultGeometryAttributeIndex;
28
        private String id;
29
        protected boolean hasOID;
30
        protected boolean allowAtomaticValues;
31

    
32
        protected DefaultFeatureType(String id) {
33
                this.id = id;
34
                this.rules = new DefaultFeatureRules();
35
                this.hasEvaluators = false;
36
                this.defaultGeometryAttributeName = null;
37
                this.defaultGeometryAttributeIndex = -1;
38
                this.allowAtomaticValues = false;
39
        }
40

    
41
        protected DefaultFeatureType() {
42
                this("default");
43
        }
44

    
45
        protected DefaultFeatureType(DefaultFeatureType other) {
46
                initialize(other, true);
47
        }
48

    
49
        protected DefaultFeatureType(DefaultFeatureType other,
50
                        boolean copyAttributes) {
51
                initialize(other, copyAttributes);
52
        }
53

    
54
        protected void initialize(DefaultFeatureType other, boolean copyAttributes) {
55
                this.id = other.getId();
56
                if (copyAttributes) {
57
                        Iterator iter = other.iterator();
58
                        DefaultFeatureAttributeDescriptor attr;
59
                        while (iter.hasNext()) {
60
                                attr = (DefaultFeatureAttributeDescriptor) iter.next();
61
                                this.intitalizeAddAttibute(attr);
62
                        }
63
                }
64
                this.defaultGeometryAttributeName = other.defaultGeometryAttributeName;
65
                this.hasEvaluators = other.hasEvaluators;
66
                this.rules = (DefaultFeatureRules) other.rules.getCopy();
67
                this.defaultGeometryAttributeIndex = other.defaultGeometryAttributeIndex;
68
                this.hasOID = other.hasOID;
69
                this.id = other.id; // XXX ???? copiar o no esto????
70
        }
71

    
72
        protected void intitalizeAddAttibute(DefaultFeatureAttributeDescriptor attr) {
73
                super.add(attr.getCopy());
74
        }
75

    
76
        public String getId() {
77
                return this.id;
78
        }
79

    
80
        public Object get(String name) {
81
                FeatureAttributeDescriptor attr;
82
                Iterator iter = this.iterator();
83
                while (iter.hasNext()) {
84
                        attr = (FeatureAttributeDescriptor) iter.next();
85
                        if (attr.getName().equalsIgnoreCase(name)) {
86
                                return attr;
87
                        }
88
                }
89
                return null;
90
        }
91

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

    
104
        public FeatureAttributeDescriptor getAttributeDescriptor(int index) {
105
                return (FeatureAttributeDescriptor) super.get(index);
106
        }
107

    
108
        public FeatureType getCopy() {
109
                return new DefaultFeatureType(this);
110
        }
111

    
112
        public int getDefaultGeometryAttributeIndex() {
113
                return this.defaultGeometryAttributeIndex;
114
        }
115

    
116
        public String getDefaultGeometryAttributeName() {
117
                return this.defaultGeometryAttributeName;
118
        }
119

    
120
        public EditableFeatureType getEditable() {
121
                return new DefaultEditableFeatureType(this);
122
        }
123

    
124
        public int getIndex(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.getIndex();
131
                        }
132
                }
133
                return -1;
134
        }
135

    
136
        public FeatureRules getRules() {
137
                return this.rules;
138
        }
139

    
140
        public boolean hasEvaluators() {
141
                return this.hasEvaluators;
142
        }
143

    
144
        public List getSRSs() {
145
                // FIXME: Falta por implementar
146
                throw new NotYetImplemented();
147
        }
148

    
149
        public IProjection getDefaultSRS() {
150
                return this.getAttributeDescriptor(
151
                                this.getDefaultGeometryAttributeIndex()).getSRS();
152
        }
153

    
154
        public void validateFeature(Feature feature, int mode) {
155
                if (Feature.UPDATE == mode){
156
                        ((DefaultFeatureRules)getRules()).validate(feature);
157
                }
158
        }
159

    
160
        public FeatureType getSubtype(String[] names) throws DataException {
161
                return new SubtypeFeatureType(this, names);
162
        }
163

    
164
        public boolean isSubtypeOf(FeatureType featureType) {
165
                return false;
166
        }
167

    
168

    
169

    
170
        class SubtypeFeatureType extends DefaultFeatureType {
171
                /**
172
                 *
173
                 */
174
                private static final long serialVersionUID = 6913732960073922540L;
175
                WeakReference parent;
176

    
177
                SubtypeFeatureType(DefaultFeatureType parent, String[] names)
178
                                throws DataException {
179
                        super(parent, false);
180
                        DefaultFeatureAttributeDescriptor attrcopy;
181
                        DefaultFeatureAttributeDescriptor attr;
182
                        // Copy attributes
183
                        for (int i = 0; i < names.length; i++) {
184
                                attr = (DefaultFeatureAttributeDescriptor) parent
185
                                                .getAttributeDescriptor(names[i]);
186
                                if (attr == null) {
187
                                        throw new SubtypeFeatureTypeNameException(names[i], parent
188
                                                        .getId());
189
                                }
190
                                attrcopy = new DefaultFeatureAttributeDescriptor(attr);
191
                                this.add(attrcopy);
192
                                attrcopy.index = i;
193
                        }
194

    
195
                        // Add missing pk fiels if any
196
                        if (!parent.hasOID()) {
197
                                Iterator iter = parent.iterator();
198
                                while (iter.hasNext()) {
199
                                        attr = (DefaultFeatureAttributeDescriptor) iter.next();
200
                                        if (attr.isPrimaryKey()
201
                                                        && this.getIndex(attr.getName()) < 0) {
202
                                                attrcopy = new DefaultFeatureAttributeDescriptor(attr);
203
                                                this.add(attrcopy);
204
                                                attrcopy.index = this.size() - 1;
205
                                        }
206
                                }
207
                        }
208

    
209
                        this.defaultGeometryAttributeIndex = this
210
                                        .getIndex(this.defaultGeometryAttributeName);
211
                        if (this.defaultGeometryAttributeIndex < 0) {
212
                                this.defaultGeometryAttributeName = null;
213
                        }
214
                        this.parent = new WeakReference(parent);
215
                }
216

    
217
                public FeatureType getSubtype(String[] names) throws DataException {
218
                        return new SubtypeFeatureType((DefaultFeatureType) this.parent
219
                                        .get(), names);
220
                }
221

    
222
                public boolean isSubtypeOf(FeatureType featureType) {
223
                        if (featureType == null) {
224
                                return false;
225
                        }
226
                        FeatureType parent = (FeatureType) this.parent.get();
227
                        return featureType.equals(parent);
228
                }
229

    
230
                public EditableFeatureType getEditable() {
231
                        throw new UnsupportedOperationException();
232
                }
233
        }
234

    
235
        public class SubtypeFeatureTypeNameException extends DataException {
236

    
237
                /**
238
                 *
239
                 */
240
                private static final long serialVersionUID = -4414242486723260101L;
241
                private final static String MESSAGE_FORMAT = "Attribute name '%(name)s' not found in type (%(type)s).";
242
                private final static String MESSAGE_KEY = "_SubtypeFeatureTypeNameException";
243

    
244
                public SubtypeFeatureTypeNameException(String name, String type) {
245
                        super(MESSAGE_FORMAT, MESSAGE_KEY, serialVersionUID);
246
                        setValue("name", name);
247
                        setValue("type", type);
248
                }
249
        }
250

    
251
        public boolean hasOID() {
252
                return hasOID;
253
        }
254
        public String toString(){
255
                StringBuffer s = new StringBuffer();
256
                s.append(this.getId());
257
                s.append(":[");
258
                String attName;
259
                for (int i = 0; i < size(); i++) {
260
                        attName =((FeatureAttributeDescriptor)get(i)).getName().toString();
261
                        s.append(attName);
262
                        if (i < size() - 1) {
263
                                s.append(',');
264
                        }
265
                }
266
                s.append(']');
267
                return s.toString();
268
        }
269

    
270
        public Iterator iterator() {
271
                return getIterator(super.iterator());
272
        }
273

    
274
        protected Iterator getIterator(Iterator iter) {
275
                return new DelegatedIterator(iter);
276
        }
277

    
278
        protected class DelegatedIterator implements Iterator {
279

    
280
                protected Iterator iterator;
281

    
282
                public DelegatedIterator(Iterator iter) {
283
                        this.iterator = iter;
284
                }
285

    
286
                public boolean hasNext() {
287
                        return iterator.hasNext();
288
                }
289

    
290
                public Object next() {
291
                        return iterator.next();
292
                }
293

    
294
                public void remove() {
295
                        throw new UnsupportedOperationException();
296
                }
297

    
298
        }
299

    
300
        public boolean allowAutomaticValues() {
301
                return this.allowAtomaticValues;
302
        }
303

    
304
}