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

History | View | Annotate | Download (18.2 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.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.exception.InitializeException;
37
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
38
import org.gvsig.fmap.dal.feature.FeatureQuery;
39
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
40
import org.gvsig.fmap.dal.feature.FeatureStore;
41
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
 *
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
    /**
108
     * Creates a FeatureQuery which will load all available Features of a type.
109
     *
110
     */
111
    public DefaultFeatureQuery() {
112
        super();
113
    }
114

    
115
    /**
116
     * Creates a FeatureQuery which will load all available Features of a type.
117
     *
118
     * @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
     *
130
     * @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
     *
145
     * @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
     *
163
     * @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
     *
175
     * @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
     *
190
     * @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
    @Override
205
    public double getScale() {
206
        Double scale = (Double) this.getQueryParameter(SCALE_PARAM_NAME);
207
        if (scale == null) {
208
            return 0;
209
        }
210
        return scale;
211
    }
212

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

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

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

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

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

    
238
    @Override
239
    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
            }
245
        }
246
    }
247

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

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

    
310
    @Override
311
    public boolean hasAttributeNames() {
312
        return !this.attributeNames.isEmpty();
313
    }
314

    
315
    @Override
316
    public void clearAttributeNames() {
317
        this.attributeNames = new ArrayList();
318
    }
319

    
320
    @Override
321
    public Evaluator getFilter() {
322
        return filter;
323
    }
324

    
325
    @Override
326
    public void setFilter(String filter) {
327
        try {
328
            this.setFilter(DALLocator.getDataManager().createExpresion(filter));
329
        } catch (Exception ex) {
330
            throw new RuntimeException("Can't create filter from '"+filter+"'",ex);
331
        }
332
    }
333

    
334
    @Override
335
    public void setFilter(Evaluator filter) {
336
        this.filter = filter;
337
        addFilterAttributes(filter);
338
//        isAddFilter = false;
339
    }
340

    
341
    @Override
342
    public void addFilter(String filter) {
343
        try {
344
            this.addFilter(DALLocator.getDataManager().createExpresion(filter));
345
        } catch (Exception ex) {
346
            throw new RuntimeException("Can't create filter from '"+filter+"'",ex);
347
        }
348
    }
349

    
350
    @Override
351
    public void addFilter(Evaluator evaluator) {
352
        if( evaluator == null ) {
353
            return;
354
        }
355
//        if (isAddFilter){
356
            if (this.filter == null){
357
                this.filter = evaluator;
358
            }else{
359
                if (evaluator != null){
360
                    if (this.filter instanceof AndEvaluator){
361
                        ((AndEvaluator)this.filter).addEvaluator(evaluator);
362
                    }else{
363
                        this.filter = new AndEvaluator(this.filter);
364
                        ((AndEvaluator)this.filter).addEvaluator(evaluator);
365
                    }
366
                }
367
            }
368
//        }else{
369
//            this.filter = evaluator;
370
//        }
371
        addFilterAttributes(evaluator);
372
//        isAddFilter = true;
373
    }
374

    
375
    @Override
376
    public void clearFilter() {
377
      this.filter = null;
378
    }
379

    
380
    private void addFilterAttributes(Evaluator evaluator){
381
        if (evaluator != null){
382
            EvaluatorFieldsInfo fieldsInfo = evaluator.getFieldsInfo();
383
            if (fieldsInfo == null){
384
                // FieldsInfo is not available in this evaluator
385
                return;
386
            }
387
            String[] fieldNames = fieldsInfo.getFieldNames();
388
            if (fieldNames== null){
389
                // fieldNames is not available in this evaluator
390
                return;
391
            }
392

    
393
            for (int i=0 ; i<fieldNames.length ; i++){
394
                addAttributeName(fieldNames[i]);
395
            }
396
        }
397
    }
398

    
399
    @Override
400
    public FeatureQueryOrder getOrder() {
401
        return order;
402
    }
403

    
404
    @Override
405
    public void setOrder(FeatureQueryOrder order) {
406
        this.order = order;
407
    }
408

    
409
    @Override
410
    public boolean hasFilter() {
411
        return this.filter != null;
412
    }
413

    
414
    @Override
415
    public boolean hasOrder() {
416
        return this.order != null && this.order.size() > 0;
417
    }
418

    
419
    @Override
420
    public Object clone() throws CloneNotSupportedException {
421
        DefaultFeatureQuery clone = (DefaultFeatureQuery) super.clone();
422

    
423
        // Clone attribute names array
424
        if (attributeNames != null) {
425
            clone.attributeNames = new ArrayList();
426
            for (int i=0 ; i<attributeNames.size() ; i++){
427
                clone.attributeNames.add(attributeNames.get(i));
428
            }
429
        }
430

    
431
        // Clone order
432
        if (order != null) {
433
            clone.order = (FeatureQueryOrder) order.clone();
434
        }
435

    
436
        return clone;
437
    }
438

    
439
    @Override
440
    public FeatureQuery getCopy() {
441
        try {
442
            return (FeatureQuery) clone();
443
        } catch (CloneNotSupportedException e) {
444
            // TODO Auto-generated catch block
445
            e.printStackTrace();
446
            return null;
447
        }
448
        // DefaultFeatureQuery aCopy = new DefaultFeatureQuery();
449
        //
450
        // aCopy.featureTypeId = this.featureTypeId;
451
        //
452
        // if (this.attributeNames != null) {
453
        // aCopy.attributeNames = (String[]) Arrays
454
        // .asList(this.attributeNames).toArray(new String[0]);
455
        // }
456
        //
457
        // aCopy.filter = this.filter;
458
        //
459
        // if (this.order != null) {
460
        // aCopy.order = this.order.getCopy();
461
        // }
462
        //
463
        // return aCopy;
464
    }
465

    
466
    @Override
467
    public String getFeatureTypeId() {
468
        return featureTypeId;
469
    }
470

    
471
    @Override
472
    public void setFeatureTypeId(String featureTypeId) {
473
        this.featureTypeId = featureTypeId;
474
    }
475

    
476
    @Override
477
    public void saveToState(PersistentState state) throws PersistenceException {
478
        // FIXME: falta por terminar de implementar
479
        state.set("queryParameters", this.queryParameters);
480
        state.set("featureTypeId", this.featureTypeId);
481
        state.set("attributeNames", this.attributeNames);
482
        // state.set("filter", this.filter);
483
        state.set("limit", this.limit);
484
        state.set("pageSize", this.pageSize);
485

    
486
    }
487

    
488
    @Override
489
    public void loadFromState(PersistentState state) throws PersistenceException {
490
        // FIXME: falta por terminar de implementar
491
        this.queryParameters = (Map) state.get("queryParameters");
492
        this.featureTypeId = state.getString("featureTypeId");
493
        this.attributeNames = state.getList("attributeNames");
494
        this.filter = (Evaluator) state.get("filter");
495
        this.limit = state.getLong("limit");
496
        this.pageSize = state.getLong("pageSize");
497

    
498
    }
499

    
500
    /**
501
     * Register the class on PersistenceManager
502
     *
503
     */
504
    public static void registerPersistent() {
505
        DynStruct definition =
506
            ToolsLocator.getPersistenceManager()
507
            .addDefinition(DefaultFeatureQuery.class,
508
                "DefaultFeatureQuery",
509
                "DefaultFeatureQuery Persistent definition",
510
                null,
511
                null);
512

    
513
        definition.addDynFieldMap("queryParameters")
514
        .setClassOfItems(Object.class)
515
        .setMandatory(true);
516

    
517
        definition.addDynFieldString("featureTypeId").setMandatory(false);
518

    
519
        definition.addDynFieldList("attributeNames")
520
        .setClassOfItems(String.class)
521
        .setMandatory(false);
522

    
523
        definition.addDynFieldObject("filter")
524
        .setClassOfValue(Evaluator.class)
525
        .setMandatory(false);
526

    
527
        definition.addDynFieldObject("order")
528
        .setClassOfValue(FeatureQueryOrder.class)
529
        .setMandatory(false);
530

    
531
        definition.addDynFieldLong("limit").setMandatory(false);
532

    
533
        definition.addDynFieldLong("pageSize").setMandatory(false);
534

    
535
    }
536

    
537
    @Override
538
    public long getLimit() {
539
        return limit;
540
    }
541

    
542
    @Override
543
    public long getPageSize() {
544
        return pageSize;
545
    }
546

    
547
    @Override
548
    public void setLimit(long limit) {
549
        this.limit = limit;
550
    }
551

    
552
    @Override
553
    public void setPageSize(long pageSize) {
554
        this.pageSize = pageSize;
555
    }
556

    
557
    @Override
558
    public String[] getConstantsAttributeNames() {
559
        return (String[])constantsAttributeNames.toArray(new String[constantsAttributeNames.size()]);
560
    }
561

    
562
    @Override
563
    public void setConstantsAttributeNames(String[] constantsAttributeNames) {
564
        this.constantsAttributeNames.clear();
565
        if (constantsAttributeNames != null){
566
            for (int i=0 ; i<constantsAttributeNames.length ; i++){
567
                this.constantsAttributeNames.add(constantsAttributeNames[i]);
568
            }
569
        }
570
    }
571

    
572
    @Override
573
    public void addConstantAttributeName(String attributeName) {
574
        //If the attribute exists finish the method
575
        for (int i=0 ; i<constantsAttributeNames.size() ; i++){
576
            if (constantsAttributeNames.get(i).equals(attributeName)){
577
                return;
578
            }
579
        }
580
        this.constantsAttributeNames.add(attributeName);
581
    }
582

    
583
    @Override
584
    public boolean hasConstantsAttributeNames() {
585
        return !this.constantsAttributeNames.isEmpty();
586
    }
587

    
588
    @Override
589
    public void clearConstantsAttributeNames() {
590
        this.constantsAttributeNames = new ArrayList();
591
    }
592

    
593
}