Statistics
| Revision:

root / trunk / libraries / libFMap_dataFile / src / org / gvsig / data / datastores / vectorial / file / dbf / DBFStore.java @ 20660

History | View | Annotate | Download (9.73 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.DataManager;
11
import org.gvsig.data.IDataCollection;
12
import org.gvsig.data.IDataExplorer;
13
import org.gvsig.data.IDataExplorerParameters;
14
import org.gvsig.data.IDataStoreParameters;
15
import org.gvsig.data.ResourceManager;
16
import org.gvsig.data.datastores.vectorial.IFeaturesWriter;
17
import org.gvsig.data.datastores.vectorial.file.DataExplorerFileParameters;
18
import org.gvsig.data.exception.CloseException;
19
import org.gvsig.data.exception.DataException;
20
import org.gvsig.data.exception.InitializeException;
21
import org.gvsig.data.exception.OpenException;
22
import org.gvsig.data.exception.ReadException;
23
import org.gvsig.data.exception.WriteException;
24
import org.gvsig.data.spatialprovisional.IExtent;
25
import org.gvsig.data.vectorial.AttributeDescriptor;
26
import org.gvsig.data.vectorial.FeatureStore;
27
import org.gvsig.data.vectorial.FeatureStoreNotification;
28
import org.gvsig.data.vectorial.FeatureType;
29
import org.gvsig.data.vectorial.IFeature;
30
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor;
31
import org.gvsig.data.vectorial.IFeatureCollection;
32
import org.gvsig.data.vectorial.IFeatureID;
33
import org.gvsig.data.vectorial.IFeatureType;
34
import org.gvsig.data.vectorial.IsNotAttributeSettingException;
35
import org.gvsig.exceptions.BaseException;
36
import org.gvsig.metadata.IMetadata;
37
import org.gvsig.metadata.IMetadataManager;
38
import org.gvsig.metadata.MetadataManager;
39

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

    
48

    
49
         public void init(IDataStoreParameters parameters) throws InitializeException {
50
                 dbfParameters=(DBFStoreParameters)parameters;
51
                 DBFResource tmpResource = new DBFResource(dbfParameters.getDBFFile());
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
                        featureType=new FeatureType();
77
                            for (int i = 0; i < fieldCount; i++) {
78
                                    IFeatureAttributeDescriptor fad;
79
                                        try {
80
                                                fad = createFeatureAttribute(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
                        featureType.add(fad);
88
                    }
89

    
90
         }
91

    
92

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

    
102
                } else if ((fieldType == 'F') || (fieldType == 'N')) {
103
                        int precision = dbf.getFieldDecimalLength(i);
104
                        if (precision > 0){
105
                                dad.setType(IFeatureAttributeDescriptor.TYPE_DOUBLE);
106
                                dad.setPrecision(precision);
107
                        } else{
108
                                dad.setType(IFeatureAttributeDescriptor.TYPE_INT);
109
                        }
110
                } else if (fieldType == 'C') {
111
                        dad.setType(IFeatureAttributeDescriptor.TYPE_STRING);
112
                } else if (fieldType == 'D') {
113
                        dad.setType(IFeatureAttributeDescriptor.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
                IFeaturesWriter writer = getFeaturesWriter();
124
        writer.init(this);
125
        writer.updateFeatureType(getDefaultFeatureType());
126
        writer.preProcess();
127
        Collection collection=getDataCollection();
128
        Iterator iterator=collection.iterator();
129
        IFeature feature;
130
        while (iterator.hasNext()) {
131
                feature= (IFeature) iterator.next();
132
                        writer.insertFeature(feature);
133
                }
134
        writer.postProcess();
135

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

    
139
        public IDataCollection getDataCollection(IFeatureType type, String filter, String order) throws ReadException {
140
                if (type==null){
141
                        type=getDefaultFeatureType();
142
                }
143
                IFeatureCollection coll;
144
                if (order == null){
145
                        if (featureManager==null){
146
                                coll=new DBFFeatureCollectionBitSet(this,type,filter);
147
                        }else{
148
                                coll=new DBFFeatureCollection(featureManager,this,type,filter);
149
                        }
150
                }else{
151
                        coll=new DBFFeatureCollectionWithFeatureID(featureManager,this,type,filter,order);
152
                }
153

    
154
                this.addObserver(new WeakReference(coll));
155
                return coll;
156

    
157
        }
158

    
159
        public IFeature getFeatureByID(IFeatureID id) throws ReadException {
160
                if (this.alterMode){
161
                    if (featureManager.contains(id)) {
162
                            return featureManager.getFeature(id,this);
163
                    }
164
            }
165
                DBFFeature feature=new DBFFeature(getDefaultFeatureType(),this,((DBFFeatureID)id).getIndex());
166
                return feature;
167
//                return getFeatureByPosition(featureType,((DBFFeatureID)id).getIndex());
168
        }
169

    
170
        public List getFeatureTypes() {
171
                featureTypes.set(0,getDefaultFeatureType());
172
        return featureTypes;
173
        }
174

    
175
        public IFeatureType getDefaultFeatureType() {
176
                if (isEditing()){
177
//                    Aqu? hay que construir un FeatureType con los cambios que se hayan hecho en la edici?n.
178
                    return attributeManager.getFeatureType();
179
            }
180
        return featureType;
181
        }
182

    
183
        public boolean isWithDefaultLegend() {
184
                return false;
185
        }
186

    
187
        public Object getDefaultLegend() {
188
                return null;
189
        }
190

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

    
195
        public boolean canAlterFeatureType() {
196
                return true;
197
        }
198

    
199
        public String getName() {
200
                return DATASTORE_NAME;
201
        }
202

    
203
        protected void doOpen() throws OpenException {
204
                //No hace nada
205

    
206
        }
207

    
208
        protected void doClose() throws CloseException {
209
                try {
210
                        dbf.close();
211
                } catch (DataException e) {
212
                        throw new CloseException(this.getName(),e);
213
                }
214

    
215
        }
216

    
217
        protected void doDispose() throws CloseException {
218
                ResourceManager resMan = ResourceManager.getResourceManager();
219

    
220
            try {
221
                        resMan.remove(this.dbf);
222
                } catch (DataException e1) {
223
                        throw new CloseException(this.getName(),e1);
224
                } catch (KeyException e) {
225
                        // TODO Auto-generated catch block
226
                        throw new CloseException(this.getName(),e);
227
                }
228

    
229
                this.dbf = null;
230
                this.featureType=null;
231
                this.metadata=null;
232

    
233
        }
234

    
235
        public boolean isEditable() {
236
                if (dbf.getFile().canWrite()) return true;
237
                return false;
238
        }
239

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

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

    
274
        protected IFeaturesWriter getFeaturesWriter() {
275
                IFeaturesWriter writer = new DBFFeaturesWriter();
276
//                writer.init(this);
277
                return writer;
278
        }
279

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

    
284

    
285
        public IDataStoreParameters getParameters() {
286
                return parameters;
287
        }
288

    
289

    
290
        public IDataExplorer getExplorer() {
291
                DataManager dsm=DataManager.getManager();
292
                IDataExplorerParameters dsp = dsm.createDataExplorerParameters(
293
                                DBFDataExplorer.DATASOURCE_NAME);
294
                ((DataExplorerFileParameters)dsp).setSource(dbfParameters.getFile().getParentFile());
295

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

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

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