Statistics
| Revision:

root / branches / Mobile_Compatible_Hito_1 / libFMap_dataFile / src / org / gvsig / data / datastores / vectorial / file / dbf / DBFStore.java @ 21571

History | View | Annotate | Download (9.81 KB)

1
package org.gvsig.data.datastores.vectorial.file.dbf;
2

    
3
import java.lang.ref.WeakReference;
4
import java.security.KeyException;
5
import java.util.ArrayList;
6
import java.util.Collection;
7
import java.util.Iterator;
8
import java.util.List;
9

    
10
import org.gvsig.data.CloseException;
11
import org.gvsig.data.DataException;
12
import org.gvsig.data.DataManager;
13
import org.gvsig.data.DataCollection;
14
import org.gvsig.data.DataExplorer;
15
import org.gvsig.data.DataExplorerParameters;
16
import org.gvsig.data.DataStoreParameters;
17
import org.gvsig.data.InitializeException;
18
import org.gvsig.data.OpenException;
19
import org.gvsig.data.ReadException;
20
import org.gvsig.data.ResourceManager;
21
import org.gvsig.data.WriteException;
22
import org.gvsig.data.datastores.vectorial.FeaturesWriter;
23
import org.gvsig.data.datastores.vectorial.file.FileExplorerParameters;
24
import org.gvsig.data.vectorial.AttributeDescriptor;
25
import org.gvsig.data.vectorial.AbstractFeatureStore;
26
import org.gvsig.data.vectorial.DefaultFeatureType;
27
import org.gvsig.data.vectorial.Feature;
28
import org.gvsig.data.vectorial.FeatureAttributeDescriptor;
29
import org.gvsig.data.vectorial.FeatureCollection;
30
import org.gvsig.data.vectorial.FeatureID;
31
import org.gvsig.data.vectorial.FeatureType;
32
import org.gvsig.data.vectorial.IsNotAttributeSettingException;
33
import org.gvsig.exceptions.BaseException;
34
import org.gvsig.fmap.geom.primitive.Envelope;
35
import org.gvsig.metadata.IMetadata;
36
import org.gvsig.metadata.IMetadataManager;
37
import org.gvsig.metadata.MetadataManager;
38
import org.opengis.filter.Filter;
39
import org.opengis.filter.sort.SortBy;
40

    
41
public class DBFStore extends AbstractFeatureStore{
42
        public static String DATASTORE_NAME = "DBFStore";
43
        private DBFResource dbf= null;
44
        protected List featureTypes = new ArrayList();//<FeatureType>
45
        protected IMetadata metadata;
46
        private DBFStoreParameters dbfParameters=null;
47

    
48

    
49
         public void init(DataStoreParameters parameters) throws InitializeException {
50
                 dbfParameters=(DBFStoreParameters)parameters;
51
                 DBFResource tmpResource = new DBFResource(dbfParameters);
52

    
53
                 ResourceManager resMan = ResourceManager.getResourceManager();
54

    
55
                 try {
56
                         this.dbf = (DBFResource)resMan.addResource(tmpResource);
57
                 } catch (DataException e1) {
58
                         throw new InitializeException(this.getName(),e1);
59
                 }
60

    
61
                 super.init(parameters,this.dbf);
62

    
63
//                 this.dbf.open();
64
//                 this.dbf.addObserver(this);
65
                 this.initFeatureType();
66
         }
67

    
68
         private void initFeatureType() throws InitializeException{
69
                    int fieldCount = -1;
70
                    try{
71
                            fieldCount =dbf.getFieldCount();
72
                    } catch (DataException e) {
73
                            throw new InitializeException(this.getName(),e);
74
                        }
75

    
76
                    defaultFeatureType=new DefaultFeatureType();
77
                            for (int i = 0; i < fieldCount; i++) {
78
                                    FeatureAttributeDescriptor fad;
79
                                        try {
80
                                                fad = createFeatureAttribute((DefaultFeatureType)defaultFeatureType, i);
81
                                        } catch (IsNotAttributeSettingException e) {
82
                                                throw new InitializeException(this.getName(),e);
83
                                        } catch (ReadException e) {
84
                                                // TODO Auto-generated catch block
85
                                                throw new InitializeException(this.getName(),e);
86
                                        }
87
                        defaultFeatureType.add(fad);
88
                    }
89

    
90
         }
91

    
92

    
93
         private FeatureAttributeDescriptor createFeatureAttribute(DefaultFeatureType fType, int i) throws ReadException, IsNotAttributeSettingException {
94
                char fieldType = dbf.getFieldType(i);
95
                AttributeDescriptor dad=(AttributeDescriptor) fType.createAttributeDescriptor();
96
                dad.loading();
97
                dad.setName(dbf.getFieldName(i));
98
                dad.setSize(dbf.getFieldLength(i));
99
                if (fieldType == 'L') {
100
                        dad.setType(FeatureAttributeDescriptor.TYPE_BOOLEAN);
101

    
102
                } else if ((fieldType == 'F') || (fieldType == 'N')) {
103
                        int precision = dbf.getFieldDecimalLength(i);
104
                        if (precision > 0){
105
                                dad.setType(FeatureAttributeDescriptor.TYPE_DOUBLE);
106
                                dad.setPrecision(precision);
107
                        } else{
108
                                dad.setType(FeatureAttributeDescriptor.TYPE_INT);
109
                        }
110
                } else if (fieldType == 'C') {
111
                        dad.setType(FeatureAttributeDescriptor.TYPE_STRING);
112
                } else if (fieldType == 'D') {
113
                        dad.setType(FeatureAttributeDescriptor.TYPE_DATE);
114
                } else {
115
//                    throw new BadFieldDriverException(getName(),null,String.valueOf(fieldType));
116
                }
117
                dad.stopLoading();
118
                      return dad;
119

    
120
            }
121

    
122
        protected void doFinishEdition() throws WriteException, ReadException {
123
                FeaturesWriter writer = getFeaturesWriter();
124
        writer.init(this);
125
        writer.updateFeatureType(this.getDefaultFeatureType());
126
        this.dbf.editing();
127
        writer.preProcess();
128
        Collection collection=getDataCollection();
129
        Iterator iterator=collection.iterator();
130
        Feature feature;
131
        while (iterator.hasNext()) {
132
                feature= (Feature) iterator.next();
133
                        writer.insertFeature(feature);
134
                }
135
        writer.postProcess();
136
        this.dbf.stopEditing();
137

    
138
        this.dbf.changed(this);
139
        }
140

    
141
        public DataCollection getDataCollection(FeatureType type, Filter filter, SortBy[] order) throws ReadException {
142
                if (type==null){
143
                        type=getDefaultFeatureType();
144
                } else {
145
                        if (!type.isSubtypeOf(this.getDefaultFeatureType())){
146
                                throw new ReadException("invalid type",this.getName());
147
                        }
148
                }
149
                FeatureCollection coll;
150
                if (order == null){
151
                        if (featureManager==null){
152
                                coll=new DBFFeatureCollectionBitSet(this,type,filter);
153
                        }else{
154
                                coll=new DBFFeatureCollection(featureManager,this,type,filter);
155
                        }
156
                }else{
157
                        coll=new DBFFeatureCollectionWithFeatureID(featureManager,this,type,filter,order);
158
                }
159

    
160
                this.addObserver(new WeakReference(coll));
161
                return coll;
162

    
163
        }
164

    
165
        public Feature getFeatureByID(FeatureID id,FeatureType featureType) throws ReadException {
166
                if (featureType == null){
167
                        featureType=this.getDefaultFeatureType();
168
                } else{
169
                        if (!featureType.isSubtypeOf(this.getDefaultFeatureType())){
170
                                throw new ReadException("invalid type",this.getName());
171
                        }
172
                }
173
                if (this.alterMode){
174
                    if (featureManager.contains(id)) {
175
                            return featureManager.getFeature(id,this,featureType);
176
                    }
177
            }
178
                DBFFeature feature=new DBFFeature(featureType,this,((DBFFeatureID)id).getIndex());
179
                return feature;
180
        }
181

    
182
        public List getFeatureTypes() {
183
                featureTypes.add(0,getDefaultFeatureType());
184
        return featureTypes;
185
        }
186

    
187
        public boolean isWithDefaultLegend() {
188
                return false;
189
        }
190

    
191
        public Object getDefaultLegend() {
192
                return null;
193
        }
194

    
195
        public Object getDefaultLabelingStrategy() {
196
                return null;
197
        }
198

    
199
        public boolean canAlterFeatureType() {
200
                return true;
201
        }
202

    
203
        public String getName() {
204
                return DATASTORE_NAME;
205
        }
206

    
207
        protected void doOpen() throws OpenException {
208
                //No hace nada
209

    
210
        }
211

    
212
        protected void doClose() throws CloseException {
213
                try {
214
                        dbf.close();
215
                } catch (DataException e) {
216
                        throw new CloseException(this.getName(),e);
217
                }
218

    
219
        }
220

    
221
        protected void doDispose() throws CloseException {
222
                super.doDispose();
223
                ResourceManager resMan = ResourceManager.getResourceManager();
224

    
225
            try {
226
                        resMan.remove(this.dbf);
227
                } catch (DataException e1) {
228
                        throw new CloseException(this.getName(),e1);
229
                } catch (KeyException e) {
230
                        // TODO Auto-generated catch block
231
                        throw new CloseException(this.getName(),e);
232
                }
233

    
234
                this.dbf = null;
235
                this.metadata=null;
236
        }
237

    
238
        public boolean isEditable() {
239
                return this.dbf.isEditable();
240
        }
241

    
242
        public IMetadata getMetadata() throws BaseException {
243
                if (metadata==null){
244
                        IMetadataManager manager=MetadataManager.getManager();
245
                        metadata=manager.create(DATASTORE_NAME);
246
                }
247
                if (this.alterMode){
248
                        Envelope extent=(Envelope)metadata.get("extent");
249
                        FeatureCollection featureCollection=(FeatureCollection)getDataCollection();
250
                    if (spatialManager.isFullExtentDirty()){
251
                            if (!featureCollection.isEmpty()){
252
                                    Iterator featureIterator=featureCollection.iterator();
253
                                    extent = ((Feature)featureIterator.next()).getExtent();
254
                                    while(featureIterator.hasNext()){
255
                                            Feature feature=(Feature)featureIterator.next();
256
                                            Envelope boundExtent=feature.getExtent();
257
                                            if (boundExtent!=null)
258
                                                    extent.add(boundExtent);
259
                                    }
260
                            }
261
                    }
262
                    metadata.set("extent",extent);
263
                }
264
                return metadata;
265
        }
266

    
267
//        protected Feature getFeatureByPosition(FeatureType ft,long position) throws ReadException {
268
////                 Field Type (C  or M)
269
////       char cfieldType = fieldTypes[fieldId];
270
////       int fieldType = getFieldType(fieldId);
271
//                DBFFeature feature=new DBFFeature(ft,this,position);
272
//                feature.load(this.dbf);
273
//                return feature;
274
//        }
275

    
276
        protected FeaturesWriter getFeaturesWriter() {
277
                FeaturesWriter writer = new DBFFeaturesWriter();
278
//                writer.init(this);
279
                return writer;
280
        }
281

    
282
        protected long getFeatureCount() throws ReadException{
283
                return dbf.getRecordCount();
284
        }
285

    
286

    
287
        public DataStoreParameters getParameters() {
288
                return parameters;
289
        }
290

    
291

    
292
        public DataExplorer getExplorer() {
293
                DataManager dsm=DataManager.getManager();
294
                DataExplorerParameters dsp = dsm.createDataExplorerParameters(
295
                                DBFDataExplorer.DATASOURCE_NAME);
296
                ((FileExplorerParameters)dsp).setSource(dbfParameters.getFile().getParentFile());
297

    
298
                DataExplorer src=null;
299
                try {
300
                        src = dsm.createDataExplorer(dsp);
301
                } catch (InitializeException e1) {
302
                        e1.printStackTrace();
303
                }
304
                return src;
305
        }
306

    
307
        /* (non-Javadoc)
308
         * @see org.gvsig.data.vectorial.FeatureStore#doRefresh()
309
         */
310
        protected void doRefresh() throws OpenException, InitializeException {
311
                this.initFeatureType();
312
        }
313

    
314
        /**
315
         * @param i
316
         * @param i2
317
         * @return
318
         * @throws ReadException
319
         */
320
        protected String getStringFieldValue(int rowIndex, int fieldId) throws ReadException {
321
                return this.dbf.getStringFieldValue(rowIndex, fieldId);
322
        }
323

    
324
}