Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_data / src / org / gvsig / data / vectorial / DefaultFeatureType.java @ 21511

History | View | Annotate | Download (7.63 KB)

1
package org.gvsig.data.vectorial;
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.data.DataException;
10
import org.gvsig.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
        public int getFieldIndex(String name) {
149
                int i;
150
                for (i=0;i<this.size();i++){
151
                        AttributeDescriptor attr=((AttributeDescriptor)this.get(i));
152
                        if (attr.getName().equals(name))
153
                                return attr.originalPosition();
154
                }
155
                return -1;
156
        }
157

    
158
        public void add(int arg0, Object arg1) {
159
                throw new RuntimeException("Not supported!");
160
        }
161

    
162
        public boolean add(Object arg0) {
163
                ((AttributeDescriptor)arg0).setOrdinal(count);
164
                count++;
165
                return super.add(arg0);
166
        }
167

    
168
        protected boolean addWithoutOrdinal(Object arg0) {
169
                int max=((FeatureAttributeDescriptor)arg0).ordinal();
170
                if (count<=max){
171
                        count=max+1;
172
                }
173
                return super.add(arg0);
174
        }
175

    
176
        public FeatureType cloneFeatureType() {
177
                DefaultFeatureType newFType = (DefaultFeatureType) this.newFeatureType();
178
                newFType.setWeakReferenceOriginal(this);
179
                if (this.allSRS != null){
180
                        newFType.allSRS = new ArrayList();
181
                        newFType.allSRS.addAll(this.allSRS);
182
                }
183
                newFType.dateFormat = this.dateFormat;
184
                newFType.defaultSRS = this.defaultSRS;
185
                newFType.featureRules.addAll(this.featureRules);
186
                if (this.fieldsId != null){
187
                        newFType.fieldsId = new String[this.fieldsId.length];
188
                        System.arraycopy(this.fieldsId, 0, newFType.fieldsId, 0, this.fieldsId.length);
189
                }
190
                newFType.geometryColumn = this.geometryColumn;
191
                if (this.geometryTypes != null){
192
                        newFType.geometryTypes = new int[this.geometryTypes.length];
193
                        System.arraycopy(this.geometryTypes, 0, newFType.geometryTypes, 0, this.geometryTypes.length);
194
                }
195
                Iterator iter = this.iterator();
196
                while (iter.hasNext()){
197
                        newFType.addWithoutOrdinal(((FeatureAttributeDescriptor)iter.next()).cloneAttribute());
198
                }
199
                newFType.count=this.count;
200
                return newFType;
201
        }
202

    
203
        protected FeatureType newFeatureType(){
204
                return new DefaultFeatureType();
205

    
206
        }
207

    
208
        public FeatureAttributeDescriptor get(String name){
209
                return (FeatureAttributeDescriptor) this.get(this.getFieldIndex(name));
210
        }
211
        protected int getCount() {
212
                return count;
213
        }
214

    
215
        public void clear(){
216
                super.clear();
217
                this.count=0;
218
        }
219
        public FeatureAttributeDescriptor getByOrder(int i) {
220
                Iterator iterator=this.iterator();
221
                while (iterator.hasNext()) {
222
                        FeatureAttributeDescriptor fad = (FeatureAttributeDescriptor) iterator.next();
223
                        if (i==fad.ordinal()){
224
                                return fad;
225
                        }
226
                }
227
                return null;
228
        }
229

    
230
        /* (non-Javadoc)
231
         * @see org.gvsig.data.vectorial.FeatureType#createNewAttributeDescriptor()
232
         */
233
        public FeatureAttributeDescriptor createNewAttributeDescriptor() {
234
                return new AttributeDescriptor(this,true);
235
        }
236

    
237

    
238
        public FeatureAttributeDescriptor createAttributeDescriptor() {
239
                return new AttributeDescriptor(this,false);
240
        }
241
        public FeatureType getSubFeatureType(String[] fields) throws DataException {
242
                DefaultFeatureType featureType=(DefaultFeatureType)this.cloneFeatureType();
243
                ArrayList toRemove = new ArrayList();
244
                Iterator iter = this.iterator();
245
                int index=-1;
246
                int i=0;
247
                String name;
248
                boolean found;
249
                for(i=0;i<fields.length;i++){
250
                        index=this.getFieldIndex(fields[i]);
251
                        if (index<0){
252
                                throw new DataException("Field '"+fields[i]+"' not found");
253
                        }
254
                }
255
                while (iter.hasNext()){
256
                        name = ((AttributeDescriptor)iter.next()).getName();
257
                        found=false;
258
                        for (i=0;i<fields.length;i++){
259
                                if (name.equals(fields[i])){
260
                                        found=true;
261
                                        break;
262
                                }
263
                        }
264
                        if (!found){
265
                                index=this.getFieldIndex(name);
266
                                toRemove.add(new Integer(index));
267
                        }
268

    
269
                }
270
                for (i=toRemove.size()-1;i>=0;i--){
271
                        featureType.remove(((Integer)toRemove.get(i)).intValue());
272
                }
273

    
274
                return featureType;
275
        }
276

    
277
        protected void setWeakReferenceOriginal(DefaultFeatureType type) {
278
                originalFeatureType = new WeakReference(type);
279
        }
280
        public boolean isSubtypeOf(FeatureType featureType) {
281
                if (featureType.equals(this)){
282
                        return true;
283
                }
284
                if (this.originalFeatureType == null){
285
                        return false;
286
                }
287
                FeatureType myOriginal = (FeatureType) this.originalFeatureType.get();
288
                if (myOriginal == null){
289
                        return false;
290
                }
291
                return myOriginal.equals(featureType);
292
        }
293

    
294
}