Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_data / src / org / gvsig / fmap / data / feature / DefaultFeatureType.java @ 23303

History | View | Annotate | Download (8.42 KB)

1
package org.gvsig.fmap.data.feature;
2

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

    
9
import org.gvsig.fmap.data.DataException;
10
import org.gvsig.fmap.data.InitializeException;
11

    
12
public class DefaultFeatureType extends ArrayList implements FeatureType{
13
        /**
14
         *
15
         */
16
        private static final long serialVersionUID = 5120922022663837016L;
17
        protected List featureRules=new ArrayList();//<FeatureRule>
18
        private String geometryColumn;
19
        private List allSRS;
20
        private String defaultSRS;
21
        private int[] geometryTypes = new int[0];
22
        private DateFormat dateFormat=DateFormat.getDateInstance();
23
        private String[] fieldsId;
24

    
25
        private int count=0;
26
        private WeakReference originalFeatureType=null;
27

    
28
        public DefaultFeatureType(){
29

    
30
        }
31
        public void addRule(FeatureRule rule) {
32
                featureRules.add(rule);
33
        }
34

    
35
        public void clearRules() {
36
                featureRules.clear();
37
        }
38

    
39

    
40
        public void validateFeatureEnd(Feature feature, FeatureStore featureStore) {
41
                Iterator rulesIterator=featureRules.iterator();
42
                FeatureRule rule;
43
                while(rulesIterator.hasNext()){
44
                        rule = (FeatureRule)rulesIterator.next();
45
                        if (rule.checkAtEnd()){
46
                                rule.validate(feature,featureStore);
47
                        }
48
                }
49
        }
50

    
51
        public void validateFeatureModifiction(Feature feature, FeatureStore featureStore) {
52
                Iterator rulesIterator=featureRules.iterator();
53
                FeatureRule rule;
54
                while(rulesIterator.hasNext()){
55
                        rule = (FeatureRule)rulesIterator.next();
56
                        if (rule.checkAtModification()){
57
                                rule.validate(feature,featureStore);
58
                        }
59
                }
60
        }
61

    
62
        public void validateFeature(Feature feature, FeatureStore featureStore) {
63
                Iterator rulesIterator=featureRules.iterator();
64
                FeatureRule rule;
65
                while(rulesIterator.hasNext()){
66
                        rule = (FeatureRule)rulesIterator.next();
67
                        rule.validate(feature,featureStore);
68
                }
69
        }
70

    
71
        public void setDefaultGeometry(String name) {
72
                this.geometryColumn=name;
73

    
74
        }
75

    
76
        public String getDefaultGeometry() {
77
                return geometryColumn;
78
        }
79

    
80
        public Feature create(FeatureStore store) throws InitializeException {
81
                return store.createFeature(this, false);
82
        }
83

    
84
        public List getAllSRS() {
85
                return allSRS;
86
        }
87

    
88
        public String getDefaultSRS() {
89
                return defaultSRS;
90
        }
91

    
92
        public int[] getGeometryTypes() {
93
                return geometryTypes;
94
        }
95

    
96

    
97
        public void setAllSRS(List allSRS) {
98
                this.allSRS = allSRS;
99
        }
100

    
101
        public void setDefaultSRS(String defaultSRS) {
102
                this.defaultSRS = defaultSRS;
103
        }
104

    
105
        public DateFormat getDateFormat() {
106
                return dateFormat;
107
        }
108

    
109
        public void setDateFormat(DateFormat dateFormat) {
110
                this.dateFormat=dateFormat;
111
        }
112

    
113
        public void setGeometryTypes(int[] geometryTypes) {
114
                this.geometryTypes=geometryTypes;
115
        }
116

    
117
        public String[] getFieldsId() {
118
                return fieldsId;
119
        }
120
        public String[] getFields() {
121
                Iterator iterator=this.iterator();
122
                ArrayList names=new ArrayList();
123
                while (iterator.hasNext()) {
124
            FeatureAttributeDescriptor fad = (FeatureAttributeDescriptor) iterator.next();
125
            names.add(fad.getName());
126
        }
127
                return (String[])names.toArray(new String[0]);
128
        }
129
        public void setFieldsId(String[] fieldsId) throws DataException {
130
                this.fieldsId = fieldsId;
131
                if (fieldsId != null) {
132
                        AttributeDescriptor attr = null;
133
                        int index = -1;
134
                        for (int i = 0; i < fieldsId.length; i++) {
135
                                index = this.getFieldIndex(fieldsId[i]);
136
                                if (!(index < 0 || index >= this.size())) {
137
                                        attr = (AttributeDescriptor) this.get(index);
138
                                        attr.loading();
139
                                        attr.setPrimaryKey(true);
140
                                        attr.stopLoading();
141
                                } else {
142
                                        throw new DataException("Field '" +fieldsId[i] + "' not found");
143
                                }
144
                        }
145
                }
146
        }
147

    
148
        /**
149
         * 
150
         * Return index of the value of the attribute 'name' in the feature values
151
         * list
152
         * 
153
         */
154
        public int getFieldIndex(String name) {
155
                int i;
156
                for (i=0;i<this.size();i++){
157
                        AttributeDescriptor attr=((AttributeDescriptor)this.get(i));
158
                        if (attr.getName().equals(name)) {
159
                                return attr.originalPosition();
160
                        }
161
                }
162
                return -1;
163
        }
164

    
165
        /**
166
         * Get the possition of attribute 'name' in the FeatureType at this moment
167
         */
168
        public int getCurrentIndex(String name) {
169

    
170
                int i;
171
                for (i = 0; i < this.size(); i++) {
172
                        AttributeDescriptor attr = ((AttributeDescriptor) this.get(i));
173
                        if (attr.getName().equals(name)) {
174
                                return i;
175
                        }
176
                }
177
                return -1;
178
        }
179

    
180

    
181

    
182
        public void add(int arg0, Object arg1) {
183
                throw new RuntimeException("Not supported!");
184
        }
185

    
186
        public boolean add(Object arg0) {
187
                ((AttributeDescriptor)arg0).setOrdinal(count);
188
                ((AttributeDescriptor) arg0).setFeatureType(this);
189
                count++;
190
                return super.add(arg0);
191
        }
192

    
193
        protected boolean addWithoutOrdinal(Object arg0) {
194
                ((AttributeDescriptor) arg0).setFeatureType(this);
195
                int max=((FeatureAttributeDescriptor)arg0).ordinal();
196
                if (count<=max){
197
                        count=max+1;
198
                }
199
                return super.add(arg0);
200
        }
201

    
202
        protected void fillFeatureType(DefaultFeatureType fType) {
203
                if (this.allSRS != null){
204
                        fType.allSRS = new ArrayList();
205
                        fType.allSRS.addAll(this.allSRS);
206
                }
207
                fType.dateFormat = this.dateFormat;
208
                fType.defaultSRS = this.defaultSRS;
209
                fType.featureRules.addAll(this.featureRules);
210
                if (this.fieldsId != null){
211
                        fType.fieldsId = new String[this.fieldsId.length];
212
                        System.arraycopy(this.fieldsId, 0, fType.fieldsId, 0,
213
                                        this.fieldsId.length);
214
                }
215
                fType.geometryColumn = this.geometryColumn;
216
                if (this.geometryTypes != null){
217
                        fType.geometryTypes = new int[this.geometryTypes.length];
218
                        System.arraycopy(this.geometryTypes, 0, fType.geometryTypes, 0,
219
                                        this.geometryTypes.length);
220
                }
221
                Iterator iter = this.iterator();
222
                while (iter.hasNext()){
223
                        fType.addWithoutOrdinal(((FeatureAttributeDescriptor) iter.next())
224
                                        .cloneAttribute());
225
                }
226
                fType.count = this.count;
227
        }
228

    
229
        public FeatureType cloneFeatureType() {
230
                DefaultFeatureType newFType = (DefaultFeatureType) this
231
                                .newFeatureType();
232
                newFType.setWeakReferenceOriginal(this);
233
                this.fillFeatureType(newFType);
234
                return newFType;
235
        }
236

    
237
        protected FeatureType newFeatureType(){
238
                return new DefaultFeatureType();
239

    
240
        }
241

    
242
        public FeatureAttributeDescriptor get(String name){
243
                return (FeatureAttributeDescriptor) this
244
                                .get(this.getCurrentIndex(name));
245
        }
246
        protected int getCount() {
247
                return count;
248
        }
249

    
250
        public void clear(){
251
                super.clear();
252
                this.count=0;
253
        }
254
        public FeatureAttributeDescriptor getByOrder(int i) {
255
                Iterator iterator=this.iterator();
256
                while (iterator.hasNext()) {
257
                        FeatureAttributeDescriptor fad = (FeatureAttributeDescriptor) iterator.next();
258
                        if (i==fad.ordinal()){
259
                                return fad;
260
                        }
261
                }
262
                return null;
263
        }
264

    
265
        /* (non-Javadoc)
266
         * @see org.gvsig.fmap.data.feature.FeatureType#createNewAttributeDescriptor()
267
         */
268
        public FeatureAttributeDescriptor createNewAttributeDescriptor() {
269
                return new AttributeDescriptor(this,true);
270
        }
271

    
272

    
273
        public FeatureAttributeDescriptor createAttributeDescriptor() {
274
                return new AttributeDescriptor(this,false);
275
        }
276
        public FeatureType getSubFeatureType(String[] fields) throws DataException {
277
                DefaultFeatureType featureType=(DefaultFeatureType)this.cloneFeatureType();
278

    
279
                if (fields != null) {
280
                        ArrayList toRemove = new ArrayList();
281
                        Iterator iter = this.iterator();
282
                        int index = -1;
283
                        int i = 0;
284
                        String name;
285
                        boolean found;
286
                        for (i = 0; i < fields.length; i++) {
287
                                index = this.getFieldIndex(fields[i]);
288
                                if (index < 0) {
289
                                        throw new DataException("Field '" + fields[i]
290
                                                        + "' not found");
291
                                }
292
                        }
293
                        while (iter.hasNext()) {
294
                                name = ((AttributeDescriptor) iter.next()).getName();
295
                                found = false;
296
                                for (i = 0; i < fields.length; i++) {
297
                                        if (name.equals(fields[i])) {
298
                                                found = true;
299
                                                break;
300
                                        }
301
                                }
302
                                if (!found) {
303
                                        index = this.getFieldIndex(name);
304
                                        toRemove.add(new Integer(index));
305
                                }
306

    
307
                        }
308
                        for (i = toRemove.size() - 1; i >= 0; i--) {
309
                                featureType.remove(((Integer) toRemove.get(i)).intValue());
310
                        }
311
                }
312

    
313
                return featureType;
314
        }
315

    
316
        protected void setWeakReferenceOriginal(DefaultFeatureType type) {
317
                originalFeatureType = new WeakReference(type);
318
        }
319
        public boolean isSubtypeOf(FeatureType featureType) {
320
                if (featureType.equals(this)){
321
                        return true;
322
                }
323
                if (this.originalFeatureType == null){
324
                        return false;
325
                }
326
                FeatureType myOriginal = (FeatureType) this.originalFeatureType.get();
327
                if (myOriginal == null){
328
                        return false;
329
                }
330
                return myOriginal.equals(featureType);
331
        }
332

    
333
}