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 22373 jmvivo
package org.gvsig.fmap.data.feature.file.dxf;
2 19737 vcaballero
3 20130 jmvivo
import java.lang.ref.WeakReference;
4 20692 jmvivo
import java.security.KeyException;
5 19737 vcaballero
import java.util.ArrayList;
6
import java.util.Collection;
7 20130 jmvivo
import java.util.Comparator;
8 19737 vcaballero
import java.util.Iterator;
9
import java.util.List;
10 20130 jmvivo
import java.util.TreeSet;
11 19737 vcaballero
12 21911 jmvivo
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 22360 jmvivo
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 23123 jmvivo
import org.gvsig.fmap.data.feature.expressionevaluator.Filter;
34 22373 jmvivo
import org.gvsig.fmap.data.feature.file.FileExplorerParameters;
35
import org.gvsig.fmap.data.feature.file.FileStore;
36 22174 vcaballero
import org.gvsig.fmap.geom.Geometry;
37 21177 vcaballero
import org.gvsig.fmap.geom.primitive.Envelope;
38 19737 vcaballero
import org.gvsig.metadata.IMetadata;
39
import org.gvsig.metadata.IMetadataManager;
40
import org.gvsig.metadata.MetadataManager;
41 23303 jmvivo
import org.gvsig.tools.exception.BaseException;
42 19737 vcaballero
43 22180 jmvivo
public class DXFStore extends FileStore {
44 19737 vcaballero
        public static String DATASTORE_NAME = "DXFStore";
45 20692 jmvivo
        private List featureTypes = new ArrayList();
46
        protected IMetadata metadata;
47 19737 vcaballero
48 20719 jmvivo
49 20692 jmvivo
        private DXFStoreParameters dxfParameters;
50 19737 vcaballero
51 20692 jmvivo
        private DXFResource dxf;
52 19737 vcaballero
53 21045 jmvivo
        public void init(DataStoreParameters parameters) throws InitializeException {
54 19737 vcaballero
55 20692 jmvivo
                dxfParameters=(DXFStoreParameters)parameters;
56 19737 vcaballero
57 20692 jmvivo
                DXFResource tmpResource = new DXFResource(dxfParameters);
58 19737 vcaballero
59 20692 jmvivo
                ResourceManager resMan = ResourceManager.getResourceManager();
60 19737 vcaballero
61 20692 jmvivo
                try {
62 23509 jmvivo
                        this.dxf = (DXFResource) resMan.addResource(tmpResource, this);
63 22407 jmvivo
                        //                        this.dxf.load();
64 20692 jmvivo
                } catch (DataException e1) {
65
                        throw new InitializeException(this.getName(),e1);
66
                }
67 19737 vcaballero
68 20692 jmvivo
                super.init(parameters,this.dxf);
69 19737 vcaballero
70 20692 jmvivo
                try {
71 20850 jmvivo
                        this.defaultFeatureType = this.dxf.getFeatureType();
72 20692 jmvivo
                } catch (ReadException e) {
73
                        throw new InitializeException(this.getName(),e);
74 19737 vcaballero
                }
75
76 20692 jmvivo
        }
77
78 21045 jmvivo
         static FeatureType newFeatureType(){
79
                        DefaultFeatureType fType= new DefaultFeatureType();
80 19737 vcaballero
81 20421 vcaballero
                        try{
82 20909 jmvivo
                        AttributeDescriptor descriptorID = (AttributeDescriptor) fType.createAttributeDescriptor();
83 20421 vcaballero
                        descriptorID.loading();
84 21045 jmvivo
                        descriptorID.setType(FeatureAttributeDescriptor.TYPE_INT);
85 19737 vcaballero
                        descriptorID.setName("ID");
86
                        descriptorID.setDefaultValue(new Integer(0));
87 20421 vcaballero
                        descriptorID.stopLoading();
88 20049 jmvivo
                        fType.add(descriptorID);
89 19737 vcaballero
90 20909 jmvivo
                        AttributeDescriptor descriptorShape = (AttributeDescriptor) fType.createAttributeDescriptor();
91 20421 vcaballero
                        descriptorShape.loading();
92 21045 jmvivo
                        descriptorShape.setType(FeatureAttributeDescriptor.TYPE_GEOMETRY);
93 19737 vcaballero
                        descriptorShape.setName("GEOMETRY");
94
                        descriptorShape.setDefaultValue(null);
95 20421 vcaballero
                        descriptorShape.stopLoading();
96 20049 jmvivo
                        fType.add(descriptorShape);
97
                        fType.setDefaultGeometry("GEOMETRY");
98 19737 vcaballero
99 20909 jmvivo
                        AttributeDescriptor descriptorEntity = (AttributeDescriptor) fType.createAttributeDescriptor();
100 20421 vcaballero
                        descriptorEntity.loading();
101 21045 jmvivo
                        descriptorEntity.setType(FeatureAttributeDescriptor.TYPE_STRING);
102 19737 vcaballero
                        descriptorEntity.setName("Entity");
103
                        descriptorEntity.setDefaultValue("Entity");
104 20421 vcaballero
                        descriptorEntity.stopLoading();
105 20049 jmvivo
                        fType.add(descriptorEntity);
106 19737 vcaballero
107 20909 jmvivo
                        AttributeDescriptor descriptorLayer = (AttributeDescriptor) fType.createAttributeDescriptor();
108 20421 vcaballero
                        descriptorLayer.loading();
109 21045 jmvivo
                        descriptorLayer.setType(FeatureAttributeDescriptor.TYPE_STRING);
110 19737 vcaballero
                        descriptorLayer.setName("Layer");
111
                        descriptorLayer.setDefaultValue("default");
112 20421 vcaballero
                        descriptorLayer.stopLoading();
113 20049 jmvivo
                        fType.add(descriptorLayer);
114 19737 vcaballero
115 20909 jmvivo
                        AttributeDescriptor descriptorColor = (AttributeDescriptor) fType.createAttributeDescriptor();
116 20421 vcaballero
                        descriptorColor.loading();
117 21045 jmvivo
                        descriptorColor.setType(FeatureAttributeDescriptor.TYPE_INT);
118 19737 vcaballero
                        descriptorColor.setName("Color");
119
                        descriptorColor.setDefaultValue(new Integer(0));
120 20421 vcaballero
                        descriptorColor.stopLoading();
121 20049 jmvivo
                        fType.add(descriptorColor);
122 19737 vcaballero
123 20909 jmvivo
                        AttributeDescriptor descriptorElevation = (AttributeDescriptor) fType.createAttributeDescriptor();
124 20421 vcaballero
                        descriptorElevation.loading();
125 21045 jmvivo
                        descriptorElevation.setType(FeatureAttributeDescriptor.TYPE_DOUBLE);
126 19737 vcaballero
                        descriptorElevation.setName("Elevation");
127
                        descriptorElevation.setDefaultValue(new Double(0));
128 20421 vcaballero
                        descriptorElevation.stopLoading();
129 20049 jmvivo
                        fType.add(descriptorElevation);
130 19737 vcaballero
131 20909 jmvivo
                        AttributeDescriptor descriptorThickness = (AttributeDescriptor) fType.createAttributeDescriptor();
132 20421 vcaballero
                        descriptorThickness.loading();
133 21045 jmvivo
                        descriptorThickness.setType(FeatureAttributeDescriptor.TYPE_DOUBLE);
134 19737 vcaballero
                        descriptorThickness.setName("Thickness");
135
                        descriptorThickness.setDefaultValue(new Double(0));
136 20421 vcaballero
                        descriptorThickness.stopLoading();
137 20049 jmvivo
                        fType.add(descriptorThickness);
138 19737 vcaballero
139 20909 jmvivo
                        AttributeDescriptor descriptorText = (AttributeDescriptor) fType.createAttributeDescriptor();
140 20421 vcaballero
                        descriptorText.loading();
141 21045 jmvivo
                        descriptorText.setType(FeatureAttributeDescriptor.TYPE_STRING);
142 19737 vcaballero
                        descriptorText.setName("Text");
143
                        descriptorText.setDefaultValue("");
144 20421 vcaballero
                        descriptorText.stopLoading();
145 20049 jmvivo
                        fType.add(descriptorText);
146 19737 vcaballero
147 20909 jmvivo
                        AttributeDescriptor descriptorHeightText = (AttributeDescriptor) fType.createAttributeDescriptor();
148 20421 vcaballero
                        descriptorHeightText.loading();
149 21045 jmvivo
                        descriptorHeightText.setType(FeatureAttributeDescriptor.TYPE_FLOAT);
150 19737 vcaballero
                        descriptorHeightText.setName("HeightText");
151
                        descriptorHeightText.setDefaultValue(new Float(10));
152 20421 vcaballero
                        descriptorHeightText.stopLoading();
153 20049 jmvivo
                        fType.add(descriptorHeightText);
154 19737 vcaballero
155 20909 jmvivo
                        AttributeDescriptor descriptorRotationText = (AttributeDescriptor) fType.createAttributeDescriptor();
156 20421 vcaballero
                        descriptorRotationText.loading();
157 21045 jmvivo
                        descriptorRotationText.setType(FeatureAttributeDescriptor.TYPE_DOUBLE);
158 19737 vcaballero
                        descriptorRotationText.setName("Rotation");
159
                        descriptorRotationText.setDefaultValue(new Double(10));
160 20421 vcaballero
                        descriptorRotationText.stopLoading();
161 20049 jmvivo
                        fType.add(descriptorRotationText);
162 22174 vcaballero
                        fType.setDefaultGeometry("GEOMETRY");
163
                        fType.setGeometryTypes(new int[]{Geometry.TYPES.GEOMETRY});
164 20421 vcaballero
                        }catch (IsNotAttributeSettingException e) {
165
                                e.printStackTrace();
166
                        }
167 19737 vcaballero
168 20049 jmvivo
                        return fType;
169
170
         }
171
172 19737 vcaballero
173 20421 vcaballero
        protected void doFinishEdition() throws WriteException, ReadException {
174 21045 jmvivo
                FeaturesWriter writer = getFeaturesWriter();
175 19737 vcaballero
        writer.init(this);
176 20850 jmvivo
        writer.updateFeatureType(defaultFeatureType);
177 20719 jmvivo
        this.dxf.editing();
178 19737 vcaballero
        writer.preProcess();
179
        Collection collection=getDataCollection();
180
        Iterator iterator=collection.iterator();
181 21045 jmvivo
        Feature feature;
182 19737 vcaballero
        while (iterator.hasNext()) {
183 21045 jmvivo
                feature= (Feature) iterator.next();
184 19737 vcaballero
                        writer.insertFeature(feature);
185
                }
186
        writer.postProcess();
187 20719 jmvivo
        this.dxf.stopEditing();
188 20692 jmvivo
        this.dxf.changed(this);
189 19737 vcaballero
190
        }
191
192 21045 jmvivo
        public DataCollection getDataCollection(FeatureType type, String filter, String order) throws ReadException {
193 23303 jmvivo
                try {
194
                        type = this.checkFeatureTypeForCollection(type);
195
                } catch (DataException e) {
196
                        throw new ReadException(this.getName(), e);
197 19737 vcaballero
                }
198 23303 jmvivo
199 21045 jmvivo
                FeatureCollection coll;
200 23123 jmvivo
                DataManager manager = DataManager.getManager();
201 20130 jmvivo
202
                //TODO aplicar filtro, orden e incluso y el featureType
203 23123 jmvivo
                Filter parser = null;
204 20130 jmvivo
                if (filter!=null){
205 23214 jmvivo
                        parser = manager.getExpressionParser().parseFilter(filter);
206 20130 jmvivo
                }
207 20692 jmvivo
//                ArrayList originalFeatures=this.dxf.getFeatures();
208 20130 jmvivo
                int num=0;
209
                if (featureManager!=null){
210
                        num=featureManager.getNum();
211
                }
212 20692 jmvivo
                int originalSize=this.dxf.getFeatureCount();
213 20130 jmvivo
                Collection allFeatures=null;
214
                if (order!=null){
215 23123 jmvivo
                        Comparator comparator = manager.getExpressionParser()
216 23214 jmvivo
                                        .parseComparator(order);
217 20130 jmvivo
                        allFeatures = new TreeSet(comparator);
218
                }else{
219
                        allFeatures=new ArrayList();
220
                }
221 22373 jmvivo
                Feature auxFeautre;
222 20130 jmvivo
                for (int i = 0; i < originalSize+num; i++) {
223 21045 jmvivo
                        Feature feature=null;
224 20692 jmvivo
                        if (i<this.dxf.getFeatureCount()){
225
                                feature=this.dxf.getFeature(i);
226 20130 jmvivo
                        }else{
227 21069 jmvivo
                                feature=featureManager.getFeature(i-originalSize,this,type);
228 20130 jmvivo
                        }
229
                        if (featureManager == null || !featureManager.isDeleted(feature)){
230 23123 jmvivo
                                if (filter == null || parser.evaluate(feature)) {
231 22373 jmvivo
                                        allFeatures.add(feature.getID());
232 20130 jmvivo
                                }
233
                        }
234
235
                }
236
237 22353 jmvivo
                coll = new DXFFeatureCollection(this, type, allFeatures);
238 20130 jmvivo
                this.addObserver(new WeakReference(coll));
239
                return coll;
240
241 19737 vcaballero
        }
242
243 21069 jmvivo
        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 21177 vcaballero
                }
