Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataFile / src / org / gvsig / fmap / data / feature / file / dxf / DXFStore.java @ 23509

History | View | Annotate | Download (12.5 KB)

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

    
43
public class DXFStore extends FileStore {
44
        public static String DATASTORE_NAME = "DXFStore";
45
        private List featureTypes = new ArrayList();
46
        protected IMetadata metadata;
47

    
48

    
49
        private DXFStoreParameters dxfParameters;
50

    
51
        private DXFResource dxf;
52

    
53
        public void init(DataStoreParameters parameters) throws InitializeException {
54

    
55
                dxfParameters=(DXFStoreParameters)parameters;
56

    
57
                DXFResource tmpResource = new DXFResource(dxfParameters);
58

    
59
                ResourceManager resMan = ResourceManager.getResourceManager();
60

    
61
                try {
62
                        this.dxf = (DXFResource) resMan.addResource(tmpResource, this);
63
                        //                        this.dxf.load();
64
                } catch (DataException e1) {
65
                        throw new InitializeException(this.getName(),e1);
66
                }
67

    
68
                super.init(parameters,this.dxf);
69

    
70
                try {
71
                        this.defaultFeatureType = this.dxf.getFeatureType();
72
                } catch (ReadException e) {
73
                        throw new InitializeException(this.getName(),e);
74
                }
75

    
76
        }
77

    
78
         static FeatureType newFeatureType(){
79
                        DefaultFeatureType fType= new DefaultFeatureType();
80

    
81
                        try{
82
                        AttributeDescriptor descriptorID = (AttributeDescriptor) fType.createAttributeDescriptor();
83
                        descriptorID.loading();
84
                        descriptorID.setType(FeatureAttributeDescriptor.TYPE_INT);
85
                        descriptorID.setName("ID");
86
                        descriptorID.setDefaultValue(new Integer(0));
87
                        descriptorID.stopLoading();
88
                        fType.add(descriptorID);
89

    
90
                        AttributeDescriptor descriptorShape = (AttributeDescriptor) fType.createAttributeDescriptor();
91
                        descriptorShape.loading();
92
                        descriptorShape.setType(FeatureAttributeDescriptor.TYPE_GEOMETRY);
93
                        descriptorShape.setName("GEOMETRY");
94
                        descriptorShape.setDefaultValue(null);
95
                        descriptorShape.stopLoading();
96
                        fType.add(descriptorShape);
97
                        fType.setDefaultGeometry("GEOMETRY");
98

    
99
                        AttributeDescriptor descriptorEntity = (AttributeDescriptor) fType.createAttributeDescriptor();
100
                        descriptorEntity.loading();
101
                        descriptorEntity.setType(FeatureAttributeDescriptor.TYPE_STRING);
102
                        descriptorEntity.setName("Entity");
103
                        descriptorEntity.setDefaultValue("Entity");
104
                        descriptorEntity.stopLoading();
105
                        fType.add(descriptorEntity);
106

    
107
                        AttributeDescriptor descriptorLayer = (AttributeDescriptor) fType.createAttributeDescriptor();
108
                        descriptorLayer.loading();
109
                        descriptorLayer.setType(FeatureAttributeDescriptor.TYPE_STRING);
110
                        descriptorLayer.setName("Layer");
111
                        descriptorLayer.setDefaultValue("default");
112
                        descriptorLayer.stopLoading();
113
                        fType.add(descriptorLayer);
114

    
115
                        AttributeDescriptor descriptorColor = (AttributeDescriptor) fType.createAttributeDescriptor();
116
                        descriptorColor.loading();
117
                        descriptorColor.setType(FeatureAttributeDescriptor.TYPE_INT);
118
                        descriptorColor.setName("Color");
119
                        descriptorColor.setDefaultValue(new Integer(0));
120
                        descriptorColor.stopLoading();
121
                        fType.add(descriptorColor);
122

    
123
                        AttributeDescriptor descriptorElevation = (AttributeDescriptor) fType.createAttributeDescriptor();
124
                        descriptorElevation.loading();
125
                        descriptorElevation.setType(FeatureAttributeDescriptor.TYPE_DOUBLE);
126
                        descriptorElevation.setName("Elevation");
127
                        descriptorElevation.setDefaultValue(new Double(0));
128
                        descriptorElevation.stopLoading();
129
                        fType.add(descriptorElevation);
130

    
131
                        AttributeDescriptor descriptorThickness = (AttributeDescriptor) fType.createAttributeDescriptor();
132
                        descriptorThickness.loading();
133
                        descriptorThickness.setType(FeatureAttributeDescriptor.TYPE_DOUBLE);
134
                        descriptorThickness.setName("Thickness");
135
                        descriptorThickness.setDefaultValue(new Double(0));
136
                        descriptorThickness.stopLoading();
137
                        fType.add(descriptorThickness);
138

    
139
                        AttributeDescriptor descriptorText = (AttributeDescriptor) fType.createAttributeDescriptor();
140
                        descriptorText.loading();
141
                        descriptorText.setType(FeatureAttributeDescriptor.TYPE_STRING);
142
                        descriptorText.setName("Text");
143
                        descriptorText.setDefaultValue("");
144
                        descriptorText.stopLoading();
145
                        fType.add(descriptorText);
146

    
147
                        AttributeDescriptor descriptorHeightText = (AttributeDescriptor) fType.createAttributeDescriptor();
148
                        descriptorHeightText.loading();
149
                        descriptorHeightText.setType(FeatureAttributeDescriptor.TYPE_FLOAT);
150
                        descriptorHeightText.setName("HeightText");
151
                        descriptorHeightText.setDefaultValue(new Float(10));
152
                        descriptorHeightText.stopLoading();
153
                        fType.add(descriptorHeightText);
154

    
155
                        AttributeDescriptor descriptorRotationText = (AttributeDescriptor) fType.createAttributeDescriptor();
156
                        descriptorRotationText.loading();
157
                        descriptorRotationText.setType(FeatureAttributeDescriptor.TYPE_DOUBLE);
158
                        descriptorRotationText.setName("Rotation");
159
                        descriptorRotationText.setDefaultValue(new Double(10));
160
                        descriptorRotationText.stopLoading();
161
                        fType.add(descriptorRotationText);
162
                        fType.setDefaultGeometry("GEOMETRY");
163
                        fType.setGeometryTypes(new int[]{Geometry.TYPES.GEOMETRY});
164
                        }catch (IsNotAttributeSettingException e) {
165
                                e.printStackTrace();
166
                        }
167

    
168
                        return fType;
169

    
170
         }
171

    
172

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

    
190
        }
