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

History | View | Annotate | Download (13.7 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
/*
25
 * AUTHORS (In addition to CIT):
26
 * 2008 {DiSiD Technologies}  {Create Parameter object to define FeatureCollections queries}
27
 */
28
package org.gvsig.fmap.dal.feature.impl;
29

    
30
import java.util.ArrayList;
31
import java.util.HashMap;
32
import java.util.List;
33
import java.util.Map;
34

    
35
import org.gvsig.fmap.dal.feature.FeatureQuery;
36
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
37
import org.gvsig.fmap.dal.feature.FeatureType;
38
import org.gvsig.tools.ToolsLocator;
39
import org.gvsig.tools.dynobject.DynStruct;
40
import org.gvsig.tools.evaluator.AndEvaluator;
41
import org.gvsig.tools.evaluator.Evaluator;
42
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo;
43
import org.gvsig.tools.persistence.PersistentState;
44
import org.gvsig.tools.persistence.exception.PersistenceException;
45

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

    
78
    public static final String SCALE_PARAM_NAME = "Scale";
79

    
80
    private Map queryParameters = new HashMap();
81

    
82
    private String featureTypeId = null;
83

    
84
    private List attributeNames = new ArrayList();
85

    
86
    private Evaluator filter;
87

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

    
95
    private FeatureQueryOrder order = new FeatureQueryOrder();
96

    
97
    private long limit;
98

    
99
    private long pageSize;
100

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
255
    public Evaluator getFilter() {
256
        return filter;
257
    }
258

    
259
    public void setFilter(Evaluator filter) {
260
        this.filter = filter;
261
        addFilterAttributes(filter);
262
        isAddFilter = false;
263
    }
264

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

    
305
    public FeatureQueryOrder getOrder() {
306
        return order;
307
    }
308

    
309
    public void setOrder(FeatureQueryOrder order) {
310
        this.order = order;
311
    }
312

    
313
    public boolean hasFilter() {
314
        return this.filter != null;
315
    }
316

    
317
    public boolean hasOrder() {
318
        return this.order != null && this.order.size() > 0;
319
    }
320

    
321
    public Object clone() throws CloneNotSupportedException {
322
        DefaultFeatureQuery clone = (DefaultFeatureQuery) super.clone();
323

    
324
        // Clone attribute names array
325
        if (attributeNames != null) {
326
            clone.attributeNames = new ArrayList();
327
            for (int i=0 ; i<attributeNames.size() ; i++){
328
                clone.attributeNames.add(attributeNames.get(i));
329
            }       
330
        }
331

    
332
        // Clone order
333
        if (order != null) {
334
            clone.order = (FeatureQueryOrder) order.clone();
335
        }
336

    
337
        return clone;
338
    }
339

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

    
366
    public String getFeatureTypeId() {
367
        return featureTypeId;
368
    }
369

    
370
    public void setFeatureTypeId(String featureTypeId) {
371
        this.featureTypeId = featureTypeId;
372
    }
373

    
374
    public void saveToState(PersistentState state) throws PersistenceException {
375
        // FIXME: falta por terminar de implementar
376
        state.set("queryParameters", this.queryParameters);
377
        state.set("featureTypeId", this.featureTypeId);
378
        state.set("attributeNames", this.attributeNames);
379
        // state.set("filter", this.filter);
380
        state.set("limit", this.limit);
381
        state.set("pageSize", this.pageSize);
382

    
383
    }
384

    
385
    public void loadFromState(PersistentState state) throws PersistenceException {
386
        // FIXME: falta por terminar de implementar
387
        this.queryParameters = (Map) state.get("queryParameters");
388
        this.featureTypeId = state.getString("featureTypeId");
389
        this.attributeNames = state.getList("attributeNames");
390
        this.filter = (Evaluator) state.get("filter");
391
        this.limit = state.getLong("limit");
392
        this.pageSize = state.getLong("pageSize");
393

    
394
    }
395

    
396
    /**
397
     * Register the class on PersistenceManager
398
     * 
399
     */
400
    public static void registerPersistent() {
401
        DynStruct definition =
402
            ToolsLocator.getPersistenceManager()
403
            .addDefinition(DefaultFeatureQuery.class,
404
                "DefaultFeatureQuery",
405
                "DefaultFeatureQuery Persistent definition",
406
                null,
407
                null);
408

    
409
        definition.addDynFieldMap("queryParameters")
410
        .setClassOfItems(Object.class)
411
        .setMandatory(true);
412

    
413
        definition.addDynFieldString("featureTypeId").setMandatory(false);
414

    
415
        definition.addDynFieldList("attributeNames")
416
        .setClassOfItems(String.class)
417
        .setMandatory(false);
418

    
419
        definition.addDynFieldObject("filter")
420
        .setClassOfValue(Evaluator.class)
421
        .setMandatory(false);
422

    
423
        definition.addDynFieldObject("order")
424
        .setClassOfValue(FeatureQueryOrder.class)
425
        .setMandatory(false);
426

    
427
        definition.addDynFieldLong("limit").setMandatory(false);
428

    
429
        definition.addDynFieldLong("pageSize").setMandatory(false);
430

    
431
    }
432

    
433
    public long getLimit() {
434
        return limit;
435
    }
436

    
437
    public long getPageSize() {
438
        return pageSize;
439
    }
440

    
441
    public void setLimit(long limit) {
442
        this.limit = limit;
443
    }
444

    
445
    public void setPageSize(long pageSize) {
446
        this.pageSize = pageSize;
447
    }
448

    
449
}