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

History | View | Annotate | Download (13.6 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 Evaluator filter;
83

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

    
91
    private FeatureQueryOrder order = new FeatureQueryOrder();
92

    
93
    private long limit;
94

    
95
    private long pageSize;
96

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

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

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

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

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

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

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

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

    
212
    public void setScale(double scale) {
213
        this.setQueryParameter(SCALE_PARAM_NAME, new Double(scale));
214
    }
215

    
216
    public Object getQueryParameter(String name) {
217
        return queryParameters.get(name);
218
    }
219

    
220
    public void setQueryParameter(String name, Object value) {
221
        queryParameters.put(name, value);
222
    }
223

    
224
    public void setFeatureType(FeatureType featureType) {
225
        this.featureTypeId = featureType.getId();
226
    }
227

    
228
    public String[] getAttributeNames() {
229
        return (String[])attributeNames.toArray(new String[attributeNames.size()]);
230
    }
231

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

    
251
    public Evaluator getFilter() {
252
        return filter;
253
    }
254

    
255
    public void setFilter(Evaluator filter) {
256
        this.filter = filter;
257
        addFilterAttributes(filter);
258
        isAddFilter = false;
259
    }
260

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

    
301
    public FeatureQueryOrder getOrder() {
302
        return order;
303
    }
304

    
305
    public void setOrder(FeatureQueryOrder order) {
306
        this.order = order;
307
    }
308

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

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

    
317
    public Object clone() throws CloneNotSupportedException {
318
        DefaultFeatureQuery clone = (DefaultFeatureQuery) super.clone();
319

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

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

    
333
        return clone;
334
    }
335

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

    
362
    public String getFeatureTypeId() {
363
        return featureTypeId;
364
    }
365

    
366
    public void setFeatureTypeId(String featureTypeId) {
367
        this.featureTypeId = featureTypeId;
368
    }
369

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

    
379
    }
380

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

    
390
    }
391

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

    
405
        definition.addDynFieldMap("queryParameters")
406
        .setClassOfItems(Object.class)
407
        .setMandatory(true);
408

    
409
        definition.addDynFieldString("featureTypeId").setMandatory(false);
410

    
411
        definition.addDynFieldList("attributeNames")
412
        .setClassOfItems(String.class)
413
        .setMandatory(false);
414

    
415
        definition.addDynFieldObject("filter")
416
        .setClassOfValue(Evaluator.class)
417
        .setMandatory(false);
418

    
419
        definition.addDynFieldObject("order")
420
        .setClassOfValue(FeatureQueryOrder.class)
421
        .setMandatory(false);
422

    
423
        definition.addDynFieldLong("limit").setMandatory(false);
424

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

    
427
    }
428

    
429
    public long getLimit() {
430
        return limit;
431
    }
432

    
433
    public long getPageSize() {
434
        return pageSize;
435
    }
436

    
437
    public void setLimit(long limit) {
438
        this.limit = limit;
439
    }
440

    
441
    public void setPageSize(long pageSize) {
442
        this.pageSize = pageSize;
443
    }
444

    
445
}