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

History | View | Annotate | Download (19 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
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
import org.apache.commons.lang3.ArrayUtils;
31
import org.apache.commons.lang3.StringUtils;
32
import org.gvsig.expressionevaluator.Expression;
33
import org.gvsig.fmap.dal.DALLocator;
34
import org.gvsig.fmap.dal.DataTypes;
35
import org.gvsig.fmap.dal.exception.DataException;
36
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
37
import org.gvsig.fmap.dal.feature.FeatureQuery;
38
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
39
import org.gvsig.fmap.dal.feature.FeatureStore;
40
import org.gvsig.fmap.dal.feature.FeatureType;
41
import org.gvsig.fmap.dal.impl.expressionevaluator.DefaultExpressionEvaluator;
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
 *
78
 * @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
    private List constantsAttributeNames = new ArrayList();
91

    
92
    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
//    private boolean isAddFilter = true;
100

    
101
    private FeatureQueryOrder order = new FeatureQueryOrder();
102

    
103
    private long limit;
104

    
105
    private long pageSize;
106

    
107
    private boolean group = false;
108
    /**
109
     * Creates a FeatureQuery which will load all available Features of a type.
110
     *
111
     */
112
    public DefaultFeatureQuery() {
113
        super();
114
    }
115

    
116
    /**
117
     * Creates a FeatureQuery which will load all available Features of a type.
118
     *
119
     * @param featureType
120
     *            the type of Features of the query
121
     */
122
    public DefaultFeatureQuery(FeatureType featureType) {
123
        super();
124
        this.setFeatureType(featureType);
125
    }
126

    
127
    /**
128
     * Creates a FeatureQuery with the type of features, a filter and the order
129
     * for the FeatureCollection.
130
     *
131
     * @param featureType
132
     *            the type of Features of the query
133
     * @param filter
134
     *            based on the properties of the Features
135
     */
136
    public DefaultFeatureQuery(FeatureType featureType, Evaluator filter) {
137
        super();
138
        this.setFeatureType(featureType);
139
        this.filter = filter;
140
    }
141

    
142
    /**
143
     * Creates a FeatureQuery with the type of features, a filter, the order for
144
     * the FeatureCollection and the view scale.
145
     *
146
     * @param featureType
147
     *            the type of Features of the query
148
     * @param filter
149
     *            based on the properties of the Features
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
     */
181
    public DefaultFeatureQuery(String[] attributeNames, Evaluator filter) {
182
        super();
183
        setAttributeNames(attributeNames);
184
        this.filter = filter;
185
    }
186

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

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

    
214
    @Override
215
    public void setScale(double scale) {
216
        this.setQueryParameter(SCALE_PARAM_NAME, scale);
217
    }
218

    
219
    @Override
220
    public Object getQueryParameter(String name) {
221
        return queryParameters.get(name);
222
    }
223

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

    
229
    @Override
230
    public void setFeatureType(FeatureType featureType) {
231
        this.featureTypeId = featureType.getId();
232
    }
233

    
234
    @Override
235
    public String[] getAttributeNames() {
236
        return (String[])attributeNames.toArray(new String[attributeNames.size()]);
237
    }
238

    
239
    @Override
240
    public void setAttributeNames(String[] attributeNames) {
241
        this.attributeNames.clear();
242
        if (attributeNames != null){
243
            for (int i=0 ; i<attributeNames.length ; i++){
244
                this.attributeNames.add(attributeNames[i]);
245
            }
246
        }
247
    }
248

    
249
    @Override
250
    public void retrievesAllAttributes() {
251
        this.attributeNames.clear();
252
    } 
253
    
254
    @Override
255
    public void addAttributeName(String attributeName){
256
        //If the attribute exists finish the method
257
        for (int i=0 ; i<attributeNames.size() ; i++){
258
            if (attributeNames.get(i).equals(attributeName)){
259
                return;
260
            }
261
        }
262
        this.attributeNames.add(attributeName);
263
    }
264

    
265
    @Override
266
    public void addEssentialAttributeNames(FeatureStore store) {
267
        FeatureType storeType;
268
        try {
269
            storeType = store.getDefaultFeatureType();
270
        } catch (DataException ex) {
271
            throw new RuntimeException("Can't access to the default feature type of tghe store", ex);
272
        }
273
        FeatureAttributeDescriptor[] pks = storeType.getPrimaryKey();
274
        if( storeType.hasOID() || ArrayUtils.isEmpty(pks) ) {
275
            FeatureAttributeDescriptor attrInt = null;
276
            FeatureAttributeDescriptor attrStr = null;
277
            FeatureAttributeDescriptor attrNotGeom = null;
278
            for (FeatureAttributeDescriptor attr : storeType) {
279
                if( attrInt == null && (attr.getType()==DataTypes.INT || attr.getType()==DataTypes.LONG) ) {
280
                    attrInt = attr;
281
                } else if( attrStr == null && attr.getType()==DataTypes.STRING ) {
282
                    attrStr = attr;
283
                } else if( attrNotGeom == null && attr.getType()!=DataTypes.GEOMETRY ) {
284
                    attrNotGeom = attr;
285
                }
286
            }
287
            if( attrInt != null ) {
288
                this.addAttributeName(attrInt.getName());
289
            } else if( attrStr != null ) {
290
                this.addAttributeName(attrStr.getName());
291
            } else if( attrNotGeom != null ) {
292
                this.addAttributeName(attrNotGeom.getName());
293
            } else {
294
                this.addAttributeName(storeType.getAttributeDescriptor(0).getName());
295
            }
296
        } else {
297
            for(FeatureAttributeDescriptor attr : pks ) {
298
                this.addAttributeName(attr.getName());
299
            }
300
        }
301
    }
302
    
303
    @Override
304
    public void addPrimaryKeyAttributeNames(FeatureStore store) {
305
        FeatureType storeType;
306
        try {
307
            storeType = store.getDefaultFeatureType();
308
        } catch (DataException ex) {
309
            throw new RuntimeException("Can't access to the default feature type of tghe store", ex);
310
        }
311
        for(FeatureAttributeDescriptor attr : storeType.getPrimaryKey() ) {
312
            this.addAttributeName(attr.getName());
313
        }
314
    }
315

    
316
    @Override
317
    public boolean hasAttributeNames() {
318
        return !this.attributeNames.isEmpty();
319
    }
320

    
321
    @Override
322
    public void clearAttributeNames() {
323
        this.attributeNames = new ArrayList();
324
    }
325

    
326
    @Override
327
    public Evaluator getFilter() {
328
        return filter;
329
    }
330

    
331
    @Override
332
    public void setFilter(Expression filter) {
333
        if( filter == null ) {
334
            this.clearFilter();
335
            return;
336
        }
337
        Evaluator x = new DefaultExpressionEvaluator(filter);
338
        this.setFilter(x);
339
    }
340

    
341
   @Override
342
    public void setFilter(String filter) {
343
        if( StringUtils.isEmpty(filter) ) {
344
            this.clearFilter();
345
            return;
346
        }
347
        try {
348
            this.setFilter(DALLocator.getDataManager().createExpresion(filter));
349
        } catch (Exception ex) {
350
            throw new RuntimeException("Can't create filter from '"+filter+"'",ex);
351
        }
352
    }
353

    
354
    @Override
355
    public void setFilter(Evaluator filter) {
356
        if( filter == null ) {
357
            this.clearFilter();
358
            return;
359
        }        
360
        this.filter = filter;
361
        addFilterAttributes(filter);
362
    }
363

    
364
    @Override
365
    public void addFilter(String filter) {
366
        if( StringUtils.isEmpty(filter) ) {
367
            return;
368
        }
369
        try {
370
            this.addFilter(DALLocator.getDataManager().createExpresion(filter));
371
        } catch (Exception ex) {
372
            throw new RuntimeException("Can't create filter from '"+filter+"'",ex);
373
        }
374
    }
375

    
376
    @Override
377
    public void addFilter(Expression filter) {
378
        Evaluator x = new DefaultExpressionEvaluator(filter);
379
        this.addFilter(x);
380
    }
381

    
382
    @Override
383
    public void addFilter(Evaluator evaluator) {
384
        if (evaluator == null) {
385
            return;
386
        }
387
        if (this.filter == null) {
388
            this.filter = evaluator;
389
        } else {
390
            if (this.filter instanceof AndEvaluator) {
391
                ((AndEvaluator) this.filter).addEvaluator(evaluator);
392
            } else {
393
                this.filter = new AndEvaluator(this.filter);
394
                ((AndEvaluator) this.filter).addEvaluator(evaluator);
395
            }
396
        }
397
        addFilterAttributes(evaluator);
398
    }
399

    
400
    @Override
401
    public void clearFilter() {
402
      this.filter = null;
403
    }
404

    
405
    private void addFilterAttributes(Evaluator evaluator){
406
        if (evaluator != null){
407
            EvaluatorFieldsInfo fieldsInfo = evaluator.getFieldsInfo();
408
            if (fieldsInfo == null){
409
                // FieldsInfo is not available in this evaluator
410
                return;
411
            }
412
            String[] fieldNames = fieldsInfo.getFieldNames();
413
            if (fieldNames== null){
414
                // fieldNames is not available in this evaluator
415
                return;
416
            }
417

    
418
            for (int i=0 ; i<fieldNames.length ; i++){
419
                addAttributeName(fieldNames[i]);
420
            }
421
        }
422
    }
423

    
424
    @Override
425
    public FeatureQueryOrder getOrder() {
426
        return order;
427
    }
428

    
429
    @Override
430
    public void setOrder(FeatureQueryOrder order) {
431
        this.order = order;
432
    }
433

    
434
    @Override
435
    public boolean hasFilter() {
436
        return this.filter != null;
437
    }
438

    
439
    @Override
440
    public boolean hasOrder() {
441
        return this.order != null && this.order.size() > 0;
442
    }
443

    
444
    @Override
445
    public Object clone() throws CloneNotSupportedException {
446
        DefaultFeatureQuery clone = (DefaultFeatureQuery) super.clone();
447

    
448
        // Clone attribute names array
449
        if (attributeNames != null) {
450
            clone.attributeNames = new ArrayList();
451
            for (int i=0 ; i<attributeNames.size() ; i++){
452
                clone.attributeNames.add(attributeNames.get(i));
453
            }
454
        }
455

    
456
        // Clone order
457
        if (order != null) {
458
            clone.order = (FeatureQueryOrder) order.clone();
459
        }
460

    
461
        return clone;
462
    }
463

    
464
    @Override
465
    public FeatureQuery getCopy() {
466
        try {
467
            return (FeatureQuery) clone();
468
        } catch (CloneNotSupportedException e) {
469
            // TODO Auto-generated catch block
470
            e.printStackTrace();
471
            return null;
472
        }
473
        // DefaultFeatureQuery aCopy = new DefaultFeatureQuery();
474
        //
475
        // aCopy.featureTypeId = this.featureTypeId;
476
        //
477
        // if (this.attributeNames != null) {
478
        // aCopy.attributeNames = (String[]) Arrays
479
        // .asList(this.attributeNames).toArray(new String[0]);
480
        // }
481
        //
482
        // aCopy.filter = this.filter;
483
        //
484
        // if (this.order != null) {
485
        // aCopy.order = this.order.getCopy();
486
        // }
487
        //
488
        // return aCopy;
489
    }
490

    
491
    @Override
492
    public String getFeatureTypeId() {
493
        return featureTypeId;
494
    }
495

    
496
    @Override
497
    public void setFeatureTypeId(String featureTypeId) {
498
        this.featureTypeId = featureTypeId;
499
    }
500

    
501
    @Override
502
    public void saveToState(PersistentState state) throws PersistenceException {
503
        // FIXME: falta por terminar de implementar
504
        state.set("queryParameters", this.queryParameters);
505
        state.set("featureTypeId", this.featureTypeId);
506
        state.set("attributeNames", this.attributeNames);
507
        // state.set("filter", this.filter);
508
        state.set("limit", this.limit);
509
        state.set("pageSize", this.pageSize);
510

    
511
    }
512

    
513
    @Override
514
    public void loadFromState(PersistentState state) throws PersistenceException {
515
        // FIXME: falta por terminar de implementar
516
        this.queryParameters = (Map) state.get("queryParameters");
517
        this.featureTypeId = state.getString("featureTypeId");
518
        this.attributeNames = state.getList("attributeNames");
519
        this.filter = (Evaluator) state.get("filter");
520
        this.limit = state.getLong("limit");
521
        this.pageSize = state.getLong("pageSize");
522

    
523
    }
524

    
525
    /**
526
     * Register the class on PersistenceManager
527
     *
528
     */
529
    public static void registerPersistent() {
530
        DynStruct definition =
531
            ToolsLocator.getPersistenceManager()
532
            .addDefinition(DefaultFeatureQuery.class,
533
                "DefaultFeatureQuery",
534
                "DefaultFeatureQuery Persistent definition",
535
                null,
536
                null);
537

    
538
        definition.addDynFieldMap("queryParameters")
539
        .setClassOfItems(Object.class)
540
        .setMandatory(true);
541

    
542
        definition.addDynFieldString("featureTypeId").setMandatory(false);
543

    
544
        definition.addDynFieldList("attributeNames")
545
        .setClassOfItems(String.class)
546
        .setMandatory(false);
547

    
548
        definition.addDynFieldObject("filter")
549
        .setClassOfValue(Evaluator.class)
550
        .setMandatory(false);
551

    
552
        definition.addDynFieldObject("order")
553
        .setClassOfValue(FeatureQueryOrder.class)
554
        .setMandatory(false);
555

    
556
        definition.addDynFieldLong("limit").setMandatory(false);
557

    
558
        definition.addDynFieldLong("pageSize").setMandatory(false);
559

    
560
    }
561

    
562
    @Override
563
    public long getLimit() {
564
        return limit;
565
    }
566

    
567
    @Override
568
    public long getPageSize() {
569
        return pageSize;
570
    }
571

    
572
    @Override
573
    public void setLimit(long limit) {
574
        this.limit = limit;
575
    }
576

    
577
    @Override
578
    public void setPageSize(long pageSize) {
579
        this.pageSize = pageSize;
580
    }
581

    
582
    @Override
583
    public String[] getConstantsAttributeNames() {
584
        return (String[])constantsAttributeNames.toArray(new String[constantsAttributeNames.size()]);
585
    }
586

    
587
    @Override
588
    public void setConstantsAttributeNames(String[] constantsAttributeNames) {
589
        this.constantsAttributeNames.clear();
590
        if (constantsAttributeNames != null){
591
            for (int i=0 ; i<constantsAttributeNames.length ; i++){
592
                this.constantsAttributeNames.add(constantsAttributeNames[i]);
593
            }
594
        }
595
    }
596

    
597
    @Override
598
    public void addConstantAttributeName(String attributeName) {
599
        //If the attribute exists finish the method
600
        for (int i=0 ; i<constantsAttributeNames.size() ; i++){
601
            if (constantsAttributeNames.get(i).equals(attributeName)){
602
                return;
603
            }
604
        }
605
        this.constantsAttributeNames.add(attributeName);
606
    }
607

    
608
    @Override
609
    public boolean hasConstantsAttributeNames() {
610
        return !this.constantsAttributeNames.isEmpty();
611
    }
612

    
613
    @Override
614
    public void clearConstantsAttributeNames() {
615
        this.constantsAttributeNames = new ArrayList();
616
    }
617

    
618
    @Override
619
    public boolean isGrouped() {
620
        return this.group;
621
    }
622

    
623
    @Override
624
    public void setGroup(boolean group) {
625
        this.group = group;
626
    }
627

    
628
}