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

History | View | Annotate | Download (18.9 KB)

1 40559 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40559 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5 40435 jjdelcerro
 *
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 40559 jjdelcerro
 * as published by the Free Software Foundation; either version 3
9 40435 jjdelcerro
 * 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 40559 jjdelcerro
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23 40435 jjdelcerro
 */
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 42971 jjdelcerro
import java.util.logging.Level;
31
import java.util.logging.Logger;
32 43358 jjdelcerro
import org.apache.commons.lang3.ArrayUtils;
33 43913 jjdelcerro
import org.apache.commons.lang3.StringUtils;
34 44023 jjdelcerro
import org.gvsig.expressionevaluator.Expression;
35 42971 jjdelcerro
import org.gvsig.fmap.dal.DALLocator;
36 43358 jjdelcerro
import org.gvsig.fmap.dal.DataTypes;
37
import org.gvsig.fmap.dal.exception.DataException;
38 42971 jjdelcerro
import org.gvsig.fmap.dal.exception.InitializeException;
39 43358 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
40 40435 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureQuery;
41
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
42 43358 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureStore;
43 40435 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureType;
44 44023 jjdelcerro
import org.gvsig.fmap.dal.impl.expressionevaluator.DefaultExpressionEvaluator;
45 40435 jjdelcerro
import org.gvsig.tools.ToolsLocator;
46
import org.gvsig.tools.dynobject.DynStruct;
47
import org.gvsig.tools.evaluator.AndEvaluator;
48
import org.gvsig.tools.evaluator.Evaluator;
49
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo;
50
import org.gvsig.tools.persistence.PersistentState;
51
import org.gvsig.tools.persistence.exception.PersistenceException;
52
53
/**
54
 * Defines the properties of a collection of Features, as a result of a query
55
 * through a FeatureStore.
56
 * <p>
57
 * A FeatureQuery is always defined by a FeatureType, or by the list of
58
 * attribute names of the FeatureStore to return.
59
 * </p>
60
 * <p>
61
 * The filter allows to select Features whose properties have values with the
62
 * characteristics defined by the filter.
63
 * </p>
64
 * <p>
65
 * The order is used to set the order of the result FeatureCollection, based on
66
 * the values of the properties of the Features.
67
 * </p>
68
 * <p>
69
 * The scale parameter can be used by the FeatureStore as a hint about the
70
 * quality or resolution of the data needed to view or operate with the data
71
 * returned. As an example, the FeatureStore may use the scale to return only a
72
 * representative subset of the data, or maybe to return Features with less
73
 * detail, like a point or a line instead of a polygon.
74
 * </p>
75
 * <p>
76
 * If an implementation of FeatureStore is able to get other parameters to
77
 * customize the behavior of the getDataCollection methods, there is an option
78
 * to set more parameters through the setAttribute method.
79
 * </p>
80 42975 jjdelcerro
 *
81 40435 jjdelcerro
 * @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
82
 */
