Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.remoteclient / src / main / java / org / gvsig / remoteclient / wfs / WFSStatus.java @ 40596

History | View | Annotate | Download (14 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.remoteclient.wfs;
25

    
26
import java.awt.geom.Rectangle2D;
27
import java.util.ArrayList;
28

    
29
import org.gvsig.compat.CompatLocator;
30
import org.gvsig.compat.lang.StringUtils;
31
import org.gvsig.fmap.geom.Geometry;
32
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
33
import org.gvsig.fmap.geom.GeometryLocator;
34
import org.gvsig.fmap.geom.GeometryManager;
35
import org.gvsig.fmap.geom.exception.CreateGeometryException;
36
import org.gvsig.fmap.geom.primitive.Envelope;
37
import org.gvsig.fmap.geom.primitive.GeneralPathX;
38
import org.gvsig.remoteclient.RemoteClientStatus;
39
import org.gvsig.remoteclient.wfs.edition.WFSTTransaction;
40
import org.gvsig.remoteclient.wfs.filters.AbstractFilter;
41
import org.gvsig.remoteclient.wfs.filters.filterencoding.FilterEncoding;
42
import org.gvsig.remoteclient.wfs.filters.operations.WFSEnvelopeFilterOperation;
43
import org.gvsig.remoteclient.wfs.filters.operations.WFSGeometryFilterOperation;
44
import org.gvsig.remoteclient.wfs.filters.operations.WFSSpatialFilterOperation;
45

    
46
/**
47
 * The status of the current WFS connection
48
 * 
49
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
50
 * @see <a href="http://www.opengeospatial.org/standards/wfs">OpenGIS Web Feature Service (WFS) Implementation Specification</a>
51
 */
52
public class WFSStatus extends RemoteClientStatus{
53
    private static final StringUtils stringUtils = CompatLocator.getStringUtils();
54
    
55
    /**
56
         *  The optional resultType attribute is used to control how 
57
         *  a web feature service responds to a GetFeature request.
58
         *  The possible values for the attribute are defined in 
59
         *  some constants in this class.
60
         */
61
        private int resultType = RESULTYPE_RESULTS;
62
        
63
        /**
64
         * The default value results indicates that a web feature 
65
         * service should generate a complete response that 
66
         * contains all the features that satisfy the request.
67
         */
68
        public static final int RESULTYPE_RESULTS = 0;
69
        
70
        /**
71
         * The value hits indicates that a web feature service should process the
72
         * GetFeature request and rather than return the entire result set, 
73
         * it should simply indicate the number of feature instance of 
74
         * the requested feature type(s) that satisfy the request.
75
         */
76
        public static final int RESULTYPE_HITS = 1;
77
                        
78
        static final String LOCKACTION_ALL = "ALL"; 
79
        static final String LOCKACTION_SOME = "SOME"; 
80
        //WFS attributes
81
        private String featureName = null;
82
        private String namespacePrefix = null;
83
        private String namespaceLocation = null;
84
        private String[] fields = null;
85
        private String onlineResource = null;
86
        private Integer timeout = new Integer(10000);
87
        private Integer maxFeatures = new Integer(100);        
88
        private String filterByAttribute = null;
89
        private WFSSpatialFilterOperation filterByArea = null;
90
        
91

    
92
        //Geometry manager
93
        private static GeometryManager geometryManager = GeometryLocator.getGeometryManager();
94
        
95
        //WFS-T LockFeature attributes
96
        private String userName = null;
97
        private String password = null;
98
        private ArrayList featuresToLock = new ArrayList();
99
        private ArrayList featuresToLockPropertieName = new ArrayList();
100
        private ArrayList featuresToLockPropertieValue = new ArrayList();
101
        private ArrayList featuresLocked = new ArrayList();
102
        private int expiry = -1;
103
        private String lockAction = null;
104
        private Rectangle2D lockedArea = null;
105
        private String lockedAreaProperty = null;
106
        
107
        //WFS-T Transaction
108
        private WFSTTransaction transaction = null;
109

    
110
        public WFSStatus(String featureName){
111
                this(featureName, null);
112
        }
113

    
114
        public WFSStatus(String featureName, String namespace){
115
                this.featureName = featureName;
116
                this.namespaceLocation = namespace;
117
                setFeatureName(featureName);
118
                lockAction = LOCKACTION_ALL;
119
                featuresLocked = new ArrayList();
120
                featuresLocked = new ArrayList();
121
                featuresToLockPropertieName = new ArrayList();
122
                featuresToLockPropertieValue = new ArrayList();                
123
        }
124

    
125
        /**
126
         * @return Returns the buffer.
127
         */
128
        public Integer getMaxFeatures() {
129
                return maxFeatures;
130
        }
131

    
132

    
133
        /**
134
         * @param buffer The buffer to set.
135
         */
136
        public void setMaxFeatures(Integer buffer) {
137
                if (buffer != null){
138
                        this.maxFeatures = buffer;
139
                }
140
        }
141

    
142

    
143
        /**
144
         * @return Returns the featureName.
145
         */
146
        public String getFeatureName() {
147
                return featureName;
148
        }
149
        
150
        public String getFeatureFullName(){
151
            if (namespacePrefix == null){
152
                return featureName;
153
            }else{
154
                return namespacePrefix + ":" + featureName;
155
            }
156
        }
157

    
158

    
159
        /**
160
         * @param featureName The featureName to set.
161
         */
162
        public void setFeatureName(String featureName) {
163
            if (featureName != null){
164
            int index = featureName.indexOf(":");
165
            if (index>0){
166
                this.featureName = featureName.substring(index+1, featureName.length());
167
                namespacePrefix = featureName.substring(0, index);
168
            }else{
169
                this.featureName = featureName;
170
            }
171
        }           
172
        }
173

    
174
        /**
175
         * @return Returns the fields.
176
         */
177
        public String[] getFields() {
178
                if (fields == null){
179
                        fields = new String[0];
180
                }
181
                return fields;
182
        }
183

    
184

    
185
        /**
186
         * @param fields The fields to set.
187
         */
188
        public void setFields(String[] fields) {
189
                this.fields = fields;
190
        }
191

    
192
        public void setFields(String fields) {
193
                if (fields == null){
194
                        return;
195
                }
196
                this.fields = stringUtils.split(fields, ",");                
197
        }
198

    
199

    
200
        /**
201
         * @return Returns the password.
202
         */
203
        public String getPassword() {
204
                return password;
205
        }
206

    
207

    
208
        /**
209
         * @param password The password to set.
210
         */
211
        public void setPassword(String password) {
212
                this.password = password;
213
        }
214

    
215

    
216
        /**
217
         * @return Returns the timeout.
218
         */
219
        public Integer getTimeout() {
220
                return timeout;
221
        }
222

    
223

    
224
        /**
225
         * @param timeout The timeout to set.
226
         */
227
        public void setTimeout(Integer timeout) {
228
                if (timeout != null){
229
                        this.timeout = timeout;
230
                }
231
        }
232

    
233

    
234
        /**
235
         * @return Returns the userName.
236
         */
237
        public String getUserName() {
238
                return userName;
239
        }
240

    
241

    
242
        /**
243
         * @param userName The userName to set.
244
         */
245
        public void setUserName(String userName) {
246
                this.userName = userName;
247
        }
248

    
249
        public String getOnlineResource() {
250
                return onlineResource;
251
        }
252

    
253

    
254
        public void setOnlineResource(String url) {
255
                onlineResource = url;
256
        }
257

    
258
        /**
259
         * @return Returns the filterQuery.
260
         */
261
        public String getFilterByAttribute() {
262
                return filterByAttribute;
263
        }
264

    
265
        /**
266
         * @param filterQuery The filterQuery to set.
267
         */
268
        public void setFilterByAttribute(String filterQuery) {
269
                this.filterByAttribute = filterQuery;
270
        }        
271

    
272
        /**
273
         * @param filterByArea 
274
         * the filterByArea to set
275
         * @param operation
276
         * The geometric operation to apply. It has to be a value from 
277
         * {@link AbstractFilter}
278
         */
279
        public void setFilterByArea(Geometry geometry, String attributeName, String srs, Integer operation) {
280
                if (geometry == null){
281
                        return;
282
                }
283
                if (attributeName == null){
284
                        attributeName = "the_geom";
285
                }
286
                if (operation == null){
287
                        operation = new Integer(AbstractFilter.GEOMETRIC_OPERATOR_INTERSECT);
288
                }
289
                //TODO add a manager to register this
290
                this.filterByArea = new WFSGeometryFilterOperation(geometry,
291
                                operation.intValue(),
292
                                attributeName, 
293
                                getNamespacePrefix(),
294
                                getNamespaceLocation(),
295
                                srs);                                
296
        }        
297
        
298
        public void removeFilterByArea(){
299
            this.filterByArea = null;
300
        }
301
        
302
        public void removeFilterByAttribute(){
303
            this.filterByAttribute = null;
304
        }
305
        
306
        public void removeAllFilters(){
307
            removeFilterByArea();
308
            removeFilterByAttribute();
309
        }
310
        
311
        /**
312
         * @param filterByArea 
313
         * the filterByArea to set
314
         * @param operation
315
         * The geometric operation to apply. It has to be a value from 
316
         * {@link AbstractFilter}
317
         */
318
        public void setFilterByArea(Envelope envelope, String attributeName, String srs, Integer operation) {
319
                if (envelope == null){
320
                        return;
321
                }
322
                if (attributeName == null){
323
                        attributeName = "the_geom";
324
                }
325
                if (operation == null){
326
                        operation = new Integer(AbstractFilter.GEOMETRIC_OPERATOR_INTERSECT);
327
                }
328
                this.filterByArea = new WFSEnvelopeFilterOperation(envelope,
329
                                operation.intValue(),
330
                                attributeName, 
331
                                getNamespacePrefix(),
332
                                getNamespaceLocation(),
333
                                srs);                                
334
        }        
335
        
336
        /**
337
         * @return the filterByArea
338
         */
339
        public WFSSpatialFilterOperation getFilterByArea() {
340
                return filterByArea;
341
        }
342

    
343
        /**
344
         * @return the filterQueryLocked
345
         */
346
        public String getFilterQueryLocked() {
347
                FilterEncoding filter = new FilterEncoding(this);
348
                filter.setQualified(true);
349
                filter.setNamepacePrefix(null);
350
                filter.setHasBlankSpaces(false);
351
                return getFilterQueryLocked(filter);        
352
        }
353

    
354
        /**
355
         * @return the filterQueryLocked
356
         */
357
        public String getFilterQueryLockedPost() {
358
                FilterEncoding filter = new FilterEncoding(this);
359
                filter.setQualified(true);
360
                return getFilterQueryLocked(filter);
361
        }
362

    
363
        /**
364
         * Create a filter encoding request
365
         * @param filter
366
         * @return
367
         */
368
        private String getFilterQueryLocked(FilterEncoding filter){
369
                if ((featuresToLock.size() == 0) && 
370
                                (getLockedArea() == null) &&
371
                                (featuresToLockPropertieName.size() == 0)){
372
                        return null;
373
                }
374
                for (int i=0 ; i<featuresToLock.size() ; i++){
375
                        filter.addFeatureById(featuresToLock.get(i));
376
                }
377
                if (featuresToLockPropertieName.size() > 0){
378
                        for (int i=0 ; i<featuresToLockPropertieName.size() ; i++){
379
                                filter.addAndClause((String)featuresToLockPropertieName.get(i),
380
                                                (String)featuresToLockPropertieValue.get(i));                                                
381
                        }                                
382
                }
383
                if (lockedArea != null){
384
                        GeneralPathX generalPath = new GeneralPathX();
385
                        generalPath.moveTo(lockedArea.getMinX(), lockedArea.getMinY());
386
                        generalPath.lineTo(lockedArea.getMaxX(), lockedArea.getMinY());
387
                        generalPath.lineTo(lockedArea.getMaxX(), lockedArea.getMaxY());
388
                        generalPath.lineTo(lockedArea.getMinX(), lockedArea.getMaxY());
389
                        generalPath.lineTo(lockedArea.getMinX(), lockedArea.getMinY());
390
                        try {
391
                                filter.clearSpatialFilters();
392
                                filter.addSpatialFilter(geometryManager.createSurface(generalPath, SUBTYPES.GEOM2D),
393
                                                lockedAreaProperty, 
394
                                                getNamespacePrefix(),
395
                                                getNamespaceLocation(),
396
                                                getSrs(), 
397
                                                AbstractFilter.BBOX_ENCLOSES);
398
                        } catch (CreateGeometryException e) {
399
                                return null;
400
                        }
401
                        
402
                }
403
                return filter.toString();        
404
        }
405

    
406
        /**
407
         * @return the expiry
408
         */
409
        public int getExpiry() {
410
                return expiry;
411
        }
412

    
413
        /**
414
         * @param expiry the expiry to set
415
         */
416
        public void setExpiry(int expiry) {
417
                this.expiry = expiry;
418
        }
419

    
420
        /**
421
         * @return the lockAction
422
         */
423
        public String getLockAction() {
424
                return lockAction;
425
        }
426

    
427
        /**
428
         * Set the lock action to all
429
         */
430
        public void setLockActionToAll() {
431
                lockAction = LOCKACTION_ALL;
432
        }
433

    
434
        /**
435
         * Set teh lock action to some
436
         */
437
        public void setLockActionToSome() {
438
                lockAction = LOCKACTION_SOME;
439
        }
440
        
441
        public WFSTTransaction createNewTransaction(){
442
            transaction = new WFSTTransaction(this);
443
            return transaction;
444
        }
445
        
446
        public WFSTTransaction getTransaction(){
447
            return transaction;
448
        }
449

    
450
        /**
451
         * @return the neumber of features blocked
452
         */
453
        public int getFeaturesToLockSize() {
454
                return featuresToLock.size();
455
        }
456

    
457
        /**
458
         * Gets an identifier of a feature that is blocked
459
         * @param i
460
         * The id position
461
         * @return
462
         * The Id
463
         */
464
        public String getFeatureToLockAt(int i){
465
                if (i>featuresToLock.size()){
466
                        return null;
467
                }
468
                return (String)featuresToLock.get(i);
469
        }
470

    
471
        /**
472
         * Remove all the features to lock
473
         */
474
        public void removeFeaturesToLock() {
475
                featuresToLock.clear();
476
                featuresLocked.clear();
477
                featuresToLockPropertieName.clear();
478
                featuresToLockPropertieValue.clear();
479
        }
480

    
481
        /**
482
         * Adds a new feature locked
483
         * @param idFeature
484
         * the feature id
485
         */
486
        public void addFeatureToLock(String idFeature) {
487
                featuresToLock.add(idFeature);
488
        }
489

    
490
        /**
491
         * Adds a new feature locked
492
         * @param idFeature
493
         * the feature id
494
         */
495
        public void addFeatureToLock(String propertyName, String propertyValue){
496
                featuresToLockPropertieName.add(propertyName);
497
                featuresToLockPropertieValue.add(propertyValue);
498
        }
499

    
500
        /**
501
         * @return the idsLocked size
502
         */
503
        public int getFeaturesLocked() {
504
                return featuresLocked.size();
505
        }
506

    
507
        /**
508
         * Gets an identifier tha is blocked
509
         * @param i
510
         * The id position
511
         * @return
512
         * The Id
513
         */
514
        public String getFeatureLockedAt(int i){
515
                if (i>featuresLocked.size()){
516
                        return null;
517
                }
518
                return (String)featuresLocked.get(i);
519
        }
520

    
521
        /**
522
         * Adds a new Id
523
         * @param idLocked
524
         * the idLocked to add
525
         */
526
        public void addFeatureLocked(String lockId) {
527
                featuresLocked.add(lockId);
528
        }
529

    
530
        /**
531
         * @return the lockedArea
532
         */
533
        public Rectangle2D getLockedArea() {
534
                return lockedArea;
535
        }
536

    
537
        /**
538
         * @param lockedArea the lockedArea to set
539
         */
540
        public void setLockedArea(Rectangle2D lockedArea, String lockedAreaProperty) {
541
                this.lockedArea = lockedArea;
542
                this.lockedAreaProperty = lockedAreaProperty;
543
        }
544

    
545
        /**
546
         * @return the namespace prefix
547
         */
548
        public String getNamespacePrefix(){
549
                return namespacePrefix;
550
        }
551

    
552
        /**
553
         * @return the namespace URL
554
         */
555
        public String getNamespaceLocation(){
556
                return namespaceLocation;
557
        }
558

    
559
        /**
560
         * @param namespaceprefix the namespaceprefix to set
561
         */
562
        public void setNamespacePrefix(String namespacePrefix) {
563
                this.namespacePrefix = namespacePrefix;
564
        }
565

    
566
        /**
567
         * @param namespace the namespace to set
568
         */
569
        public void setNamespaceLocation(String namespaceLocation) {
570
                this.namespaceLocation = namespaceLocation;                
571
        }
572

    
573
        /**
574
         * @return the resultType
575
         */
576
        public int getResultType() {
577
                return resultType;
578
        }
579

    
580
        /**
581
         * @param resultType the resultType to set
582
         */
583
        public void setResultType(int resultType) {
584
                this.resultType = resultType;
585
        }
586
}