Statistics
| Revision:

svn-gvsig-desktop / tmp / trunk / cliente / WorkSpace_WPSClient / extWPSCallejero / src / es / logex / gvsig / wps / wps_0_4_0 / WPSProtocolHandler0_4_0.java @ 26594

History | View | Annotate | Download (11 KB)

1
package es.logex.gvsig.wps.wps_0_4_0;
2

    
3
import java.io.File;
4
import java.net.URL;
5
import java.util.ArrayList;
6
import java.util.List;
7

    
8
import javax.xml.bind.JAXBException;
9

    
10
import net.opengeospatial.wps.impl0_4_0.OutputDefinitionType;
11

    
12
import org.apache.log4j.Logger;
13
import org.tigris.frogs.OWSException;
14
import org.tigris.frogs.commons.Xml;
15
import org.tigris.frogs.ows.CodeType;
16
import org.tigris.frogs.ows.impl_0_3_20.BoundingBox;
17
import org.tigris.frogs.plugin.wpsint.impl_0_4_0.DataInputs;
18
import org.tigris.frogs.plugin.wpsint.impl_0_4_0.DataInputsType;
19
import org.tigris.frogs.plugin.wpsint.impl_0_4_0.DataOutputs;
20
import org.tigris.frogs.plugin.wpsint.impl_0_4_0.DataOutputsType;
21
import org.tigris.frogs.plugin.wpsint.impl_0_4_0.ExecuteBoundingBoxData;
22
import org.tigris.frogs.plugin.wpsint.impl_0_4_0.ExecuteLiteralData;
23
import org.tigris.frogs.plugin.wpsint.impl_0_4_0.ExecuteRequest;
24
import org.tigris.frogs.plugin.wpsint.impl_0_4_0.ExecuteResponseType;
25
import org.tigris.frogs.plugin.wpsint.impl_0_4_0.Input;
26
import org.tigris.frogs.plugin.wpsint.impl_0_4_0.InputType;
27
import org.tigris.frogs.plugin.wpsint.impl_0_4_0.OutputType;
28
import org.tigris.frogs.plugin.wpsint.impl_0_4_0.ProcessDeclarationRunnable;
29
import org.tigris.frogs.plugin.wpsint.impl_0_4_0.DataInputsType.ValueFormChoice;
30
import org.tigris.frogs.plugin.wpsint.impl_0_4_0.client.DiscoveryConnector;
31
import org.tigris.frogs.plugin.wpsint.impl_0_4_0.client.DiscoveryRequest;
32
import org.w3c.dom.Document;
33

    
34

    
35
import es.logex.gvsig.fmap.drivers.WPSException;
36
import es.logex.gvsig.wps.client.WPSProcess;
37
import es.logex.gvsig.wps.client.WPSProtocolHandler;
38

    
39
public class WPSProtocolHandler0_4_0 extends WPSProtocolHandler {
40

    
41
        private static Logger logger = Logger
42
                        .getLogger(WPSProtocolHandler0_4_0.class.getName());
43
        private DiscoveryConnector discoveryConnector;
44
        ProcessDeclarationRunnable process = null;
45
        DiscoveryRequest discoveryRequest;
46

    
47
        @Override
48
        public boolean parseCapabilities(File f) {
49
                // TODO Auto-generated method stub
50
                return true;
51
        }
52

    
53
        @Override
54
        public Document describeAllProcess() {
55
                // TODO Auto-generated method stub
56
                return null;
57
        }
58

    
59
        @Override
60
        public Document describeProcess(String processName) {
61
                // TODO Auto-generated method stub
62
                return null;
63
        }
64

    
65
        private boolean createProcess(String processName) {
66

    
67
                try {
68

    
69
                        logger.debug("Crear Proceso de nombre " + processName);
70
                        process = discoveryConnector.getProcessDescriptor(processName);
71

    
72
                        if (process != null) {
73
                                logger.debug("Crear Proceso de nombre " + processName);
74
                        } else {
75
                                logger.debug("El proceso no existe " + processName);
76
                                return false;
77
                        }
78

    
79
                        discoveryRequest = discoveryConnector
80
                                        .PrepareExecuteRequest(process);
81
                        
82
                        logger.debug("Preparado el proceso" + process.getProcessName()
83
                                        + " para ser ejecutado");
84
                } catch (Throwable the) {
85
                        the.printStackTrace();
86
                        return false;
87
                }
88
                return true;
89

    
90
        }
91

    
92
        @Override
93
        public void getCapabilites() throws OWSException, JAXBException, Throwable {
94

    
95
                try {
96

    
97
                        logger.info("New DiscoveryConnector");
98

    
99
                        discoveryConnector = new DiscoveryConnector();
100
                        discoveryConnector.StartDiscoveryFromURL(this.host);
101

    
102
                        this.serviceIdentification = discoveryConnector
103
                                        .getServiceIdentification();
104
                        this.serviceProvider = discoveryConnector.getServiceProvider();
105

    
106
                        logger.info("Discovered Server :" + this.host);
107
                } catch (JAXBException jex) {
108
                        logger.error("JAXBException: " + jex.getMessage());
109
                        throw jex;
110
                } catch (OWSException owsex) {
111
                        logger.error("OWSException : " + owsex.getMessage());
112
                        throw owsex;
113
                } catch (Throwable thex) {
114
                        logger.error("General Exception: " + thex.getMessage());
115
                        throw thex;
116
                }
117

    
118
        }
119

    
120
        @Override
121
        public Document getWSLD() {
122
                // TODO Auto-generated method stub
123
                return null;
124
        }
125

    
126
        public String getVersion() {
127
                return discoveryConnector.getVersion();
128
        }
129

    
130
        @SuppressWarnings("finally")
131
        public List<String> executeProcessGetCandidates(String municipio, String calle, String numero) {
132
                List<String> lista = new ArrayList<String>();
133
                try {
134
                        
135
                        //TODO: Esto se puede reutilizar
136
                        if(!createProcess("GetCandidates")){
137
                                throw new WPSException("Error obteniendo el proceso GetMap");
138
                        }
139
                        
140
                    ExecuteRequest execRequest = discoveryRequest.getExecuteRequest();
141
                        
142
                    DataInputs[] wpsDataInputs = new org.tigris.frogs.plugin.wpsint.impl_0_4_0.DataInputs[ process.getInputs().length ];
143

    
144
                for (int index=0; index<process.getInputs().length; index++) {
145
                        org.tigris.frogs.plugin.wpsint.impl_0_4_0.Input input =
146
                                (Input)process.getInputs()[index];
147
                
148
                        if ( input != null ) {
149
                                        org.tigris.frogs.plugin.wpsint.impl_0_4_0.DataInputs wpsDataInput =
150
                                                wpsDataInputs[ index ] =
151
                                                        DiscoveryConnector.CreateDataInput( input );
152

    
153
                                        DiscoveryConnector.CreateInputFormChoice(input,wpsDataInput,process.getStoreSupported());
154
                                }
155
                }
156
        
157
                        execRequest.setInputs( wpsDataInputs );
158
                        ValueFormChoice value;
159
                        for (int i = 0; i < wpsDataInputs.length; i++) {
160
                                value = setParameterValueGetCandidates(wpsDataInputs[i].getIdentifier(), municipio,
161
                                                calle, numero);
162
                                ((DataInputs) wpsDataInputs[i]).setInputFormChoice(value);
163
                                
164
                        }
165

    
166
                        execRequest.setInputs(wpsDataInputs);
167
                        
168
                        executeRequest(execRequest);
169

    
170
                        // debemos rellenar la lista de candidatos.
171
                        if (process.isProcessSucceeded()) {
172
                                // OutputType[] out = process.getOutputs();
173
                                ExecuteResponseType exResponse = discoveryRequest
174
                                                .getExecuteResponse();
175
                                DataOutputsType[] outputs = exResponse.getDataOutputs();
176

    
177
                                for (DataOutputsType dOut : outputs) {
178
                                        org.tigris.frogs.plugin.wpsint.impl_0_4_0.DataOutputsType.ValueFormChoice outValue = 
179
                                                ((DataOutputs) dOut).getOutputFormChoice();
180
                                        lista.add((String)outValue.getComplexDataOr().getAnyValue()[0]);
181
                                }
182
                        } 
183

    
184
                } catch (Throwable the) {
185
                        the.printStackTrace();
186
                        return null;
187
                } finally {
188
                        return lista;
189
                }
190

    
191
        }
192
        
193
        private ValueFormChoice setParameterValueGetCandidates(CodeType identifier,
194
                        String municipio, String calle, String numero) throws Exception {
195
                
196
                String paramString = null;
197
                ValueFormChoice value = DataInputs.newValueFormChoiceImpl();
198

    
199
                if (identifier.getValue().toUpperCase().equals("MUNICIPIO"))
200
                        paramString = municipio;
201
                else {
202
                        if (identifier.getValue().toUpperCase().equals("CALLE")){
203
                                paramString = calle;
204
                        } else {
205
                                if (identifier.getValue().toUpperCase().equals("NUMERO")) {
206
                                        paramString = numero;
207
                                }
208
                        }
209
                }
210
                /*if (paramString.equals(null)) {
211
                        throw new Exception("No se encuentra el parámetro " + identifier.getValue().toUpperCase() + " en el proceso" );
212
                }*/
213
                ExecuteLiteralData literalData = new ExecuteLiteralData(paramString,
214
                                null, null);
215
                value.setLiteralDataOr(literalData);
216
                
217
                return value;
218
                
219
        }
220

    
221
        public File executeProcessGetLayer(BoundingBox bbox, String layerName,
222
                        String detailLevel) {
223

    
224
                try {
225
                        if(createProcess("GetMap")==false)
226
                        {// el nombre del proceso debería estar
227
                                                                                // determinado según la versión del
228
                                                                                // protocolo de callejeros.
229
                                throw new WPSException("Error obteniendo el proceso GetMap");
230
                        }
231
                    ExecuteRequest execRequest = discoveryRequest.getExecuteRequest();
232
                        
233
                    DataInputs[] wpsDataInputs = new org.tigris.frogs.plugin.wpsint.impl_0_4_0.DataInputs[ process.getInputs().length ];
234

    
235
                for (int index=0; index<process.getInputs().length; index++) {
236
                        org.tigris.frogs.plugin.wpsint.impl_0_4_0.Input input =
237
                                (Input)process.getInputs()[index];
238
                
239
                        if ( input != null ) {
240
                                        org.tigris.frogs.plugin.wpsint.impl_0_4_0.DataInputs wpsDataInput =
241
                                                wpsDataInputs[ index ] =
242
                                                        DiscoveryConnector.CreateDataInput( input );
243

    
244
                                        DiscoveryConnector.CreateInputFormChoice(input,wpsDataInput,process.getStoreSupported());
245
                                }
246
                }
247
        
248
                        execRequest.setInputs( wpsDataInputs );
249
                    
250
                        ValueFormChoice value;
251
                        for (int i = 0; i < wpsDataInputs.length; i++) {
252
                                value = setParameterValue(wpsDataInputs[i].getIdentifier(), bbox,
253
                                                layerName, detailLevel);
254
                                ((DataInputs) wpsDataInputs[i]).setInputFormChoice(value);
255
                                
256
                        }
257

    
258
                        execRequest.setInputs(wpsDataInputs);
259
                        
260
                        executeRequest(execRequest);
261

    
262
                        // debemos recuperar la url dónde está el gml y bajarlo.
263
                        if (process.isProcessSucceeded()) {
264
                                // OutputType[] out = process.getOutputs();
265
                                ExecuteResponseType exResponse = discoveryRequest
266
                                                .getExecuteResponse();
267
                                DataOutputsType[] outputs = exResponse.getDataOutputs();
268

    
269
                                
270
                                org.tigris.frogs.plugin.wpsint.impl_0_4_0.DataOutputsType.ValueFormChoice outValue = ((DataOutputs) outputs[0])
271
                                                .getOutputFormChoice();
272

    
273
                                URL urlFile = new URL(outValue.getComplexValueReferenceOr()
274
                                                .getReference());
275
                        
276
                                String fileName = urlFile.toString();
277
                                int last = fileName.lastIndexOf('_');
278
                                if (last >= 0 && last < fileName.length() - 1) {
279
                                        fileName = urlFile.toString().substring(last + 1);
280
                                }
281
                                
282
                                File file = org.gvsig.remoteClient.utils.Utilities
283
                                                .downloadFile(urlFile, fileName, null);
284

    
285
                                return file;
286

    
287
                        } else {
288
                                return null;
289
                        }
290

    
291
                } catch (Throwable the) {
292
                        the.printStackTrace();
293
                        return null;
294
                }
295

    
296
        }
297

    
298
        private ValueFormChoice setParameterValue(CodeType identifier,
299
                        BoundingBox bbox, String layerName, String detailLevel) {
300
                // ,, String layerName, String detailLevel) {
301

    
302
                ValueFormChoice value = DataInputs.newValueFormChoiceImpl();
303

    
304
                if (process.isBoundingBoxExpected(identifier.getValue())) {
305
                        // BoundingBox
306

    
307
                        ExecuteBoundingBoxData box = new ExecuteBoundingBoxData();
308
                        box.setLowerCorner(bbox.getLowerCorner());
309
                        box.setUpperCorner(bbox.getUpperCorner());
310
                        box.setCrsAttribute(bbox.getCrsAttribute());
311

    
312
                        value.setBoundingBoxDataOr(box);
313
                } else if (process.isLiteralDataExpected(identifier.getValue())) {
314
                        String paramString ;
315
                        if (identifier.getValue().equalsIgnoreCase("detailLevel"))
316
                                paramString = detailLevel;
317
                        else
318
                                paramString = layerName;
319
                        
320
                        ExecuteLiteralData literalData = new ExecuteLiteralData(paramString,
321
                                        null, null);
322
                        value.setLiteralDataOr(literalData);
323
                }
324

    
325
                /*
326
                 * if (identifier.getClass() == ExecuteBoundingBoxData.class) {
327
                 * 
328
                 *  } else { if (identifier.getClass() == ExecuteLiteralData.class) {
329
                 * 
330
                 * if (identifier.getValue() == "layerName") { ExecuteLiteralData layer=
331
                 * new ExecuteLiteralData(layerName,null,null);
332
                 * value.setLiteralDataOr(layer); }
333
                 * 
334
                 * if (identifier.getValue() == "detailLevel") { ExecuteLiteralData
335
                 * layer= new ExecuteLiteralData(detailLevel,null,null);
336
                 * value.setLiteralDataOr(layer); } }
337
                 *  }
338
                 */
339

    
340
                return value;
341
        }
342

    
343
        private boolean executeRequest(ExecuteRequest execRequest) {
344

    
345
                discoveryRequest.setExecuteRequest(execRequest);
346

    
347
                // discoveryRequest.StartTask();
348
                discoveryRequest.run();
349
                Throwable ex = discoveryRequest.getLastExceptionOccured();
350
                if (ex != null) {
351
                        ex.printStackTrace();
352
                        return false;
353
                }
354

    
355
                return true;
356
        }
357

    
358
        @Override
359
        public boolean executeProcess(String processName) {
360
                // TODO Auto-generated method stub
361
                return false;
362
        }
363

    
364

    
365

    
366
        
367
        public DiscoveryConnector getDiscoveryConnector() {
368
                return discoveryConnector;
369
        }
370

    
371
}