Statistics
| Revision:

root / trunk / libraries / libFMap_dataFile / src / org / gvsig / data / datastores / vectorial / file / dxf / DXFStore.java @ 20744

History | View | Annotate | Download (12.4 KB)

1
package org.gvsig.data.datastores.vectorial.file.dxf;
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.Comparator;
8
import java.util.Iterator;
9
import java.util.List;
10
import java.util.TreeSet;
11

    
12
import org.gvsig.data.DataManager;
13
import org.gvsig.data.IDataCollection;
14
import org.gvsig.data.IDataExplorer;
15
import org.gvsig.data.IDataExplorerParameters;
16
import org.gvsig.data.IDataStoreParameters;
17
import org.gvsig.data.ResourceManager;
18
import org.gvsig.data.datastores.vectorial.IFeaturesWriter;
19
import org.gvsig.data.datastores.vectorial.file.FileExplorerParameters;
20
import org.gvsig.data.exception.CloseException;
21
import org.gvsig.data.exception.DataException;
22
import org.gvsig.data.exception.InitializeException;
23
import org.gvsig.data.exception.OpenException;
24
import org.gvsig.data.exception.ReadException;
25
import org.gvsig.data.exception.WriteException;
26
import org.gvsig.data.spatialprovisional.IExtent;
27
import org.gvsig.data.vectorial.AttributeDescriptor;
28
import org.gvsig.data.vectorial.FeatureStore;
29
import org.gvsig.data.vectorial.FeatureType;
30
import org.gvsig.data.vectorial.IFeature;
31
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor;
32
import org.gvsig.data.vectorial.IFeatureCollection;
33
import org.gvsig.data.vectorial.IFeatureID;
34
import org.gvsig.data.vectorial.IFeatureType;
35
import org.gvsig.data.vectorial.IsNotAttributeSettingException;
36
import org.gvsig.data.vectorial.IsNotFeatureSettingException;
37
import org.gvsig.data.vectorial.MemoryFeature;
38
import org.gvsig.data.vectorial.expressionevaluator.FeatureComparator;
39
import org.gvsig.data.vectorial.expressionevaluator.FeatureFilter;
40
import org.gvsig.exceptions.BaseException;
41
import org.gvsig.metadata.IMetadata;
42
import org.gvsig.metadata.IMetadataManager;
43
import org.gvsig.metadata.MetadataManager;
44

    
45
public class DXFStore extends FeatureStore{
46
        public static String DATASTORE_NAME = "DXFStore";
47

    
48
        private IFeatureType featureType;
49
        private List featureTypes = new ArrayList();
50
        protected IMetadata metadata;
51

    
52

    
53
        private DXFStoreParameters dxfParameters;
54

    
55
        private DXFResource dxf;
56

    
57
        public void init(IDataStoreParameters parameters) throws InitializeException {
58

    
59
                dxfParameters=(DXFStoreParameters)parameters;
60

    
61
                DXFResource tmpResource = new DXFResource(dxfParameters);
62

    
63
                ResourceManager resMan = ResourceManager.getResourceManager();
64

    
65
                try {
66
                        this.dxf = (DXFResource)resMan.addResource(tmpResource);
67
                } catch (DataException e1) {
68
                        throw new InitializeException(this.getName(),e1);
69
                }
70

    
71
                super.init(parameters,this.dxf);
72

    
73
                try {
74
                        this.featureType = this.dxf.getFeatureType();
75
                } catch (ReadException e) {
76
                        throw new InitializeException(this.getName(),e);
77
                }
78

    
79
        }
80

    
81
         static IFeatureType newFeatureType(){
82
                        FeatureType fType= new FeatureType();
83

    
84
                        try{
85
                        AttributeDescriptor descriptorID = new AttributeDescriptor();
86
                        descriptorID.loading();
87
                        descriptorID.setType(IFeatureAttributeDescriptor.TYPE_INT);
88
                        descriptorID.setName("ID");
89
                        descriptorID.setDefaultValue(new Integer(0));
90
                        descriptorID.stopLoading();
91
                        fType.add(descriptorID);
92

    
93
                        AttributeDescriptor descriptorShape = new AttributeDescriptor();
94
                        descriptorShape.loading();
95
                        descriptorShape.setType(IFeatureAttributeDescriptor.TYPE_GEOMETRY);
96
                        descriptorShape.setName("GEOMETRY");
97
                        descriptorShape.setDefaultValue(null);
98
                        descriptorShape.stopLoading();
99
                        fType.add(descriptorShape);
100
                        fType.setDefaultGeometry("GEOMETRY");
101

    
102
                        AttributeDescriptor descriptorEntity = new AttributeDescriptor();
103
                        descriptorEntity.loading();
104
                        descriptorEntity.setType(IFeatureAttributeDescriptor.TYPE_STRING);
105
                        descriptorEntity.setName("Entity");
106
                        descriptorEntity.setDefaultValue("Entity");
107
                        descriptorEntity.stopLoading();
108
                        fType.add(descriptorEntity);
109

    
110
                        AttributeDescriptor descriptorLayer = new AttributeDescriptor();
111
                        descriptorLayer.loading();
112
                        descriptorLayer.setType(IFeatureAttributeDescriptor.TYPE_STRING);
113
                        descriptorLayer.setName("Layer");
114
                        descriptorLayer.setDefaultValue("default");
115
                        descriptorLayer.stopLoading();
116
                        fType.add(descriptorLayer);
117

    
118
                        AttributeDescriptor descriptorColor = new AttributeDescriptor();
119
                        descriptorColor.loading();
120
                        descriptorColor.setType(IFeatureAttributeDescriptor.TYPE_INT);
121
                        descriptorColor.setName("Color");
122
                        descriptorColor.setDefaultValue(new Integer(0));
123
                        descriptorColor.stopLoading();
124
                        fType.add(descriptorColor);
125

    
126
                        AttributeDescriptor descriptorElevation = new AttributeDescriptor();
127
                        descriptorElevation.loading();
128
                        descriptorElevation.setType(IFeatureAttributeDescriptor.TYPE_DOUBLE);
129
                        descriptorElevation.setName("Elevation");
130
                        descriptorElevation.setDefaultValue(new Double(0));
131
                        descriptorElevation.stopLoading();
132
                        fType.add(descriptorElevation);
133

    
134
                        AttributeDescriptor descriptorThickness = new AttributeDescriptor();
135
                        descriptorThickness.loading();
136
                        descriptorThickness.setType(IFeatureAttributeDescriptor.TYPE_DOUBLE);
137
                        descriptorThickness.setName("Thickness");
138
                        descriptorThickness.setDefaultValue(new Double(0));
139
                        descriptorThickness.stopLoading();
140
                        fType.add(descriptorThickness);
141

    
142
                        AttributeDescriptor descriptorText = new AttributeDescriptor();
143
                        descriptorText.loading();
144
                        descriptorText.setType(IFeatureAttributeDescriptor.TYPE_STRING);
145
                        descriptorText.setName("Text");
146
                        descriptorText.setDefaultValue("");
147
                        descriptorText.stopLoading();
148
                        fType.add(descriptorText);
149

    
150
                        AttributeDescriptor descriptorHeightText = new AttributeDescriptor();
151
                        descriptorHeightText.loading();
152
                        descriptorHeightText.setType(IFeatureAttributeDescriptor.TYPE_FLOAT);
153
                        descriptorHeightText.setName("HeightText");
154
                        descriptorHeightText.setDefaultValue(new Float(10));
155
                        descriptorHeightText.stopLoading();
156
                        fType.add(descriptorHeightText);
157

    
158
                        AttributeDescriptor descriptorRotationText = new AttributeDescriptor();
159
                        descriptorRotationText.loading();
160
                        descriptorRotationText.setType(IFeatureAttributeDescriptor.TYPE_DOUBLE);
161
                        descriptorRotationText.setName("Rotation");
162
                        descriptorRotationText.setDefaultValue(new Double(10));
163
                        descriptorRotationText.stopLoading();
164
                        fType.add(descriptorRotationText);
165
                        }catch (IsNotAttributeSettingException e) {
166
                                e.printStackTrace();
167
                        }
168

    
169
                        return fType;
170

    
171
         }
172

    
173

    
174
        protected void doFinishEdition() throws WriteException, ReadException {
175
                IFeaturesWriter writer = getFeaturesWriter();
176
        writer.init(this);
177
        writer.updateFeatureType(featureType);
178
        this.dxf.editing();
179
        writer.preProcess();
180
        Collection collection=getDataCollection();
181
        Iterator iterator=collection.iterator();
182
        IFeature feature;
183
        while (iterator.hasNext()) {
184
                feature= (IFeature) iterator.next();
185
                        writer.insertFeature(feature);
186
                }
187
        writer.postProcess();
188
        this.dxf.stopEditing();
189
        this.dxf.changed(this);
190

    
191
        }
192

    
193
        public IDataCollection getDataCollection(IFeatureType type, String filter, String order) throws ReadException {
194
                if (type==null){
195
                        type=getDefaultFeatureType();
196
                }
197
                IFeatureCollection coll;
198

    
199
                //TODO aplicar filtro, orden e incluso y el featureType
200
                FeatureFilter parser = null;
201
                if (filter!=null){
202
                        parser = new FeatureFilter(filter,type);
203
                }
204
//                ArrayList originalFeatures=this.dxf.getFeatures();
205
                int num=0;
206
                if (featureManager!=null){
207
                        num=featureManager.getNum();
208
                }
209
                int originalSize=this.dxf.getFeatureCount();
210
                Collection allFeatures=null;
211
                if (order!=null){
212
                        Comparator comparator = new FeatureComparator(type,order);
213
                        allFeatures = new TreeSet(comparator);
214
                }else{
215
                        allFeatures=new ArrayList();
216
                }
217
                for (int i = 0; i < originalSize+num; i++) {
218
                        IFeature feature=null;
219
                        if (i<this.dxf.getFeatureCount()){
220
                                feature=this.dxf.getFeature(i);
221
                        }else{
222
                                feature=featureManager.getFeature(i-originalSize,this);
223
                        }
224
                        if (featureManager == null || !featureManager.isDeleted(feature)){
225
                                try {
226
                                                MemoryFeature auxfeature=new DXFFeature(type,false);
227
                                                auxfeature.loading();
228
//                                                auxfeature.setDefaultGeometry(feature.getDefaultGeometry());
229
                                                Iterator iterator=type.iterator();
230
                                                while (iterator.hasNext()) {
231
                                                        IFeatureAttributeDescriptor fad = (IFeatureAttributeDescriptor) iterator.next();
232
                                                        int index=((AttributeDescriptor)fad).originalPosition();
233

    
234
                                                        if (fad.getDataType().equals(IFeatureAttributeDescriptor.TYPE_GEOMETRY)){
235
                                                                auxfeature.setGeometry(index,feature.get(index));
236
                                                        } else {
237
                                                                auxfeature.set(index,feature.get(index));
238
                                                        }
239
                                                }
240
                                                if (filter==null || parser.match(auxfeature)){
241
                                                        allFeatures.add(auxfeature);
242
                                                }
243
                                                auxfeature.stopLoading();
244

    
245
                                } catch (IsNotFeatureSettingException e) {
246
                                        throw new ReadException(this.getName(),e);
247
                                }
248
                        }
249

    
250
                }
251

    
252
                coll=new DXFFeatureCollection(allFeatures);
253
                this.addObserver(new WeakReference(coll));
254
                return coll;
255

    
256
        }
257

    
258
        public IFeature getFeatureByID(IFeatureID id) throws ReadException {
259
                if (this.alterMode){
260
                    if (featureManager.contains(id)) {
261
                            return featureManager.getFeature(id,this);
262
                    }
263
            }
264
                return id.getFeature(featureType);
265
        }
266

    
267
        public List getFeatureTypes() {
268
                featureTypes.set(0,getDefaultFeatureType());
269
        return featureTypes;
270
        }
271

    
272
        public IFeatureType getDefaultFeatureType() {
273
                if (isEditing()){
274
//                    Aqu? hay que construir un FeatureType con los cambios que se hayan hecho en la edici?n.
275
                    return attributeManager.getFeatureType();
276
            }
277
        return featureType;
278
        }
279

    
280
        public boolean isWithDefaultLegend() {
281
                return false;
282
        }
283

    
284
        public Object getDefaultLegend() {
285
                return null;
286
        }
287

    
288
        public Object getDefaultLabelingStrategy() {
289
                return null;
290
        }
291

    
292
        public boolean canAlterFeatureType() {
293
                return false;
294
        }
295

    
296
        public String getName() {
297
                return DATASTORE_NAME;
298
        }
299

    
300
        protected void doOpen() throws OpenException {
301

    
302
        }
303

    
304
        protected void doClose() throws CloseException {
305
                this.dxf.close();
306
        }
307

    
308
        protected void doDispose() throws CloseException {
309
                super.doDispose();
310
                ResourceManager resMan = ResourceManager.getResourceManager();
311

    
312
            try {
313
                        resMan.remove(this.dxf);
314
                } catch (DataException e1) {
315
                        throw new CloseException(this.getName(),e1);
316
                } catch (KeyException e) {
317
                        throw new CloseException(this.getName(),e);
318
                }
319

    
320
                this.dxf = null;
321
                this.featureType=null;
322
                this.metadata=null;
323
        }
324

    
325
        public boolean isEditable() {
326
                return this.dxf.isEditable();
327
        }
328

    
329
        public IMetadata getMetadata() throws BaseException {
330
                if (metadata==null){
331
                        IMetadataManager manager=MetadataManager.getManager();
332
                        metadata=manager.create(DATASTORE_NAME);
333
                        IExtent extent=this.dxf.getFullExtent();
334
                        metadata.set("extent",extent);
335
                        String srs=getSRS();
336
                        metadata.set("srs",srs);
337
                }
338
                if (this.alterMode){
339
                        IExtent extent=(IExtent)metadata.get("extent");
340
                        IFeatureCollection featureCollection=(IFeatureCollection)getDataCollection();
341
                    if (spatialManager.isFullExtentDirty()){
342
                            if (!featureCollection.isEmpty()){
343
                                    Iterator featureIterator=featureCollection.iterator();
344
                                    extent = ((IFeature)featureIterator.next()).getExtent();
345
                                    while(featureIterator.hasNext()){
346
                                            IFeature feature=(IFeature)featureIterator.next();
347
                                            IExtent boundExtent=feature.getExtent();
348
                                            if (boundExtent!=null)
349
                                                    extent.add(boundExtent);
350
                                    }
351
                            }
352
                    }
353
                    metadata.set("extent",extent);
354
                }
355
                return metadata;
356
        }
357
        private String getSRS() {
358
                // TODO Auto-generated method stub
359
                return null;
360
        }
361

    
362

    
363
        protected IFeaturesWriter getFeaturesWriter() {
364
                IFeaturesWriter writer = new DXFFeaturesWriter();
365
//                writer.init(this);
366
                return writer;
367
        }
368
        public IDataStoreParameters getParameters() {
369
                return parameters;
370
        }
371

    
372
        public IDataExplorer getExplorer() {
373
                DataManager dsm=DataManager.getManager();
374
                IDataExplorerParameters dsp = dsm.createDataExplorerParameters(
375
                                DXFDataExplorer.DATASOURCE_NAME);
376
                ((FileExplorerParameters)dsp).setSource(dxfParameters.getFile().getParentFile());
377

    
378
                IDataExplorer src=null;
379
                try {
380
                        src = dsm.createDataExplorer(dsp);
381
                } catch (InitializeException e1) {
382
                        e1.printStackTrace();
383
                }
384
                return src;
385
        }
386

    
387
        /* (non-Javadoc)
388
         * @see org.gvsig.data.vectorial.FeatureStore#doRefresh()
389
         */
390
        protected void doRefresh() throws OpenException, InitializeException {
391
                // TODO Auto-generated method stub
392

    
393
        }
394
}