Statistics
| Revision:

root / trunk / libraries / libFMap_dataFile / src / org / gvsig / data / datastores / vectorial / file / dgn / DGNStore.java @ 20744

History | View | Annotate | Download (11.2 KB)

1
package org.gvsig.data.datastores.vectorial.file.dgn;
2

    
3
import java.io.File;
4
import java.lang.ref.WeakReference;
5
import java.security.KeyException;
6
import java.util.ArrayList;
7
import java.util.Collection;
8
import java.util.Comparator;
9
import java.util.Iterator;
10
import java.util.List;
11
import java.util.TreeSet;
12

    
13
import org.gvsig.data.DataManager;
14
import org.gvsig.data.IDataCollection;
15
import org.gvsig.data.IDataExplorer;
16
import org.gvsig.data.IDataExplorerParameters;
17
import org.gvsig.data.IDataStoreParameters;
18
import org.gvsig.data.ResourceManager;
19
import org.gvsig.data.datastores.vectorial.IFeaturesWriter;
20
import org.gvsig.data.datastores.vectorial.file.FileExplorerParameters;
21
import org.gvsig.data.exception.CloseException;
22
import org.gvsig.data.exception.DataException;
23
import org.gvsig.data.exception.InitializeException;
24
import org.gvsig.data.exception.OpenException;
25
import org.gvsig.data.exception.ReadException;
26
import org.gvsig.data.exception.WriteException;
27
import org.gvsig.data.spatialprovisional.IExtent;
28
import org.gvsig.data.vectorial.AttributeDescriptor;
29
import org.gvsig.data.vectorial.FeatureStore;
30
import org.gvsig.data.vectorial.FeatureType;
31
import org.gvsig.data.vectorial.IFeature;
32
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor;
33
import org.gvsig.data.vectorial.IFeatureCollection;
34
import org.gvsig.data.vectorial.IFeatureID;
35
import org.gvsig.data.vectorial.IFeatureType;
36
import org.gvsig.data.vectorial.IsNotAttributeSettingException;
37
import org.gvsig.data.vectorial.expressionevaluator.FeatureComparator;
38
import org.gvsig.data.vectorial.expressionevaluator.FeatureFilter;
39
import org.gvsig.exceptions.BaseException;
40
import org.gvsig.metadata.IMetadata;
41
import org.gvsig.metadata.IMetadataManager;
42
import org.gvsig.metadata.MetadataManager;
43

    
44
public class DGNStore extends FeatureStore{
45
        public static String DATASTORE_NAME = "DGNStore";
46

    
47
        private List featureTypes = new ArrayList();//<IFeatureType>
48
        private IFeatureType featureType;
49
        protected IMetadata metadata;
50
        private DGNStoreParameters dgnParameters=null;
51

    
52
        private DGNResource dgn;
53

    
54
        public void init(IDataStoreParameters parameters) throws InitializeException {
55

    
56
                dgnParameters=(DGNStoreParameters)parameters;
57

    
58
                DGNResource tmpResource = new DGNResource(dgnParameters);
59

    
60
                ResourceManager resMan = ResourceManager.getResourceManager();
61

    
62
                try {
63
                        this.dgn = (DGNResource)resMan.addResource(tmpResource);
64
                } catch (DataException e1) {
65
                        throw new InitializeException(this.getName(),e1);
66
                }
67

    
68
                super.init(parameters,this.dgn);
69

    
70
                try {
71
                        this.featureType = this.dgn.getFeatureType();
72
                } catch (ReadException e) {
73
                        throw new InitializeException(this.getName(),e);
74
                }
75

    
76
        }
77

    
78
        static IFeatureType newFeatureType(){
79
                FeatureType fType= new FeatureType();
80
                try {
81
                        AttributeDescriptor descriptorID = new AttributeDescriptor();
82
                        descriptorID.loading();
83
                        descriptorID.setType(IFeatureAttributeDescriptor.TYPE_INT);
84
                        descriptorID.setName("ID");
85
                        descriptorID.setDefaultValue(new Integer(0));
86
                        descriptorID.stopLoading();
87
                        fType.add(descriptorID);
88
                        fType.setFieldsId(new String[] {"ID"});
89

    
90
                        AttributeDescriptor descriptorEntity = new AttributeDescriptor();
91
                        descriptorEntity.loading();
92
                        descriptorEntity.setType(IFeatureAttributeDescriptor.TYPE_STRING);
93
                        descriptorEntity.setName("Entity");
94
                        descriptorEntity.setDefaultValue("Entity");
95
                        descriptorEntity.stopLoading();
96
                        fType.add(descriptorEntity);
97

    
98
                        AttributeDescriptor descriptorLayer = new AttributeDescriptor();
99
                        descriptorLayer.loading();
100
                        descriptorLayer.setType(IFeatureAttributeDescriptor.TYPE_INT);
101
                        descriptorLayer.setName("Layer");
102
                        descriptorLayer.setDefaultValue(new Integer(0));
103
                        descriptorLayer.stopLoading();
104
                        fType.add(descriptorLayer);
105

    
106
                        AttributeDescriptor descriptorColor = new AttributeDescriptor();
107
                        descriptorColor.loading();
108
                        descriptorColor.setType(IFeatureAttributeDescriptor.TYPE_INT);
109
                        descriptorColor.setName("Color");
110
                        descriptorColor.setDefaultValue(new Integer(0));
111
                        descriptorColor.stopLoading();
112
                        fType.add(descriptorColor);
113

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

    
126
                        AttributeDescriptor descriptorHeightText = new AttributeDescriptor();
127
                        descriptorHeightText.loading();
128
                        descriptorHeightText.setType(IFeatureAttributeDescriptor.TYPE_FLOAT);
129
                        descriptorHeightText.setName("HeightText");
130
                        descriptorHeightText.setDefaultValue(new Float(0));
131
                        descriptorHeightText.stopLoading();
132
                        fType.add(descriptorHeightText);
133

    
134
                        AttributeDescriptor descriptorRotationText = new AttributeDescriptor();
135
                        descriptorRotationText.loading();
136
                        descriptorRotationText.setType(IFeatureAttributeDescriptor.TYPE_DOUBLE);
137
                        descriptorRotationText.setName("RotationText");
138
                        descriptorRotationText.setDefaultValue(new Double(0));
139
                        descriptorRotationText.stopLoading();
140
                        fType.add(descriptorRotationText);
141

    
142
                        AttributeDescriptor descriptorText = new AttributeDescriptor();
143
                        descriptorText.loading();
144
                        descriptorText.setType(IFeatureAttributeDescriptor.TYPE_STRING);
145
                        descriptorText.setName("Text");
146
                        descriptorText.setDefaultValue("");
147
                        descriptorText.stopLoading();
148
                        fType.add(descriptorText);
149

    
150
                        AttributeDescriptor descriptorShape = new AttributeDescriptor();
151
                        descriptorShape.loading();
152

    
153
                        descriptorShape.setType(IFeatureAttributeDescriptor.TYPE_GEOMETRY);
154

    
155
                        descriptorShape.setName("GEOMETRY");
156
                        descriptorShape.setDefaultValue(null);
157
                        descriptorShape.stopLoading();
158
                        fType.add(descriptorShape);
159
                        fType.setDefaultGeometry("GEOMETRY");
160
                } catch (IsNotAttributeSettingException e) {
161
                        e.printStackTrace();
162
                }
163
                return fType;
164

    
165

    
166
        }
167

    
168
        protected void doFinishEdition() throws WriteException, ReadException{
169

    
170
        }
171

    
172
        public IDataCollection getDataCollection(IFeatureType type, String filter, String order) throws ReadException {
173
                if (type==null){
174
                        type=getDefaultFeatureType();
175
                }
176
                IFeatureCollection coll;
177

    
178
                //TODO aplicar filtro, orden e incluso y el featureType
179
                FeatureFilter parser = null;
180
                if (filter!=null){
181
                        parser = new FeatureFilter(filter,type);
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 = new FeatureComparator(type,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
                        IFeature feature=null;
199
                        if (i<this.dgn.getFeatureCount()){
200
                                feature=this.dgn.getFeature(i);
201
                        }else{
202
                                feature=featureManager.getFeature(i-originalSize,this);
203
                        }
204
                        if (featureManager == null || !featureManager.isDeleted(feature)){
205

    
206
                                DGNFeature auxfeature=new DGNFeature(feature);
207
                                if (filter==null || parser.match(auxfeature)){
208
//                                        System.err.println(i);
209
//                                        j++;
210
//                                        System.err.println("j= "+j);
211
                                        if (!allFeatures.add(auxfeature)){
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(allFeatures);
224
                this.addObserver(new WeakReference(coll));
225
                return coll;
226
        }
227

    
228
        public IFeature getFeatureByID(IFeatureID id) throws ReadException {
229
                if (this.alterMode){
230
                        if (featureManager.contains(id)) {
231
                                return featureManager.getFeature(id,this);
232

    
233
                        }
234
                }
235
                return id.getFeature(featureType);
236
        }
237

    
238
        public List getFeatureTypes() {
239
                featureTypes.set(0,getDefaultFeatureType());
240
                return featureTypes;
241
        }
242

    
243
        public IFeatureType getDefaultFeatureType() {
244
                if (isEditing()){
245
//                        Aqu? hay que construir un FeatureType con los cambios que se hayan hecho en la edici?n.
246
                        return attributeManager.getFeatureType();
247
                }
248
                return featureType;
249
        }
250

    
251
        public boolean isWithDefaultLegend() {
252
                return true;
253
        }
254

    
255
        public Object getDefaultLegend() throws ReadException {
256
                return this.dgn.getDefaultLegend();
257
        }
258

    
259
        public Object getDefaultLabelingStrategy() {
260
                return null;
261
        }
262

    
263
        public boolean canAlterFeatureType() {
264
                return false;
265
        }
266

    
267
        public String getName() {
268
                return DATASTORE_NAME;
269
        }
270

    
271
        protected void doOpen() throws OpenException {
272

    
273
        }
274

    
275
        protected void doClose() throws CloseException {
276
                this.dgn.close();
277
        }
278

    
279
        protected void doDispose() throws CloseException {
280
                super.doDispose();
281
                ResourceManager resMan = ResourceManager.getResourceManager();
282

    
283
            try {
284
                        resMan.remove(this.dgn);
285
                } catch (DataException e1) {
286
                        throw new CloseException(this.getName(),e1);
287
                } catch (KeyException e) {
288
                        throw new CloseException(this.getName(),e);
289
                }
290

    
291
                this.dgn = null;
292
                this.featureType=null;
293
                this.metadata=null;
294
        }
295

    
296
        public boolean isEditable() {
297
                return false;
298
        }
299

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

    
333

    
334
        protected IFeaturesWriter getFeaturesWriter() {
335
                return null;
336
        }
337
        public IDataStoreParameters getParameters() {
338
                return parameters;
339
        }
340

    
341
        public IDataExplorer getExplorer() {
342
                DataManager dsm=DataManager.getManager();
343
                IDataExplorerParameters dsp = dsm.createDataExplorerParameters(
344
                                DGNDataExplorer.DATASOURCE_NAME);
345
                ((FileExplorerParameters)dsp).setSource(dgnParameters.getFile().getParentFile());
346

    
347
                IDataExplorer src=null;
348
                try {
349
                        src = dsm.createDataExplorer(dsp);
350
                } catch (InitializeException e1) {
351
                        e1.printStackTrace();
352
                }
353
                return src;
354
        }
355

    
356

    
357

    
358
        /* (non-Javadoc)
359
         * @see org.gvsig.data.vectorial.FeatureStore#doRefresh()
360
         */
361
        protected void doRefresh() throws OpenException, InitializeException {
362
                // TODO Auto-generated method stub
363
        }
364

    
365
}