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

History | View | Annotate | Download (18.8 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
        Evaluator x = new DefaultExpressionEvaluator(filter);
336
        this.setFilter(x);
337
    }
338

    
339
   @Override
340
    public void setFilter(String filter) {
341
        if( StringUtils.isEmpty(filter) ) {
342
            this.clearFilter();
343
            return;
344
        }
345
        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

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

    
362
    @Override
363
    public void addFilter(String filter) {
364
        if( StringUtils.isEmpty(filter) ) {
365
            return;
366
        }
367
        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

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

    
380
    @Override
381
    public void addFilter(Evaluator evaluator) {
382
        if (evaluator == null) {
383
            return;
384
        }
385
        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
            }
394
        }
395
        addFilterAttributes(evaluator);
396
    }
397

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

    
403
    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

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

    
422
    @Override
423
    public FeatureQueryOrder getOrder() {
424
        return order;
425
    }
426

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

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

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

    
442
    @Override
443
    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
            }
452
        }
453

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

    
459
        return clone;
460
    }
461

    
462
    @Override
463
    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
    @Override
490
    public String getFeatureTypeId() {
491
        return featureTypeId;
492
    }
493

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

    
499
    @Override
500
    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
    @Override
512
    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
     *
526
     */
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
    @Override
561
    public long getLimit() {
562
        return limit;
563
    }
564

    
565
    @Override
566
    public long getPageSize() {
567
        return pageSize;
568
    }
569

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

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

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

    
585
    @Override
586
    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
            }
592
        }
593
    }
594

    
595
    @Override
596
    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
            }
602
        }
603
        this.constantsAttributeNames.add(attributeName);
604
    }
605

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

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

    
616
}