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
/**
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 java.util.logging.Level;
31
import java.util.logging.Logger;
32
import org.apache.commons.lang3.ArrayUtils;
33
import org.apache.commons.lang3.StringUtils;
34
import org.gvsig.expressionevaluator.Expression;
35
import org.gvsig.fmap.dal.DALLocator;
36
import org.gvsig.fmap.dal.DataTypes;
37
import org.gvsig.fmap.dal.exception.DataException;
38
import org.gvsig.fmap.dal.exception.InitializeException;
39
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
40
import org.gvsig.fmap.dal.feature.FeatureQuery;
41
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
42
import org.gvsig.fmap.dal.feature.FeatureStore;
43
import org.gvsig.fmap.dal.feature.FeatureType;
44
import org.gvsig.fmap.dal.impl.expressionevaluator.DefaultExpressionEvaluator;
45
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
 *
81
 * @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
    private List constantsAttributeNames = new ArrayList();
94

    
95
    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
//    private boolean isAddFilter = true;
103

    
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
     *
113
     */
114
    public DefaultFeatureQuery() {
115
        super();
116
    }
117

    
118
    /**
119
     * Creates a FeatureQuery which will load all available Features of a type.
120
     *
121
     * @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
     *
133
     * @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
     *
148
     * @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
     *
166
     * @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
     *
178
     * @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
     *
193
     * @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
    @Override
208
    public double getScale() {
209
        Double scale = (Double) this.getQueryParameter(SCALE_PARAM_NAME);
210
        if (scale == null) {
211
            return 0;
212
        }
213
        return scale;
214
    }
215

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

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

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

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

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

    
241
    @Override
242
    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
            }
248
        }
249
    }
250

    
251
    @Override
252
    public void retrievesAllAttributes() {
253
        this.attributeNames.clear();
254
    } 
255
    
256
    @Override
257
    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
            }
263
        }
264
        this.attributeNames.add(attributeName);
265
    }
266

    
267
    @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
    public boolean hasAttributeNames() {
320
        return !this.attributeNames.isEmpty();
321
    }
322

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

    
328
    @Override
329
    public Evaluator getFilter() {
330
        return filter;
331
    }
332

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

    
343
   @Override
344
    public void setFilter(String filter) {
345
        if( StringUtils.isEmpty(filter) ) {
346
            this.clearFilter();
347
            return;
348
        }
349
        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

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

    
366
    @Override
367
    public void addFilter(String filter) {
368
        if( StringUtils.isEmpty(filter) ) {
369
            return;
370
        }
371
        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

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

    
384
    @Override
385
    public void addFilter(Evaluator evaluator) {
386
        if (evaluator == null) {
387
            return;
388
        }
389
        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
            }
398
        }
399
        addFilterAttributes(evaluator);
400
    }
401

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

    
407
    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

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

    
426
    @Override
427
    public FeatureQueryOrder getOrder() {
428
        return order;
429
    }
430

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

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

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

    
446
    @Override
447
    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
            }
456
        }
457

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

    
463
        return clone;
464
    }
465

    
466
    @Override
467
    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
    @Override
494
    public String getFeatureTypeId() {
495
        return featureTypeId;
496
    }
497

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

    
503
    @Override
504
    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
    @Override
516
    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
     *
530
     */
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
    @Override
565
    public long getLimit() {
566
        return limit;
567
    }
568

    
569
    @Override
570
    public long getPageSize() {
571
        return pageSize;
572
    }
573

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

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

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

    
589
    @Override
590
    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
            }
596
        }
597
    }
598

    
599
    @Override
600
    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
            }
606
        }
607
        this.constantsAttributeNames.add(attributeName);
608
    }
609

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

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

    
620
}