Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_data_dwg / src / org / gvsig / fmap / data / feature / file / dwg / DWGStore.java @ 23233

History | View | Annotate | Download (12.1 KB)

1
package org.gvsig.fmap.data.feature.file.dwg;
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.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 DWGStore extends FileStore{
43
        public static String DATASTORE_NAME = "DWGStore";
44

    
45
        private List featureTypes = new ArrayList();//<FeatureType>
46
        protected IMetadata metadata;
47
        private DWGStoreParameters dwgParameters=null;
48

    
49
        private DWGResource dwg;
50

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

    
53
                dwgParameters=(DWGStoreParameters)parameters;
54
                try {
55
                DWGResource tmpResource = new DWGResource(dwgParameters);
56
                ResourceManager resMan = ResourceManager.getResourceManager();
57

    
58
                try {
59
                        this.dwg = (DWGResource)resMan.addResource(tmpResource);
60
                } catch (DataException e1) {
61
                        throw new InitializeException(this.getName(),e1);
62
                }
63

    
64
                super.init(parameters,this.dwg);
65

    
66

    
67
                        this.defaultFeatureType = this.dwg.getFeatureType();
68
                } catch (ReadException e) {
69
                        throw new InitializeException(this.getName(),e);
70
                } catch (DataException e) {
71
                        throw new InitializeException(this.getName(),e);
72
                }
73

    
74
        }
75

    
76

    
77
        static FeatureType newFeatureType() throws DataException{
78
                DefaultFeatureType fType= new DefaultFeatureType();
79
                AttributeDescriptor descriptorID = (AttributeDescriptor) fType.createAttributeDescriptor();
80
                descriptorID.loading();
81
                descriptorID.setType(FeatureAttributeDescriptor.TYPE_INT);
82
                descriptorID.setName("ID");
83
                descriptorID.setDefaultValue(new Integer(0));
84
                descriptorID.stopLoading();
85
                fType.add(descriptorID);
86
                fType.setFieldsId(new String[] {"ID"});
87

    
88
                AttributeDescriptor descriptorFShape = (AttributeDescriptor) fType.createAttributeDescriptor();
89
                descriptorFShape.loading();
90
                descriptorFShape.setType(FeatureAttributeDescriptor.TYPE_STRING);
91
                descriptorFShape.setName("FShape");
92
                descriptorFShape.setDefaultValue("FShape");
93
                descriptorFShape.stopLoading();
94
                fType.add(descriptorFShape);
95

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

    
104
                AttributeDescriptor descriptorLayer = (AttributeDescriptor) fType.createAttributeDescriptor();
105
                descriptorLayer.loading();
106
                descriptorLayer.setType(FeatureAttributeDescriptor.TYPE_STRING);
107
                descriptorLayer.setName("Layer");
108
                descriptorLayer.setDefaultValue("");
109
                descriptorLayer.stopLoading();
110
                fType.add(descriptorLayer);
111

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

    
120
                AttributeDescriptor descriptorElevation = (AttributeDescriptor) fType.createAttributeDescriptor();
121
                descriptorElevation.loading();
122
                descriptorElevation.setType(FeatureAttributeDescriptor.TYPE_DOUBLE);
123
                descriptorElevation.setName("Elevation");
124
                descriptorElevation.setDefaultValue(new Double(0));
125
                fType.add(descriptorElevation);
126

    
127
                AttributeDescriptor descriptorThickness = (AttributeDescriptor) fType.createAttributeDescriptor();
128
                descriptorThickness.loading();
129
                descriptorThickness.setType(FeatureAttributeDescriptor.TYPE_DOUBLE);
130
                descriptorThickness.setName("Thickness");
131
                descriptorThickness.setDefaultValue(new Double(0));
132
                fType.add(descriptorThickness);
133

    
134
                AttributeDescriptor descriptorHeightText = (AttributeDescriptor) fType.createAttributeDescriptor();
135
                descriptorHeightText.loading();
136
                descriptorHeightText.setType(FeatureAttributeDescriptor.TYPE_DOUBLE);
137
                descriptorHeightText.setName("HeightText");
138
                descriptorHeightText.setDefaultValue(new Double(0));
139
                descriptorHeightText.stopLoading();
140
                fType.add(descriptorHeightText);
141

    
142
                AttributeDescriptor descriptorRotationText = (AttributeDescriptor) fType.createAttributeDescriptor();
143
                descriptorRotationText.loading();
144
                descriptorRotationText.setType(FeatureAttributeDescriptor.TYPE_DOUBLE);
145
                descriptorRotationText.setName("RotationText");
146
                descriptorRotationText.setDefaultValue(new Double(0));
147
                descriptorRotationText.stopLoading();
148
                fType.add(descriptorRotationText);
149

    
150
                AttributeDescriptor descriptorText = (AttributeDescriptor) fType.createAttributeDescriptor();
151
                descriptorText.loading();
152
                descriptorText.setType(FeatureAttributeDescriptor.TYPE_STRING);
153
                descriptorText.setName("Text");
154
                descriptorText.setDefaultValue("");
155
                descriptorText.stopLoading();
156
                fType.add(descriptorText);
157

    
158
                AttributeDescriptor descriptorShape = (AttributeDescriptor) fType.createAttributeDescriptor();
159
                descriptorShape.loading();
160

    
161
                descriptorShape.setType(FeatureAttributeDescriptor.TYPE_GEOMETRY);
162

    
163
                descriptorShape.setName("GEOMETRY");
164
                descriptorShape.setDefaultValue(null);
165
                descriptorShape.stopLoading();
166
                fType.add(descriptorShape);
167
                fType.setDefaultGeometry("GEOMETRY");
168
                fType.setGeometryTypes(new int[]{Geometry.TYPES.GEOMETRY});
169
                return fType;
170

    
171

    
172
        }
