Statistics
| Revision:

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

History | View | Annotate | Download (12.8 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.HashMap;
30
import java.util.Iterator;
31
import java.util.Map;
32

    
33
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
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.persistence.PersistentState;
42
import org.gvsig.tools.persistence.exception.PersistenceException;
43

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

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

    
78
    private Map queryParameters = new HashMap();
79

    
80
    private String featureTypeId = null;
81

    
82
    private String[] attributeNames;
83

    
84
    private Evaluator filter;
85

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

    
93
    private FeatureQueryOrder order = new FeatureQueryOrder();
94

    
95
    private long limit;
96

    
97
    private long pageSize;
98

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

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

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

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

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

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

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

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

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

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

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

    
226
    public void setFeatureType(FeatureType featureType) {
227
        this.featureTypeId = featureType.getId();
228
        Iterator iter = featureType.iterator();
229
        String attrs[] = new String[featureType.size()];
230
        FeatureAttributeDescriptor attr;
231
        int i = 0;
232
        while (iter.hasNext()) {
233
            attr = (FeatureAttributeDescriptor) iter.next();
234
            attrs[i] = attr.getName();
235
            i++;
236
        }
237
        this.attributeNames = attrs;
238

    
239
    }
240

    
241
    public String[] getAttributeNames() {
242
        return attributeNames;
243
    }
244

    
245
    public void setAttributeNames(String[] attributeNames) {
246
        this.attributeNames = attributeNames;
247
    }
248

    
249
    public Evaluator getFilter() {
250
        return filter;
251
    }
252

    
253
    public void setFilter(Evaluator filter) {
254
        this.filter = filter;
255
        isAddFilter = false;
256
    }
257

    
258
    public void addFilter(Evaluator targetFilter) {
259
        if (isAddFilter){
260
            if (this.filter == null){
261
                this.filter = targetFilter;
262
            }else{
263
                if (targetFilter != null){
264
                    if (this.filter instanceof AndEvaluator){
265
                        ((AndEvaluator)this.filter).addEvaluator(targetFilter);
266
                    }else{
267
                        this.filter = new AndEvaluator(this.filter);
268
                        ((AndEvaluator)this.filter).addEvaluator(targetFilter);
269
                    }
270
                }
271
            }
272
        }else{
273
            this.filter = targetFilter;
274
        }
275
        isAddFilter = true;
276
    }
277

    
278
    public FeatureQueryOrder getOrder() {
279
        return order;
280
    }
281

    
282
    public void setOrder(FeatureQueryOrder order) {
283
        this.order = order;
284
    }
285

    
286
    public boolean hasFilter() {
287
        return this.filter != null;
288
    }
289

    
290
    public boolean hasOrder() {
291
        return this.order != null && this.order.size() > 0;
292
    }
293

    
294
    public Object clone() throws CloneNotSupportedException {
295
        DefaultFeatureQuery clone = (DefaultFeatureQuery) super.clone();
296

    
297
        // Clone attribute names array
298
        if (attributeNames != null) {
299
            clone.attributeNames = new String[attributeNames.length];
300
            System.arraycopy(attributeNames, 0, clone.attributeNames, 0,
301
                attributeNames.length);
302
        }
303

    
304
        // Clone order
305
        if (order != null) {
306
            clone.order = (FeatureQueryOrder) order.clone();
307
        }
308

    
309
        return clone;
310
    }
311

    
312
    public FeatureQuery getCopy() {
313
        try {
314
            return (FeatureQuery) clone();
315
        } catch (CloneNotSupportedException e) {
316
            // TODO Auto-generated catch block
317
            e.printStackTrace();
318
            return null;
319
        }
320
        // DefaultFeatureQuery aCopy = new DefaultFeatureQuery();
321
        //
322
        // aCopy.featureTypeId = this.featureTypeId;
323
        //
324
        // if (this.attributeNames != null) {
325
        // aCopy.attributeNames = (String[]) Arrays
326
        // .asList(this.attributeNames).toArray(new String[0]);
327
        // }
328
        //
329
        // aCopy.filter = this.filter;
330
        //
331
        // if (this.order != null) {
332
        // aCopy.order = this.order.getCopy();
333
        // }
334
        //
335
        // return aCopy;
336
    }
337

    
338
    public String getFeatureTypeId() {
339
        return featureTypeId;
340
    }
341

    
342
    public void setFeatureTypeId(String featureTypeId) {
343
        this.featureTypeId = featureTypeId;
344
    }
345

    
346
    public void saveToState(PersistentState state) throws PersistenceException {
347
        // FIXME: falta por terminar de implementar
348
        state.set("queryParameters", this.queryParameters);
349
        state.set("featureTypeId", this.featureTypeId);
350
        state.set("attributeNames", this.attributeNames);
351
        // state.set("filter", this.filter);
352
        state.set("limit", this.limit);
353
        state.set("pageSize", this.pageSize);
354

    
355
    }
356

    
357
    public void loadFromState(PersistentState state) throws PersistenceException {
358
        // FIXME: falta por terminar de implementar
359
        this.queryParameters = (Map) state.get("queryParameters");
360
        this.featureTypeId = state.getString("featureTypeId");
361
        this.attributeNames = (String[]) state.getArray("attributeNames", String.class);
362
        this.filter = (Evaluator) state.get("filter");
363
        this.limit = state.getLong("limit");
364
        this.pageSize = state.getLong("pageSize");
365

    
366
    }
367

    
368
    /**
369
     * Register the class on PersistenceManager
370
     * 
371
     */
372
    public static void registerPersistent() {
373
        DynStruct definition =
374
            ToolsLocator.getPersistenceManager()
375
            .addDefinition(DefaultFeatureQuery.class,
376
                "DefaultFeatureQuery",
377
                "DefaultFeatureQuery Persistent definition",
378
                null,
379
                null);
380

    
381
        definition.addDynFieldMap("queryParameters")
382
        .setClassOfItems(Object.class)
383
        .setMandatory(true);
384

    
385
        definition.addDynFieldString("featureTypeId").setMandatory(false);
386

    
387
        definition.addDynFieldList("attributeNames")
388
        .setClassOfItems(String.class)
389
        .setMandatory(false);
390

    
391
        definition.addDynFieldObject("filter")
392
        .setClassOfValue(Evaluator.class)
393
        .setMandatory(false);
394

    
395
        definition.addDynFieldObject("order")
396
        .setClassOfValue(FeatureQueryOrder.class)
397
        .setMandatory(false);
398

    
399
        definition.addDynFieldLong("limit").setMandatory(false);
400

    
401
        definition.addDynFieldLong("pageSize").setMandatory(false);
402

    
403
    }
404

    
405
    public long getLimit() {
406
        return limit;
407
    }
408

    
409
    public long getPageSize() {
410
        return pageSize;
411
    }
412

    
413
    public void setLimit(long limit) {
414
        this.limit = limit;
415
    }
416

    
417
    public void setPageSize(long pageSize) {
418
        this.pageSize = pageSize;
419
    }
420

    
421
}