191

    
192
        public DataCollection getDataCollection(FeatureType type, String filter, String order) throws ReadException {
193
                try {
194
                        type = this.checkFeatureTypeForCollection(type);
195
                } catch (DataException e) {
196
                        throw new ReadException(this.getName(), e);
197
                }
198

    
199
                FeatureCollection coll;
200
                DataManager manager = DataManager.getManager();
201

    
202
                //TODO aplicar filtro, orden e incluso y el featureType
203
                Filter parser = null;
204
                if (filter!=null){
205
                        parser = manager.getExpressionParser().parseFilter(filter);
206
                }
207
//                ArrayList originalFeatures=this.dxf.getFeatures();
208
                int num=0;
209
                if (featureManager!=null){
210
                        num=featureManager.getNum();
211
                }
212
                int originalSize=this.dxf.getFeatureCount();
213
                Collection allFeatures=null;
214
                if (order!=null){
215
                        Comparator comparator = manager.getExpressionParser()
216
                                        .parseComparator(order);
217
                        allFeatures = new TreeSet(comparator);
218
                }else{
219
                        allFeatures=new ArrayList();
220
                }
221
                Feature auxFeautre;
222
                for (int i = 0; i < originalSize+num; i++) {
223
                        Feature feature=null;
224
                        if (i<this.dxf.getFeatureCount()){
225
                                feature=this.dxf.getFeature(i);
226
                        }else{
227
                                feature=featureManager.getFeature(i-originalSize,this,type);
228
                        }
229
                        if (featureManager == null || !featureManager.isDeleted(feature)){
230
                                if (filter == null || parser.evaluate(feature)) {
231
                                        allFeatures.add(feature.getID());
232
                                }
233
                        }
234

    
235
                }
236

    
237
                coll = new DXFFeatureCollection(this, type, allFeatures);
238
                this.addObserver(new WeakReference(coll));
239
                return coll;
240

    
241
        }
242

    
243
        public Feature getFeatureByID(FeatureID id,FeatureType featureType) throws ReadException {
244
                if (featureType==null){
245
                        featureType=getDefaultFeatureType();
246
                }else{
247
                        if (!featureType.isSubtypeOf(this.getDefaultFeatureType())){
248
                                throw new ReadException("invalid type",this.getName());
249
                        }
250
                }
251
                if (this.alterMode){
252
                    if (featureManager.contains(id)) {
253
                            return featureManager.getFeature(id,this,featureType);
254
                    }
255
            }
256
                return id.getFeature(featureType);
257
        }
