Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dataFile / src / org / gvsig / fmap / data / feature / file / dgn / DGNStore.java @ 23303

History | View | Annotate | Download (11.6 KB)

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

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

    
49
        private DGNResource dgn;
50

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

    
53
                dgnParameters=(DGNStoreParameters)parameters;
54
                try {
55
                DGNResource tmpResource = new DGNResource(dgnParameters);
56
                ResourceManager resMan = ResourceManager.getResourceManager();
57

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

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

    
66

    
67
                        this.defaultFeatureType = this.dgn.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
        static FeatureType newFeatureType() throws DataException{
77
                DefaultFeatureType fType= new DefaultFeatureType();
78
                AttributeDescriptor descriptorID = (AttributeDescriptor) fType.createAttributeDescriptor();
79
                descriptorID.loading();
80
                descriptorID.setType(FeatureAttributeDescriptor.TYPE_INT);
81
                descriptorID.setName("ID");
82
                descriptorID.setDefaultValue(new Integer(0));
83
                descriptorID.stopLoading();
84
                fType.add(descriptorID);
85
                fType.setFieldsId(new String[] {"ID"});
86

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

    
95
                AttributeDescriptor descriptorLayer = (AttributeDescriptor) fType.createAttributeDescriptor();
96
                descriptorLayer.loading();
97
                descriptorLayer.setType(FeatureAttributeDescriptor.TYPE_INT);
98
                descriptorLayer.setName("Layer");
99
                descriptorLayer.setDefaultValue(new Integer(0));
100
                descriptorLayer.stopLoading();
101
                fType.add(descriptorLayer);
102

    
103
                AttributeDescriptor descriptorColor = (AttributeDescriptor) fType.createAttributeDescriptor();
104
                descriptorColor.loading();
105
                descriptorColor.setType(FeatureAttributeDescriptor.TYPE_INT);
106
                descriptorColor.setName("Color");
107
                descriptorColor.setDefaultValue(new Integer(0));
108
                descriptorColor.stopLoading();
109
                fType.add(descriptorColor);
110

    
111
                //                DefaultAttributeDescriptor descriptorElevation = new DefaultAttributeDescriptor();
112
                //                descriptorElevation.setType(FeatureAttributeDescriptor.TYPE_DOUBLE);
113
                //                descriptorElevation.setName("Elevation");
114
                //                descriptorElevation.setDefaultValue(new Double(0));
115
                //                featureType.add(descriptorElevation);
116
                //
117
                //                DefaultAttributeDescriptor descriptorThickness = new DefaultAttributeDescriptor();
118
                //                descriptorThickness.setType(FeatureAttributeDescriptor.TYPE_DOUBLE);
119
                //                descriptorThickness.setName("Thickness");
120
                //                descriptorThickness.setDefaultValue(new Double(0));
121
                //                featureType.add(descriptorThickness);
122

    
123
                AttributeDescriptor descriptorHeightText = (AttributeDescriptor) fType.createAttributeDescriptor();
124
                descriptorHeightText.loading();
125
                descriptorHeightText.setType(FeatureAttributeDescriptor.TYPE_FLOAT);
126
                descriptorHeightText.setName("HeightText");
127
                descriptorHeightText.setDefaultValue(new Float(0));
128
                descriptorHeightText.stopLoading();
129
                fType.add(descriptorHeightText);
130

    
131
                AttributeDescriptor descriptorRotationText = (AttributeDescriptor) fType.createAttributeDescriptor();
132
                descriptorRotationText.loading();
133
                descriptorRotationText.setType(FeatureAttributeDescriptor.TYPE_DOUBLE);
134
                descriptorRotationText.setName("RotationText");
135
                descriptorRotationText.setDefaultValue(new Double(0));
136
                descriptorRotationText.stopLoading();
137
                fType.add(descriptorRotationText);
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 descriptorShape = (AttributeDescriptor) fType.createAttributeDescriptor();
148
                descriptorShape.loading();
149

    
150
                descriptorShape.setType(FeatureAttributeDescriptor.TYPE_GEOMETRY);
151

    
152
                descriptorShape.setName("GEOMETRY");
153
                descriptorShape.setDefaultValue(null);
154
                descriptorShape.stopLoading();
155
                fType.add(descriptorShape);
156
                fType.setDefaultGeometry("GEOMETRY");
157
                fType.setGeometryTypes(new int[]{Geometry.TYPES.GEOMETRY});
158
                return fType;
159

    
160

    
161
        }
162

    
163
        protected void doFinishEdition() throws WriteException, ReadException{
164

    
165
        }
166

    
167
        public DataCollection getDataCollection(FeatureType type, String filter, String order) throws ReadException {
168
                try {
169
                        type = this.checkFeatureTypeForCollection(type);
170
                } catch (DataException e) {
171
                        throw new ReadException(this.getName(), e);
172
                }
173

    
174
                FeatureCollection coll;
175

    
176
                DataManager manager = DataManager.getManager();
177
                //TODO aplicar filtro, orden e incluso y el featureType
178
                Filter parser = null;
179
                if (filter!=null){
180
                        parser = manager.getExpressionParser().parseFilter(filter);
181
                }
182
//                ArrayList originalFeatures=this.getFeatures();
183
                int num=0;
184
                if (featureManager!=null){
185
                        num=featureManager.getNum();
186
                }
187
                int originalSize=this.dgn.getFeatureCount();
188
                Collection allFeatures = null;
189
                if (order!=null){
190
                        Comparator comparator = manager.getExpressionParser()
191
                                        .parseComparator(order);
192
                        allFeatures = new TreeSet(comparator);
193
                }else{
194
                        allFeatures=new ArrayList();
195
                }
196
//                int j=0;
197
                for (int i = 0; i < originalSize+num; i++) {
198
                        Feature feature=null;
199
                        if (i<this.dgn.getFeatureCount()){
200
                                feature=this.dgn.getFeature(i);
201
                        }else{
202
                                feature=featureManager.getFeature(i-originalSize,this,type);
203
                        }
204
                        if (featureManager == null || !featureManager.isDeleted(feature)){
205

    
206
                                if (filter == null || parser.evaluate(feature)) {
207
//                                        System.err.println(i);
208
//                                        j++;
209
//                                        System.err.println("j= "+j);
210
                                        if (!allFeatures.add(feature)) {
211
                                                //????
212
//                                                System.err.println("Fallo  " +auxfeature);
213
                                        }else{
214
//                                                System.out.println("Correcto  " +auxfeature);
215
                                        }
216
                                }
217

    
218
                        }
219

    
220
                }
221

    
222
                coll = new DGNFeatureCollection(this, type, allFeatures);
223
                this.addObserver(new WeakReference(coll));
224
                return coll;
225
        }
226

    
227
        public Feature getFeatureByID(FeatureID id,FeatureType featureType) throws ReadException {
228
                if (featureType==null){
229
                        featureType=getDefaultFeatureType();
230
                }else{
231
                        if (!featureType.isSubtypeOf(this.getDefaultFeatureType())){
232
                                throw new ReadException("invalid type",this.getName());
233
                        }
234
                }
235

    
236
                if (this.alterMode){
237
                        if (featureManager.contains(id)) {
238
                                return featureManager.getFeature(id,this,featureType);
239

    
240
                        }
241
                }
242
                return id.getFeature(featureType);
243
        }
244

    
245
        public List getFeatureTypes() {
246
                featureTypes.clear();
247
                featureTypes.add(getDefaultFeatureType());
248
                return featureTypes;
249
        }
250

    
251
        public Object getDefaultLegend() throws ReadException {
252
                return this.dgn.getDefaultLegend();
253
        }
254

    
255
        public boolean canAlterFeatureType() {
256
                return false;
257
        }
258

    
259
        public String getName() {
260
                return DATASTORE_NAME;
261
        }
262

    
263
        protected void doOpen() throws OpenException {
264

    
265
        }
266

    
267
        protected void doClose() throws CloseException {
268
                this.dgn.close();
269
        }
270

    
271
        protected void doDispose() throws CloseException {
272
                super.doDispose();
273
                ResourceManager resMan = ResourceManager.getResourceManager();
274

    
275
            try {
276
                        resMan.remove(this.dgn);
277
                } catch (DataException e1) {
278
                        throw new CloseException(this.getName(),e1);
279
                } catch (KeyException e) {
280
                        throw new CloseException(this.getName(),e);
281
                }
282

    
283
                this.dgn = null;
284
                this.defaultFeatureType=null;
285
                this.metadata=null;
286
        }
287

    
288
        public boolean isEditable() {
289
                return false;
290
        }
291

    
292
        public IMetadata getMetadata() throws BaseException {
293
                if (metadata==null){
294
                        IMetadataManager manager=MetadataManager.getManager();
295
                        metadata=manager.create(DATASTORE_NAME);
296
                        Envelope extent=this.dgn.getFullExtent();
297
                        metadata.set("extent",extent);
298
                        String srs=getSRS();
299
                        metadata.set("srs",srs);
300
                        metadata.set("WithDefaultLegend", Boolean.TRUE);
301
                }
302
                if (this.alterMode){
303
                        Envelope extent=(Envelope)metadata.get("extent");
304
                        FeatureCollection featureCollection=(FeatureCollection)getDataCollection();
305
                        if (spatialManager.isFullExtentDirty()){
306
                                if (!featureCollection.isEmpty()){
307
                                        Iterator featureIterator=featureCollection.iterator();
308
                                        extent = ((Feature)featureIterator.next()).getExtent();
309
                                        while(featureIterator.hasNext()){
310
                                                Feature feature=(Feature)featureIterator.next();
311
                                                Envelope boundExtent=feature.getExtent();
312
                                                if (boundExtent!=null) {
313
                                                        extent.add(boundExtent);
314
                                                }
315
                                        }
316
                                }
317
                        }
318
                        metadata.set("extent",extent);
319
                }
320
                return metadata;
321
        }
322
        private String getSRS() {
323
                // TODO Auto-generated method stub
324
                return null;
325
        }
326

    
327

    
328
        protected FeaturesWriter getFeaturesWriter() {
329
                return null;
330
        }
331
        public DataStoreParameters getParameters() {
332
                return parameters;
333
        }
334

    
335
        public DataExplorer getExplorer() throws InitializeException {
336
                DataManager dsm=DataManager.getManager();
337
                DataExplorerParameters dsp = dsm.createDataExplorerParameters(
338
                                DGNDataExplorer.DATASOURCE_NAME);
339
                ((FileExplorerParameters)dsp).setSource(dgnParameters.getFile().getParentFile());
340

    
341
                DataExplorer src=null;
342
                try {
343
                        src = dsm.createDataExplorer(dsp);
344
                } catch (InitializeException e1) {
345
                        e1.printStackTrace();
346
                }
347
                return src;
348
        }
349

    
350

    
351

    
352
        /* (non-Javadoc)
353
         * @see org.gvsig.fmap.data.feature.FeatureStore#doRefresh()
354
         */
355
        protected void doRefresh() throws OpenException, InitializeException {
356
                // TODO Auto-generated method stub
357
        }
358

    
359
        public boolean canWriteGeometry(int gvSIGgeometryType) {
360
                return false;
361
        }
362

    
363
        public Object getDefaultLabelingStrategy() throws ReadException {
364
                return this.dgn.getDefaultLabelingStrategy();
365
        }
366

    
367
}