251 19737 vcaballero
                if (this.alterMode){
252
                    if (featureManager.contains(id)) {
253 21069 jmvivo
                            return featureManager.getFeature(id,this,featureType);
254 19737 vcaballero
                    }
255
            }
256 21069 jmvivo
                return id.getFeature(featureType);
257 19737 vcaballero
        }
258
259
        public List getFeatureTypes() {
260 22174 vcaballero
                featureTypes.clear();
261
                featureTypes.add(getDefaultFeatureType());
262
                return featureTypes;
263 19737 vcaballero
        }
264
265
        public boolean isWithDefaultLegend() {
266
                return false;
267
        }
268
269 21613 jmvivo
        public Object getDefaultLegend() throws ReadException {
270
                return this.dxf.getDefaultLegend();
271 19737 vcaballero
        }
272
273 21613 jmvivo
        public Object getDefaultLabelingStrategy() throws ReadException {
274
                return this.dxf.getDefaultLabelingStrategy();
275 19737 vcaballero
        }
276
277
        public boolean canAlterFeatureType() {
278 20719 jmvivo
                return false;
279 19737 vcaballero
        }
280
281
        public String getName() {
282
                return DATASTORE_NAME;
283
        }
284
285 20626 jmvivo
        protected void doOpen() throws OpenException {
286 19737 vcaballero
287
        }
