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

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