Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.arcims.feature.extension / src / main / java / org / gvsig / fmap / dal / store / arcims / ArcImsStoreProvider.java @ 32356

History | View | Annotate | Download (12.4 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2010 Prodevelop S.L. main development
26
 * http://www.prodevelop.es
27
 */
28

    
29
package org.gvsig.fmap.dal.store.arcims;
30

    
31
import java.io.IOException;
32
import java.util.List;
33
import java.util.Vector;
34

    
35
import org.cresques.cts.IProjection;
36
import org.gvsig.fmap.crs.CRSFactory;
37
import org.gvsig.fmap.dal.DALLocator;
38
import org.gvsig.fmap.dal.DataManager;
39
import org.gvsig.fmap.dal.DataServerExplorer;
40
import org.gvsig.fmap.dal.DataStoreParameters;
41
import org.gvsig.fmap.dal.exception.DataException;
42
import org.gvsig.fmap.dal.exception.InitializeException;
43
import org.gvsig.fmap.dal.exception.OpenException;
44
import org.gvsig.fmap.dal.exception.ReadException;
45
import org.gvsig.fmap.dal.feature.FeatureQuery;
46
import org.gvsig.fmap.dal.feature.FeatureStore;
47
import org.gvsig.fmap.dal.feature.FeatureType;
48
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider;
49
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider;
50
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer;
51
import org.gvsig.fmap.dal.serverexplorer.arcims.ArcImsServerExplorer;
52
import org.gvsig.fmap.dal.serverexplorer.arcims.ArcImsServerExplorerParameters;
53
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
54
import org.gvsig.fmap.geom.Geometry;
55
import org.gvsig.fmap.geom.Geometry.TYPES;
56
import org.gvsig.fmap.geom.primitive.Envelope;
57
import org.gvsig.remoteclient.ogc.OGCClientOperation;
58
import org.gvsig.remoteclient.wfs.WFSClient;
59
import org.gvsig.remoteclient.wfs.WFSFeature;
60
import org.gvsig.remoteclient.wfs.WFSStatus;
61
import org.gvsig.remoteclient.wfs.exceptions.WFSException;
62
import org.gvsig.remoteclient.wfs.filters.filterencoding.FilterEncoding;
63
import org.gvsig.remoteclient.wfs.schema.XMLElement;
64
import org.gvsig.tools.ToolsLocator;
65
import org.gvsig.tools.dynobject.DynClass;
66
import org.gvsig.tools.dynobject.DynObjectManager;
67
import org.gvsig.tools.evaluator.EvaluatorFieldValue;
68
import org.gvsig.tools.evaluator.EvaluatorFieldValueMatch;
69
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo;
70
import org.slf4j.Logger;
71
import org.slf4j.LoggerFactory;
72

    
73
/**
74
 * @author vsanjaime
75
 */
76
public class ArcImsStoreProvider extends AbstractFeatureStoreProvider implements
77
                ResourceConsumer {
78
        private static final Logger logger = LoggerFactory
79
                        .getLogger(ArcImsStoreProvider.class);
80
        public static String NAME = "ArcIMSStore";
81
        public static String DESCRIPTION = "ArcIMS store to load ArcIMS resources";
82
        private static final String DYNCLASS_NAME = "ArcIMSStore";
83
        protected static DynClass DYNCLASS = null;
84
        private IProjection projection;
85

    
86
        boolean isFilterByAreaSupported = true;
87

    
88
        // WFS Parameters
89
        private WFSClient wfsClient;
90
        private WFSStatus wfsStatus;
91

    
92
        public ArcImsStoreProvider(DataStoreParameters params,
93
                        DataStoreProviderServices storeServices) throws InitializeException {
94
                super(params, storeServices, ToolsLocator.getDynObjectManager()
95
                                .createDynObject(DYNCLASS));
96
        }
97

    
98
        protected void retrieveFile() throws InitializeException {
99
                try {
100
                        ArcImsStoreParameters wfsParameters = getWFSParameters();
101
                        try {
102
                                if (wfsParameters.getVersion() == null) {
103
                                        wfsClient = new WFSClient(wfsParameters.getUrl());
104
                                        wfsParameters.setVersion(wfsClient.getVersion());
105
                                } else {
106
                                        wfsClient = new WFSClient(wfsParameters.getUrl(),
107
                                                        wfsParameters.getVersion());
108
                                }
109
                        } catch (IOException e) {
110
                                throw new InitializeException(e);
111
                        }
112
                        wfsStatus = new WFSStatus(wfsParameters.getFeatureType(),
113
                                        wfsParameters.getFeaturePrefix());
114
                        wfsStatus.setNamespace(wfsParameters.getFeatureNamespace());
115
                        wfsStatus.setFields(wfsParameters.getFields());
116
                        // wfsStatus.setFilterQuery(wfsParameters.getFilterEncoding());
117
                        wfsStatus.setTimeout(wfsParameters.getTimeOut());
118
                        wfsStatus.setBuffer(wfsParameters.getMaxFeatures());
119
                        wfsStatus.setUserName(wfsParameters.getUser());
120
                        wfsStatus.setPassword(wfsParameters.getPassword());
121

    
122
                        m_Fich = wfsClient.getFeature(wfsStatus, true, null);
123
                } catch (WFSException e) {
124
                        throw new InitializeException("Impossible to retrieve the file", e);
125
                }
126
        }
127

    
128
        private ArcImsStoreParameters getWFSParameters() {
129
                return (ArcImsStoreParameters) getParameters();
130
        }
131

    
132
        /*
133
         * (non-Javadoc)
134
         * 
135
         * @see org.gvsig.fmap.dal.store.gpe.GPEStoreProvider#open()
136
         */
137
        public void open() throws OpenException {
138
                super.open();
139
                try {
140
                        List featureTypes = this.getFeatureStore().getFeatureTypes();
141
                        for (int i = 0; i < featureTypes.size(); i++) {
142
                                FeatureType featureType = (FeatureType) featureTypes.get(i);
143

    
144
                        }
145
                } catch (DataException e) {
146
                        throw new OpenException("Reading the geometry type", e);
147
                }
148
        }
149

    
150
        protected static void registerDynClass() {
151
                DynObjectManager dynman = ToolsLocator.getDynObjectManager();
152
                DynClass dynClass;
153
                if (DYNCLASS == null) {
154

    
155
                        dynClass = dynman.add(DYNCLASS_NAME);
156
                        dynClass.extend(dynman.get(FeatureStore.DYNCLASS_NAME));
157

    
158
                        DYNCLASS = dynClass;
159
                }
160

    
161
        }
162

    
163
        /*
164
         * (non-Javadoc)
165
         * 
166
         * @see
167
         * org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider#getExplorer()
168
         */
169
        public DataServerExplorer getExplorer() throws ReadException {
170
                DataManager manager = DALLocator.getDataManager();
171
                ArcImsServerExplorerParameters params;
172
                try {
173
                        params = (ArcImsServerExplorerParameters) manager
174
                                        .createServerExplorerParameters(ArcImsServerExplorer.NAME);
175
                        params.setUrl(wfsClient.getHost());
176
                        return manager.createServerExplorer(params);
177
                } catch (Exception e) {
178
                        throw new ReadException(this.getName(), e);
179
                }
180
        }
181

    
182
        /*
183
         * (non-Javadoc)
184
         * 
185
         * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#getName()
186
         */
187
        public String getName() {
188
                return NAME;
189
        }
190

    
191
        /*
192
         * (non-Javadoc)
193
         * 
194
         * @see
195
         * org.gvsig.fmap.dal.store.gpe.GPEStoreProvider#createSet(org.gvsig.fmap
196
         * .dal.feature.FeatureQuery)
197
         */
198
        @Override
199
        public FeatureSetProvider createSet(FeatureQuery query)
200
                        throws DataException {
201
                return createSet(query, null);
202
        }
203

    
204
        /*
205
         * (non-Javadoc)
206
         * 
207
         * @see
208
         * org.gvsig.fmap.dal.store.gpe.GPEStoreProvider#createSet(org.gvsig.fmap
209
         * .dal.feature.FeatureQuery, org.gvsig.fmap.dal.feature.FeatureType)
210
         */
211
        @Override
212
        public FeatureSetProvider createSet(FeatureQuery query,
213
                        FeatureType featureType) throws DataException {
214
                // If it is possible to execute a spatial query...
215
                // if (!executeSpatialQuery(query)){
216
                executeQuery();
217
                // }
218
                isOpen = false;
219
                open();
220
                return super.createSet(query, featureType);
221
        }
222

    
223
        /**
224
         * It executes a wfs Query without spatial filter
225
         * 
226
         * @throws DataException
227
         */
228
        private void executeQuery() throws DataException {
229
                try {
230
                        wfsStatus.setResultType(WFSStatus.RESULTYPE_RESULTS);
231
                        m_Fich = wfsClient.getFeature(wfsStatus, true, null);
232
                } catch (WFSException e) {
233
                        throw new InitializeException("Impossible to retrieve the file", e);
234
                }
235
        }
236

    
237
        /**
238
         * Executes a new query sending the FilterEncoding request to the server
239
         * 
240
         * @param query
241
         *            The Query to send
242
         * @throws InitializeException
243
         *             If there is a problem with the request or the parsing process
244
         */
245
        private boolean executeSpatialQuery(FeatureQuery query)
246
                        throws DataException {
247
                EvaluatorFieldsInfo fieldsInfo = query.getFilter().getFieldsInfo();
248
                String[] fields = fieldsInfo.getFieldNames();
249
                for (int i = 0; i < fields.length; i++) {
250
                        EvaluatorFieldValue[] evaluatorFieldValues = fieldsInfo
251
                                        .getFieldValues(fields[i]);
252
                        for (int j = 0; j < evaluatorFieldValues.length; j++) {
253
                                if (EvaluatorFieldValue.MATCH == evaluatorFieldValues[j]
254
                                                .getType()) {
255
                                        EvaluatorFieldValueMatch evaluatorFieldValueMatch = (EvaluatorFieldValueMatch) evaluatorFieldValues[j];
256
                                        Object area = evaluatorFieldValueMatch.getValue();
257
                                        if (area instanceof Envelope) {
258
                                                executeSpatialQueryFromEnvelope(
259
                                                                evaluatorFieldValueMatch.getFieldName(),
260
                                                                (Envelope) area);
261
                                                return true;
262
                                        } else if (area instanceof Geometry) {
263
                                                executeSpatialQueryFromGeometry(
264
                                                                evaluatorFieldValueMatch.getFieldName(),
265
                                                                (Geometry) area);
266
                                                return true;
267
                                        }
268
                                }
269
                        }
270
                }
271
                return false;
272
        }
273

    
274
        private void executeSpatialQueryFromGeometry(String the_geom,
275
                        Geometry geometry) throws DataException {
276
                wfsStatus.setFilterByArea(geometry, the_geom, projection.getAbrev(),
277
                                FilterEncoding.GEOMETRIC_OPERATOR_CONTAINS);
278
                executeSpatialQuery();
279
        }
280

    
281
        private void executeSpatialQueryFromEnvelope(String the_geom,
282
                        Envelope envelope) throws DataException {
283
                wfsStatus.setFilterByArea(envelope, the_geom, projection.getAbrev(),
284
                                FilterEncoding.GEOMETRIC_OPERATOR_CONTAINS);
285
                executeSpatialQuery();
286
        }
287

    
288
        private void executeSpatialQuery() throws DataException {
289
                try {
290
                        wfsStatus.setProtocol(OGCClientOperation.PROTOCOL_POST);
291
                        // wfsStatus.setResultType(WFSStatus.RESULTYPE_HITS);
292
                        // wfsClient.getFeature(wfsStatus, true, null);
293
                        // WFSGetFeatureRequestInformation requestInformation =
294
                        // (WFSGetFeatureRequestInformation)wfsClient.getLastWfsRequestInformation();
295
                        // WFSServiceInformation serviceInformation =
296
                        // wfsClient.getServiceInformation();
297
                        // if (requestInformation.getNumberOfFeatures() != 0){
298
                        // //TODO DefaultMaxFeatures
299
                        // if (requestInformation.getNumberOfFeatures() ==
300
                        // wfsStatus.getBuffer()){
301
                        // //Divide the space in 4 rectangles
302
                        // WFSSpatialFilterOperation spatialFilterOperation =
303
                        // wfsStatus.getFilterByArea();
304
                        //                                        
305
                        // }
306
                        // }
307
                        // If it is not possible to calculate the
308
                        wfsStatus.setResultType(WFSStatus.RESULTYPE_RESULTS);
309
                        m_Fich = wfsClient.getFeature(wfsStatus, true, null);
310
                } catch (WFSException e) {
311
                        throw new InitializeException("Impossible to retrieve the file", e);
312
                }
313
        }
314

    
315
        public Object getSourceId() {
316
                StringBuffer sourceID = new StringBuffer(getWFSParameters().getUrl());
317
                sourceID.append("_" + getWFSParameters().getFeatureType());
318
                int hash = sourceID.hashCode();
319
                if (sourceID.length() > 60) {
320
                        return sourceID.substring(0, 60) + "_" + hash;
321
                } else {
322
                        return sourceID.toString() + "_" + hash;
323
                }
324
        }
325

    
326
        protected IProjection getSRS() {
327
                if (projection == null) {
328
                        try {
329
                                wfsClient.getCapabilities(wfsStatus, false, null);
330
                                wfsClient.describeFeatureType(wfsStatus, false, null);
331
                                WFSFeature feature = (WFSFeature) wfsClient.getFeatures().get(
332
                                                wfsStatus.getFeatureName());
333
                                if (feature.getSrs().size() > 0) {
334
                                        projection = CRSFactory.getCRS(getEPSGCode((String) feature
335
                                                        .getSrs().get(0)));
336
                                }
337
                        } catch (WFSException e) {
338
                                logger.error("Error retrieving the feature type");
339
                        }
340
                }
341
                return projection;
342
        }
343

    
344
        private String getEPSGCode(String srs) {
345
                if (srs == null) {
346
                        return null;
347
                }
348
                if (srs.startsWith("urn:x-ogc:def:crs:")) {
349
                        String newString = srs.substring(srs.lastIndexOf(":") + 1, srs
350
                                        .length());
351
                        if (srs.indexOf("EPSG") > 0) {
352
                                if (newString.indexOf("EPSG") < 0) {
353
                                        newString = "EPSG:" + newString;
354
                                }
355
                        }
356
                        return newString;
357
                }
358
                if (srs.toLowerCase().startsWith("crs:")) {
359
                        return srs.substring(4, srs.length());
360
                }
361
                return srs;
362
        }
363

    
364
        protected int getGeometryType() {
365
                WFSFeature feature = (WFSFeature) wfsClient.getFeatures().get(
366
                                wfsStatus.getFeatureName());
367
                Vector field = feature.getFields();
368
                if (field.size() > 0) {
369
                        XMLElement rootElement = (XMLElement) field.get(0);
370
                        Vector children = rootElement.getChildren();
371
                        for (int i = 0; i < children.size(); i++) {
372
                                XMLElement child = (XMLElement) children.get(i);
373
                                String type = child.getEntityType().getName();
374
                                if (type != null) {
375
                                        int index = type.indexOf(":");
376
                                        if (index > -1) {
377
                                                type = type.substring(index + 1);
378
                                        }
379
                                        Object obj = xmlSchemaManager
380
                                                        .getApplicationGeometryTypeByFormat("gml", type);
381
                                        if (obj != null) {
382
                                                return (Integer) obj;
383
                                        }
384
                                }
385
                        }
386
                }
387
                return TYPES.GEOMETRY;
388
        }
389
}