Statistics
| Revision:

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

History | View | Annotate | Download (14.1 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.evaluator.EvaluatorFieldsInfo;
44
import org.gvsig.tools.persistence.PersistentState;
45
import org.gvsig.tools.persistence.exception.PersistenceException;
46

    
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
 * 
75
 * @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
76
 */
77
public class DefaultFeatureQuery implements FeatureQuery {
78

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

    
81
    private Map queryParameters = new HashMap();
82

    
83
    private String featureTypeId = null;
84

    
85
    private List attributeNames = new ArrayList();
86

    
87
    private Evaluator filter;
88

    
89
    /**
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
    private FeatureQueryOrder order = new FeatureQueryOrder();
97

    
98
    private long limit;
99

    
100
    private long pageSize;
101

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

    
112
    /**
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

    
123
    /**
124
     * Creates a FeatureQuery with the type of features, a filter and the order
125
     * for the FeatureCollection.
126
     * 
127
     * @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
     * 
144
     * @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
        double scale) {
155
        this.setFeatureType(featureType);
156
        this.filter = filter;
157
        this.setScale(scale);
158
    }
159

    
160
    /**
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
    public DefaultFeatureQuery(String[] attributeNames) {
168
        super();
169
        setAttributeNames(attributeNames);
170
    }
171

    
172
    /**
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
    public DefaultFeatureQuery(String[] attributeNames, Evaluator filter) {
184
        super();
185
        setAttributeNames(attributeNames);
186
        this.filter = filter;
187
    }
188

    
189
    /**
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
    public DefaultFeatureQuery(String[] attributeNames, Evaluator filter,
203
        double scale) {
204
        setAttributeNames(attributeNames);
205
        this.filter = filter;
206
        this.setScale(scale);
207
    }
208

    
209
    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

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

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

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

    
229
    public void setFeatureType(FeatureType featureType) {
230
        this.featureTypeId = featureType.getId();
231
        Iterator iter = featureType.iterator();
232
        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
        setAttributeNames(attrs);
241
    }
242

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

    
247
    public void setAttributeNames(String[] attributeNames) {
248
        this.attributeNames.clear();
249
        if (attributeNames != null){
250
            for (int i=0 ; i<attributeNames.length ; i++){
251
                this.attributeNames.add(attributeNames[i]);
252
            }   
253
        }
254
    }
255
    
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

    
266
    public Evaluator getFilter() {
267
        return filter;
268
    }
269

    
270
    public void setFilter(Evaluator filter) {
271
        this.filter = filter;
272
        addFilterAttributes(filter);
273
        isAddFilter = false;
274
    }
275

    
276
    public void addFilter(Evaluator evaluator) {
277
        if (isAddFilter){
278
            if (this.filter == null){
279
                this.filter = evaluator;
280
            }else{
281
                if (evaluator != null){
282
                    if (this.filter instanceof AndEvaluator){
283
                        ((AndEvaluator)this.filter).addEvaluator(evaluator);
284
                    }else{
285
                        this.filter = new AndEvaluator(this.filter);
286
                        ((AndEvaluator)this.filter).addEvaluator(evaluator);
287
                    }
288
                }
289
            }
290
        }else{
291
            this.filter = evaluator;
292
        }
293
        addFilterAttributes(evaluator);
294
        isAddFilter = true;
295
    }
296
    
297
    private void addFilterAttributes(Evaluator evaluator){
298
        if (evaluator != null){
299
            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
            for (int i=0 ; i<fieldNames.length ; i++){
311
                addAttributeName(fieldNames[i]);
312
            }
313
        }
314
    }
315

    
316
    public FeatureQueryOrder getOrder() {
317
        return order;
318
    }
319

    
320
    public void setOrder(FeatureQueryOrder order) {
321
        this.order = order;
322
    }
323

    
324
    public boolean hasFilter() {
325
        return this.filter != null;
326
    }
327

    
328
    public boolean hasOrder() {
329
        return this.order != null && this.order.size() > 0;
330
    }
331

    
332
    public Object clone() throws CloneNotSupportedException {
333
        DefaultFeatureQuery clone = (DefaultFeatureQuery) super.clone();
334

    
335
        // Clone attribute names array
336
        if (attributeNames != null) {
337
            clone.attributeNames = new ArrayList();
338
            for (int i=0 ; i<attributeNames.size() ; i++){
339
                clone.attributeNames.add(attributeNames.get(i));
340
            }       
341
        }
342

    
343
        // Clone order
344
        if (order != null) {
345
            clone.order = (FeatureQueryOrder) order.clone();
346
        }
347

    
348
        return clone;
349
    }
350

    
351
    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

    
377
    public String getFeatureTypeId() {
378
        return featureTypeId;
379
    }
380

    
381
    public void setFeatureTypeId(String featureTypeId) {
382
        this.featureTypeId = featureTypeId;
383
    }
384

    
385
    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

    
394
    }
395

    
396
    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
        this.attributeNames = state.getList("attributeNames");
401
        this.filter = (Evaluator) state.get("filter");
402
        this.limit = state.getLong("limit");
403
        this.pageSize = state.getLong("pageSize");
404

    
405
    }
406

    
407
    /**
408
     * Register the class on PersistenceManager
409
     * 
410
     */
411
    public static void registerPersistent() {
412
        DynStruct definition =
413
            ToolsLocator.getPersistenceManager()
414
            .addDefinition(DefaultFeatureQuery.class,
415
                "DefaultFeatureQuery",
416
                "DefaultFeatureQuery Persistent definition",
417
                null,
418
                null);
419

    
420
        definition.addDynFieldMap("queryParameters")
421
        .setClassOfItems(Object.class)
422
        .setMandatory(true);
423

    
424
        definition.addDynFieldString("featureTypeId").setMandatory(false);
425

    
426
        definition.addDynFieldList("attributeNames")
427
        .setClassOfItems(String.class)
428
        .setMandatory(false);
429

    
430
        definition.addDynFieldObject("filter")
431
        .setClassOfValue(Evaluator.class)
432
        .setMandatory(false);
433

    
434
        definition.addDynFieldObject("order")
435
        .setClassOfValue(FeatureQueryOrder.class)
436
        .setMandatory(false);
437

    
438
        definition.addDynFieldLong("limit").setMandatory(false);
439

    
440
        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
}