Statistics
| Revision:

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

History | View | Annotate | Download (12.3 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.TreeSet;
10

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

    
42
public class DXFStore extends FileStore {
43
        public static String DATASTORE_NAME = "DXFStore";
44
        protected IMetadata metadata;
45

    
46

    
47
        private DXFStoreParameters dxfParameters;
48

    
49
        private DXFResource dxf;
50

    
51
        public void init(DataStoreParameters parameters) throws InitializeException {
52

    
53
                dxfParameters=(DXFStoreParameters)parameters;
54

    
55
                DXFResource tmpResource = new DXFResource(dxfParameters);
56

    
57
                ResourceManager resMan = ResourceManager.getResourceManager();
58

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

    
66
                super.init(parameters,this.dxf);
67

    
68
                try {
69
                        this.defaultFeatureType = this.dxf.getFeatureType();
70
                        this.featureTypes = new ArrayList();
71
                        this.featureTypes.add(defaultFeatureType);
72

    
73
                } catch (ReadException e) {
74
                        throw new InitializeException(this.getName(),e);
75
                }
76

    
77
        }
78

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

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

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

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

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

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

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

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

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

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

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

    
169
                        return fType;
170

    
171
         }
172

    
173

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

    
191
        }
192

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

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

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

    
236
                }
237

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

    
242
        }
243

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

    
260
        public boolean isWithDefaultLegend() {
261
                return false;
262
        }
263

    
264
        public Object getDefaultLegend() throws ReadException {
265
                return this.dxf.getDefaultLegend();
266
        }
267

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

    
272
        public boolean canAlterFeatureType() {
273
                return false;
274
        }
275

    
276
        public String getName() {
277
                return DATASTORE_NAME;
278
        }
279

    
280
        protected void doOpen() throws OpenException {
281

    
282
        }
283

    
284
        protected void doClose() throws CloseException {
285
                this.dxf.close();
286
        }
287

    
288
        protected void doDispose() throws CloseException {
289
                super.doDispose();
290
                ResourceManager resMan = ResourceManager.getResourceManager();
291

    
292
            try {
293
                        resMan.remove(this.dxf, this);
294
                } catch (DataException e1) {
295
                        throw new CloseException(this.getName(),e1);
296
                } catch (KeyException e) {
297
                        throw new CloseException(this.getName(),e);
298
                }
299

    
300
                this.dxf = null;
301
                this.defaultFeatureType=null;
302
                this.metadata=null;
303
        }
304

    
305
        public boolean isEditable() {
306
                return this.dxf.isEditable();
307
        }
308

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

    
344

    
345
        protected FeaturesWriter getFeaturesWriter() {
346
                FeaturesWriter writer = new DXFFeaturesWriter();
347
//                writer.init(this);
348
                return writer;
349
        }
350
        public DataStoreParameters getParameters() {
351
                return parameters;
352
        }
353

    
354
        public DataExplorer getExplorer() throws InitializeException {
355
                DataManager dsm=DataManager.getManager();
356
                DataExplorerParameters dsp = dsm.createDataExplorerParameters(
357
                                DXFDataExplorer.DATASOURCE_NAME);
358
                ((FileExplorerParameters)dsp).setSource(dxfParameters.getFile().getParentFile());
359

    
360
                DataExplorer src=null;
361
                try {
362
                        src = dsm.createDataExplorer(dsp);
363
                } catch (InitializeException e1) {
364
                        e1.printStackTrace();
365
                }
366
                return src;
367
        }
368

    
369
        /* (non-Javadoc)
370
         * @see org.gvsig.fmap.data.feature.FeatureStore#doRefresh()
371
         */
372
        protected void doRefresh() throws OpenException, InitializeException {
373
                // TODO Auto-generated method stub
374

    
375
        }
376
        public boolean canWriteGeometry(int gvSIGgeometryType) {
377
                return getFeaturesWriter().canWriteGeometry(gvSIGgeometryType);
378
        }
379

    
380
}