258

    
259
        public List getFeatureTypes() {
260
                featureTypes.clear();
261
                featureTypes.add(getDefaultFeatureType());
262
                return featureTypes;
263
        }
264

    
265
        public boolean isWithDefaultLegend() {
266
                return false;
267
        }
268

    
269
        public Object getDefaultLegend() throws ReadException {
270
                return this.dxf.getDefaultLegend();
271
        }
272

    
273
        public Object getDefaultLabelingStrategy() throws ReadException {
274
                return this.dxf.getDefaultLabelingStrategy();
275
        }
276

    
277
        public boolean canAlterFeatureType() {
278
                return false;
279
        }
280

    
281
        public String getName() {
282
                return DATASTORE_NAME;
283
        }
284

    
285
        protected void doOpen() throws OpenException {
286

    
287
        }
288

    
289
        protected void doClose() throws CloseException {
290
                this.dxf.close();
291
        }
292

    
293
        protected void doDispose() throws CloseException {
294
                super.doDispose();
295
                ResourceManager resMan = ResourceManager.getResourceManager();
296

    
297
            try {
298
                        resMan.remove(this.dxf, this);
299
                } catch (DataException e1) {
300
                        throw new CloseException(this.getName(),e1);
301
                } catch (KeyException e) {
302
                        throw new CloseException(this.getName(),e);
303
                }
304

    
305
                this.dxf = null;
306
                this.defaultFeatureType=null;
307
                this.metadata=null;
308
        }
309

    
310
        public boolean isEditable() {
311
                return this.dxf.isEditable();
312
        }
313

    
314
        public IMetadata getMetadata() throws BaseException {
315
                if (metadata==null){
316
                        IMetadataManager manager=MetadataManager.getManager();
317
                        metadata=manager.create(DATASTORE_NAME);
318
                        Envelope extent=this.dxf.getFullExtent();
319
                        metadata.set("extent",extent);
320
                        String srs=getSRS();
321
                        metadata.set("srs",srs);
322
                        metadata.set("WithDefaultLegend", Boolean.TRUE);
323
                }
324
                if (this.alterMode){
325
                        Envelope extent=(Envelope)metadata.get("extent");
326
                        FeatureCollection featureCollection=(FeatureCollection)getDataCollection();
327
                    if (spatialManager.isFullExtentDirty()){
328
                            if (!featureCollection.isEmpty()){
329
                                    Iterator featureIterator=featureCollection.iterator();
330
                                    extent = ((Feature)featureIterator.next()).getExtent();
331
                                    while(featureIterator.hasNext()){
332
                                            Feature feature=(Feature)featureIterator.next();
333
                                            Envelope boundExtent=feature.getExtent();
334
                                            if (boundExtent!=null) {
335
                                                        extent.add(boundExtent);
336
                                                }
337
                                    }
338
                            }
339
                    }
340
                    metadata.set("extent",extent);
341
                }
342
                return metadata;
343
        }
344
        private String getSRS() {
345
                // TODO Auto-generated method stub
346
                return null;
347
        }
348

    
349

    
350
        protected FeaturesWriter getFeaturesWriter() {
351
                FeaturesWriter writer = new DXFFeaturesWriter();
352
//                writer.init(this);
353
                return writer;
354
        }
355
        public DataStoreParameters getParameters() {
356
                return parameters;
357
        }
358

    
359
        public DataExplorer getExplorer() throws InitializeException {
360
                DataManager dsm=DataManager.getManager();
361
                DataExplorerParameters dsp = dsm.createDataExplorerParameters(
362
                                DXFDataExplorer.DATASOURCE_NAME);
363
                ((FileExplorerParameters)dsp).setSource(dxfParameters.getFile().getParentFile());
364

    
365
                DataExplorer src=null;
366
                try {
367
                        src = dsm.createDataExplorer(dsp);
368
                } catch (InitializeException e1) {
369
                        e1.printStackTrace();
370
                }
371
                return src;
372
        }
373

    
374
        /* (non-Javadoc)
375
         * @see org.gvsig.fmap.data.feature.FeatureStore#doRefresh()
376
         */
377
        protected void doRefresh() throws OpenException, InitializeException {
378
                // TODO Auto-generated method stub
379

    
380
        }
381
        public boolean canWriteGeometry(int gvSIGgeometryType) {
382
                return getFeaturesWriter().canWriteGeometry(gvSIGgeometryType);
383
        }
384

    
385
}