Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dataFile / src / org / gvsig / fmap / data / feature / file / dbf / DBFStore.java @ 23303

History | View | Annotate | Download (9.99 KB)

1
package org.gvsig.fmap.data.feature.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.fmap.data.CloseException;
11
import org.gvsig.fmap.data.DataCollection;
12
import org.gvsig.fmap.data.DataException;
13
import org.gvsig.fmap.data.DataExplorer;
14
import org.gvsig.fmap.data.DataExplorerParameters;
15
import org.gvsig.fmap.data.DataManager;
16
import org.gvsig.fmap.data.DataStoreParameters;
17
import org.gvsig.fmap.data.InitializeException;
18
import org.gvsig.fmap.data.OpenException;
19
import org.gvsig.fmap.data.ReadException;
20
import org.gvsig.fmap.data.ResourceManager;
21
import org.gvsig.fmap.data.WriteException;
22
import org.gvsig.fmap.data.feature.AttributeDescriptor;
23
import org.gvsig.fmap.data.feature.DefaultFeatureType;
24
import org.gvsig.fmap.data.feature.Feature;
25
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor;
26
import org.gvsig.fmap.data.feature.FeatureCollection;
27
import org.gvsig.fmap.data.feature.FeatureID;
28
import org.gvsig.fmap.data.feature.FeatureType;
29
import org.gvsig.fmap.data.feature.FeaturesWriter;
30
import org.gvsig.fmap.data.feature.InitializeWriterException;
31
import org.gvsig.fmap.data.feature.IsNotAttributeSettingException;
32
import org.gvsig.fmap.data.feature.file.FileExplorerParameters;
33
import org.gvsig.fmap.data.feature.file.FileStore;
34
import org.gvsig.metadata.IMetadata;
35
import org.gvsig.metadata.IMetadataManager;
36
import org.gvsig.metadata.MetadataManager;
37
import org.gvsig.tools.exception.BaseException;
38

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

    
46

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

    
51
                 ResourceManager resMan = ResourceManager.getResourceManager();
52

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

    
59
                 super.init(parameters,this.dbf);
60

    
61
//                 this.dbf.open();
62
//                 this.dbf.addObserver(this);
63
                 this.initFeatureType();
64
         }
65

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

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

    
88
         }
89

    
90

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

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

    
118
            }
119

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

    
136
        this.dbf.changed(this);
137
        }
138

    
139
        public DataCollection getDataCollection(FeatureType type, String filter, String order) throws ReadException {
140
                try {
141
                        type = this.checkFeatureTypeForCollection(type);
142
                } catch (DataException e) {
143
                        throw new ReadException(this.getName(), e);
144
                }
145

    
146
                FeatureCollection coll;
147
                if (order == null){
148
                        if (featureManager==null){
149
                                coll=new DBFFeatureCollectionBitSet(this,type,filter);
150
                        }else{
151
                                coll=new DBFFeatureCollection(featureManager,this,type,filter);
152
                        }
153
                }else{
154
                        coll=new DBFFeatureCollectionWithFeatureID(featureManager,this,type,filter,order);
155
                }
156

    
157
                this.addObserver(new WeakReference(coll));
158
                return coll;
159

    
160
        }
161

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

    
180
        /**
181
         *
182
         * Only for persistenced features
183
         *
184
         * @param index
185
         * @param featureType
186
         * @return
187
         * @throws ReadException
188
         */
189
        protected Feature getFeatureByIndex(long index, FeatureType featureType)
190
                        throws ReadException {
191
                if (index >= this.getFeatureCount()) {
192
                        throw new ReadException(this.getName(),
193
                                        "Unsupported operation for not persistented features");
194
                }
195
                DBFFeature feature = new DBFFeature(featureType, this, index);
196
                return feature;
197
        }
198

    
199
        /***
200
         * NOT supported in Alter Mode
201
         *
202
         * @param index
203
         * @return
204
         * @throws ReadException
205
         */
206
        protected Feature getFeatureByIndex(long index) throws ReadException {
207
                return this.getFeatureByIndex(index, this.getDefaultFeatureType());
208
        }
209

    
210
        public List getFeatureTypes() {
211
                featureTypes.add(0,getDefaultFeatureType());
212
        return featureTypes;
213
        }
214

    
215
        public boolean canAlterFeatureType() {
216
                return true;
217
        }
218

    
219
        public String getName() {
220
                return DATASTORE_NAME;
221
        }
222

    
223
        protected void doOpen() throws OpenException {
224
                //No hace nada
225

    
226
        }
227

    
228
        protected void doClose() throws CloseException {
229
                try {
230
                        dbf.close();
231
                } catch (DataException e) {
232
                        throw new CloseException(this.getName(),e);
233
                }
234

    
235
        }
236

    
237
        protected void doDispose() throws CloseException {
238
                super.doDispose();
239
                ResourceManager resMan = ResourceManager.getResourceManager();
240

    
241
            try {
242
                        resMan.remove(this.dbf);
243
                } catch (DataException e1) {
244
                        throw new CloseException(this.getName(),e1);
245
                } catch (KeyException e) {
246
                        // TODO Auto-generated catch block
247
                        throw new CloseException(this.getName(),e);
248
                }
249

    
250
                this.dbf = null;
251
                this.metadata=null;
252
        }
253

    
254
        public boolean isEditable() {
255
                return this.dbf.isEditable();
256
        }
257

    
258
        public IMetadata getMetadata() throws BaseException {
259
                if (metadata==null){
260
                        IMetadataManager manager=MetadataManager.getManager();
261
                        metadata=manager.create(DATASTORE_NAME);
262
                }
263
                return metadata;
264
        }
265

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

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

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

    
285

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

    
290

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

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

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

    
313
        /**
314
         * @param i
315
         * @param i2
316
         * @return
317
         * @throws ReadException
318
         */
319
        protected String getStringFieldValue(int rowIndex, int fieldId) throws ReadException {
320
                return this.dbf.getStringFieldValue(rowIndex, fieldId);
321
        }
322
        public boolean canWriteGeometry(int gvSIGgeometryType) {
323
                try {
324
                        return getFeaturesWriter().canWriteGeometry(gvSIGgeometryType);
325
                } catch (InitializeWriterException e) {
326
                        e.printStackTrace();
327
                }
328
                return false;
329
        }
330

    
331
        public String getClassName() {
332
                // TODO Auto-generated method stub
333
                return null;
334
        }
335
}