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 @ 41212

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

    
311
    public FeatureQueryOrder getOrder() {
312
        return order;
313
    }
314

    
315
    public void setOrder(FeatureQueryOrder order) {
316
        this.order = order;
317
    }
318

    
319
    public boolean hasFilter() {
320
        return this.filter != null;
321
    }
322

    
323
    public boolean hasOrder() {
324
        return this.order != null && this.order.size() > 0;
325
    }
326

    
327
    public Object clone() throws CloneNotSupportedException {
328
        DefaultFeatureQuery clone = (DefaultFeatureQuery) super.clone();
329

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

    
338
        // Clone order
339
        if (order != null) {
340
            clone.order = (FeatureQueryOrder) order.clone();
341
        }
342

    
343
        return clone;
344
    }
345

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

    
372
    public String getFeatureTypeId() {
373
        return featureTypeId;
374
    }
375

    
376
    public void setFeatureTypeId(String featureTypeId) {
377
        this.featureTypeId = featureTypeId;
378
    }
379

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

    
389
    }
390

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

    
400
    }
401

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

    
415
        definition.addDynFieldMap("queryParameters")
416
        .setClassOfItems(Object.class)
417
        .setMandatory(true);
418

    
419
        definition.addDynFieldString("featureTypeId").setMandatory(false);
420

    
421
        definition.addDynFieldList("attributeNames")
422
        .setClassOfItems(String.class)
423
        .setMandatory(false);
424

    
425
        definition.addDynFieldObject("filter")
426
        .setClassOfValue(Evaluator.class)
427
        .setMandatory(false);
428

    
429
        definition.addDynFieldObject("order")
430
        .setClassOfValue(FeatureQueryOrder.class)
431
        .setMandatory(false);
432

    
433
        definition.addDynFieldLong("limit").setMandatory(false);
434

    
435
        definition.addDynFieldLong("pageSize").setMandatory(false);
436

    
437
    }
438

    
439
    public long getLimit() {
440
        return limit;
441
    }
442

    
443
    public long getPageSize() {
444
        return pageSize;
445
    }
446

    
447
    public void setLimit(long limit) {
448
        this.limit = limit;
449
    }
450

    
451
    public void setPageSize(long pageSize) {
452
        this.pageSize = pageSize;
453
    }
454

    
455
    public String[] getConstantsAttributeNames() {
456
        return (String[])constantsAttributeNames.toArray(new String[constantsAttributeNames.size()]);
457
    }
458

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

    
478
    public boolean hasConstantsAttributeNames() {
479
        return !this.constantsAttributeNames.isEmpty();
480
    }
481

    
482
    public void clearConstantsAttributeNames() {
483
        this.constantsAttributeNames = new ArrayList();
484
    }
485

    
486
}