288
289 20626 jmvivo
        protected void doClose() throws CloseException {
290 20692 jmvivo
                this.dxf.close();
291 19737 vcaballero
        }
292
293 20626 jmvivo
        protected void doDispose() throws CloseException {
294
                super.doDispose();
295 20692 jmvivo
                ResourceManager resMan = ResourceManager.getResourceManager();
296
297
            try {
298 23509 jmvivo
                        resMan.remove(this.dxf, this);
299 20692 jmvivo
                } 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 20850 jmvivo
                this.defaultFeatureType=null;
307 20692 jmvivo
                this.metadata=null;
308 19737 vcaballero
        }
309
310
        public boolean isEditable() {
311 20719 jmvivo
                return this.dxf.isEditable();
312 19737 vcaballero
        }
313
314
        public IMetadata getMetadata() throws BaseException {
315
                if (metadata==null){
316
                        IMetadataManager manager=MetadataManager.getManager();
317
                        metadata=manager.create(DATASTORE_NAME);
318 21177 vcaballero
                        Envelope extent=this.dxf.getFullExtent();
319 19737 vcaballero
                        metadata.set("extent",extent);
320
                        String srs=getSRS();
321
                        metadata.set("srs",srs);
322 21613 jmvivo
                        metadata.set("WithDefaultLegend", Boolean.TRUE);
323 19737 vcaballero
                }
324
                if (this.alterMode){
325 21177 vcaballero
                        Envelope extent=(Envelope)metadata.get("extent");
326 21045 jmvivo
                        FeatureCollection featureCollection=(FeatureCollection)getDataCollection();
327 19737 vcaballero
                    if (spatialManager.isFullExtentDirty()){
328
                            if (!featureCollection.isEmpty()){
329
                                    Iterator featureIterator=featureCollection.iterator();
330 21045 jmvivo
                                    extent = ((Feature)featureIterator.next()).getExtent();
331 19737 vcaballero
                                    while(featureIterator.hasNext()){
332 21045 jmvivo
                                            Feature feature=(Feature)featureIterator.next();
333 21177 vcaballero
                                            Envelope boundExtent=feature.getExtent();
334 22180 jmvivo
                                            if (boundExtent!=null) {
335
                                                        extent.add(boundExtent);
336
                                                }
337 19737 vcaballero
                                    }
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 21045 jmvivo
        protected FeaturesWriter getFeaturesWriter() {
351
                FeaturesWriter writer = new DXFFeaturesWriter();
352 19940 vcaballero
//                writer.init(this);
353 19737 vcaballero
                return writer;
354
        }
355 21045 jmvivo
        public DataStoreParameters getParameters() {
356 19737 vcaballero
                return parameters;
357
        }
358 19907 vcaballero
359 21594 jmvivo
        public DataExplorer getExplorer() throws InitializeException {
360 20424 vcaballero
                DataManager dsm=DataManager.getManager();
361 21045 jmvivo
                DataExplorerParameters dsp = dsm.createDataExplorerParameters(
362 20424 vcaballero
                                DXFDataExplorer.DATASOURCE_NAME);
363 20744 jmvivo
                ((FileExplorerParameters)dsp).setSource(dxfParameters.getFile().getParentFile());
364 20424 vcaballero
365 21045 jmvivo
                DataExplorer src=null;
366 20424 vcaballero
                try {
367
                        src = dsm.createDataExplorer(dsp);
368
                } catch (InitializeException e1) {
369
                        e1.printStackTrace();
370
                }
371
                return src;
372 19907 vcaballero
        }
373 20660 jmvivo
374
        /* (non-Javadoc)
375 22360 jmvivo
         * @see org.gvsig.fmap.data.feature.FeatureStore#doRefresh()
376 20660 jmvivo
         */
377
        protected void doRefresh() throws OpenException, InitializeException {
378
                // TODO Auto-generated method stub
379
380
        }
381 21709 vcaballero
        public boolean canWriteGeometry(int gvSIGgeometryType) {
382
                return getFeaturesWriter().canWriteGeometry(gvSIGgeometryType);
383
        }
384 22332 vcaballero
385 19737 vcaballero
}