Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / applications / appCatalogAndGazetteerClient / src / es / gva / cit / gazetteer / wfs / drivers / WFSServiceDriver.java @ 18432

History | View | Annotate | Download (8.63 KB)

1 16004 jpiera
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 18432 jmvivo
        public DiscoveryServiceCapabilities getCapabilities(URI uri) {
87 16004 jpiera
                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 18304 jpiera
                        client.getCapabilities(status, true, null);
99 16004 jpiera
                } catch (Exception e) {
100
                        capabilities.setServerMessage(e.toString());
101
                        return capabilities;
102 18432 jmvivo
                }
103 16004 jpiera
                setServerAnswerReady(client.getServiceInformation().name);
104
                Hashtable features = client.getFeatures();
105 18432 jmvivo
                setFeatureTypes(convertFeatureNames(features));
106
                capabilities.setServerMessage(client.getServiceInformation().name +
107 16004 jpiera
                                client.getServiceInformation().abstr);
108
                capabilities.setFeatureTypes(getFeatureTypes());
109
                return capabilities;
110 18432 jmvivo
        }
111 16004 jpiera
112
        /**
113
         * Convert the features from the remote services format to
114 18432 jmvivo
         * the gazetteer format
115 16004 jpiera
         * @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 18432 jmvivo
                return featureTypes;
130 16004 jpiera
        }
131
132
        /*
133
         * (non-Javadoc)
134
         * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#describeFeatureType(java.net.URI, java.lang.String)
135
         */
136 18432 jmvivo
        public FeatureTypeAttribute[] describeFeatureType(URI uri, String feature) {
137 16004 jpiera
                status.setFeatureName(feature);
138 18304 jpiera
                try{
139 18432 jmvivo
                        client.describeFeatureType(status, false, null);
140 18304 jpiera
                }catch (Exception e){
141
                        //Impossible to retrieve the attributes
142
                        return new FeatureTypeAttribute[0];
143
                }
144 16004 jpiera
                WFSFeature wfsFeature = (WFSFeature)client.getFeatures().get(feature);
145
                if ((wfsFeature.getSrs() != null) && (wfsFeature.getSrs().size() > 0)){
146
                        this.setProjection((String)wfsFeature.getSrs().get(0));
147
                }
148
                return covertFeatuteAttributes(wfsFeature);
149 18432 jmvivo
        }
150 16004 jpiera
151
        /*
152
         * (non-Javadoc)
153
         * @see es.gva.cit.gazetteer.drivers.AsbtractGazetteerServiceDriver#isDescribeFeatureTypeNeeded()
154
         */
155
        public boolean isDescribeFeatureTypeNeeded(){
156
                return true;
157
        }
158
159
        /**
160
         * Convert the feature attributes
161
         * @param feature
162
         * a Remote clients feature
163
         * @return
164
         * A list of attributes
165
         */
166
        private FeatureTypeAttribute[] covertFeatuteAttributes(WFSFeature feature) {
167
                XMLElement element = (XMLElement)feature.getFields().get(0);
168
                XMLComplexType type = (XMLComplexType)element.getEntityType();
169
                Vector fields = type.getAttributes();
170
                FeatureTypeAttribute[] attributes = new FeatureTypeAttribute[fields.size()];
171
                for (int i=0 ; i<fields.size(); i++){
172
                        XMLElement child = (XMLElement)fields.get(i);
173
                        attributes[i] = new FeatureTypeAttribute(child.getName(),0,false,child.getEntityType().getName());
174
                }
175
                return attributes;
176
        }
177
178
        /*
179
         * (non-Javadoc)
180
         * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#getFeature(java.net.URI, es.gva.cit.gazetteer.querys.Query)
181
         */
