Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultFeatureQuery.java @ 37327

History | View | Annotate | Download (14.1 KB)

1 27575 jmvivo
/* gvSIG. Geographic Information System of the Valencian Government
2 33659 fdiaz
 *
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 27575 jmvivo
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 35328 jpiera
import java.util.ArrayList;
30 27575 jmvivo
import java.util.HashMap;
31
import java.util.Iterator;
32 35328 jpiera
import java.util.List;
33 27575 jmvivo
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 30154 cordinyana
import org.gvsig.tools.ToolsLocator;
40 32880 jjdelcerro
import org.gvsig.tools.dynobject.DynStruct;
41 35198 jpiera
import org.gvsig.tools.evaluator.AndEvaluator;
42 27575 jmvivo
import org.gvsig.tools.evaluator.Evaluator;
43 35489 fdiaz
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo;
44 27575 jmvivo
import org.gvsig.tools.persistence.PersistentState;
45 32880 jjdelcerro
import org.gvsig.tools.persistence.exception.PersistenceException;
46 27575 jmvivo
47
/**
48
 * Defines the properties of a collection of Features, as a result of a query
49
 * through a FeatureStore.
50
 * <p>
51
 * A FeatureQuery is always defined by a FeatureType, or by the list of
52
 * attribute names of the FeatureStore to return.
53
 * </p>
54
 * <p>
55
 * The filter allows to select Features whose properties have values with the
56
 * characteristics defined by the filter.
57
 * </p>
58
 * <p>
59
 * The order is used to set the order of the result FeatureCollection, based on
60
 * the values of the properties of the Features.
61
 * </p>
62
 * <p>
63
 * The scale parameter can be used by the FeatureStore as a hint about the
64
 * quality or resolution of the data needed to view or operate with the data
65
 * returned. As an example, the FeatureStore may use the scale to return only a
66
 * representative subset of the data, or maybe to return Features with less
67
 * detail, like a point or a line instead of a polygon.
68
 * </p>
69
 * <p>
70
 * If an implementation of FeatureStore is able to get other parameters to
71
 * customize the behavior of the getDataCollection methods, there is an option
72
 * to set more parameters through the setAttribute method.
73
 * </p>
74 31113 cordinyana
 *
75
 * @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
76 27575 jmvivo
 */
