Statistics
| Revision:

root / trunk / applications / appCatalogAndGazetteerClient / src / es / gva / cit / gazetteer / wfs / drivers / WFSServiceDriver.java @ 15558

History | View | Annotate | Download (8.51 KB)

1

    
2
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
 *
4
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 *
22
 *  Generalitat Valenciana
23
 *   Conselleria d'Infraestructures i Transport
24
 *   Av. Blasco Ib??ez, 50
25
 *   46010 VALENCIA
26
 *   SPAIN
27
 *
28
 *      +34 963862235
29
 *   gvsig@gva.es
30
 *      www.gvsig.gva.es
31
 *
32
 *    or
33
 *
34
 *   IVER T.I. S.A
35
 *   Salamanca 50
36
 *   46005 Valencia
37
 *   Spain
38
 *
39
 *   +34 963163400
40
 *   dac@iver.es
41
 */
42
package es.gva.cit.gazetteer.wfs.drivers;
43
import java.io.File;
44
import java.net.MalformedURLException;
45
import java.net.URI;
46
import java.util.ArrayList;
47
import java.util.Hashtable;
48
import java.util.Iterator;
49
import java.util.Vector;
50

    
51
import org.gvsig.remoteClient.gml.GMLReader;
52
import org.gvsig.remoteClient.gml.IGMLFeaturesIterator;
53
import org.gvsig.remoteClient.gml.exceptions.GMLException;
54
import org.gvsig.remoteClient.gml.schemas.XMLElement;
55
import org.gvsig.remoteClient.gml.types.XMLComplexType;
56
import org.gvsig.remoteClient.wfs.WFSClient;
57
import org.gvsig.remoteClient.wfs.WFSFeature;
58
import org.gvsig.remoteClient.wfs.WFSStatus;
59
import org.gvsig.remoteClient.wfs.filters.FilterEncoding;
60

    
61
import com.iver.utiles.swing.jcomboServer.ServerData;
62

    
63
import es.gva.cit.catalog.drivers.DiscoveryServiceCapabilities;
64
import es.gva.cit.catalog.utils.CatalogConstants;
65
import es.gva.cit.gazetteer.drivers.AbstractGazetteerServiceDriver;
66
import es.gva.cit.gazetteer.drivers.GazetteerCapabilities;
67
import es.gva.cit.gazetteer.querys.Feature;
68
import es.gva.cit.gazetteer.querys.FeatureType;
69
import es.gva.cit.gazetteer.querys.FeatureTypeAttribute;
70
import es.gva.cit.gazetteer.querys.GazetteerQuery;
71
import es.gva.cit.gazetteer.utils.GazetteerFormatter;
72
import es.gva.cit.gazetteer.utils.GazetteerGeometriesFactory;
73

    
74
/**
75
 * Driver for the WFS protocol
76
 * @author Jorge Piera Llodra (piera_jor@gva.es)
77
 */
