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

History | View | Annotate | Download (13.7 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 {DiSiD Technologies}  {Create Parameter object to define FeatureCollections queries}
26
 */
27
package org.gvsig.fmap.dal.feature.impl;
28

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

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

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

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

    
79
    private Map queryParameters = new HashMap();
80

    
81
    private String featureTypeId = null;
82

    
83
    private List attributeNames = new ArrayList();
84

    
85
    private Evaluator filter;
86

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

    
94
    private FeatureQueryOrder order = new FeatureQueryOrder();
95

    
96
    private long limit;
97

    
98
    private long pageSize;
99

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
336
        return clone;
337
    }
338

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

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

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

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

    
382
    }
383

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

    
393
    }
394

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

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

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

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

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

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

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

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

    
430
    }
431

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

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

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

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

    
448
}