Statistics
| Revision:

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

History | View | Annotate | Download (11.5 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.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.expressionevaluator.Filter;
32
import org.gvsig.fmap.data.feature.file.FileExplorerParameters;
33
import org.gvsig.fmap.data.feature.file.FileStore;
34
import org.gvsig.fmap.geom.Geometry;
35
import org.gvsig.fmap.geom.primitive.Envelope;
36
import org.gvsig.metadata.IMetadata;
37
import org.gvsig.metadata.IMetadataManager;
38
import org.gvsig.metadata.MetadataManager;
39
import org.gvsig.tools.exception.BaseException;
40

    
41
public class DGNStore extends FileStore{
42
        public static String DATASTORE_NAME = "DGNStore";
43

    
44
        protected IMetadata metadata;
45
        private DGNStoreParameters dgnParameters=null;
46

    
47
        private DGNResource dgn;
48

    
49
        public void init(DataStoreParameters parameters) throws InitializeException {
50

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

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

    
62
                        super.init(parameters, this.dgn);
63

    
64

    
65
                        this.defaultFeatureType = this.dgn.getFeatureType();
66
                        this.featureTypes = new ArrayList();
67
                        this.featureTypes.add(defaultFeatureType);
68

    
69
                } catch (ReadException e) {
70
                        throw new InitializeException(this.getName(),e);
71
                } catch (DataException e) {
72
                        throw new InitializeException(this.getName(),e);
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 descriptorEntity = (AttributeDescriptor) fType.createAttributeDescriptor();
89
                descriptorEntity.loading();
90
                descriptorEntity.setType(FeatureAttributeDescriptor.TYPE_STRING);
91
                descriptorEntity.setName("Entity");
92
                descriptorEntity.setDefaultValue("Entity");
93
                descriptorEntity.stopLoading();
94
                fType.add(descriptorEntity);
95

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

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

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

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

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

    
151
                descriptorShape.setType(FeatureAttributeDescriptor.TYPE_GEOMETRY);
152

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

    
161

    
162
        }
163

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

    
166
        }
167

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

    
175
                FeatureCollection coll;
176

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

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

    
219
                        }
220

    
221
                }
222

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

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

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

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

    
246
        public Object getDefaultLegend() throws ReadException {
247
                return this.dgn.getDefaultLegend();
248
        }
249

    
250
        public boolean canAlterFeatureType() {
251
                return false;
252
        }
253

    
254
        public String getName() {
255
                return DATASTORE_NAME;
256
        }
257

    
258
        protected void doOpen() throws OpenException {
259

    
260
        }
261

    
262
        protected void doClose() throws CloseException {
263
                this.dgn.close();
264
        }
265

    
266
        protected void doDispose() throws CloseException {
267
                super.doDispose();
268
                ResourceManager resMan = ResourceManager.getResourceManager();
269

    
270
            try {
271
                        resMan.remove(this.dgn, this);
272
                } catch (DataException e1) {
273
                        throw new CloseException(this.getName(),e1);
274
                } catch (KeyException e) {
275
                        throw new CloseException(this.getName(),e);
276
                }
277

    
278
                this.dgn = null;
279
                this.defaultFeatureType=null;
280
                this.metadata=null;
281
        }
282

    
283
        public boolean isEditable() {
284
                return false;
285
        }
286

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

    
322

    
323
        protected FeaturesWriter getFeaturesWriter() {
324
                return null;
325
        }
326
        public DataStoreParameters getParameters() {
327
                return parameters;
328
        }
329

    
330
        public DataExplorer getExplorer() throws InitializeException {
331
                DataManager dsm=DataManager.getManager();
332
                DataExplorerParameters dsp = dsm.createDataExplorerParameters(
333
                                DGNDataExplorer.DATASOURCE_NAME);
334
                ((FileExplorerParameters)dsp).setSource(dgnParameters.getFile().getParentFile());
335

    
336
                DataExplorer src=null;
337
                try {
338
                        src = dsm.createDataExplorer(dsp);
339
                } catch (InitializeException e1) {
340
                        e1.printStackTrace();
341
                }
342
                return src;
343
        }
344

    
345

    
346

    
347
        /* (non-Javadoc)
348
         * @see org.gvsig.fmap.data.feature.FeatureStore#doRefresh()
349
         */
350
        protected void doRefresh() throws OpenException, InitializeException {
351
                // TODO Auto-generated method stub
352
        }
353

    
354
        public boolean canWriteGeometry(int gvSIGgeometryType) {
355
                return false;
356
        }
357

    
358
        public Object getDefaultLabelingStrategy() throws ReadException {
359
                return this.dgn.getDefaultLabelingStrategy();
360
        }
361

    
362
}