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

History | View | Annotate | Download (18.8 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
        Evaluator x = new DefaultExpressionEvaluator(filter);
336
        this.setFilter(x);
337
    }
338
339
   @Override
340 42971 jjdelcerro
    public void setFilter(String filter) {
341 43913 jjdelcerro
        if( StringUtils.isEmpty(filter) ) {
342
            this.clearFilter();
343
            return;
344
        }
345 42971 jjdelcerro
        try {
346
            this.setFilter(DALLocator.getDataManager().createExpresion(filter));
347
        } catch (Exception ex) {
348
            throw new RuntimeException("Can't create filter from '"+filter+"'",ex);
349
        }
350
    }
351 40435 jjdelcerro
352 43358 jjdelcerro
    @Override
353 40435 jjdelcerro
    public void setFilter(Evaluator filter) {
354 43913 jjdelcerro
        if( filter == null ) {
355
            this.clearFilter();
356
            return;
357
        }
358 40435 jjdelcerro
        this.filter = filter;
359
        addFilterAttributes(filter);
360
    }
361
362 42795 jjdelcerro
    @Override
363 42971 jjdelcerro
    public void addFilter(String filter) {
364 43913 jjdelcerro
        if( StringUtils.isEmpty(filter) ) {
365
            return;
366
        }
367 42971 jjdelcerro
        try {
368
            this.addFilter(DALLocator.getDataManager().createExpresion(filter));
369
        } catch (Exception ex) {
370
            throw new RuntimeException("Can't create filter from '"+filter+"'",ex);
371
        }
372
    }
373 42975 jjdelcerro
374 42971 jjdelcerro
    @Override
375 44023 jjdelcerro
    public void addFilter(Expression filter) {
376
        Evaluator x = new DefaultExpressionEvaluator(filter);
377
        this.addFilter(x);
378
    }
379
380
    @Override
381 40435 jjdelcerro
    public void addFilter(Evaluator evaluator) {
382 43913 jjdelcerro
        if (evaluator == null) {
383 42795 jjdelcerro
            return;
384
        }
385 43913 jjdelcerro
        if (this.filter == null) {
386
            this.filter = evaluator;
387
        } else {
388
            if (this.filter instanceof AndEvaluator) {
389
                ((AndEvaluator) this.filter).addEvaluator(evaluator);
390
            } else {
391
                this.filter = new AndEvaluator(this.filter);
392
                ((AndEvaluator) this.filter).addEvaluator(evaluator);
393 40435 jjdelcerro
            }
394 43913 jjdelcerro
        }
395 40435 jjdelcerro
        addFilterAttributes(evaluator);
396
    }
397 42975 jjdelcerro
398 43358 jjdelcerro
    @Override
399 42975 jjdelcerro
    public void clearFilter() {
400
      this.filter = null;
401
    }
402
403 40435 jjdelcerro
    private void addFilterAttributes(Evaluator evaluator){
404
        if (evaluator != null){
405
            EvaluatorFieldsInfo fieldsInfo = evaluator.getFieldsInfo();
406
            if (fieldsInfo == null){
407
                // FieldsInfo is not available in this evaluator
408
                return;
409
            }
410
            String[] fieldNames = fieldsInfo.getFieldNames();
411
            if (fieldNames== null){
412
                // fieldNames is not available in this evaluator
413
                return;
414
            }
415 42975 jjdelcerro
416 40435 jjdelcerro
            for (int i=0 ; i<fieldNames.length ; i++){
417
                addAttributeName(fieldNames[i]);
418
            }
419
        }
420
    }
421
422 43358 jjdelcerro
    @Override
423 40435 jjdelcerro
    public FeatureQueryOrder getOrder() {
424
        return order;
425
    }
426
427 43358 jjdelcerro
    @Override
428 40435 jjdelcerro
    public void setOrder(FeatureQueryOrder order) {
429
        this.order = order;
430
    }
431
432 43358 jjdelcerro
    @Override
433 40435 jjdelcerro
    public boolean hasFilter() {
434
        return this.filter != null;
435
    }
436
437 43358 jjdelcerro
    @Override
438 40435 jjdelcerro
    public boolean hasOrder() {
439
        return this.order != null && this.order.size() > 0;
440
    }
441
442 43358 jjdelcerro
    @Override
443 40435 jjdelcerro
    public Object clone() throws CloneNotSupportedException {
444
        DefaultFeatureQuery clone = (DefaultFeatureQuery) super.clone();
445
446
        // Clone attribute names array
447
        if (attributeNames != null) {
448
            clone.attributeNames = new ArrayList();
449
            for (int i=0 ; i<attributeNames.size() ; i++){
450
                clone.attributeNames.add(attributeNames.get(i));
451 42975 jjdelcerro
            }
452 40435 jjdelcerro
        }
453
454
        // Clone order
455
        if (order != null) {
456
            clone.order = (FeatureQueryOrder) order.clone();
457
        }
458
459
        return clone;
460
    }
461
462 43358 jjdelcerro
    @Override
463 40435 jjdelcerro
    public FeatureQuery getCopy() {
464
        try {
465
            return (FeatureQuery) clone();
466
        } catch (CloneNotSupportedException e) {
467
            // TODO Auto-generated catch block
468
            e.printStackTrace();
469
            return null;
470
        }
471
        // DefaultFeatureQuery aCopy = new DefaultFeatureQuery();
472
        //
473
        // aCopy.featureTypeId = this.featureTypeId;
474
        //
475
        // if (this.attributeNames != null) {
476
        // aCopy.attributeNames = (String[]) Arrays
477
        // .asList(this.attributeNames).toArray(new String[0]);
478
        // }
479
        //
480
        // aCopy.filter = this.filter;
481
        //
482
        // if (this.order != null) {
483
        // aCopy.order = this.order.getCopy();
484
        // }
485
        //
486
        // return aCopy;
487
    }