182 18432 jmvivo
        public Feature[] getFeature(URI uri, GazetteerQuery query) throws GMLException {
183 16004 jpiera
                //Set the filter
184
                String sQuery = getFilterSQL(query);
185
                if (sQuery != null){
186
                        status.setFilterQuery(sQuery);
187
                }
188
                status.setFields(new String[0]);
189 18304 jpiera
                try{
190 18432 jmvivo
                        File file = client.getFeature(status, false, null);
191
                        return parseOutputFile(file,query.getFieldAttribute());
192 18304 jpiera
                }catch(Exception e){
193
                        return new Feature[0];
194 18432 jmvivo
                }
195
        }
196 16004 jpiera
197
        /**
198
         * Parses the GML file
199
         * @param file
200
         * @return
201
         * @throws GMLException
202
         */
203
        protected Feature[] parseOutputFile(File file, String fieldAttribute) throws GMLException{
204
                GMLReader reader = new GMLReader(file,new GazetteerGeometriesFactory(fieldAttribute));
205
                IGMLFeaturesIterator it = reader.getFeaturesIterator();
206 18432 jmvivo
                ArrayList features = new ArrayList();
207 16004 jpiera
                while (it.hasNext()){
208
                        Object feature = it.next();
209
                        if (feature != null){
210
                                features.add(feature);
211
                        }
212 18432 jmvivo
                }
213 16004 jpiera
                Feature[] auxFeatures = new Feature[features.size()];
214
                for (int i=0 ; i<features.size() ; i++){
215
                        auxFeatures[i] = (Feature)features.get(i);
216
                }
217
                return auxFeatures;
218
        }
219
220
        /**
221
         * Creates a SQL filter to do the search
222
         * @return
223
         * A standard SQL query
224
         */
225
        protected String getFilterSQL(GazetteerQuery query){
226
                StringBuffer buffer = new StringBuffer();
227
                if ((query.getName() == null) || (query.getName().equals(""))){
228
                        return null;
229
                }
230
                if (query.getNameFilter().equals(CatalogConstants.EXACT_WORDS)){
231
                        buffer.append("(" + query.getFieldAttribute() + " = " + query.getName() + ")");
232
                }else{
233
                        String conector = null;
234
                        if (query.getNameFilter().equals(CatalogConstants.ALL_WORDS)){
235 18432 jmvivo
                                conector = "AND";
236 16004 jpiera
                        }else if (query.getNameFilter().equals(CatalogConstants.ANY_WORD)){
237
                                conector = "OR";
238
                        }
239
                        String[] words = query.getName().split(" ");
240
                        for (int i=0 ; i<words.length ; i++){
241
                                buffer.append("(" + query.getFieldAttribute() + " = " + words[i] + ")");
242
                                if (i  < words.length - 1){
243 18432 jmvivo
                                        buffer.append(" " + conector + " ");
244
                                }
245 16004 jpiera
                        }
246 18432 jmvivo
                }
247 16004 jpiera
                FilterEncoding fe = GazetteerFormatter.createFilter();
248
                fe.setQuery(buffer.toString());
249
                return fe.toString();
250
        }
251
252
        /*
253
         * (non-Javadoc)
254
         * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#isProtocolSupported(java.net.URI)
255
         */
256 18432 jmvivo
        public boolean isProtocolSupported(URI uri) {
257 16004 jpiera
                return true;
258 18432 jmvivo
        }
259 16004 jpiera
260
        /*
261
         * (non-Javadoc)
262
         * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#getDefaultPort()
263
         */
264
        public int getDefaultPort() {
265
                return 80;
266
        }
267
268
        /*
269
         * (non-Javadoc)
270
         * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#getDefaultSchema()
271
         */
272
        public String getDefaultSchema() {
273
                return "http";
274
        }
275
276
        /*
277
         * (non-Javadoc)
278
         * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#getServiceName()
279
         */
280
        public String getServiceName() {
281
                return ServerData.SERVER_SUBTYPE_GAZETTEER_WFS;
282 18432 jmvivo
        }
283 16004 jpiera
}