173

    
174
        protected void doFinishEdition() throws WriteException, ReadException{
175

    
176
        }
177

    
178
        public DataCollection getDataCollection(FeatureType type, String filter, String order) throws ReadException {
179
                if (type==null){
180
                        type=getDefaultFeatureType();
181
                }else{
182
                        if (!type.isSubtypeOf(this.getDefaultFeatureType())){
183
                                throw new ReadException("invalid type",this.getName());
184
                        }
185
                }
186
                FeatureCollection coll;
187

    
188
                DataManager manager = DataManager.getManager();
189
                //TODO aplicar filtro, orden e incluso y el featureType
190
                Filter parser = null;
191
                if (filter!=null){
192
                        parser = manager.getExpressionParser().parseFilter(filter);
193
                }
194
//                ArrayList originalFeatures=this.getFeatures();
195
                int num=0;
196
                if (featureManager!=null){
197
                        num=featureManager.getNum();
198
                }
199
                int originalSize=this.dwg.getFeatureCount();
200
                Collection allFeatures = null;
201
                if (order!=null){
202
                        Comparator comparator = manager.getExpressionParser()
203
                                        .parseComparator(order);
204
                        allFeatures = new TreeSet(comparator);
205
                }else{
206
                        allFeatures=new ArrayList();
207
                }
208
//                int j=0;
209
                for (int i = 0; i < originalSize+num; i++) {
210
                        Feature feature=null;
211
                        if (i<this.dwg.getFeatureCount()){
212
                                feature=this.dwg.getFeature(i);
213
                        }else{
214
                                feature=featureManager.getFeature(i-originalSize,this,type);
215
                        }
216
                        if (featureManager == null || !featureManager.isDeleted(feature)){
217

    
218
                                if (filter == null || parser.evaluate(feature)) {
219
//                                        System.err.println(i);
220
//                                        j++;
221
//                                        System.err.println("j= "+j);
222
                                        if (!allFeatures.add(feature)) {
223
                                                //????
224
//                                                System.err.println("Fallo  " +auxfeature);
225
                                        }else{
226
//                                                System.out.println("Correcto  " +auxfeature);
227
                                        }
228
                                }
229

    
230
                        }
231

    
232
                }
233

    
234
                coll = new DWGFeatureCollection(this, type, allFeatures);
235
                this.addObserver(new WeakReference(coll));
236
                return coll;
237
        }
238

    
239
        public Feature getFeatureByID(FeatureID id,FeatureType featureType) throws ReadException {
240
                if (featureType==null){
241
                        featureType=getDefaultFeatureType();
242
                }else{
243
                        if (!featureType.isSubtypeOf(this.getDefaultFeatureType())){
244
                                throw new ReadException("invalid type",this.getName());
245
                        }
246
                }
247

    
248
                if (this.alterMode){
249
                        if (featureManager.contains(id)) {
250
                                return featureManager.getFeature(id,this,featureType);
251

    
252
                        }
253
                }
254
                return id.getFeature(featureType);
255
        }
256

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

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

    
267
        public boolean canAlterFeatureType() {
268
                return false;
269
        }
270

    
271
        public String getName() {
272
                return DATASTORE_NAME;
273
        }
274

    
275
        protected void doOpen() throws OpenException {
276

    
277
        }
278

    
279
        protected void doClose() throws CloseException {
280
                this.dwg.close();
281
        }
282

    
283
        protected void doDispose() throws CloseException {
284
                super.doDispose();
285
                ResourceManager resMan = ResourceManager.getResourceManager();
286

    
287
            try {
288
                        resMan.remove(this.dwg);
289
                } catch (DataException e1) {
290
                        throw new CloseException(this.getName(),e1);
291
                } catch (KeyException e) {
292
                        throw new CloseException(this.getName(),e);
293
                }
294

    
295
                this.dwg = null;
296
                this.defaultFeatureType=null;
297
                this.metadata=null;
298
        }
299

    
300
        public boolean isEditable() {
301
                return false;
302
        }
303

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

    
339

    
340
        protected FeaturesWriter getFeaturesWriter() {
341
                return null;
342
        }
343
        public DataStoreParameters getParameters() {
344
                return parameters;
345
        }
346

    
347
        public DataExplorer getExplorer() throws InitializeException {
348
                DataManager dsm=DataManager.getManager();
349
                DataExplorerParameters dsp = dsm.createDataExplorerParameters(
350
                                DWGDataExplorer.DATASOURCE_NAME);
351
                ((FileExplorerParameters)dsp).setSource(dwgParameters.getFile().getParentFile());
352

    
353
                DataExplorer src=null;
354
                try {
355
                        src = dsm.createDataExplorer(dsp);
356
                } catch (InitializeException e1) {
357
                        e1.printStackTrace();
358
                }
359
                return src;
360
        }
361

    
362

    
363

    
364
        /* (non-Javadoc)
365
         * @see org.gvsig.fmap.data.feature.FeatureStore#doRefresh()
366
         */
367
        protected void doRefresh() throws OpenException, InitializeException {
368
                // TODO Auto-generated method stub
369
        }
370

    
371
        public boolean canWriteGeometry(int gvSIGgeometryType) {
372
                return false;
373
        }
374

    
375
        public Object getDefaultLabelingStrategy() throws ReadException {
376
                return this.dwg.getDefaultLabelingStrategy();
377
        }
378

    
379
}