Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dalfile / src / org / gvsig / fmap / dal / store / dbf / DBFStoreProvider.java @ 39083

History | View | Annotate | Download (21.8 KB)

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

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

    
14
import org.slf4j.Logger;
15
import org.slf4j.LoggerFactory;
16

    
17
import org.gvsig.fmap.dal.DALLocator;
18
import org.gvsig.fmap.dal.DataManager;
19
import org.gvsig.fmap.dal.DataServerExplorer;
20
import org.gvsig.fmap.dal.DataStoreNotification;
21
import org.gvsig.fmap.dal.DataTypes;
22
import org.gvsig.fmap.dal.FileHelper;
23
import org.gvsig.fmap.dal.exception.CloseException;
24
import org.gvsig.fmap.dal.exception.DataException;
25
import org.gvsig.fmap.dal.exception.FileNotFoundException;
26
import org.gvsig.fmap.dal.exception.InitializeException;
27
import org.gvsig.fmap.dal.exception.OpenException;
28
import org.gvsig.fmap.dal.exception.ReadException;
29
import org.gvsig.fmap.dal.exception.UnsupportedVersionException;
30
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
31
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
32
import org.gvsig.fmap.dal.feature.EditableFeatureType;
33
import org.gvsig.fmap.dal.feature.Feature;
34
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
35
import org.gvsig.fmap.dal.feature.FeatureQuery;
36
import org.gvsig.fmap.dal.feature.FeatureSet;
37
import org.gvsig.fmap.dal.feature.FeatureStore;
38
import org.gvsig.fmap.dal.feature.FeatureType;
39
import org.gvsig.fmap.dal.feature.exception.PerformEditingException;
40
import org.gvsig.fmap.dal.feature.exception.UnknownDataTypeException;
41
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider;
42
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
43
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
44
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider;
45
import org.gvsig.fmap.dal.resource.ResourceAction;
46
import org.gvsig.fmap.dal.resource.exception.AccessResourceException;
47
import org.gvsig.fmap.dal.resource.exception.ResourceException;
48
import org.gvsig.fmap.dal.resource.exception.ResourceExecuteException;
49
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyChangesException;
50
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyCloseException;
51
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyOpenException;
52
import org.gvsig.fmap.dal.resource.file.FileResource;
53
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer;
54
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
55
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer;
56
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters;
57
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
58
import org.gvsig.fmap.dal.store.dbf.utils.DbaseFile;
59
import org.gvsig.metadata.MetadataLocator;
60
import org.gvsig.metadata.MetadataManager;
61
import org.gvsig.metadata.exceptions.MetadataException;
62
import org.gvsig.tools.dispose.DisposableIterator;
63
import org.gvsig.tools.dynobject.DynObject;
64
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
65
import org.gvsig.tools.exception.BaseException;
66

    
67
public class DBFStoreProvider extends AbstractFeatureStoreProvider implements
68
                ResourceConsumer {
69

    
70
    private static final Logger LOG = LoggerFactory.getLogger(DBFStoreProvider.class);
71

    
72
        public static String NAME = "DBF";
73
        public static String DESCRIPTION = "DBF file";
74
        private static final Locale ukLocale = new Locale("en", "UK");
75
        
76
        public static final String METADATA_DEFINITION_NAME = NAME;
77
        private static final String METADATA_ENCODING = "Encoding";
78
        private static final String METADATA_CODEPAGE = "CodePage";
79
                
80
        private DbaseFile dbfFile = null;
81
        private ResourceProvider dbfResource;
82
        private long counterNewsOIDs = -1;
83
        private DBFFeatureWriter writer;
84
        
85
        private static long lastLogTime = 0;
86

    
87

    
88
        protected static void registerMetadataDefinition() throws MetadataException {
89
                MetadataManager manager = MetadataLocator.getMetadataManager();
90
                if( manager.getDefinition(METADATA_DEFINITION_NAME)==null ) {
91
                        manager.addDefinition(
92
                                        METADATA_DEFINITION_NAME, 
93
                                        DBFStoreParameters.class.getResourceAsStream("DBFStoreMetadata.xml"),
94
                                        DBFStoreParameters.class.getClassLoader()
95
                        );
96
                }
97
        }
98

    
99
        public DBFStoreProvider(DBFStoreParameters params,
100
                        DataStoreProviderServices storeServices)
101
                        throws InitializeException {
102
                super(
103
                                params, 
104
                                storeServices,
105
                                FileHelper.newMetadataContainer(METADATA_DEFINITION_NAME)
106
                );
107
                this.init(params, storeServices);
108
        }
109

    
110
        protected DBFStoreProvider(DBFStoreParameters params,
111
                        DataStoreProviderServices storeServices, DynObject metadata)
112
                        throws InitializeException {
113
                super(params, storeServices, metadata);
114
                this.init(params, storeServices);
115
        }
116

    
117
        protected void init(DBFStoreParameters params,
118
                        DataStoreProviderServices storeServices) throws InitializeException {
119
                if( params == null ) {
120
                        throw new InitializeException( new NullPointerException("params is null") );
121
                }
122
                File theFile = getDBFParameters().getDBFFile();
123
                if( theFile == null ) {
124
                        throw new InitializeException( new NullPointerException("dbf file is null") );
125
                }
126
                initResource(params, storeServices);
127

    
128
                Charset charset = params.getEncoding();
129
                this.dbfFile = new DbaseFile(theFile, charset);
130

    
131
                writer = new DBFFeatureWriter(this.getProviderName());
132

    
133
                this.initFeatureType();
134

    
135
        }
136

    
137
        public Object getDynValue(String name) throws DynFieldNotFoundException {
138
                try {
139
                        this.open();
140
                } catch (OpenException e) {
141
                        throw new RuntimeException(e);
142
                }
143
                if( METADATA_ENCODING.equalsIgnoreCase(name) ) {
144
                        return this.dbfFile.getOriginalCharset();
145
                } else if( METADATA_CODEPAGE.equalsIgnoreCase(name) ) {
146
                        return new Byte(this.dbfFile.getCodePage());
147
                }
148
                return super.getDynValue(name);
149
        }
150

    
151
        protected void initResource(DBFStoreParameters params,
152
                        DataStoreProviderServices storeServices) throws InitializeException {
153

    
154
                File theFile = getDBFParameters().getDBFFile();
155
                dbfResource =
156
                                this.createResource(FileResource.NAME,
157
                                                new Object[] { theFile.getAbsolutePath() });
158
                dbfResource.addConsumer(this);
159
        }
160

    
161
        public String getProviderName() {
162
                return NAME;
163
        }
164

    
165
        protected DBFStoreParameters getDBFParameters() {
166
                return (DBFStoreParameters) super.getParameters();
167
        }
168

    
169

    
170
        public DataServerExplorer getExplorer() throws ReadException {
171
                DataManager manager = DALLocator.getDataManager();
172
                FilesystemServerExplorerParameters params;
173
                try {
174
                        params = (FilesystemServerExplorerParameters) manager
175
                                        .createServerExplorerParameters(FilesystemServerExplorer.NAME);
176
                        params.setRoot(this.getDBFParameters().getDBFFile().getParent());
177
                        return manager.openServerExplorer(FilesystemServerExplorer.NAME,params);
178
                } catch (DataException e) {
179
                        throw new ReadException(this.getProviderName(), e);
180
                } catch (ValidateDataParametersException e) {
181
                        // TODO Auto-generated catch block
182
                        throw new ReadException(this.getProviderName(), e);
183
                }
184
        }
185

    
186
        protected FeatureProvider internalGetFeatureProviderByReference(
187
                        FeatureReferenceProviderServices reference, FeatureType featureType)
188
                        throws DataException {
189
                return this.getFeatureProviderByIndex(
190
                                ((Long) reference.getOID()).longValue(), featureType);
191
        }
192

    
193
        public void performChanges(Iterator deleteds, Iterator inserteds,
194
                        Iterator updateds, Iterator originalFeatureTypesUpdated)
195
                        throws PerformEditingException {
196

    
197
                try {
198
                        final FeatureStore store =
199
                                        this.getStoreServices().getFeatureStore();
200
                        getResource().execute(new ResourceAction() {
201

    
202
                                public Object run() throws Exception {
203
                                        FeatureSet set = null;
204
                                        DisposableIterator iter = null;
205
                                        try {
206
                                                set = store.getFeatureSet();
207
                                                DBFStoreParameters tmpParams =
208
                                                                (DBFStoreParameters) getDBFParameters().getCopy();
209

    
210
                                                tmpParams.setDBFFile(tmpParams.getDBFFileName()
211
                                                                + ".tmp");
212

    
213
                                                writer.begin(tmpParams, store.getDefaultFeatureType(),
214
                                                                set.getSize());
215

    
216
                                                iter = set.fastIterator();
217
                                                while (iter.hasNext()) {
218
                                                        Feature feature = (Feature) iter.next();
219
                                                        writer.append(feature);
220
                                                }
221

    
222
                                                writer.end();
223

    
224
                                                try {
225
                                                        close();
226
                                                } catch (CloseException e1) {
227
                                                        throw new PerformEditingException(getProviderName(), e1);
228
                                                }
229
                                                getDBFParameters().getDBFFile().delete();
230
                                                tmpParams.getDBFFile().renameTo(
231
                                                                getDBFParameters().getDBFFile());
232

    
233
                                                resourcesNotifyChanges();
234
                                                initFeatureType();
235
                                        } finally {
236
                                                if (set != null) {
237
                                                        set.dispose();
238
                                                }
239
                                                if (iter != null) {
240
                                                        iter.dispose();
241
                                                }
242
                                        }
243
                                        return null;
244
                                }
245
                        });
246
                } catch (ResourceExecuteException e) {
247
                        throw new PerformEditingException(this.getProviderName(), e);
248
                }
249

    
250
                this.counterNewsOIDs = -1;
251
        }
252

    
253
        /*
254
         * ==================================================
255
         */
256

    
257
        public FeatureProvider createFeatureProvider(FeatureType type) throws DataException {
258
                return new DBFFeatureProvider(this, type);
259
        }
260

    
261

    
262
        /*
263
         * ===================================================
264
         */
265

    
266

    
267

    
268
        protected void initFeatureType() throws InitializeException {
269
                try {
270
                        FeatureType defaultType =
271
                                        this.getTheFeatureType().getNotEditableCopy();
272
                        List types = new ArrayList(1);
273
                        types.add(defaultType);
274
                        this.getStoreServices().setFeatureTypes(types, defaultType);
275
                } catch (OpenException e) {
276
                        throw new InitializeException(getResource().toString(), e);
277
                }
278
        }
279

    
280
        protected EditableFeatureType getTheFeatureType()
281
                        throws InitializeException, OpenException {
282
                try {
283
                        this.open();
284
                } catch (DataException e) {
285
                        throw new InitializeException(this.getProviderName(), e);
286
                }
287
                return (EditableFeatureType) getResource().execute(
288
                                new ResourceAction() {
289

    
290
                                        public Object run() throws Exception {
291
                                                int fieldCount = -1;
292
                                                fieldCount = dbfFile.getFieldCount();
293

    
294
                                                EditableFeatureType fType =
295
                                                                getStoreServices().createFeatureType();
296

    
297
                                                fType.setHasOID(true);
298
                                                int precision;
299
                                                for (int i = 0; i < fieldCount; i++) {
300
                                                        char fieldType = dbfFile.getFieldType(i);
301
                                                        EditableFeatureAttributeDescriptor attr;
302

    
303
                                                        if (fieldType == 'L') {
304
                                                                attr =
305
                                                                                fType.add(dbfFile.getFieldName(i),
306
                                                                                                DataTypes.BOOLEAN);
307
                                                                attr.setDefaultValue(new Boolean(false));
308
                                                                attr.setAllowNull(false);
309

    
310
                                                        } else if ((fieldType == 'F') || (fieldType == 'N')) {
311
                                                                precision = dbfFile.getFieldDecimalLength(i);
312
                                                                if (precision > 0) {
313
                                                                        attr =
314
                                                                                        fType.add(dbfFile.getFieldName(i),
315
                                                                                                        DataTypes.DOUBLE,
316
                                                                                                        dbfFile.getFieldLength(i));
317
                                                                        attr.setPrecision(precision);
318
                                                                        attr.setDefaultValue(new Double(0));
319

    
320
                                                                } else {
321
                                                                    int length = dbfFile.getFieldLength(i);
322
                                                                    int type = DataTypes.INT;
323
                                                                    if (length > 9){
324
                                                                        type = DataTypes.LONG;
325
                                                                    }
326
                                                                        attr =
327
                                                                                        fType.add(dbfFile.getFieldName(i),
328
                                                                                                type,
329
                                                                                                        length);
330
                                                                        attr.setDefaultValue(new Integer(0));
331
                                                                }
332
                                                                attr.setAllowNull(false);
333

    
334
                                                        } else if (fieldType == 'C') {
335
                                                                attr =
336
                                                                                fType.add(dbfFile.getFieldName(i),
337
                                                                                                DataTypes.STRING);
338
                                                                attr.setSize(dbfFile.getFieldLength(i));
339
                                                                attr.setDefaultValue("");
340
                                                                
341
                                                                
342
                                                                attr.setAllowNull(false);
343

    
344
                                                        } else if (fieldType == 'D') {
345
                                                                attr =
346
                                                                                fType.add(dbfFile.getFieldName(i),
347
                                                                                                DataTypes.DATE);
348
                                                                /*
349
                                                                 * def value 1-1-1970
350
                                                                 */
351
                                                                attr.setDefaultValue(new Date(0));
352
                                                                attr.setAllowNull(false);
353
                                                        } else {
354
                                                                throw new InitializeException(getProviderName(),
355
                                                                                new UnknownDataTypeException(
356
                                                                                                dbfFile.getFieldName(i), ""
357
                                                                                                                + fieldType, getProviderName()));
358
                                                        }
359
                                                }
360
                                                return fType;
361
                                        }
362
                                });
363
        }
364

    
365

    
366
        protected void loadValue(FeatureProvider featureProvider, int rowIndex,
367
                        FeatureAttributeDescriptor descriptor) throws ReadException {
368
            
369
                if (descriptor.getEvaluator() != null) {
370
                        // Nothing to do
371
                        return;
372
                }
373

    
374
                int dbfIndex = this.dbfFile.getFieldIndex(descriptor.getName());
375
                
376
                if (dbfIndex < 0) {
377
                    // Someone asked to load a field
378
                    // which does not exist in the DBF file. This can happen 
379
                    // in editing process when a field has been added
380
                    // in the current editing session, so we simply do nothing.
381
                    // The expansion manager is expected to manage those new fields
382
                    // and their values.
383
                    long curr_time = System.currentTimeMillis();
384
                    // This ensures not more than one log every 2 seconds
385
                    if (curr_time - lastLogTime > 2000) {
386
                        LOG.info("Warning: The requested field does not exist in the DBF file. Assumed it's a new field in editing mode.");
387
                        lastLogTime = curr_time;
388
                    }
389
                    return;
390
                }
391
                
392
                String value = null;
393
                try {
394
                        value = this.dbfFile.getStringFieldValue(rowIndex, dbfIndex);
395
                } catch (DataException e) {
396
                        throw new ReadException(this.getProviderName(), e);
397
                }
398
                value = value.trim();
399
                int fieldType = descriptor.getType();
400
                switch (fieldType) {
401
                case DataTypes.STRING:
402
                        featureProvider.set(descriptor.getIndex(), value);
403
                        break;
404

    
405
                case DataTypes.DOUBLE:
406
                        try {
407
                                featureProvider.set(descriptor.getIndex(), new Double(value));
408
                        } catch (NumberFormatException e) {
409
                                featureProvider.set(descriptor.getIndex(), null);
410
                        }
411
                        break;
412

    
413
                case DataTypes.INT:
414
                        try {
415
                                featureProvider.set(descriptor.getIndex(), new Integer(value));
416
                        } catch (NumberFormatException e) {
417
                                featureProvider.set(descriptor.getIndex(), null);
418
                        }
419
                        break;
420

    
421
                case DataTypes.FLOAT:
422
                        try {
423
                                featureProvider.set(descriptor.getIndex(), new Float(value));
424
                        } catch (NumberFormatException e) {
425
                                featureProvider.set(descriptor.getIndex(), null);
426
                        }
427
                        break;
428

    
429
                case DataTypes.LONG:
430
                        try {
431
                                featureProvider.set(descriptor.getIndex(), new Long(value));
432
                        } catch (NumberFormatException e) {
433
                                featureProvider.set(descriptor.getIndex(), null);
434
                        }
435
                        break;
436

    
437
                case DataTypes.BOOLEAN:
438
                        if (value.equalsIgnoreCase("T")){
439
                                featureProvider.set(descriptor.getIndex(), Boolean.TRUE);
440
                        } else {
441
                                featureProvider.set(descriptor.getIndex(), Boolean.FALSE);
442
                                
443
                        }
444
                        break;
445

    
446
                case DataTypes.BYTE:
447
                        try {
448
                                featureProvider.set(descriptor.getIndex(), new Byte(value));
449
                        } catch (NumberFormatException e) {
450
                                featureProvider.set(descriptor.getIndex(), null);
451
                        }
452
                        break;
453

    
454
                case DataTypes.DATE:
455
                        if (value.equals("")){
456
                                value=null;
457
                                return;
458
                        }
459
                        String year = value.substring(0, 4);
460
                        String month = value.substring(4, 6);
461
                        String day = value.substring(6, 8);
462
                        DateFormat df;
463
                        if (descriptor.getDateFormat() == null){
464
                                df = DateFormat.getDateInstance(DateFormat.SHORT,
465
                                                ukLocale);
466
                        } else{
467
                                df = descriptor.getDateFormat();
468
                        }
469
                        /*
470
                         * Calendar c = Calendar.getInstance(); c.clear();
471
                         * c.set(Integer.parseInt(year), Integer.parseInt(month),
472
                         * Integer.parseInt(day)); c.set(Calendar.MILLISECOND, 0);
473
                         */
474
                        String strAux = month + "/" + day + "/" + year;
475
                        Date dat = null;
476
                        try {
477
                                dat = df.parse(strAux);
478
                        } catch (ParseException e) {
479
                                throw new ReadException(this.getProviderName(), e);
480
                        }
481
                        featureProvider.set(descriptor.getIndex(), dat);
482
                        break;
483

    
484

    
485
                default:
486
                        featureProvider
487
                                        .set(descriptor.getIndex(), descriptor.getDefaultValue());
488
                        break;
489
                }
490
        }
491

    
492

    
493
        /***
494
         * NOT supported in Alter Mode
495
         *
496
         * @param index
497
         * @return
498
         * @throws ReadException
499
         */
500
        protected FeatureProvider getFeatureProviderByIndex(long index) throws DataException {
501
                return this
502
                                .getFeatureProviderByIndex(index, this.getStoreServices()
503
                                .getDefaultFeatureType());
504
        }
505

    
506
        public long getFeatureCount() throws ReadException, OpenException,
507
                        ResourceNotifyChangesException {
508
                this.open();
509
                return ((Long) getResource().execute(new ResourceAction() {
510
                        public Object run() throws Exception {
511
                                return Long.valueOf(dbfFile.getRecordCount());
512
                        }
513
                })).longValue();
514
        }
515

    
516
        public FeatureSetProvider createSet(FeatureQuery query, FeatureType featureType)
517
                        throws DataException {
518
                return new DBFSetProvider(this, query, featureType);
519
        }
520

    
521
        public boolean canCreate() {
522
                return true;
523
        }
524

    
525
        public boolean canWriteGeometry(int geometryType, int geometrySubType) throws DataException {
526
                return false;
527
        }
528

    
529
        public void open() throws OpenException {
530
                if (this.dbfFile.isOpen()) {
531
                        return;
532
                }
533
                try {
534
                        getResource().execute(new ResourceAction() {
535
                                public Object run() throws Exception {
536
                                        openFile();
537
                                        resourcesOpen();
538
                                        return null;
539
                                }
540
                        });
541

    
542
                } catch (ResourceExecuteException e) {
543
                        throw new OpenException(this.getProviderName(), e);
544
                }
545
        }
546

    
547
        protected void openFile() throws FileNotFoundException,
548
                        UnsupportedVersionException, IOException, DataException {
549
                this.dbfFile.open();
550
        }
551

    
552
        public void close() throws CloseException {
553
                if (dbfFile == null || !this.dbfFile.isOpen()) {
554
                        return;
555
                }
556
                super.close();
557
                //Cerrar recurso
558
                try {
559
                        getResource().execute(new ResourceAction() {
560
                                public Object run() throws Exception {
561
                                        closeFile();
562
                                        resourcesNotifyClose();
563
                                        return null;
564
                                }
565
                        });
566
                } catch (ResourceExecuteException e) {
567
                        throw new CloseException(this.getProviderName(), e);
568
                }
569
        }
570

    
571
        protected void closeFile() throws CloseException {
572
                this.dbfFile.close();
573
        }
574

    
575
        @Override
576
        protected void doDispose() throws BaseException {
577
                this.close();
578
                dbfFile = null;
579
                disposeResource();
580
                super.doDispose();
581
        }
582

    
583
        protected void disposeResource() {
584
                this.dbfResource.removeConsumer(this);
585
                dbfResource = null;
586
        }
587

    
588
        public boolean closeResourceRequested(ResourceProvider resource) {
589
                try {
590
                        this.close();
591
                } catch (CloseException e) {
592
                        return false;
593
                }
594
                return true;
595
        }
596

    
597
        public boolean allowWrite() {
598
                File file;
599
                try {
600
                        file = ((File) this.dbfResource.get());
601
                } catch (AccessResourceException e) {
602
                        return false;
603
                }
604
                return file.canWrite();
605
        }
606

    
607
        public void refresh() throws OpenException {
608
                try {
609
                        this.close();
610
                } catch (CloseException e) {
611
                        throw new OpenException(this.getProviderName(), e);
612
                }
613
                this.open();
614
                try {
615
                        this.initFeatureType();
616
                } catch (InitializeException e) {
617
                        throw new OpenException(this.getProviderName(), e);
618
                }
619
        }
620

    
621
        /**
622
         *
623
         * @param index
624
         * @param featureType
625
         * @return
626
         * @throws ReadException
627
         */
628
        protected FeatureProvider getFeatureProviderByIndex(long index,
629
                        FeatureType featureType) throws DataException {
630
                FeatureProvider featureProvider = this.createFeatureProvider(featureType);
631
                featureProvider.setOID(new Long(index));
632
                return featureProvider;
633
        }
634

    
635
        protected void initFeatureProviderByIndex(FeatureProvider featureProvider,
636
                        long index, FeatureType featureType) throws DataException {
637
                featureProvider.setOID(new Long(index));
638
        }
639

    
640
        /**
641
         *
642
         * @param featureProvider
643
         * @throws DataException
644
         */
645
        protected void loadFeatureProviderByIndex(FeatureProvider featureProvider)
646
                        throws DataException {
647
            
648
                long index = ((Long) featureProvider.getOID()).longValue();
649
        int rec_count = this.dbfFile.getRecordCount();
650
        
651
        if (index >= rec_count) {
652

    
653
            ReadException rex = new ReadException(NAME,
654
                new ArrayIndexOutOfBoundsException(
655
                "Index of requested feature (" +
656
                index + ") is >= record count (" + rec_count + ")"));
657
            
658
            LOG.info("Error while loading feature. ", rex);
659
            throw rex;
660
        }                
661
                
662
                Iterator iterator = featureProvider.getType().iterator();
663
                while (iterator.hasNext()) {
664
                        FeatureAttributeDescriptor descriptor =
665
                                        (FeatureAttributeDescriptor) iterator.next();
666
                        this.loadValue(featureProvider, (int) index, descriptor);
667
                }
668
        }
669

    
670
        public int getOIDType() {
671
                return DataTypes.LONG;
672
        }
673

    
674
        public Object createNewOID() {
675
                if (this.counterNewsOIDs < 0) {
676
                        try {
677
                                this.counterNewsOIDs = this.getFeatureCount();
678
                        } catch (DataException e) {
679
                                e.printStackTrace();
680
                        }
681

    
682
                }else{
683
                        this.counterNewsOIDs++;
684
                }
685
                return new Long(counterNewsOIDs);
686
        }
687

    
688
        public boolean supportsAppendMode() {
689
                return true;
690
        }
691

    
692

    
693
        public void append(final FeatureProvider featureProvider)
694
                        throws DataException {
695
                getResource().execute(new ResourceAction() {
696
                        public Object run() throws Exception {
697
                                writer.append(getStoreServices().createFeature(featureProvider));
698
                                return null;
699
                        }
700
                });
701
        }
702

    
703
        public void beginAppend() throws DataException {
704
                this.close();
705
                getResource().execute(new ResourceAction() {
706
                        public Object run() throws Exception {
707
                                writer.begin(getDBFParameters(),
708
                                                getStoreServices().getDefaultFeatureType(),
709
                                                getStoreServices().getFeatureStore().getFeatureCount());
710
                                return null;
711
                        }
712
                });
713
        }
714

    
715
        public void endAppend() throws DataException {
716
                getResource().execute(new ResourceAction() {
717
                        public Object run() throws Exception {
718
                                writer.end();
719
                            resourcesNotifyChanges();
720
                                counterNewsOIDs = -1;
721
                                return null;
722
                        }
723
                });
724
        }
725

    
726
        /*
727
         * (non-Javadoc)
728
         *
729
         * @see
730
         * org.gvsig.fmap.dal.resource.spi.ResourceConsumer#resourceChanged(org.
731
         * gvsig.fmap.dal.resource.spi.ResourceProvider)
732
         */
733
        public void resourceChanged(ResourceProvider resource) {
734
                this.getStoreServices().notifyChange(
735
                                DataStoreNotification.RESOURCE_CHANGED,
736
                                resource);
737
        }
738

    
739
        /**
740
         *
741
         * @throws ResourceNotifyChangesException
742
         */
743
        protected void resourcesNotifyChanges()
744
                        throws ResourceNotifyChangesException {
745
                this.dbfResource.notifyChanges();
746
        }
747

    
748
        /**
749
         * @throws ResourceNotifyCloseException
750
         *
751
         */
752
        protected void resourcesNotifyClose() throws ResourceNotifyCloseException {
753
                this.dbfResource.notifyClose();
754
        }
755

    
756
        /**
757
         * @throws ResourceNotifyOpenException
758
         *
759
         */
760
        protected void resourcesOpen() throws ResourceNotifyOpenException {
761
                this.dbfResource.notifyOpen();
762
        }
763

    
764
        public Object getSourceId() {
765
                return this.getDBFParameters().getFile();
766
        }
767
        
768
        public String getName() {
769
                String name = this.getDBFParameters().getFile().getName();
770
                int n = name.lastIndexOf(".");
771
                if( n<1 ) {
772
                        return name;
773
                }
774
                return name.substring(0, n);
775
        }
776
        
777
        public String getFullName() {
778
                return this.getDBFParameters().getFile().getAbsolutePath();
779
        }
780

    
781
        protected void resourceCloseRequest() throws ResourceException {
782
                this.dbfResource.closeRequest();
783
        }
784

    
785
        public ResourceProvider getResource() {
786
                return dbfResource;
787
        }
788
}