83
public class DefaultFeatureQuery implements FeatureQuery {
84
85
    public static final String SCALE_PARAM_NAME = "Scale";
86
87
    private Map queryParameters = new HashMap();
88
89
    private String featureTypeId = null;
90
91
    private List attributeNames = new ArrayList();
92
93 41212 jjdelcerro
    private List constantsAttributeNames = new ArrayList();
94
95 40435 jjdelcerro
    private Evaluator filter;
96
97
    /**
98
     * This boolean value is used to know if the current filter
99
     * has been added using the {@link FeatureQuery#addFilter(Evaluator)}
100
     * method or the {@link FeatureQuery#setFilter(Evaluator)} method.
101
     */
102 42798 jjdelcerro
//    private boolean isAddFilter = true;
103 40435 jjdelcerro
104
    private FeatureQueryOrder order = new FeatureQueryOrder();
105
106
    private long limit;
107
108
    private long pageSize;
109
110
    /**
111
     * Creates a FeatureQuery which will load all available Features of a type.
112 42975 jjdelcerro
     *
113 40435 jjdelcerro
     */
114
    public DefaultFeatureQuery() {
115
        super();
116
    }
117
118
    /**
119
     * Creates a FeatureQuery which will load all available Features of a type.
120 42975 jjdelcerro
     *
121 40435 jjdelcerro
     * @param featureType
122
     *            the type of Features of the query
123
     */
124
    public DefaultFeatureQuery(FeatureType featureType) {
125
        super();
126
        this.setFeatureType(featureType);
127
    }
128
129
    /**
130
     * Creates a FeatureQuery with the type of features, a filter and the order
131
     * for the FeatureCollection.
132 42975 jjdelcerro
     *
133 40435 jjdelcerro
     * @param featureType
134
     *            the type of Features of the query
135
     * @param filter
136
     *            based on the properties of the Features
137
     */
138
    public DefaultFeatureQuery(FeatureType featureType, Evaluator filter) {
139
        super();
140
        this.setFeatureType(featureType);
141
        this.filter = filter;
142
    }
143
144
    /**
145
     * Creates a FeatureQuery with the type of features, a filter, the order for
146
     * the FeatureCollection and the view scale.
147 42975 jjdelcerro
     *
148 40435 jjdelcerro
     * @param featureType
149
     *            the type of Features of the query
150
     * @param filter
151
     *            based on the properties of the Features
152
     * @param scale
153
     *            to view the Features.
154
     */
155
    public DefaultFeatureQuery(FeatureType featureType, Evaluator filter,
156
        double scale) {
157
        this.setFeatureType(featureType);
158
        this.filter = filter;
159
        this.setScale(scale);
160
    }
161
162
    /**
163
     * Creates a FeatureQuery which will load a list of attribute names of all
164
     * available Features.
165 42975 jjdelcerro
     *
166 40435 jjdelcerro
     * @param attributeNames
167
     *            the list of attribute names to load
168
     */
169
    public DefaultFeatureQuery(String[] attributeNames) {
170
        super();
171
        setAttributeNames(attributeNames);
172
    }
173
174
    /**
175
     * Creates a FeatureQuery with the list of attribute names of feature, a
176
     * filter and the order for the FeatureCollection.
177 42975 jjdelcerro
     *
178 40435 jjdelcerro
     * @param attributeNames
179
     *            the list of attribute names to load
180
     * @param filter
181
     *            based on the properties of the Features
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 42975 jjdelcerro
     *
193 40435 jjdelcerro
     * @param attributeNames
194
     *            the list of attribute names to load
195
     * @param filter
196
     *            based on the properties of the Features
197
     * @param scale
198
     *            to view the Features.
199
     */
200
    public DefaultFeatureQuery(String[] attributeNames, Evaluator filter,
201
        double scale) {
202
        setAttributeNames(attributeNames);
203
        this.filter = filter;
204
        this.setScale(scale);
205
    }
206
207 43358 jjdelcerro
    @Override
208 40435 jjdelcerro
    public double getScale() {
209
        Double scale = (Double) this.getQueryParameter(SCALE_PARAM_NAME);
210
        if (scale == null) {
211
            return 0;
212
        }
213 43358 jjdelcerro
        return scale;
214 40435 jjdelcerro
    }
215
216 43358 jjdelcerro
    @Override
217 40435 jjdelcerro
    public void setScale(double scale) {
218 43358 jjdelcerro
        this.setQueryParameter(SCALE_PARAM_NAME, scale);
219 40435 jjdelcerro
    }
220
221 43358 jjdelcerro
    @Override
222 40435 jjdelcerro
    public Object getQueryParameter(String name) {
223
        return queryParameters.get(name);
224
    }
225
226 43358 jjdelcerro
    @Override
227 40435 jjdelcerro
    public void setQueryParameter(String name, Object value) {
228
        queryParameters.put(name, value);
229
    }
230
231 43358 jjdelcerro
    @Override
232 40435 jjdelcerro
    public void setFeatureType(FeatureType featureType) {
233
        this.featureTypeId = featureType.getId();
234
    }
235
236 43358 jjdelcerro
    @Override
237 40435 jjdelcerro
    public String[] getAttributeNames() {
238
        return (String[])attributeNames.toArray(new String[attributeNames.size()]);
239
    }
240
241 43358 jjdelcerro
    @Override
242 40435 jjdelcerro
    public void setAttributeNames(String[] attributeNames) {
243
        this.attributeNames.clear();
244
        if (attributeNames != null){
245
            for (int i=0 ; i<attributeNames.length ; i++){
246
                this.attributeNames.add(attributeNames[i]);
247 42975 jjdelcerro
            }
248 40435 jjdelcerro
        }
249
    }
250 42975 jjdelcerro
251 43358 jjdelcerro
    @Override
252 43558 jjdelcerro
    public void retrievesAllAttributes() {
253
        this.attributeNames.clear();
254
    }
255
256
    @Override
257 40435 jjdelcerro
    public void addAttributeName(String attributeName){
258
        //If the attribute exists finish the method
259
        for (int i=0 ; i<attributeNames.size() ; i++){
260
            if (attributeNames.get(i).equals(attributeName)){
261
                return;
262 42975 jjdelcerro
            }
263
        }
264 40435 jjdelcerro
        this.attributeNames.add(attributeName);
265
    }
266
267 43358 jjdelcerro
    @Override
268
    public void addEssentialAttributeNames(FeatureStore store) {
269
        FeatureType storeType;
270
        try {
271
            storeType = store.getDefaultFeatureType();
272
        } catch (DataException ex) {
273
            throw new RuntimeException("Can't access to the default feature type of tghe store", ex);
274
        }
275
        FeatureAttributeDescriptor[] pks = storeType.getPrimaryKey();
276
        if( storeType.hasOID() || ArrayUtils.isEmpty(pks) ) {
277
            FeatureAttributeDescriptor attrInt = null;
278
            FeatureAttributeDescriptor attrStr = null;
279
            FeatureAttributeDescriptor attrNotGeom = null;
280
            for (FeatureAttributeDescriptor attr : storeType) {
281
                if( attrInt == null && (attr.getType()==DataTypes.INT || attr.getType()==DataTypes.LONG) ) {
282
                    attrInt = attr;
283
                } else if( attrStr == null && attr.getType()==DataTypes.STRING ) {
284
                    attrStr = attr;
285
                } else if( attrNotGeom == null && attr.getType()!=DataTypes.GEOMETRY ) {
286
                    attrNotGeom = attr;
287
                }
288
            }
289
            if( attrInt != null ) {
290
                this.addAttributeName(attrInt.getName());
291
            } else if( attrStr != null ) {
292
                this.addAttributeName(attrStr.getName());
293
            } else if( attrNotGeom != null ) {
294
                this.addAttributeName(attrNotGeom.getName());
295
            } else {
296
                this.addAttributeName(storeType.getAttributeDescriptor(0).getName());
297
            }
298
        } else {
299
            for(FeatureAttributeDescriptor attr : pks ) {
300
                this.addAttributeName(attr.getName());
301
            }
302
        }
303
    }
304
305
    @Override
306
    public void addPrimaryKeyAttributeNames(FeatureStore store) {
307
        FeatureType storeType;
308
        try {
309
            storeType = store.getDefaultFeatureType();
310
        } catch (DataException ex) {
311
            throw new RuntimeException("Can't access to the default feature type of tghe store", ex);
312
        }
313
        for(FeatureAttributeDescriptor attr : storeType.getPrimaryKey() ) {
314
            this.addAttributeName(attr.getName());
315
        }
316
    }
317
318
    @Override
319 41212 jjdelcerro
    public boolean hasAttributeNames() {
320
        return !this.attributeNames.isEmpty();
321
    }
322
323 43358 jjdelcerro
    @Override
324 41212 jjdelcerro
    public void clearAttributeNames() {
325
        this.attributeNames = new ArrayList();
326
    }
327
328 43358 jjdelcerro
    @Override
329 40435 jjdelcerro
    public Evaluator getFilter() {
330
        return filter;
331
    }
332 42975 jjdelcerro
333 42971 jjdelcerro
    @Override
334 44023 jjdelcerro
    public void setFilter(Expression filter) {
335 44259 jjdelcerro
        if( filter == null ) {
336
            this.clearFilter();
337
            return;
338
        }
339 44023 jjdelcerro
        Evaluator x = new DefaultExpressionEvaluator(filter);
340
        this.setFilter(x);
341
    }
342
343
   @Override
344 42971 jjdelcerro
    public void setFilter(String filter) {
345 43913 jjdelcerro
        if( StringUtils.isEmpty(filter) ) {
346
            this.clearFilter();
347
            return;
348
        }
349 42971 jjdelcerro
        try {
350
            this.setFilter(DALLocator.getDataManager().createExpresion(filter));
351
        } catch (Exception ex) {
352
            throw new RuntimeException("Can't create filter from '"+filter+"'",ex);
353
        }
354
    }
355 40435 jjdelcerro
356 43358 jjdelcerro
    @Override
357 40435 jjdelcerro
    public void setFilter(Evaluator filter) {
358 43913 jjdelcerro
        if( filter == null ) {
359
            this.clearFilter();
360
            return;
361
        }
362 40435 jjdelcerro
        this.filter = filter;
363
        addFilterAttributes(filter);
364
    }
365
366 42795 jjdelcerro
    @Override
367 42971 jjdelcerro
    public void addFilter(String filter) {
368 43913 jjdelcerro
        if( StringUtils.isEmpty(filter) ) {
369
            return;
370
        }
371 42971 jjdelcerro
        try {
372
            this.addFilter(DALLocator.getDataManager().createExpresion(filter));
373
        } catch (Exception ex) {
374
            throw new RuntimeException("Can't create filter from '"+filter+"'",ex);
375
        }
376
    }
377 42975 jjdelcerro
378 42971 jjdelcerro
    @Override
379 44023 jjdelcerro
    public void addFilter(Expression filter) {
380
        Evaluator x = new DefaultExpressionEvaluator(filter);
381
        this.addFilter(x);
382
    }
383
384
    @Override
385 40435 jjdelcerro
    public void addFilter(Evaluator evaluator) {
386 43913 jjdelcerro
        if (evaluator == null) {
387 42795 jjdelcerro
            return;
388
        }
389 43913 jjdelcerro
        if (this.filter == null) {
390
            this.filter = evaluator;
391
        } else {
392
            if (this.filter instanceof AndEvaluator) {
393
                ((AndEvaluator) this.filter).addEvaluator(evaluator);
394
            } else {
395
                this.filter = new AndEvaluator(this.filter);
396
                ((AndEvaluator) this.filter).addEvaluator(evaluator);
397 40435 jjdelcerro
            }
398 43913 jjdelcerro
        }
399 40435 jjdelcerro
        addFilterAttributes(evaluator);
400
    }
401 42975 jjdelcerro
402 43358 jjdelcerro
    @Override
403 42975 jjdelcerro
    public void clearFilter() {
404
      this.filter = null;
405
    }
406
407 40435 jjdelcerro
    private void addFilterAttributes(Evaluator evaluator){
408
        if (evaluator != null){
409
            EvaluatorFieldsInfo fieldsInfo = evaluator.getFieldsInfo();
410
            if (fieldsInfo == null){
411
                // FieldsInfo is not available in this evaluator
412
                return;
413
            }
414
            String[] fieldNames = fieldsInfo.getFieldNames();
415
            if (fieldNames== null){
416
                // fieldNames is not available in this evaluator
417
                return;
418
            }
419 42975 jjdelcerro
420 40435 jjdelcerro
            for (int i=0 ; i<fieldNames.length ; i++){
421
                addAttributeName(fieldNames[i]);
422
            }
423
        }
424
    }
425
426 43358 jjdelcerro
    @Override
427 40435 jjdelcerro
    public FeatureQueryOrder getOrder() {
428
        return order;
429
    }
430
431 43358 jjdelcerro
    @Override
432 40435 jjdelcerro
    public void setOrder(FeatureQueryOrder order) {
433
        this.order = order;
434
    }
435
436 43358 jjdelcerro
    @Override
437 40435 jjdelcerro
    public boolean hasFilter() {
438
        return this.filter != null;
439
    }
440
441 43358 jjdelcerro
    @Override
442 40435 jjdelcerro
    public boolean hasOrder() {
443
        return this.order != null && this.order.size() > 0;
444
    }
445
446 43358 jjdelcerro
    @Override
447 40435 jjdelcerro
    public Object clone() throws CloneNotSupportedException {
448
        DefaultFeatureQuery clone = (DefaultFeatureQuery) super.clone();
449
450
        // Clone attribute names array
451
        if (attributeNames != null) {
452
            clone.attributeNames = new ArrayList();
453
            for (int i=0 ; i<attributeNames.size() ; i++){
454
                clone.attributeNames.add(attributeNames.get(i));
455 42975 jjdelcerro
            }
456 40435 jjdelcerro
        }
457
458
        // Clone order
459
        if (order != null) {
460
            clone.order = (FeatureQueryOrder) order.clone();
461
        }
462
463
        return clone;
464
    }
465
466 43358 jjdelcerro
    @Override
467 40435 jjdelcerro
    public FeatureQuery getCopy() {
468
        try {
469
            return (FeatureQuery) clone();
470
        } catch (CloneNotSupportedException e) {
471
            // TODO Auto-generated catch block
472
            e.printStackTrace();
473
            return null;
474
        }
475
        // DefaultFeatureQuery aCopy = new DefaultFeatureQuery();
476
        //
477
        // aCopy.featureTypeId = this.featureTypeId;
478
        //
479
        // if (this.attributeNames != null) {
480
        // aCopy.attributeNames = (String[]) Arrays
481
        // .asList(this.attributeNames).toArray(new String[0]);
482
        // }
483
        //
484
        // aCopy.filter = this.filter;
485
        //
486
        // if (this.order != null) {
487
        // aCopy.order = this.order.getCopy();
488
        // }
489
        //
490
        // return aCopy;
491
    }
492
493 43358 jjdelcerro
    @Override
494 40435 jjdelcerro
    public String getFeatureTypeId() {
495
        return featureTypeId;
496
    }
497
498 43358 jjdelcerro
    @Override
499 40435 jjdelcerro
    public void setFeatureTypeId(String featureTypeId) {
500
        this.featureTypeId = featureTypeId;
501
    }
502
503 43358 jjdelcerro
    @Override
504 40435 jjdelcerro
    public void saveToState(PersistentState state) throws PersistenceException {
505
        // FIXME: falta por terminar de implementar
506
        state.set("queryParameters", this.queryParameters);
507
        state.set("featureTypeId", this.featureTypeId);
508
        state.set("attributeNames", this.attributeNames);
509
        // state.set("filter", this.filter);
510
        state.set("limit", this.limit);
511
        state.set("pageSize", this.pageSize);
512
513
    }
514
515 43358 jjdelcerro
    @Override
516 40435 jjdelcerro
    public void loadFromState(PersistentState state) throws PersistenceException {
517
        // FIXME: falta por terminar de implementar
518
        this.queryParameters = (Map) state.get("queryParameters");
519
        this.featureTypeId = state.getString("featureTypeId");
520
        this.attributeNames = state.getList("attributeNames");
521
        this.filter = (Evaluator) state.get("filter");
522
        this.limit = state.getLong("limit");
523
        this.pageSize = state.getLong("pageSize");
524
525
    }
526
527
    /**
528
     * Register the class on PersistenceManager
529 42975 jjdelcerro
     *
530 40435 jjdelcerro
     */
531
    public static void registerPersistent() {
532
        DynStruct definition =
533
            ToolsLocator.getPersistenceManager()
534
            .addDefinition(DefaultFeatureQuery.class,
535
                "DefaultFeatureQuery",
536
                "DefaultFeatureQuery Persistent definition",
537
                null,
538
                null);
539
540
        definition.addDynFieldMap("queryParameters")
541
        .setClassOfItems(Object.class)
542
        .setMandatory(true);
543
544
        definition.addDynFieldString("featureTypeId").setMandatory(false);
545
546
        definition.addDynFieldList("attributeNames")
547
        .setClassOfItems(String.class)
548
        .setMandatory(false);
549
550
        definition.addDynFieldObject("filter")
551
        .setClassOfValue(Evaluator.class)
552
        .setMandatory(false);
553
554
        definition.addDynFieldObject("order")
555
        .setClassOfValue(FeatureQueryOrder.class)
556
        .setMandatory(false);
557
558
        definition.addDynFieldLong("limit").setMandatory(false);
559
560
        definition.addDynFieldLong("pageSize").setMandatory(false);
561
562
    }
563
564 43358 jjdelcerro
    @Override
565 40435 jjdelcerro
    public long getLimit() {
566
        return limit;
567
    }
568
569 43358 jjdelcerro
    @Override
570 40435 jjdelcerro
    public long getPageSize() {
571
        return pageSize;
572
    }
573
574 43358 jjdelcerro
    @Override
575 40435 jjdelcerro
    public void setLimit(long limit) {
576
        this.limit = limit;
577
    }
578
579 43358 jjdelcerro
    @Override
580 40435 jjdelcerro
    public void setPageSize(long pageSize) {
581
        this.pageSize = pageSize;
582
    }
583
584 43358 jjdelcerro
    @Override
585 41212 jjdelcerro
    public String[] getConstantsAttributeNames() {
586
        return (String[])constantsAttributeNames.toArray(new String[constantsAttributeNames.size()]);
587
    }
588
589 43358 jjdelcerro
    @Override
590 41212 jjdelcerro
    public void setConstantsAttributeNames(String[] constantsAttributeNames) {
591
        this.constantsAttributeNames.clear();
592
        if (constantsAttributeNames != null){
593
            for (int i=0 ; i<constantsAttributeNames.length ; i++){
594
                this.constantsAttributeNames.add(constantsAttributeNames[i]);
595 42975 jjdelcerro
            }
596 41212 jjdelcerro
        }
597
    }
598 42975 jjdelcerro
599 43358 jjdelcerro
    @Override
600 41212 jjdelcerro
    public void addConstantAttributeName(String attributeName) {
601
        //If the attribute exists finish the method
602
        for (int i=0 ; i<constantsAttributeNames.size() ; i++){
603
            if (constantsAttributeNames.get(i).equals(attributeName)){
604
                return;
605 42975 jjdelcerro
            }
606
        }
607 41212 jjdelcerro
        this.constantsAttributeNames.add(attributeName);
608
    }
609
610 43358 jjdelcerro
    @Override
611 41212 jjdelcerro
    public boolean hasConstantsAttributeNames() {
612
        return !this.constantsAttributeNames.isEmpty();
613
    }
614
615 43358 jjdelcerro
    @Override
616 41212 jjdelcerro
    public void clearConstantsAttributeNames() {
617
        this.constantsAttributeNames = new ArrayList();
618
    }
619
620 40435 jjdelcerro
}