Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataFile / src / org / gvsig / fmap / data / store / dbf / DBFStoreProvider.java @ 23914

History | View | Annotate | Download (11.5 KB)

1
package org.gvsig.fmap.data.store.dbf;
2

    
3
import java.io.File;
4
import java.text.DateFormat;
5
import java.text.ParseException;
6
import java.util.ArrayList;
7
import java.util.Date;
8
import java.util.Iterator;
9
import java.util.List;
10
import java.util.Locale;
11

    
12
import org.gvsig.fmap.data.DataExplorer;
13
import org.gvsig.fmap.data.DataStoreParameters;
14
import org.gvsig.fmap.data.DataTypes;
15
import org.gvsig.fmap.data.exceptions.CloseException;
16
import org.gvsig.fmap.data.exceptions.CreateException;
17
import org.gvsig.fmap.data.exceptions.DataException;
18
import org.gvsig.fmap.data.exceptions.InitializeException;
19
import org.gvsig.fmap.data.exceptions.OpenException;
20
import org.gvsig.fmap.data.exceptions.ReadException;
21
import org.gvsig.fmap.data.feature.EditableFeatureType;
22
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor;
23
import org.gvsig.fmap.data.feature.FeatureQuery;
24
import org.gvsig.fmap.data.feature.FeatureReference;
25
import org.gvsig.fmap.data.feature.FeatureType;
26
import org.gvsig.fmap.data.feature.exceptions.PerformEditingException;
27
import org.gvsig.fmap.data.feature.exceptions.UnknowDataTypeException;
28
import org.gvsig.fmap.data.feature.impl.DefaultFeatureType;
29
import org.gvsig.fmap.data.feature.spi.AbstractFeatureStoreProvider;
30
import org.gvsig.fmap.data.feature.spi.FeatureData;
31
import org.gvsig.fmap.data.feature.spi.FeatureSetProvider;
32
import org.gvsig.fmap.data.feature.spi.FeatureStoreProvider;
33
import org.gvsig.fmap.data.feature.spi.FeatureStoreProviderServices;
34
import org.gvsig.fmap.data.resource.exception.AccessResourceException;
35
import org.gvsig.fmap.data.resource.exception.ResourceBeginException;
36
import org.gvsig.fmap.data.resource.exception.ResourceNotifyChangesException;
37
import org.gvsig.fmap.data.resource.exception.ResourceNotifyCloseException;
38
import org.gvsig.fmap.data.resource.exception.ResourceNotifyDisposeException;
39
import org.gvsig.fmap.data.resource.file.FileResource;
40
import org.gvsig.fmap.data.resource.spi.ResourceConsumer;
41
import org.gvsig.fmap.data.resource.spi.ResourceProvider;
42
import org.gvsig.fmap.data.store.dbf.utils.DbaseFile;
43
import org.gvsig.metadata.Metadata;
44
import org.gvsig.tools.exception.NotYetImplemented;
45

    
46
import com.iver.utiles.XMLEntity;
47
import com.iver.utiles.XMLException;
48

    
49
public class DBFStoreProvider extends AbstractFeatureStoreProvider implements
50
                ResourceConsumer {
51
        public static String STORE_NAME = "DBFStore";
52
        public static String STORE_DESCRIPTION = "DBF file";
53
        //        private DBFResource dbf = null;
54
        private DbaseFile dbfFile = null;
55
        private ResourceProvider resource;
56
        protected Metadata metadata;
57
        private static final Locale ukLocale = new Locale("en", "UK");
58

    
59
        public String getName() {
60
                return STORE_NAME;
61
        }
62

    
63
        protected DBFStoreParameters getParameters() {
64
                return (DBFStoreParameters) this.store.getParameters();
65
        }
66

    
67
        public FeatureStoreProvider initialize(FeatureStoreProviderServices store)
68
                        throws InitializeException {
69
                super.initialize(store);
70
                DBFStoreParameters dbfParameters = (DBFStoreParameters) this.store
71
                                .getParameters();
72

    
73
                resource = this.createResource(FileResource.RESOURCE_TYPE_NAME,
74
                                new Object[] { getParameters().getFile() });
75
                resource.addConsumer(this);
76

    
77
//                DBFResource tmpResource = new DBFResource(dbfParameters);
78
//
79
//                try {
80
//                        this.dbf = (DBFResource) this.store.addResource(tmpResource);
81
//                } catch (DataException e1) {
82
//                        throw new InitializeException(this.getName(), e1);
83
//                }
84

    
85
                this.initFeatureType();
86
                return this;
87
        }
88

    
89

    
90

    
91

    
92

    
93
        public DataExplorer getExplorer() throws ReadException {
94
                // TODO Auto-generated method stub
95
                return null;
96
        }
97

    
98
        public FeatureData getFeatureDataByReference(FeatureReference reference, FeatureType featureType)
99
                        throws DataException {
100

    
101
                return this.getFeatureDataByIndex(((DBFFeatureReference) reference).getIndex(),
102
                                featureType);
103
        }
104

    
105
        public void performEditing() throws PerformEditingException {
106
                // TODO Auto-generated method stub
107

    
108
        }
109

    
110
        public XMLEntity getXMLEntity() throws XMLException {
111
                // TODO Auto-generated method stub
112
                return null;
113
        }
114

    
115
        public void setXMLEntity(XMLEntity xml) throws XMLException {
116
                // TODO Auto-generated method stub
117

    
118
        }
119

    
120
        /**
121
         *
122
         * Only for persistenced features
123
         *
124
         * @param index
125
         * @param featureType
126
         * @return
127
         * @throws ReadException
128
         */
129
        protected FeatureData getFeatureDataByIndex(long index,
130
                        FeatureType featureType) throws DataException {
131
                this.open();
132
                this.resource.begin();
133
                try {
134
                if (index >= this.getFeatureCount()) {
135
                        // FIXME
136
                        throw new ArrayIndexOutOfBoundsException("" + index);
137
                }
138
                FeatureData featureData = this.createFeatureData(featureType);
139
                Iterator iterator = featureType.iterator();
140
                while (iterator.hasNext()) {
141
                        FeatureAttributeDescriptor descriptor = (FeatureAttributeDescriptor) iterator
142
                                        .next();
143
                        this.loadValue(featureData, (int) index, descriptor);
144

    
145
                }
146

    
147
                featureData.setReference(new DBFFeatureReference(this, index));
148

    
149
                return featureData;
150
                } finally {
151
                        this.resource.end();
152
                }
153
        }
154

    
155
        /*
156
         * ==================================================
157
         */
158

    
159
        public FeatureData createFeatureData(FeatureType type) throws DataException {
160
                return new DBFFeatureData((DefaultFeatureType) type);
161
        }
162

    
163

    
164
        /*
165
         * ===================================================
166
         */
167

    
168
        FeatureStoreProviderServices getProviderServices() {
169
                return this.store;
170
        }
171

    
172

    
173
        private void initFeatureType() throws InitializeException {
174
                try {
175
                        this.open();
176
                        this.resource.begin();
177
                } catch (DataException e) {
178
                        throw new InitializeException(this.getName(), e);
179
                }
180
                try {
181
                        int fieldCount = -1;
182
                        fieldCount = dbfFile.getFieldCount();
183

    
184
                        EditableFeatureType fType = this.store.createFeatureType();
185

    
186
                        for (int i = 0; i < fieldCount; i++) {
187
                                char fieldType = dbfFile.getFieldType(i);
188
                                int attrType;
189
                                int precision = 0;
190

    
191
                                if (fieldType == 'L') {
192
                                        attrType = DataTypes.BOOLEAN;
193

    
194
                                } else if ((fieldType == 'F') || (fieldType == 'N')) {
195
                                        precision = dbfFile.getFieldDecimalLength(i);
196
                                        if (precision > 0) {
197
                                                attrType = DataTypes.DOUBLE;
198
                                        } else {
199
                                                attrType = DataTypes.INT;
200
                                        }
201
                                } else if (fieldType == 'C') {
202
                                        attrType = DataTypes.STRING;
203
                                } else if (fieldType == 'D') {
204
                                        attrType = DataTypes.DATE;
205
                                } else {
206
                                        throw new InitializeException(this.getName(),
207
                                                        new UnknowDataTypeException(
208
                                                                        dbfFile.getFieldName(i), "" + fieldType,
209
                                                                        this.getName()));
210
                                }
211
                                fType.add(dbfFile.getFieldName(i), attrType,
212
                                                dbfFile.getFieldLength(i)).setPrecision(precision);
213
                        }
214

    
215

    
216
                        FeatureType defaultType = fType.getNotEditableCopy();
217
                        List types = new ArrayList(1);
218
                        types.add(defaultType);
219
                        this.store.setFeatureTypes(types, defaultType);
220
                } finally {
221
                        this.resource.end();
222
                }
223
        }
224

    
225

    
226
        protected void loadValue(FeatureData featureData, int rowIndex,
227
                        FeatureAttributeDescriptor descriptor) throws ReadException {
228
                if (descriptor.getEvaluator() != null) {
229
                        // Nothing to do
230
                        return;
231
                }
232

    
233
                int fieldIndex = descriptor.getIndex(); // TODO check it's correct
234
                String value = null;
235
                try {
236
                        value = this.dbfFile.getStringFieldValue(rowIndex, fieldIndex);
237
                } catch (DataException e) {
238
                        throw new ReadException(this.store.getName(), e);
239
                }
240
                value = value.trim();
241
                int fieldType = descriptor.getDataType();
242
                switch (fieldType) {
243
                case DataTypes.STRING:
244
                        featureData.set(fieldIndex, value);
245
                        break;
246

    
247
                case DataTypes.DOUBLE:
248
                        try {
249
                                featureData.set(fieldIndex, new Double(value));
250
                        } catch (NumberFormatException e) {
251
                                featureData.set(fieldIndex, null);
252
                        }
253
                        break;
254

    
255
                case DataTypes.INT:
256
                        try {
257
                                featureData.set(fieldIndex, new Integer(value));
258
                        } catch (NumberFormatException e) {
259
                                featureData.set(fieldIndex, null);
260
                        }
261
                        break;
262

    
263
                case DataTypes.FLOAT:
264
                        try {
265
                                featureData.set(fieldIndex, new Float(value));
266
                        } catch (NumberFormatException e) {
267
                                featureData.set(fieldIndex, null);
268
                        }
269
                        break;
270

    
271
                case DataTypes.LONG:
272
                        try {
273
                                featureData.set(fieldIndex, new Long(value));
274
                        } catch (NumberFormatException e) {
275
                                featureData.set(fieldIndex, null);
276
                        }
277
                        break;
278

    
279
                case DataTypes.BOOLEAN:
280
                        featureData.set(fieldIndex, new Boolean(value));
281
                        break;
282

    
283
                case DataTypes.BYTE:
284
                        try {
285
                                featureData.set(fieldIndex, new Byte(value));
286
                        } catch (NumberFormatException e) {
287
                                featureData.set(fieldIndex, null);
288
                        }
289
                        break;
290

    
291
                case DataTypes.DATE:
292
                        String year = value.substring(0, 4);
293
                        String month = value.substring(4, 6);
294
                        String day = value.substring(6, 8);
295
                        DateFormat df;
296
                        if (descriptor.getDateFormat() == null){
297
                                df = DateFormat.getDateInstance(DateFormat.SHORT,
298
                                                ukLocale);
299
                        } else{
300
                                df = descriptor.getDateFormat();
301
                        }
302
                        /*
303
                         * Calendar c = Calendar.getInstance(); c.clear();
304
                         * c.set(Integer.parseInt(year), Integer.parseInt(month),
305
                         * Integer.parseInt(day)); c.set(Calendar.MILLISECOND, 0);
306
                         */
307
                        String strAux = month + "/" + day + "/" + year;
308
                        Date dat = null;
309
                        try {
310
                                dat = df.parse(strAux);
311
                        } catch (ParseException e) {
312
                                throw new ReadException(this.store.getName(), e);
313
                        }
314
                        featureData.set(fieldIndex, dat);
315
                        break;
316

    
317

    
318
                default:
319
                        featureData.set(fieldIndex, descriptor.getDefaultValue());
320
                        break;
321
                }
322
        }
323

    
324

    
325
        /***
326
         * NOT supported in Alter Mode
327
         *
328
         * @param index
329
         * @return
330
         * @throws ReadException
331
         */
332
        protected FeatureData getFeatureDataByIndex(long index) throws DataException {
333
                return this
334
                                .getFeatureDataByIndex(index, this.store
335
                                .getDefaultFeatureType());
336
        }
337

    
338
        protected int getFeatureCount() throws ReadException, OpenException,
339
                        ResourceNotifyChangesException {
340
                this.open();
341
                try {
342
                        this.resource.begin();
343
                } catch (ResourceBeginException e) {
344
                        throw new ReadException(this.getName(), e);
345
                }
346
                try {
347
                        return this.dbfFile.getRecordCount();
348
                } finally {
349
                        this.resource.end();
350
                }
351
        }
352

    
353
        public FeatureSetProvider createSet(FeatureQuery query)
354
                        throws DataException {
355
                return new DBFSetProvider(this, query);
356
        }
357

    
358
        public boolean canCreate() {
359
                return true;
360
        }
361

    
362
        public boolean canWriteGeometry(int geometryType) {
363
                return false;
364
        }
365

    
366
        public void create(DataStoreParameters parameters, FeatureType featureType,
367
                        boolean overwrite) throws CreateException {
368
                //TODO
369
                throw new NotYetImplemented();
370
        }
371

    
372
        public void open() throws OpenException {
373
                if (this.dbfFile.isOpen()) {
374
                        return;
375
                }
376

    
377

    
378

    
379
        }
380

    
381
        public void close() throws CloseException {
382
                super.close();
383
                if (!this.dbfFile.isOpen()) {
384
                        return;
385
                }
386
                //Cerrar recurso
387
                try {
388
                        this.resource.begin();
389
                } catch (ResourceBeginException e) {
390
                        throw new CloseException(this.getName(), e);
391
                }
392
                try {
393
                        this.dbfFile.close();
394
                        this.resource.notifyClose();
395

    
396
                } catch (ResourceNotifyCloseException e) {
397
                        throw new CloseException(this.getName(), e);
398
                } finally {
399
                        this.resource.end();
400
                }
401
        }
402

    
403
        public void dispose() throws CloseException {
404
                this.close();
405
                try {
406
                        this.resource.notifyDispose();
407
                } catch (ResourceNotifyDisposeException e) {
408
                        throw new CloseException(this.getName(), e);
409
                }
410
                dbfFile = null;
411
                this.resource.removeConsumer(this);
412
                resource = null;
413
                metadata = null;
414
                super.dispose();
415
        }
416

    
417
        public boolean closeResourceRequested(ResourceProvider resource) {
418
                try {
419
                        this.close();
420
                } catch (CloseException e) {
421
                        return false;
422
                }
423
                return true;
424
        }
425

    
426
        public boolean allowWrite() {
427
                File file;
428
                try {
429
                        file = (File) this.resource.get();
430
                } catch (AccessResourceException e) {
431
                        return false;
432
                }
433
                return super.allowWrite() && file.canWrite();
434
        }
435

    
436
        public void refresh() throws OpenException {
437
                try {
438
                        this.close();
439
                } catch (CloseException e) {
440
                        throw new OpenException(this.getName(), e);
441
                }
442
                this.open();
443
                try {
444
                        this.initFeatureType();
445
                } catch (InitializeException e) {
446
                        throw new OpenException(this.getName(), e);
447
                }
448
        }
449

    
450

    
451
}