488
489 43358 jjdelcerro
    @Override
490 40435 jjdelcerro
    public String getFeatureTypeId() {
491
        return featureTypeId;
492
    }
493
494 43358 jjdelcerro
    @Override
495 40435 jjdelcerro
    public void setFeatureTypeId(String featureTypeId) {
496
        this.featureTypeId = featureTypeId;
497
    }
498
499 43358 jjdelcerro
    @Override
500 40435 jjdelcerro
    public void saveToState(PersistentState state) throws PersistenceException {
501
        // FIXME: falta por terminar de implementar
502
        state.set("queryParameters", this.queryParameters);
503
        state.set("featureTypeId", this.featureTypeId);
504
        state.set("attributeNames", this.attributeNames);
505
        // state.set("filter", this.filter);
506
        state.set("limit", this.limit);
507
        state.set("pageSize", this.pageSize);
508
509
    }
510
511 43358 jjdelcerro
    @Override
512 40435 jjdelcerro
    public void loadFromState(PersistentState state) throws PersistenceException {
513
        // FIXME: falta por terminar de implementar
514
        this.queryParameters = (Map) state.get("queryParameters");
515
        this.featureTypeId = state.getString("featureTypeId");
516
        this.attributeNames = state.getList("attributeNames");
517
        this.filter = (Evaluator) state.get("filter");
518
        this.limit = state.getLong("limit");
519
        this.pageSize = state.getLong("pageSize");
520
521
    }
522
523
    /**
524
     * Register the class on PersistenceManager
525 42975 jjdelcerro
     *
526 40435 jjdelcerro
     */
527
    public static void registerPersistent() {
528
        DynStruct definition =
529
            ToolsLocator.getPersistenceManager()
530
            .addDefinition(DefaultFeatureQuery.class,
531
                "DefaultFeatureQuery",
532
                "DefaultFeatureQuery Persistent definition",
533
                null,
534
                null);
535
536
        definition.addDynFieldMap("queryParameters")
537
        .setClassOfItems(Object.class)
538
        .setMandatory(true);
539
540
        definition.addDynFieldString("featureTypeId").setMandatory(false);
541
542
        definition.addDynFieldList("attributeNames")
543
        .setClassOfItems(String.class)
544
        .setMandatory(false);
545
546
        definition.addDynFieldObject("filter")
547
        .setClassOfValue(Evaluator.class)
548
        .setMandatory(false);
549
550
        definition.addDynFieldObject("order")
551
        .setClassOfValue(FeatureQueryOrder.class)
552
        .setMandatory(false);
553
554
        definition.addDynFieldLong("limit").setMandatory(false);
555
556
        definition.addDynFieldLong("pageSize").setMandatory(false);
557
558
    }
559
560 43358 jjdelcerro
    @Override
561 40435 jjdelcerro
    public long getLimit() {
562
        return limit;
563
    }
564
565 43358 jjdelcerro
    @Override
566 40435 jjdelcerro
    public long getPageSize() {
567
        return pageSize;
568
    }
569
570 43358 jjdelcerro
    @Override
571 40435 jjdelcerro
    public void setLimit(long limit) {
572
        this.limit = limit;
573
    }
574
575 43358 jjdelcerro
    @Override
576 40435 jjdelcerro
    public void setPageSize(long pageSize) {
577
        this.pageSize = pageSize;
578
    }
579
580 43358 jjdelcerro
    @Override
581 41212 jjdelcerro
    public String[] getConstantsAttributeNames() {
582
        return (String[])constantsAttributeNames.toArray(new String[constantsAttributeNames.size()]);
583
    }
584
585 43358 jjdelcerro
    @Override
586 41212 jjdelcerro
    public void setConstantsAttributeNames(String[] constantsAttributeNames) {
587
        this.constantsAttributeNames.clear();
588
        if (constantsAttributeNames != null){
589
            for (int i=0 ; i<constantsAttributeNames.length ; i++){
590
                this.constantsAttributeNames.add(constantsAttributeNames[i]);
591 42975 jjdelcerro
            }
592 41212 jjdelcerro
        }
593
    }
594 42975 jjdelcerro
595 43358 jjdelcerro
    @Override
596 41212 jjdelcerro
    public void addConstantAttributeName(String attributeName) {
597
        //If the attribute exists finish the method
598
        for (int i=0 ; i<constantsAttributeNames.size() ; i++){
599
            if (constantsAttributeNames.get(i).equals(attributeName)){
600
                return;
601 42975 jjdelcerro
            }
602
        }
603 41212 jjdelcerro
        this.constantsAttributeNames.add(attributeName);
604
    }
605
606 43358 jjdelcerro
    @Override
607 41212 jjdelcerro
    public boolean hasConstantsAttributeNames() {
608
        return !this.constantsAttributeNames.isEmpty();
609
    }
610
611 43358 jjdelcerro
    @Override
612 41212 jjdelcerro
    public void clearConstantsAttributeNames() {
613
        this.constantsAttributeNames = new ArrayList();
614
    }
615
616 40435 jjdelcerro
}