77
public class DefaultFeatureQuery implements FeatureQuery {
78
79 33659 fdiaz
    public static final String SCALE_PARAM_NAME = "Scale";
80 27575 jmvivo
81 33659 fdiaz
    private Map queryParameters = new HashMap();
82 27575 jmvivo
83 33659 fdiaz
    private String featureTypeId = null;
84 27575 jmvivo
85 35328 jpiera
    private List attributeNames = new ArrayList();
86 27575 jmvivo
87
    private Evaluator filter;
88
89 35198 jpiera
    /**
90
     * This boolean value is used to know if the current filter
91
     * has been added using the {@link FeatureQuery#addFilter(Evaluator)}
92
     * method or the {@link FeatureQuery#setFilter(Evaluator)} method.
93
     */
94
    private boolean isAddFilter = true;
95
96 27575 jmvivo
    private FeatureQueryOrder order = new FeatureQueryOrder();
97
98 33659 fdiaz
    private long limit;
99 31113 cordinyana
100 33659 fdiaz
    private long pageSize;
101 31113 cordinyana
102 27575 jmvivo
    /**
103
     * Creates a FeatureQuery which will load all available Features of a type.
104 33659 fdiaz
     *
105 27575 jmvivo
     * @param featureType
106
     *            the type of Features of the query
107
     */
108
    public DefaultFeatureQuery() {
109
        super();
110
    }
111
112 33659 fdiaz
    /**
113
     * Creates a FeatureQuery which will load all available Features of a type.
114
     *
115
     * @param featureType
116
     *            the type of Features of the query
117
     */
118
    public DefaultFeatureQuery(FeatureType featureType) {
119
        super();
120
        this.setFeatureType(featureType);
121
    }
122 27575 jmvivo
123
    /**
124
     * Creates a FeatureQuery with the type of features, a filter and the order
125
     * for the FeatureCollection.
126 33659 fdiaz
     *
127 27575 jmvivo
     * @param featureType
128
     *            the type of Features of the query
129
     * @param filter
130
     *            based on the properties of the Features
131
     * @param order
132
     *            for the result
133
     */
134
    public DefaultFeatureQuery(FeatureType featureType, Evaluator filter) {
135
        super();
136
        this.setFeatureType(featureType);
137
        this.filter = filter;
138
    }
139
140
    /**
141
     * Creates a FeatureQuery with the type of features, a filter, the order for
142
     * the FeatureCollection and the view scale.
143 33659 fdiaz
     *
144 27575 jmvivo
     * @param featureType
145
     *            the type of Features of the query
146
     * @param filter
147
     *            based on the properties of the Features
148
     * @param order
149
     *            for the result
150
     * @param scale
151
     *            to view the Features.
152
     */
153
    public DefaultFeatureQuery(FeatureType featureType, Evaluator filter,
154 33659 fdiaz
        double scale) {
155 27575 jmvivo
        this.setFeatureType(featureType);
156
        this.filter = filter;
157 29520 jmvivo
        this.setScale(scale);
158 27575 jmvivo
    }
159
160 33659 fdiaz
    /**
161
     * Creates a FeatureQuery which will load a list of attribute names of all
162
     * available Features.
163
     *
164
     * @param attributeNames
165
     *            the list of attribute names to load
166
     */
167 27575 jmvivo
    public DefaultFeatureQuery(String[] attributeNames) {
168
        super();
169 35328 jpiera
        setAttributeNames(attributeNames);
170 27575 jmvivo
    }
171
172 33659 fdiaz
    /**
173
     * Creates a FeatureQuery with the list of attribute names of feature, a
174
     * filter and the order for the FeatureCollection.
175
     *
176
     * @param attributeNames
177
     *            the list of attribute names to load
178
     * @param filter
179
     *            based on the properties of the Features
180
     * @param order
181
     *            for the result
182
     */
183 27575 jmvivo
    public DefaultFeatureQuery(String[] attributeNames, Evaluator filter) {
184
        super();
185 35328 jpiera
        setAttributeNames(attributeNames);
186 27575 jmvivo
        this.filter = filter;
187
    }
188
189 33659 fdiaz
    /**
190
     * Creates a FeatureQuery with the list of attribute names of feature, a
191
     * filter, the order for the FeatureCollection and the view scale.
192
     *
193
     * @param attributeNames
194
     *            the list of attribute names to load
195
     * @param filter
196
     *            based on the properties of the Features
197
     * @param order
198
     *            for the result
199
     * @param scale
200
     *            to view the Features.
201
     */
202 27575 jmvivo
    public DefaultFeatureQuery(String[] attributeNames, Evaluator filter,
203 33659 fdiaz
        double scale) {
204 35328 jpiera
        setAttributeNames(attributeNames);
205 27575 jmvivo
        this.filter = filter;
206 29520 jmvivo
        this.setScale(scale);
207 27575 jmvivo
    }
208
209 33659 fdiaz
    public double getScale() {
210
        Double scale = (Double) this.getQueryParameter(SCALE_PARAM_NAME);
211
        if (scale == null) {
212
            return 0;
213
        }
214
        return scale.doubleValue();
215
    }
216 27575 jmvivo
217 33659 fdiaz
    public void setScale(double scale) {
218
        this.setQueryParameter(SCALE_PARAM_NAME, new Double(scale));
219
    }
220 27575 jmvivo
221 33659 fdiaz
    public Object getQueryParameter(String name) {
222
        return queryParameters.get(name);
223
    }
224 27575 jmvivo
225 33659 fdiaz
    public void setQueryParameter(String name, Object value) {
226
        queryParameters.put(name, value);
227
    }
228 27575 jmvivo
229
    public void setFeatureType(FeatureType featureType) {
230
        this.featureTypeId = featureType.getId();
231
        Iterator iter = featureType.iterator();
232 33659 fdiaz
        String attrs[] = new String[featureType.size()];
233
        FeatureAttributeDescriptor attr;
234
        int i = 0;
235
        while (iter.hasNext()) {
236
            attr = (FeatureAttributeDescriptor) iter.next();
237
            attrs[i] = attr.getName();
238
            i++;
239
        }
240 35328 jpiera
        setAttributeNames(attrs);
241 27575 jmvivo
    }
242
243
    public String[] getAttributeNames() {
244 35328 jpiera
        return (String[])attributeNames.toArray(new String[attributeNames.size()]);
245 27575 jmvivo
    }
246
247
    public void setAttributeNames(String[] attributeNames) {
248 35328 jpiera
        this.attributeNames.clear();
249 35464 jpiera
        if (attributeNames != null){
250
            for (int i=0 ; i<attributeNames.length ; i++){
251
                this.attributeNames.add(attributeNames[i]);
252
            }
253
        }
254 27575 jmvivo
    }
255 35328 jpiera
256
    public void addAttributeName(String attributeName){
257
        //If the attribute exists finish the method
258
        for (int i=0 ; i<attributeNames.size() ; i++){
259
            if (attributeNames.get(i).equals(attributeName)){
260
                return;
261
            }
262
        }
263
        this.attributeNames.add(attributeName);
264
    }
265 27575 jmvivo
266
    public Evaluator getFilter() {
267
        return filter;
268
    }
269
270
    public void setFilter(Evaluator filter) {
271
        this.filter = filter;
272 35328 jpiera
        addFilterAttributes(filter);
273 35198 jpiera
        isAddFilter = false;
274 27575 jmvivo
    }
275
276 35328 jpiera
    public void addFilter(Evaluator evaluator) {
277 35198 jpiera
        if (isAddFilter){
278
            if (this.filter == null){
279 35328 jpiera
                this.filter = evaluator;
280 35198 jpiera
            }else{
281 35328 jpiera
                if (evaluator != null){
282 35198 jpiera
                    if (this.filter instanceof AndEvaluator){
283 35328 jpiera
                        ((AndEvaluator)this.filter).addEvaluator(evaluator);
284 35198 jpiera
                    }else{
285
                        this.filter = new AndEvaluator(this.filter);
286 35328 jpiera
                        ((AndEvaluator)this.filter).addEvaluator(evaluator);
287 35198 jpiera
                    }
288
                }
289
            }
290
        }else{
291 35328 jpiera
            this.filter = evaluator;
292 35198 jpiera
        }
293 35328 jpiera
        addFilterAttributes(evaluator);
294 35198 jpiera
        isAddFilter = true;
295
    }
296 35328 jpiera
297
    private void addFilterAttributes(Evaluator evaluator){
298
        if (evaluator != null){
299 35489 fdiaz
            EvaluatorFieldsInfo fieldsInfo = evaluator.getFieldsInfo();
300
            if (fieldsInfo == null){
301
                // FieldsInfo is not available in this evaluator
302
                return;
303
            }
304
            String[] fieldNames = fieldsInfo.getFieldNames();
305
            if (fieldNames== null){
306
                // fieldNames is not available in this evaluator
307
                return;
308
            }
309
310 35328 jpiera
            for (int i=0 ; i<fieldNames.length ; i++){
311
                addAttributeName(fieldNames[i]);
312
            }
313
        }
314
    }
315 35198 jpiera
316 27575 jmvivo
    public FeatureQueryOrder getOrder() {
317
        return order;
318
    }
319
320 33659 fdiaz
    public void setOrder(FeatureQueryOrder order) {
321
        this.order = order;
322
    }
323 27575 jmvivo
324 33659 fdiaz
    public boolean hasFilter() {
325
        return this.filter != null;
326
    }
327 27575 jmvivo
328 33659 fdiaz
    public boolean hasOrder() {
329
        return this.order != null && this.order.size() > 0;
330
    }
331 27575 jmvivo
332 33659 fdiaz
    public Object clone() throws CloneNotSupportedException {
333
        DefaultFeatureQuery clone = (DefaultFeatureQuery) super.clone();
334 27575 jmvivo
335 33659 fdiaz
        // Clone attribute names array
336
        if (attributeNames != null) {
337 35328 jpiera
            clone.attributeNames = new ArrayList();
338
            for (int i=0 ; i<attributeNames.size() ; i++){
339
                clone.attributeNames.add(attributeNames.get(i));
340
            }
341 33659 fdiaz
        }
342 27575 jmvivo
343 33659 fdiaz
        // Clone order
344
        if (order != null) {
345
            clone.order = (FeatureQueryOrder) order.clone();
346
        }
347 27575 jmvivo
348 33659 fdiaz
        return clone;
349
    }
350 27575 jmvivo
351 33659 fdiaz
    public FeatureQuery getCopy() {
352
        try {
353
            return (FeatureQuery) clone();
354
        } catch (CloneNotSupportedException e) {
355
            // TODO Auto-generated catch block
356
            e.printStackTrace();
357
            return null;
358
        }
359
        // DefaultFeatureQuery aCopy = new DefaultFeatureQuery();
360
        //
361
        // aCopy.featureTypeId = this.featureTypeId;
362
        //
363
        // if (this.attributeNames != null) {
364
        // aCopy.attributeNames = (String[]) Arrays
365
        // .asList(this.attributeNames).toArray(new String[0]);
366
        // }
367
        //
368
        // aCopy.filter = this.filter;
369
        //
370
        // if (this.order != null) {
371
        // aCopy.order = this.order.getCopy();
372
        // }
373
        //
374
        // return aCopy;
375
    }
376 27575 jmvivo
377 33659 fdiaz
    public String getFeatureTypeId() {
378
        return featureTypeId;
379
    }
380 27575 jmvivo
381 33659 fdiaz
    public void setFeatureTypeId(String featureTypeId) {
382
        this.featureTypeId = featureTypeId;
383
    }
384 27575 jmvivo
385 33659 fdiaz
    public void saveToState(PersistentState state) throws PersistenceException {
386
        // FIXME: falta por terminar de implementar
387
        state.set("queryParameters", this.queryParameters);
388
        state.set("featureTypeId", this.featureTypeId);
389
        state.set("attributeNames", this.attributeNames);
390
        // state.set("filter", this.filter);
391
        state.set("limit", this.limit);
392
        state.set("pageSize", this.pageSize);
393 30208 jmvivo
394 33659 fdiaz
    }
395 27575 jmvivo
396 33659 fdiaz
    public void loadFromState(PersistentState state) throws PersistenceException {
397
        // FIXME: falta por terminar de implementar
398
        this.queryParameters = (Map) state.get("queryParameters");
399
        this.featureTypeId = state.getString("featureTypeId");
400 35328 jpiera
        this.attributeNames = state.getList("attributeNames");
401 33659 fdiaz
        this.filter = (Evaluator) state.get("filter");
402
        this.limit = state.getLong("limit");
403
        this.pageSize = state.getLong("pageSize");
404 29520 jmvivo
405 33659 fdiaz
    }
406 29520 jmvivo
407 33659 fdiaz
    /**
408
     * Register the class on PersistenceManager
409
     *
410
     */
411
    public static void registerPersistent() {
412
        DynStruct definition =
413
            ToolsLocator.getPersistenceManager()
414 35198 jpiera
            .addDefinition(DefaultFeatureQuery.class,
415
                "DefaultFeatureQuery",
416
                "DefaultFeatureQuery Persistent definition",
417
                null,
418
                null);
419 30208 jmvivo
420 33659 fdiaz
        definition.addDynFieldMap("queryParameters")
421 35198 jpiera
        .setClassOfItems(Object.class)
422
        .setMandatory(true);
423 27575 jmvivo
424 33659 fdiaz
        definition.addDynFieldString("featureTypeId").setMandatory(false);
425 31113 cordinyana
426 33659 fdiaz
        definition.addDynFieldList("attributeNames")
427 35198 jpiera
        .setClassOfItems(String.class)
428
        .setMandatory(false);
429 31113 cordinyana
430 33659 fdiaz
        definition.addDynFieldObject("filter")
431 35198 jpiera
        .setClassOfValue(Evaluator.class)
432
        .setMandatory(false);
433 31113 cordinyana
434 33659 fdiaz
        definition.addDynFieldObject("order")
435 35198 jpiera
        .setClassOfValue(FeatureQueryOrder.class)
436
        .setMandatory(false);
437
438 33659 fdiaz
        definition.addDynFieldLong("limit").setMandatory(false);
439 31113 cordinyana
440 33659 fdiaz
        definition.addDynFieldLong("pageSize").setMandatory(false);
441
442
    }
443
444
    public long getLimit() {
445
        return limit;
446
    }
447
448
    public long getPageSize() {
449
        return pageSize;
450
    }
451
452
    public void setLimit(long limit) {
453
        this.limit = limit;
454
    }
455
456
    public void setPageSize(long pageSize) {
457
        this.pageSize = pageSize;
458
    }
459
460
}