svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extWFS2 / src / org / gvsig / fmap / dal / store / wfs / WFSStoreProvider.java @ 36044
History | View | Annotate | Download (25.1 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 |
* 2009 {Iver T.I.} {Task}
|
26 |
*/
|
27 |
|
28 |
package org.gvsig.fmap.dal.store.wfs; |
29 |
|
30 |
import java.io.File; |
31 |
import java.io.IOException; |
32 |
import java.util.ArrayList; |
33 |
import java.util.Iterator; |
34 |
import java.util.List; |
35 |
|
36 |
import org.cresques.cts.ICoordTrans; |
37 |
import org.cresques.cts.IProjection; |
38 |
import org.slf4j.Logger; |
39 |
import org.slf4j.LoggerFactory; |
40 |
|
41 |
import org.gvsig.fmap.crs.CRSFactory; |
42 |
import org.gvsig.fmap.dal.DALLocator; |
43 |
import org.gvsig.fmap.dal.DataManager; |
44 |
import org.gvsig.fmap.dal.DataServerExplorer; |
45 |
import org.gvsig.fmap.dal.DataStoreParameters; |
46 |
import org.gvsig.fmap.dal.DataTypes; |
47 |
import org.gvsig.fmap.dal.exception.ConnectionServerException; |
48 |
import org.gvsig.fmap.dal.exception.DataException; |
49 |
import org.gvsig.fmap.dal.exception.InitializeException; |
50 |
import org.gvsig.fmap.dal.exception.OpenException; |
51 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
52 |
import org.gvsig.fmap.dal.exception.ReadException; |
53 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
54 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
55 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
56 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
57 |
import org.gvsig.fmap.dal.feature.FeatureType; |
58 |
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider; |
59 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
60 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
61 |
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider; |
62 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
63 |
import org.gvsig.fmap.dal.serverexplorer.wfs.WFSServerExplorer; |
64 |
import org.gvsig.fmap.dal.serverexplorer.wfs.WFSServerExplorerParameters; |
65 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
66 |
import org.gvsig.fmap.dal.store.gpe.GPEStoreProvider; |
67 |
import org.gvsig.fmap.geom.Geometry; |
68 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
69 |
import org.gvsig.fmap.geom.GeometryLocator; |
70 |
import org.gvsig.fmap.geom.GeometryManager; |
71 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
72 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
73 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
74 |
import org.gvsig.fmap.geom.primitive.Envelope; |
75 |
import org.gvsig.gpe.lib.api.exceptions.WriterHandlerCreationException; |
76 |
import org.gvsig.metadata.MetadataLocator; |
77 |
import org.gvsig.metadata.MetadataManager; |
78 |
import org.gvsig.metadata.exceptions.MetadataException; |
79 |
import org.gvsig.remoteclient.ogc.OGCClientOperation; |
80 |
import org.gvsig.remoteclient.utils.BoundaryBox; |
81 |
import org.gvsig.remoteclient.wfs.WFSClient; |
82 |
import org.gvsig.remoteclient.wfs.WFSFeature; |
83 |
import org.gvsig.remoteclient.wfs.WFSFeatureField; |
84 |
import org.gvsig.remoteclient.wfs.WFSStatus; |
85 |
import org.gvsig.remoteclient.wfs.exceptions.WFSException; |
86 |
import org.gvsig.remoteclient.wfs.filters.filterencoding.FilterEncoding; |
87 |
import org.gvsig.remoteclient.wfs.request.WFSGetFeatureRequestInformation; |
88 |
import org.gvsig.remoteclient.wfs.request.WFSTransactionRequestInformation; |
89 |
import org.gvsig.tools.ToolsLocator; |
90 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
91 |
import org.gvsig.tools.dynobject.DynStruct; |
92 |
import org.gvsig.tools.evaluator.EvaluatorFieldValue; |
93 |
import org.gvsig.tools.evaluator.EvaluatorFieldValueMatch; |
94 |
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo; |
95 |
import org.gvsig.xmlschema.lib.api.XMLSchemaLocator; |
96 |
import org.gvsig.xmlschema.lib.api.XMLSchemaManager; |
97 |
import org.gvsig.xmlschema.lib.api.som.IXSGeometryTypeDefinition; |
98 |
import org.gvsig.xmlschema.lib.api.som.IXSTypeDefinition; |
99 |
|
100 |
|
101 |
/**
|
102 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
|
103 |
*/
|
104 |
public class WFSStoreProvider extends AbstractFeatureStoreProvider { |
105 |
private static final Logger logger = LoggerFactory.getLogger(WFSStoreProvider.class); |
106 |
public static String NAME = "WFSStore"; |
107 |
public static String DESCRIPTION = "WFS store to load WFS resources"; |
108 |
|
109 |
private static final MetadataManager metadataManager = MetadataLocator.getMetadataManager(); |
110 |
private static final String METADATA_DEFINITION_NAME = "WFSStore"; |
111 |
private static final String METADATA_DEFINITION_DESCRIPTION = "WFSStore metadata definition"; |
112 |
|
113 |
private IProjection projection;
|
114 |
private long featureCount = -1; |
115 |
private boolean isKnownEnvelope = false; |
116 |
|
117 |
/**
|
118 |
* If the user has selected the filter by area option
|
119 |
*/
|
120 |
private boolean hasFilterByArea = false; |
121 |
|
122 |
/**
|
123 |
* GML provider used by delegation to read the GML files
|
124 |
*/
|
125 |
private GPEStoreProvider storeProviderDelegate = null; |
126 |
|
127 |
private static final DataManager dataManager = DALLocator.getDataManager(); |
128 |
private static final XMLSchemaManager xmlSchemaManager = XMLSchemaLocator.getXMLSchemaManager(); |
129 |
|
130 |
boolean isFilterByAreaSupported = true; |
131 |
|
132 |
//WFS Parameters
|
133 |
private WFSClient wfsClient;
|
134 |
private WFSStatus wfsStatus;
|
135 |
|
136 |
public WFSStoreProvider(DataStoreParameters params,
|
137 |
DataStoreProviderServices storeServices) |
138 |
throws InitializeException {
|
139 |
super(params, storeServices,
|
140 |
ToolsLocator.getDynObjectManager().createDynObject(metadataManager.getDefinition(METADATA_DEFINITION_NAME))); |
141 |
initParams(); |
142 |
try {
|
143 |
initFeatureType(); |
144 |
} catch (WFSException e) {
|
145 |
throw new InitializeException(e); |
146 |
} catch (ReadException e) {
|
147 |
throw new InitializeException(e); |
148 |
} |
149 |
} |
150 |
|
151 |
private void initParams() throws InitializeException{ |
152 |
try {
|
153 |
WFSStoreParameters wfsParameters = getWFSParameters(); |
154 |
try {
|
155 |
if (wfsParameters.getVersion() == null){ |
156 |
wfsClient = new WFSClient(wfsParameters.getUrl());
|
157 |
wfsParameters.setVersion(wfsClient.getVersion()); |
158 |
}else{
|
159 |
wfsClient = new WFSClient(wfsParameters.getUrl(), wfsParameters.getVersion());
|
160 |
} |
161 |
} catch (IOException e) { |
162 |
throw new InitializeException(e); |
163 |
} |
164 |
wfsStatus = new WFSStatus( wfsParameters.getFeatureType(),
|
165 |
wfsParameters.getFeatureNamespace()); |
166 |
wfsStatus.setNamespacePrefix(wfsParameters.getFeaturePrefix()); |
167 |
wfsStatus.setFields(wfsParameters.getFields()); |
168 |
|
169 |
//Select the filter by area
|
170 |
wfsStatus.setFilterByArea(wfsParameters.getFilterByAreaGeometry(), wfsParameters.getFilterByAreaAttribute(), |
171 |
wfsParameters.getFilterByAreaCrs(), wfsParameters.getFilterByAreaOperation()); |
172 |
this.hasFilterByArea = (wfsStatus.getFilterByArea() != null); |
173 |
|
174 |
//Select the filter by attribute
|
175 |
wfsStatus.setFilterByAttribute(wfsParameters.getFilterEncodingByAttribute()); |
176 |
|
177 |
wfsStatus.setTimeout(wfsParameters.getTimeOut()); |
178 |
wfsStatus.setMaxFeatures(wfsParameters.getMaxFeatures()); |
179 |
wfsStatus.setUserName(wfsParameters.getUser()); |
180 |
wfsStatus.setPassword(wfsParameters.getPassword()); |
181 |
|
182 |
//Setting the envelope for the layer
|
183 |
wfsClient.getCapabilities(wfsStatus, false, null); |
184 |
|
185 |
Envelope envelope = calculateFullEnvelope(); |
186 |
if (envelope != null){ |
187 |
this.setDynValue("Envelope", envelope); |
188 |
isKnownEnvelope = true;
|
189 |
} |
190 |
} catch (WFSException e) {
|
191 |
throw new InitializeException("Impossible to retrieve the file", e); |
192 |
} |
193 |
} |
194 |
|
195 |
private WFSFeature getSelectedWFSFeature() throws ReadException, WFSException{ |
196 |
WFSStoreParameters wfsParameters = getWFSParameters(); |
197 |
|
198 |
return ((WFSServerExplorer)getExplorer()).getFeatureInfo(wfsParameters.getFeatureNamespace(),
|
199 |
wfsParameters.getFeatureType()); |
200 |
} |
201 |
|
202 |
private void initFeatureType() throws WFSException, ReadException{ |
203 |
WFSFeature feature = getSelectedWFSFeature(); |
204 |
|
205 |
if (feature == null){ |
206 |
//TODO read from GML
|
207 |
throw new WFSException("It is not possible to parse the schema"); |
208 |
} |
209 |
|
210 |
projection = CRSFactory.getCRS(feature.getDefaultSRS()); |
211 |
this.setDynValue("CRS", projection); |
212 |
|
213 |
EditableFeatureType featureType = getStoreServices().createFeatureType(); |
214 |
|
215 |
for (int i=0 ; i<feature.getFieldSize() ; i++){ |
216 |
WFSFeatureField featureField = feature.getFieldAt(i); |
217 |
int dataType = DataTypes.STRING;
|
218 |
String typeName = featureField.getType();
|
219 |
EditableFeatureAttributeDescriptor attributeDescriptor = null;
|
220 |
|
221 |
IXSTypeDefinition typeDefinition = null;
|
222 |
|
223 |
if (typeName != null){ |
224 |
typeDefinition = xmlSchemaManager.getTypeDefinition(typeName); |
225 |
} |
226 |
|
227 |
if (typeDefinition != null){ |
228 |
if (typeDefinition.getDataType() != null){ |
229 |
if (typeDefinition.getDataType().getType() == DataTypes.GEOMETRY){
|
230 |
dataType = typeDefinition.getDataType().getType(); |
231 |
} |
232 |
} |
233 |
attributeDescriptor = featureType.add(featureField.getName(), dataType); |
234 |
if (typeDefinition instanceof IXSGeometryTypeDefinition){ |
235 |
if (feature.getSrs().size() > 0){ |
236 |
attributeDescriptor.setSRS(CRSFactory.getCRS((String)feature.getSrs().get(0))); |
237 |
} |
238 |
attributeDescriptor.setGeometryType(((IXSGeometryTypeDefinition)typeDefinition).getGeometryType()); |
239 |
attributeDescriptor.setGeometrySubType(SUBTYPES.GEOM3D); |
240 |
featureType.setDefaultGeometryAttributeName(featureField.getName()); |
241 |
} |
242 |
}else{
|
243 |
attributeDescriptor = featureType.add(typeName, dataType); |
244 |
} |
245 |
} |
246 |
featureType.setHasOID(true);
|
247 |
|
248 |
FeatureType defaultType = featureType.getNotEditableCopy(); |
249 |
List types = new ArrayList(1); |
250 |
types.add(defaultType); |
251 |
this.getStoreServices().setFeatureTypes(types, defaultType);
|
252 |
} |
253 |
|
254 |
private Envelope calculateFullEnvelope() {
|
255 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
256 |
|
257 |
try{
|
258 |
WFSFeature feature = getSelectedWFSFeature(); |
259 |
if (feature != null){ |
260 |
if (feature.getSrs().size() > 0){ |
261 |
String srs = feature.getDefaultSRS();
|
262 |
BoundaryBox boundaryBox = feature.getBbox(srs); |
263 |
if (boundaryBox == null) |
264 |
{ |
265 |
boundaryBox = feature.getBbox(srs); |
266 |
} |
267 |
//The projection is found
|
268 |
if (boundaryBox != null){ |
269 |
return geometryManager.createEnvelope(boundaryBox.getXmin(),
|
270 |
boundaryBox.getYmin(), boundaryBox.getXmax(), boundaryBox.getYmax(), SUBTYPES.GEOM2D); |
271 |
} |
272 |
//Check if there is a latlon envelope
|
273 |
boundaryBox = feature.getLatLonBbox(); |
274 |
if (boundaryBox != null){ |
275 |
Envelope envelope = geometryManager.createEnvelope(boundaryBox.getXmin(), |
276 |
boundaryBox.getYmin(), boundaryBox.getXmax(), boundaryBox.getYmax(), SUBTYPES.GEOM2D); |
277 |
if (!"EPSG:4326".equals(srs)){ |
278 |
IProjection sourceProjection = CRSFactory.getCRS("EPSG:4326");
|
279 |
IProjection destinationProjection = CRSFactory.getCRS(srs); |
280 |
if (destinationProjection != null){ |
281 |
ICoordTrans coordTrans = sourceProjection.getCT(destinationProjection); |
282 |
return envelope.convert(coordTrans);
|
283 |
} |
284 |
} |
285 |
} |
286 |
}else{
|
287 |
//If there is not a SRS is thrown a request for the first 500 features
|
288 |
int odlMaxFeatures = wfsStatus.getMaxFeatures();
|
289 |
wfsStatus.setMaxFeatures(500);
|
290 |
createSet(null, null); |
291 |
return storeProviderDelegate.getEnvelope();
|
292 |
} |
293 |
} |
294 |
}catch(CreateEnvelopeException e){
|
295 |
logger.error("Impossible to create an envelope", e);
|
296 |
} catch (ReadException e) {
|
297 |
logger.error("Impossible to create an envelope", e);
|
298 |
} catch (WFSException e) {
|
299 |
logger.error("Impossible to create an envelope", e);
|
300 |
} catch (DataException e) {
|
301 |
logger.error("Impossible to create an envelope", e);
|
302 |
} |
303 |
return null; |
304 |
} |
305 |
|
306 |
private WFSStoreParameters getWFSParameters() {
|
307 |
return (WFSStoreParameters) getParameters();
|
308 |
} |
309 |
|
310 |
/* (non-Javadoc)
|
311 |
* @see org.gvsig.fmap.dal.store.gpe.GPEStoreProvider#open()
|
312 |
*/
|
313 |
public void open() throws OpenException { |
314 |
try {
|
315 |
List featureTypes = this.getFeatureStore().getFeatureTypes(); |
316 |
for (int i=0 ; i<featureTypes.size() ; i++){ |
317 |
FeatureType featureType = (FeatureType)featureTypes.get(i); |
318 |
} |
319 |
} catch (DataException e) {
|
320 |
throw new OpenException("Reading the geometry type", e); |
321 |
} |
322 |
} |
323 |
|
324 |
protected static void registerMetadataDefinition() throws MetadataException { |
325 |
if (metadataManager.getDefinition(METADATA_DEFINITION_NAME) == null) { |
326 |
DynStruct metadataDefinition = metadataManager.addDefinition( |
327 |
METADATA_DEFINITION_NAME, METADATA_DEFINITION_DESCRIPTION); |
328 |
|
329 |
metadataDefinition.addDynFieldObject("Envelope").setClassOfValue(Envelope.class).setMandatory(false); |
330 |
metadataDefinition.addDynFieldObject("CRS").setClassOfValue(IProjection.class).setMandatory(true); |
331 |
|
332 |
metadataDefinition.extend(metadataManager |
333 |
.getDefinition(FeatureStore.METADATA_DEFINITION_NAME)); |
334 |
} |
335 |
} |
336 |
|
337 |
/* (non-Javadoc)
|
338 |
* @see org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider#getExplorer()
|
339 |
*/
|
340 |
public DataServerExplorer getExplorer() throws ReadException { |
341 |
DataManager manager = DALLocator.getDataManager(); |
342 |
WFSServerExplorerParameters params; |
343 |
try {
|
344 |
params = (WFSServerExplorerParameters) manager |
345 |
.createServerExplorerParameters(WFSServerExplorer.NAME); |
346 |
params.setUrl(wfsClient.getHost()); |
347 |
return manager.openServerExplorer(WFSServerExplorer.NAME,params);
|
348 |
} catch (Exception e) { |
349 |
throw new ReadException(this.getName(), e); |
350 |
} |
351 |
} |
352 |
|
353 |
public String getProviderName() { |
354 |
return NAME;
|
355 |
} |
356 |
|
357 |
public FeatureSetProvider createSet(FeatureQuery query)
|
358 |
throws DataException {
|
359 |
return createSet(query, null); |
360 |
} |
361 |
|
362 |
public FeatureSetProvider createSet(FeatureQuery query,
|
363 |
FeatureType featureType) throws DataException {
|
364 |
if ((featureCount == -1) || |
365 |
(storeProviderDelegate == null) ||
|
366 |
(featureCount >= wfsStatus.getMaxFeatures())){ |
367 |
|
368 |
File file = null; |
369 |
|
370 |
if (query == null){ |
371 |
file = executeQuery(); |
372 |
}else{
|
373 |
//If it is possible to execute a spatial query...
|
374 |
if ((query.getFilter() != null) && (!hasFilterByArea)){ |
375 |
file = executeSpatialQuery(query); |
376 |
}else{
|
377 |
file = executeQuery(); |
378 |
} |
379 |
|
380 |
} |
381 |
storeProviderDelegate = createProvider(file); |
382 |
} |
383 |
|
384 |
return storeProviderDelegate.createSet(query, featureType);
|
385 |
} |
386 |
|
387 |
/**
|
388 |
* It creates the provider that is used to retrieve features from a file.
|
389 |
* @param file
|
390 |
* the file that contains the downloaded file
|
391 |
* @return
|
392 |
* @throws InitializeException
|
393 |
* @throws ProviderNotRegisteredException
|
394 |
*/
|
395 |
private GPEStoreProvider createProvider(File file) throws InitializeException, ProviderNotRegisteredException{ |
396 |
return new GPEStoreProvider(createStoreParameters(file), getStoreServices()); |
397 |
} |
398 |
|
399 |
private DataStoreParameters createStoreParameters(File file) throws InitializeException, ProviderNotRegisteredException{ |
400 |
DataStoreParameters parameters = dataManager.createStoreParameters(GPEStoreProvider.NAME); |
401 |
|
402 |
parameters.setDynValue("File", file.getAbsolutePath());
|
403 |
parameters.setDynValue("Envelope", this.getDynValue("Envelope")); |
404 |
return parameters;
|
405 |
} |
406 |
|
407 |
/**
|
408 |
* It executes a wfs Query without spatial filter
|
409 |
* @throws DataException
|
410 |
*/
|
411 |
private File executeQuery() throws DataException{ |
412 |
try{
|
413 |
wfsStatus.setResultType(WFSStatus.RESULTYPE_RESULTS); |
414 |
if (!hasFilterByArea){
|
415 |
wfsStatus.removeFilterByArea(); |
416 |
} |
417 |
return wfsClient.getFeature(wfsStatus, true, null); |
418 |
} catch (WFSException e) {
|
419 |
throw new InitializeException("Impossible to retrieve the file", e); |
420 |
} |
421 |
} |
422 |
|
423 |
/**
|
424 |
* Executes a new query sending the FilterEncoding request to the server
|
425 |
* @param query
|
426 |
* The Query to send
|
427 |
* @throws InitializeException
|
428 |
* If there is a problem with the request or the parsing process
|
429 |
*/
|
430 |
private File executeSpatialQuery(FeatureQuery query) throws DataException{ |
431 |
EvaluatorFieldsInfo fieldsInfo = query.getFilter().getFieldsInfo(); |
432 |
String[] fields = fieldsInfo.getFieldNames(); |
433 |
for (int i=0 ; i<fields.length ; i++){ |
434 |
EvaluatorFieldValue[] evaluatorFieldValues = fieldsInfo.getFieldValues(fields[i]);
|
435 |
for (int j=0 ; j<evaluatorFieldValues.length ; j++){ |
436 |
if (EvaluatorFieldValue.MATCH == evaluatorFieldValues[j].getType()){
|
437 |
EvaluatorFieldValueMatch evaluatorFieldValueMatch = (EvaluatorFieldValueMatch)evaluatorFieldValues[j]; |
438 |
Object area = evaluatorFieldValueMatch.getValue();
|
439 |
if (area instanceof Envelope){ |
440 |
return executeSpatialQueryFromEnvelope(evaluatorFieldValueMatch.getFieldName(),
|
441 |
(Envelope)area); |
442 |
}else if (area instanceof Geometry){ |
443 |
return executeSpatialQueryFromGeometry(evaluatorFieldValueMatch.getFieldName(),
|
444 |
(Geometry)area); |
445 |
} |
446 |
} |
447 |
} |
448 |
} |
449 |
return null; |
450 |
} |
451 |
|
452 |
private File executeSpatialQueryFromGeometry(String the_geom, Geometry geometry) throws DataException{ |
453 |
wfsStatus.setFilterByArea(geometry, the_geom, projection.getAbrev(), |
454 |
FilterEncoding.GEOMETRIC_OPERATOR_CONTAINS); |
455 |
return executeSpatialQuery();
|
456 |
} |
457 |
|
458 |
private File executeSpatialQueryFromEnvelope(String the_geom, Envelope envelope) throws DataException{ |
459 |
wfsStatus.setFilterByArea(envelope, the_geom, projection.getAbrev(), |
460 |
FilterEncoding.GEOMETRIC_OPERATOR_CONTAINS); |
461 |
return executeSpatialQuery();
|
462 |
} |
463 |
|
464 |
private File executeSpatialQuery() throws DataException{ |
465 |
try {
|
466 |
wfsStatus.setProtocol(OGCClientOperation.PROTOCOL_POST); |
467 |
//If it is not possible to calculate the
|
468 |
wfsStatus.setResultType(WFSStatus.RESULTYPE_RESULTS); |
469 |
return wfsClient.getFeature(wfsStatus, true, null); |
470 |
} catch (WFSException e) {
|
471 |
throw new InitializeException("Impossible to retrieve the file", e); |
472 |
} |
473 |
} |
474 |
|
475 |
public Object getSourceId() { |
476 |
StringBuffer sourceID = new StringBuffer(getWFSParameters().getUrl()); |
477 |
sourceID.append("_" + getWFSParameters().getFeatureType());
|
478 |
int hash = sourceID.hashCode();
|
479 |
if (sourceID.length() > 60){ |
480 |
return sourceID.substring(0, 60) + "_" + hash; |
481 |
}else{
|
482 |
return sourceID.toString() + "_" + hash; |
483 |
} |
484 |
} |
485 |
|
486 |
public Object createNewOID() { |
487 |
try {
|
488 |
return String.valueOf(getFeatureCount()+1); |
489 |
} catch (DataException e) {
|
490 |
return String.valueOf(Math.random()); |
491 |
} |
492 |
} |
493 |
|
494 |
public long getFeatureCount() throws DataException { |
495 |
if (featureCount == -1){ |
496 |
wfsStatus.setResultType(WFSStatus.RESULTYPE_HITS); |
497 |
try {
|
498 |
//The filter encoding is better supported using POST
|
499 |
if (hasFilterByArea){
|
500 |
wfsStatus.setProtocol(OGCClientOperation.PROTOCOL_POST); |
501 |
}else{
|
502 |
wfsStatus.setProtocol(OGCClientOperation.PROTOCOL_GET); |
503 |
} |
504 |
File file = wfsClient.getFeature(wfsStatus, true, null); |
505 |
featureCount = ((WFSGetFeatureRequestInformation)wfsClient.getLastWfsRequestInformation()).getNumberOfFeatures(); |
506 |
//If the service doesn't support RESULTTYPE_HITS, parse the file and get the FeatureCount
|
507 |
if (featureCount == -1){ |
508 |
storeProviderDelegate = createProvider(file); |
509 |
featureCount = storeProviderDelegate.getFeatureCount(); |
510 |
} |
511 |
} catch (WFSException e) {
|
512 |
throw new ConnectionServerException("Error calculating the feature count", e); |
513 |
} |
514 |
} |
515 |
return featureCount;
|
516 |
} |
517 |
|
518 |
public int getOIDType() { |
519 |
return DataTypes.STRING;
|
520 |
} |
521 |
|
522 |
public ResourceProvider getResource() {
|
523 |
return storeProviderDelegate.getResource();
|
524 |
} |
525 |
|
526 |
protected FeatureProvider internalGetFeatureProviderByReference(
|
527 |
FeatureReferenceProviderServices reference, FeatureType featureType) |
528 |
throws DataException {
|
529 |
return storeProviderDelegate.internalGetFeatureProviderByReference(reference, featureType);
|
530 |
} |
531 |
|
532 |
public String getFullName() { |
533 |
return getWFSParameters().getUrl() + "_" + getWFSParameters().getFeatureType(); |
534 |
} |
535 |
|
536 |
public String getName() { |
537 |
return getFullName();
|
538 |
} |
539 |
|
540 |
public boolean isKnownEnvelope(){ |
541 |
return isKnownEnvelope;
|
542 |
} |
543 |
|
544 |
public boolean hasRetrievedFeaturesLimit(){ |
545 |
return true; |
546 |
} |
547 |
|
548 |
public int getRetrievedFeaturesLimit(){ |
549 |
//WFS 1.0.0 can not know where is the limit
|
550 |
if (getWFSParameters().getVersion().equals("1.0.0")){ |
551 |
return 0; |
552 |
}else{
|
553 |
return wfsClient.getServiceInformation().getMaxFeatures();
|
554 |
} |
555 |
} |
556 |
|
557 |
public void performChanges(Iterator deleteds, Iterator inserteds, |
558 |
Iterator updateds, Iterator featureTypesChanged) |
559 |
throws DataException {
|
560 |
WFSTTransactionBuilder transactionBuilder = new WFSTTransactionBuilder(wfsStatus, wfsClient);
|
561 |
|
562 |
//Create the Query
|
563 |
try {
|
564 |
//Add new features
|
565 |
while (inserteds.hasNext()){
|
566 |
transactionBuilder.addInsertFeature((FeatureProvider)inserteds.next()); |
567 |
} |
568 |
//Add deleted features
|
569 |
while (deleteds.hasNext()){
|
570 |
transactionBuilder.addDeleteFeature( |
571 |
internalGetFeatureProviderByReference((FeatureReferenceProviderServices)deleteds.next(), null).getOID().toString());;
|
572 |
} |
573 |
//Add updated features
|
574 |
while (updateds.hasNext()){
|
575 |
transactionBuilder.addUpdateFeature((FeatureProvider)updateds.next()); |
576 |
} |
577 |
} catch (WriterHandlerCreationException e) {
|
578 |
throw new WFSTTRansactionException(e); |
579 |
} catch (GeometryOperationNotSupportedException e) {
|
580 |
throw new WFSTTRansactionException(e); |
581 |
} catch (GeometryOperationException e) {
|
582 |
throw new WFSTTRansactionException(e); |
583 |
} |
584 |
|
585 |
//Send the query to the server
|
586 |
try {
|
587 |
wfsClient.transaction(wfsStatus, true, null); |
588 |
WFSTransactionRequestInformation transactionRequestInformation = (WFSTransactionRequestInformation)wfsClient.getLastWfsRequestInformation(); |
589 |
if (transactionRequestInformation.getStatus() == WFSTransactionRequestInformation.STATUS_FAILED){
|
590 |
throw new WFSTTRansactionException(transactionRequestInformation.getMessage()); |
591 |
} |
592 |
} catch (WFSException e) {
|
593 |
throw new WFSTTRansactionException(e); |
594 |
} |
595 |
} |
596 |
|
597 |
public boolean allowWrite() { |
598 |
return wfsClient.getServiceInformation().isOperationSupported("Transaction"); |
599 |
} |
600 |
|
601 |
public Envelope getEnvelope() throws DataException { |
602 |
return (Envelope) this.getDynValue("Envelope"); |
603 |
} |
604 |
|
605 |
public FeatureProvider createFeatureProvider(FeatureType type)
|
606 |
throws DataException {
|
607 |
return super.createFeatureProvider(type); |
608 |
} |
609 |
|
610 |
|
611 |
} |
612 |
|