78
public class WFSServiceDriver extends AbstractGazetteerServiceDriver {
79
        protected WFSClient client = null;
80
        protected WFSStatus status = null;
81

    
82
        /*
83
         * (non-Javadoc)
84
         * @see es.gva.cit.catalogClient.drivers.IDiscoveryServiceDriver#getCapabilities(java.net.URI)
85
         */
86
        public DiscoveryServiceCapabilities getCapabilities(URI uri) {        
87
                GazetteerCapabilities capabilities = new GazetteerCapabilities();
88
                String sURL = null;
89
                try {
90
                        sURL = uri.toURL().toString();
91
                } catch (MalformedURLException e) {
92
                        setServerAnswerReady("errorServerNotFound");
93
                        return null;
94
                }
95
                status = new WFSStatus(sURL);
96
                try {
97
                        client = new WFSClient(sURL);
98
                } catch (Exception e) {
99
                        capabilities.setServerMessage(e.toString());
100
                        return capabilities;
101
                }
102
                client.getCapabilities(status, true, null);
103
                setServerAnswerReady(client.getServiceInformation().name);
104
                Hashtable features = client.getFeatures();
105
                setFeatureTypes(convertFeatureNames(features));       
106
                capabilities.setServerMessage(client.getServiceInformation().name + 
107
                                client.getServiceInformation().abstr);
108
                capabilities.setFeatureTypes(getFeatureTypes());
109
                return capabilities;
110
        } 
111

    
112
        /**
113
         * Convert the features from the remote services format to
114
         * the gazetteer format 
115
         * @param features
116
         * @return
117
         */
118
        private FeatureType[] convertFeatureNames(Hashtable features) {
119
                Iterator it = features.keySet().iterator();
120
                FeatureType[] featureTypes = new FeatureType[features.size()];
121
                int i = 0;
122
                while (it.hasNext()){
123
                        String featureName = (String)it.next();
124
                        WFSFeature feature = (WFSFeature)features.get(featureName);
125
                        featureTypes[i] = new FeatureType(featureName);
126
                        featureTypes[i].setTitle(feature.getTitle());
127
                        i++;
128
                }
129
                return featureTypes;           
130
        }
131

    
132
        /*
133
         * (non-Javadoc)
134
         * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#describeFeatureType(java.net.URI, java.lang.String)
135
         */
136
        public FeatureTypeAttribute[] describeFeatureType(URI uri, String feature) {        
137
                status.setFeatureName(feature);
138
                client.describeFeatureType(status);
139
                WFSFeature wfsFeature = (WFSFeature)client.getFeatures().get(feature);
140
                if ((wfsFeature.getSrs() != null) && (wfsFeature.getSrs().size() > 0)){
141
                        this.setProjection((String)wfsFeature.getSrs().get(0));
142
                }
143
                return covertFeatuteAttributes(wfsFeature);
144
        } 
145

    
146
        /*
147
         * (non-Javadoc)
148
         * @see es.gva.cit.gazetteer.drivers.AsbtractGazetteerServiceDriver#isDescribeFeatureTypeNeeded()
149
         */
150
        public boolean isDescribeFeatureTypeNeeded(){
151
                return true;
152
        }
153

    
154
        /**
155
         * Convert the feature attributes
156
         * @param feature
157
         * a Remote clients feature
158
         * @return
159
         * A list of attributes
160
         */
161
        private FeatureTypeAttribute[] covertFeatuteAttributes(WFSFeature feature) {
162
                XMLElement element = (XMLElement)feature.getFields().get(0);
163
                XMLComplexType type = (XMLComplexType)element.getEntityType();
164
                Vector fields = type.getAttributes();
165
                FeatureTypeAttribute[] attributes = new FeatureTypeAttribute[fields.size()];
166
                for (int i=0 ; i<fields.size(); i++){
167
                        XMLElement child = (XMLElement)fields.get(i);
168
                        attributes[i] = new FeatureTypeAttribute(child.getName(),0,false,child.getEntityType().getName());
169
                }
170
                return attributes;
171
        }
172

    
173
        /*
174
         * (non-Javadoc)
175
         * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#getFeature(java.net.URI, es.gva.cit.gazetteer.querys.Query)
176
         */
177
        public Feature[] getFeature(URI uri, GazetteerQuery query) throws GMLException {        
178
                
179
                //Set the filter
180
                String sQuery = getFilterSQL(query);
181
                if (sQuery != null){
182
                        status.setFilterQuery(sQuery);
183
                }
184
                status.setFields(new String[0]);
185
                File file = client.getFeature(status);
186
                return parseOutputFile(file,query.getFieldAttribute());                 
187
        } 
188

    
189
        /**
190
         * Parses the GML file
191
         * @param file
192
         * @return
193
         * @throws GMLException
194
         */
195
        protected Feature[] parseOutputFile(File file, String fieldAttribute) throws GMLException{
196
                GMLReader reader = new GMLReader(file,new GazetteerGeometriesFactory(fieldAttribute));
197
                IGMLFeaturesIterator it = reader.getFeaturesIterator();
198
                ArrayList features = new ArrayList();                 
199
                while (it.hasNext()){
200
                        Object feature = it.next();
201
                        if (feature != null){
202
                                features.add(feature);
203
                        }
204
                } 
205
                Feature[] auxFeatures = new Feature[features.size()];
206
                for (int i=0 ; i<features.size() ; i++){
207
                        auxFeatures[i] = (Feature)features.get(i);
208
                }
209
                return auxFeatures;
210
        }
211

    
212
        /**
213
         * Creates a SQL filter to do the search
214
         * @return
215
         * A standard SQL query
216
         */
217
        protected String getFilterSQL(GazetteerQuery query){
218
                StringBuffer buffer = new StringBuffer();
219
                if ((query.getName() == null) || (query.getName().equals(""))){
220
                        return null;
221
                }
222
                if (query.getNameFilter().equals(CatalogConstants.EXACT_WORDS)){
223
                        buffer.append("(" + query.getFieldAttribute() + " = " + query.getName() + ")");
224
                }else{
225
                        String conector = null;
226
                        if (query.getNameFilter().equals(CatalogConstants.ALL_WORDS)){
227
                                conector = "AND";                    
228
                        }else if (query.getNameFilter().equals(CatalogConstants.ANY_WORD)){
229
                                conector = "OR";
230
                        }
231
                        String[] words = query.getName().split(" ");
232
                        for (int i=0 ; i<words.length ; i++){
233
                                buffer.append("(" + query.getFieldAttribute() + " = " + words[i] + ")");
234
                                if (i  < words.length - 1){
235
                                        buffer.append(" " + conector + " ");                                    
236
                                }                            
237
                        }
238
                }           
239
                FilterEncoding fe = GazetteerFormatter.createFilter();
240
                fe.setQuery(buffer.toString());
241
                return fe.toString();
242
        }
243

    
244
        /*
245
         * (non-Javadoc)
246
         * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#isProtocolSupported(java.net.URI)
247
         */
248
        public boolean isProtocolSupported(URI uri) {        
249
                return true;
250
        } 
251

    
252
        /*
253
         * (non-Javadoc)
254
         * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#getDefaultPort()
255
         */
256
        public int getDefaultPort() {
257
                return 80;
258
        }
259

    
260
        /*
261
         * (non-Javadoc)
262
         * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#getDefaultSchema()
263
         */
264
        public String getDefaultSchema() {
265
                return "http";
266
        }
267

    
268
        /*
269
         * (non-Javadoc)
270
         * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#getServiceName()
271
         */
272
        public String getServiceName() {
273
                return ServerData.SERVER_SUBTYPE_GAZETTEER_WFS;
274
        }        
275
}