Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / feature / impl / DefaultFeatureQuery.java @ 42795

History | View | Annotate | Download (15 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.fmap.dal.feature.impl;
25

    
26
import java.util.ArrayList;
27
import java.util.HashMap;
28
import java.util.List;
29
import java.util.Map;
30

    
31
import org.gvsig.fmap.dal.feature.FeatureQuery;
32
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
33
import org.gvsig.fmap.dal.feature.FeatureType;
34
import org.gvsig.tools.ToolsLocator;
35
import org.gvsig.tools.dynobject.DynStruct;
36
import org.gvsig.tools.evaluator.AndEvaluator;
37
import org.gvsig.tools.evaluator.Evaluator;
38
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo;
39
import org.gvsig.tools.persistence.PersistentState;
40
import org.gvsig.tools.persistence.exception.PersistenceException;
41

    
42
/**
43
 * Defines the properties of a collection of Features, as a result of a query
44
 * through a FeatureStore.
45
 * <p>
46
 * A FeatureQuery is always defined by a FeatureType, or by the list of
47
 * attribute names of the FeatureStore to return.
48
 * </p>
49
 * <p>
50
 * The filter allows to select Features whose properties have values with the
51
 * characteristics defined by the filter.
52
 * </p>
53
 * <p>
54
 * The order is used to set the order of the result FeatureCollection, based on
55
 * the values of the properties of the Features.
56
 * </p>
57
 * <p>
58
 * The scale parameter can be used by the FeatureStore as a hint about the
59
 * quality or resolution of the data needed to view or operate with the data
60
 * returned. As an example, the FeatureStore may use the scale to return only a
61
 * representative subset of the data, or maybe to return Features with less
62
 * detail, like a point or a line instead of a polygon.
63
 * </p>
64
 * <p>
65
 * If an implementation of FeatureStore is able to get other parameters to
66
 * customize the behavior of the getDataCollection methods, there is an option
67
 * to set more parameters through the setAttribute method.
68
 * </p>
69
 * 
70
 * @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
71
 */
72
public class DefaultFeatureQuery implements FeatureQuery {
73

    
74
    public static final String SCALE_PARAM_NAME = "Scale";
75

    
76
    private Map queryParameters = new HashMap();
77

    
78
    private String featureTypeId = null;
79

    
80
    private List attributeNames = new ArrayList();
81

    
82
    private List constantsAttributeNames = new ArrayList();
83

    
84
    private Evaluator filter;
85

    
86
    /**
87
     * This boolean value is used to know if the current filter
88
     * has been added using the {@link FeatureQuery#addFilter(Evaluator)}
89
     * method or the {@link FeatureQuery#setFilter(Evaluator)} method.
90
     */
91
    private boolean isAddFilter = true;
92

    
93
    private FeatureQueryOrder order = new FeatureQueryOrder();
94

    
95
    private long limit;
96

    
97
    private long pageSize;
98

    
99
    /**
100
     * Creates a FeatureQuery which will load all available Features of a type.
101
     * 
102
     * @param featureType
103
     *            the type of Features of the query
104
     */
105
    public DefaultFeatureQuery() {
106
        super();
107
    }
108

    
109
    /**
110
     * Creates a FeatureQuery which will load all available Features of a type.
111
     * 
112
     * @param featureType
113
     *            the type of Features of the query
114
     */
115
    public DefaultFeatureQuery(FeatureType featureType) {
116
        super();
117
        this.setFeatureType(featureType);
118
    }
119

    
120
    /**
121
     * Creates a FeatureQuery with the type of features, a filter and the order
122
     * for the FeatureCollection.
123
     * 
124
     * @param featureType
125
     *            the type of Features of the query
126
     * @param filter
127
     *            based on the properties of the Features
128
     * @param order
129
     *            for the result
130
     */
131
    public DefaultFeatureQuery(FeatureType featureType, Evaluator filter) {
132
        super();
133
        this.setFeatureType(featureType);
134
        this.filter = filter;
135
    }
136

    
137
    /**
138
     * Creates a FeatureQuery with the type of features, a filter, the order for
139
     * the FeatureCollection and the view scale.
140
     * 
141
     * @param featureType
142
     *            the type of Features of the query
143
     * @param filter
144
     *            based on the properties of the Features
145
     * @param order
146
     *            for the result
147
     * @param scale
148
     *            to view the Features.
149
     */
150
    public DefaultFeatureQuery(FeatureType featureType, Evaluator filter,
151
        double scale) {
152
        this.setFeatureType(featureType);
153
        this.filter = filter;
154
        this.setScale(scale);
155
    }
156

    
157
    /**
158
     * Creates a FeatureQuery which will load a list of attribute names of all
159
     * available Features.
160
     * 
161
     * @param attributeNames
162
     *            the list of attribute names to load
163
     */
164
    public DefaultFeatureQuery(String[] attributeNames) {
165
        super();
166
        setAttributeNames(attributeNames);
167
    }
168

    
169
    /**
170
     * Creates a FeatureQuery with the list of attribute names of feature, a
171
     * filter and the order for the FeatureCollection.
172
     * 
173
     * @param attributeNames
174
     *            the list of attribute names to load
175
     * @param filter
176
     *            based on the properties of the Features
177
     * @param order
178
     *            for the result
179
     */
180
    public DefaultFeatureQuery(String[] attributeNames, Evaluator filter) {
181
        super();
182
        setAttributeNames(attributeNames);
183
        this.filter = filter;
184
    }
185

    
186
    /**
187
     * Creates a FeatureQuery with the list of attribute names of feature, a
188
     * filter, the order for the FeatureCollection and the view scale.
189
     * 
190
     * @param attributeNames
191
     *            the list of attribute names to load
192
     * @param filter
193
     *            based on the properties of the Features
194
     * @param order
195
     *            for the result
196
     * @param scale
197
     *            to view the Features.
198
     */
199
    public DefaultFeatureQuery(String[] attributeNames, Evaluator filter,
200
        double scale) {
201
        setAttributeNames(attributeNames);
202
        this.filter = filter;
203
        this.setScale(scale);
204
    }
205

    
206
    public double getScale() {
207
        Double scale = (Double) this.getQueryParameter(SCALE_PARAM_NAME);
208
        if (scale == null) {
209
            return 0;
210
        }
211
        return scale.doubleValue();
212
    }
213

    
214
    public void setScale(double scale) {
215
        this.setQueryParameter(SCALE_PARAM_NAME, new Double(scale));
216
    }
217

    
218
    public Object getQueryParameter(String name) {
219
        return queryParameters.get(name);
220
    }
221

    
222
    public void setQueryParameter(String name, Object value) {
223
        queryParameters.put(name, value);
224
    }
225

    
226
    public void setFeatureType(FeatureType featureType) {
227
        this.featureTypeId = featureType.getId();
228
    }
229

    
230
    public String[] getAttributeNames() {
231
        return (String[])attributeNames.toArray(new String[attributeNames.size()]);
232
    }
233

    
234
    public void setAttributeNames(String[] attributeNames) {
235
        this.attributeNames.clear();
236
        if (attributeNames != null){
237
            for (int i=0 ; i<attributeNames.length ; i++){
238
                this.attributeNames.add(attributeNames[i]);
239
            }   
240
        }
241
    }
242
    
243
    public void addAttributeName(String attributeName){
244
        //If the attribute exists finish the method
245
        for (int i=0 ; i<attributeNames.size() ; i++){
246
            if (attributeNames.get(i).equals(attributeName)){
247
                return;
248
            }            
249
        } 
250
        this.attributeNames.add(attributeName);
251
    }
252

    
253
    public boolean hasAttributeNames() {
254
        return !this.attributeNames.isEmpty();
255
    }
256

    
257
    public void clearAttributeNames() {
258
        this.attributeNames = new ArrayList();
259
    }
260

    
261
    public Evaluator getFilter() {
262
        return filter;
263
    }
264

    
265
    public void setFilter(Evaluator filter) {
266
        this.filter = filter;
267
        addFilterAttributes(filter);
268
        isAddFilter = false;
269
    }
270

    
271
    @Override
272
    public void addFilter(Evaluator evaluator) {
273
        if( evaluator == null ) {
274
            return;
275
        }
276
        if (isAddFilter){
277
            if (this.filter == null){
278
                this.filter = evaluator;
279
            }else{
280
                if (evaluator != null){
281
                    if (this.filter instanceof AndEvaluator){
282
                        ((AndEvaluator)this.filter).addEvaluator(evaluator);
283
                    }else{
284
                        this.filter = new AndEvaluator(this.filter);
285
                        ((AndEvaluator)this.filter).addEvaluator(evaluator);
286
                    }
287
                }
288
            }
289
        }else{
290
            this.filter = evaluator;
291
        }
292
        addFilterAttributes(evaluator);
293
        isAddFilter = true;
294
    }
295
    
296
    private void addFilterAttributes(Evaluator evaluator){
297
        if (evaluator != null){
298
            EvaluatorFieldsInfo fieldsInfo = evaluator.getFieldsInfo();
299
            if (fieldsInfo == null){
300
                // FieldsInfo is not available in this evaluator
301
                return;
302
            }
303
            String[] fieldNames = fieldsInfo.getFieldNames();
304
            if (fieldNames== null){
305
                // fieldNames is not available in this evaluator
306
                return;
307
            }
308
            
309
            for (int i=0 ; i<fieldNames.length ; i++){
310
                addAttributeName(fieldNames[i]);
311
            }
312
        }
313
    }
314

    
315
    public FeatureQueryOrder getOrder() {
316
        return order;
317
    }
318

    
319
    public void setOrder(FeatureQueryOrder order) {
320
        this.order = order;
321
    }
322

    
323
    public boolean hasFilter() {
324
        return this.filter != null;
325
    }
326

    
327
    public boolean hasOrder() {
328
        return this.order != null && this.order.size() > 0;
329
    }
330

    
331
    public Object clone() throws CloneNotSupportedException {
332
        DefaultFeatureQuery clone = (DefaultFeatureQuery) super.clone();
333

    
334
        // Clone attribute names array
335
        if (attributeNames != null) {
336
            clone.attributeNames = new ArrayList();
337
            for (int i=0 ; i<attributeNames.size() ; i++){
338
                clone.attributeNames.add(attributeNames.get(i));
339
            }       
340
        }
341

    
342
        // Clone order
343
        if (order != null) {
344
            clone.order = (FeatureQueryOrder) order.clone();
345
        }
346

    
347
        return clone;
348
    }
349

    
350
    public FeatureQuery getCopy() {
351
        try {
352
            return (FeatureQuery) clone();
353
        } catch (CloneNotSupportedException e) {
354
            // TODO Auto-generated catch block
355
            e.printStackTrace();
356
            return null;
357
        }
358
        // DefaultFeatureQuery aCopy = new DefaultFeatureQuery();
359
        //
360
        // aCopy.featureTypeId = this.featureTypeId;
361
        //
362
        // if (this.attributeNames != null) {
363
        // aCopy.attributeNames = (String[]) Arrays
364
        // .asList(this.attributeNames).toArray(new String[0]);
365
        // }
366
        //
367
        // aCopy.filter = this.filter;
368
        //
369
        // if (this.order != null) {
370
        // aCopy.order = this.order.getCopy();
371
        // }
372
        //
373
        // return aCopy;
374
    }
375

    
376
    public String getFeatureTypeId() {
377
        return featureTypeId;
378
    }
379

    
380
    public void setFeatureTypeId(String featureTypeId) {
381
        this.featureTypeId = featureTypeId;
382
    }
383

    
384
    public void saveToState(PersistentState state) throws PersistenceException {
385
        // FIXME: falta por terminar de implementar
386
        state.set("queryParameters", this.queryParameters);
387
        state.set("featureTypeId", this.featureTypeId);
388
        state.set("attributeNames", this.attributeNames);
389
        // state.set("filter", this.filter);
390
        state.set("limit", this.limit);
391
        state.set("pageSize", this.pageSize);
392

    
393
    }
394

    
395
    public void loadFromState(PersistentState state) throws PersistenceException {
396
        // FIXME: falta por terminar de implementar
397
        this.queryParameters = (Map) state.get("queryParameters");
398
        this.featureTypeId = state.getString("featureTypeId");
399
        this.attributeNames = state.getList("attributeNames");
400
        this.filter = (Evaluator) state.get("filter");
401
        this.limit = state.getLong("limit");
402
        this.pageSize = state.getLong("pageSize");
403

    
404
    }
405

    
406
    /**
407
     * Register the class on PersistenceManager
408
     * 
409
     */
410
    public static void registerPersistent() {
411
        DynStruct definition =
412
            ToolsLocator.getPersistenceManager()
413
            .addDefinition(DefaultFeatureQuery.class,
414
                "DefaultFeatureQuery",
415
                "DefaultFeatureQuery Persistent definition",
416
                null,
417
                null);
418

    
419
        definition.addDynFieldMap("queryParameters")
420
        .setClassOfItems(Object.class)
421
        .setMandatory(true);
422

    
423
        definition.addDynFieldString("featureTypeId").setMandatory(false);
424

    
425
        definition.addDynFieldList("attributeNames")
426
        .setClassOfItems(String.class)
427
        .setMandatory(false);
428

    
429
        definition.addDynFieldObject("filter")
430
        .setClassOfValue(Evaluator.class)
431
        .setMandatory(false);
432

    
433
        definition.addDynFieldObject("order")
434
        .setClassOfValue(FeatureQueryOrder.class)
435
        .setMandatory(false);
436

    
437
        definition.addDynFieldLong("limit").setMandatory(false);
438

    
439
        definition.addDynFieldLong("pageSize").setMandatory(false);
440

    
441
    }
442

    
443
    public long getLimit() {
444
        return limit;
445
    }
446

    
447
    public long getPageSize() {
448
        return pageSize;
449
    }
450

    
451
    public void setLimit(long limit) {
452
        this.limit = limit;
453
    }
454

    
455
    public void setPageSize(long pageSize) {
456
        this.pageSize = pageSize;
457
    }
458

    
459
    public String[] getConstantsAttributeNames() {
460
        return (String[])constantsAttributeNames.toArray(new String[constantsAttributeNames.size()]);
461
    }
462

    
463
    public void setConstantsAttributeNames(String[] constantsAttributeNames) {
464
        this.constantsAttributeNames.clear();
465
        if (constantsAttributeNames != null){
466
            for (int i=0 ; i<constantsAttributeNames.length ; i++){
467
                this.constantsAttributeNames.add(constantsAttributeNames[i]);
468
            }   
469
        }
470
    }
471
    
472
    public void addConstantAttributeName(String attributeName) {
473
        //If the attribute exists finish the method
474
        for (int i=0 ; i<constantsAttributeNames.size() ; i++){
475
            if (constantsAttributeNames.get(i).equals(attributeName)){
476
                return;
477
            }            
478
        } 
479
        this.constantsAttributeNames.add(attributeName);
480
    }
481

    
482
    public boolean hasConstantsAttributeNames() {
483
        return !this.constantsAttributeNames.isEmpty();
484
    }
485

    
486
    public void clearConstantsAttributeNames() {
487
        this.constantsAttributeNames = new ArrayList();
488
    }
489

    
490
}