Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultFeatureQuery.java @ 35328

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.Iterator;
32
import java.util.List;
33
import java.util.Map;
34

    
35
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
36
import org.gvsig.fmap.dal.feature.FeatureQuery;
37
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
38
import org.gvsig.fmap.dal.feature.FeatureType;
39
import org.gvsig.tools.ToolsLocator;
40
import org.gvsig.tools.dynobject.DynStruct;
41
import org.gvsig.tools.evaluator.AndEvaluator;
42
import org.gvsig.tools.evaluator.Evaluator;
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
        Iterator iter = featureType.iterator();
231
        String attrs[] = new String[featureType.size()];
232
        FeatureAttributeDescriptor attr;
233
        int i = 0;
234
        while (iter.hasNext()) {
235
            attr = (FeatureAttributeDescriptor) iter.next();
236
            attrs[i] = attr.getName();
237
            i++;
238
        }
239
        setAttributeNames(attrs);
240
    }
241

    
242
    public String[] getAttributeNames() {
243
        return (String[])attributeNames.toArray(new String[attributeNames.size()]);
244
    }
245

    
246
    public void setAttributeNames(String[] attributeNames) {
247
        this.attributeNames.clear();
248
        for (int i=0 ; i<attributeNames.length ; i++){
249
            this.attributeNames.add(attributeNames[i]);
250
        }      
251
    }
252
    
253
    public void addAttributeName(String attributeName){
254
        //If the attribute exists finish the method
255
        for (int i=0 ; i<attributeNames.size() ; i++){
256
            if (attributeNames.get(i).equals(attributeName)){
257
                return;
258
            }            
259
        } 
260
        this.attributeNames.add(attributeName);
261
    }
262

    
263
    public Evaluator getFilter() {
264
        return filter;
265
    }
266

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

    
273
    public void addFilter(Evaluator evaluator) {
274
        if (isAddFilter){
275
            if (this.filter == null){
276
                this.filter = evaluator;
277
            }else{
278
                if (evaluator != null){
279
                    if (this.filter instanceof AndEvaluator){
280
                        ((AndEvaluator)this.filter).addEvaluator(evaluator);
281
                    }else{
282
                        this.filter = new AndEvaluator(this.filter);
283
                        ((AndEvaluator)this.filter).addEvaluator(evaluator);
284
                    }
285
                }
286
            }
287
        }else{
288
            this.filter = evaluator;
289
        }
290
        addFilterAttributes(evaluator);
291
        isAddFilter = true;
292
    }
293
    
294
    private void addFilterAttributes(Evaluator evaluator){
295
        if (evaluator != null){
296
            String[] fieldNames = evaluator.getFieldsInfo().getFieldNames();
297
            for (int i=0 ; i<fieldNames.length ; i++){
298
                addAttributeName(fieldNames[i]);
299
            }
300
        }
301
    }
302

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

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

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

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

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

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

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

    
335
        return clone;
336
    }
337

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

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

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

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

    
381
    }
382

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

    
392
    }
393

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

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

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

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

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

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

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

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

    
429
    }
430

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

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

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

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

    
447
}