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

History | View | Annotate | Download (18.4 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.fmap.dal.DALLocator;
35
import org.gvsig.fmap.dal.DataTypes;
36
import org.gvsig.fmap.dal.exception.DataException;
37
import org.gvsig.fmap.dal.exception.InitializeException;
38
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
39
import org.gvsig.fmap.dal.feature.FeatureQuery;
40
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
41
import org.gvsig.fmap.dal.feature.FeatureStore;
42
import org.gvsig.fmap.dal.feature.FeatureType;
43
import org.gvsig.tools.ToolsLocator;
44
import org.gvsig.tools.dynobject.DynStruct;
45
import org.gvsig.tools.evaluator.AndEvaluator;
46
import org.gvsig.tools.evaluator.Evaluator;
47
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo;
48
import org.gvsig.tools.persistence.PersistentState;
49
import org.gvsig.tools.persistence.exception.PersistenceException;
50

    
51
/**
52
 * Defines the properties of a collection of Features, as a result of a query
53
 * through a FeatureStore.
54
 * <p>
55
 * A FeatureQuery is always defined by a FeatureType, or by the list of
56
 * attribute names of the FeatureStore to return.
57
 * </p>
58
 * <p>
59
 * The filter allows to select Features whose properties have values with the
60
 * characteristics defined by the filter.
61
 * </p>
62
 * <p>
63
 * The order is used to set the order of the result FeatureCollection, based on
64
 * the values of the properties of the Features.
65
 * </p>
66
 * <p>
67
 * The scale parameter can be used by the FeatureStore as a hint about the
68
 * quality or resolution of the data needed to view or operate with the data
69
 * returned. As an example, the FeatureStore may use the scale to return only a
70
 * representative subset of the data, or maybe to return Features with less
71
 * detail, like a point or a line instead of a polygon.
72
 * </p>
73
 * <p>
74
 * If an implementation of FeatureStore is able to get other parameters to
75
 * customize the behavior of the getDataCollection methods, there is an option
76
 * to set more parameters through the setAttribute method.
77
 * </p>
78
 *
79
 * @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
80
 */
81
public class DefaultFeatureQuery implements FeatureQuery {
82

    
83
    public static final String SCALE_PARAM_NAME = "Scale";
84

    
85
    private Map queryParameters = new HashMap();
86

    
87
    private String featureTypeId = null;
88

    
89
    private List attributeNames = new ArrayList();
90

    
91
    private List constantsAttributeNames = new ArrayList();
92

    
93
    private Evaluator filter;
94

    
95
    /**
96
     * This boolean value is used to know if the current filter
97
     * has been added using the {@link FeatureQuery#addFilter(Evaluator)}
98
     * method or the {@link FeatureQuery#setFilter(Evaluator)} method.
99
     */
100
//    private boolean isAddFilter = true;
101

    
102
    private FeatureQueryOrder order = new FeatureQueryOrder();
103

    
104
    private long limit;
105

    
106
    private long pageSize;
107

    
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(String filter) {
333
        if( StringUtils.isEmpty(filter) ) {
334
            this.clearFilter();
335
            return;
336
        }
337
        try {
338
            this.setFilter(DALLocator.getDataManager().createExpresion(filter));
339
        } catch (Exception ex) {
340
            throw new RuntimeException("Can't create filter from '"+filter+"'",ex);
341
        }
342
    }
343

    
344
    @Override
345
    public void setFilter(Evaluator filter) {
346
        if( filter == null ) {
347
            this.clearFilter();
348
            return;
349
        }        
350
        this.filter = filter;
351
        addFilterAttributes(filter);
352
    }
353

    
354
    @Override
355
    public void addFilter(String filter) {
356
        if( StringUtils.isEmpty(filter) ) {
357
            return;
358
        }
359
        try {
360
            this.addFilter(DALLocator.getDataManager().createExpresion(filter));
361
        } catch (Exception ex) {
362
            throw new RuntimeException("Can't create filter from '"+filter+"'",ex);
363
        }
364
    }
365

    
366
    @Override
367
    public void addFilter(Evaluator evaluator) {
368
        if (evaluator == null) {
369
            return;
370
        }
371
        if (this.filter == null) {
372
            this.filter = evaluator;
373
        } else {
374
            if (this.filter instanceof AndEvaluator) {
375
                ((AndEvaluator) this.filter).addEvaluator(evaluator);
376
            } else {
377
                this.filter = new AndEvaluator(this.filter);
378
                ((AndEvaluator) this.filter).addEvaluator(evaluator);
379
            }
380
        }
381
        addFilterAttributes(evaluator);
382
    }
383

    
384
    @Override
385
    public void clearFilter() {
386
      this.filter = null;
387
    }
388

    
389
    private void addFilterAttributes(Evaluator evaluator){
390
        if (evaluator != null){
391
            EvaluatorFieldsInfo fieldsInfo = evaluator.getFieldsInfo();
392
            if (fieldsInfo == null){
393
                // FieldsInfo is not available in this evaluator
394
                return;
395
            }
396
            String[] fieldNames = fieldsInfo.getFieldNames();
397
            if (fieldNames== null){
398
                // fieldNames is not available in this evaluator
399
                return;
400
            }
401

    
402
            for (int i=0 ; i<fieldNames.length ; i++){
403
                addAttributeName(fieldNames[i]);
404
            }
405
        }
406
    }
407

    
408
    @Override
409
    public FeatureQueryOrder getOrder() {
410
        return order;
411
    }
412

    
413
    @Override
414
    public void setOrder(FeatureQueryOrder order) {
415
        this.order = order;
416
    }
417

    
418
    @Override
419
    public boolean hasFilter() {
420
        return this.filter != null;
421
    }
422

    
423
    @Override
424
    public boolean hasOrder() {
425
        return this.order != null && this.order.size() > 0;
426
    }
427

    
428
    @Override
429
    public Object clone() throws CloneNotSupportedException {
430
        DefaultFeatureQuery clone = (DefaultFeatureQuery) super.clone();
431

    
432
        // Clone attribute names array
433
        if (attributeNames != null) {
434
            clone.attributeNames = new ArrayList();
435
            for (int i=0 ; i<attributeNames.size() ; i++){
436
                clone.attributeNames.add(attributeNames.get(i));
437
            }
438
        }
439

    
440
        // Clone order
441
        if (order != null) {
442
            clone.order = (FeatureQueryOrder) order.clone();
443
        }
444

    
445
        return clone;
446
    }
447

    
448
    @Override
449
    public FeatureQuery getCopy() {
450
        try {
451
            return (FeatureQuery) clone();
452
        } catch (CloneNotSupportedException e) {
453
            // TODO Auto-generated catch block
454
            e.printStackTrace();
455
            return null;
456
        }
457
        // DefaultFeatureQuery aCopy = new DefaultFeatureQuery();
458
        //
459
        // aCopy.featureTypeId = this.featureTypeId;
460
        //
461
        // if (this.attributeNames != null) {
462
        // aCopy.attributeNames = (String[]) Arrays
463
        // .asList(this.attributeNames).toArray(new String[0]);
464
        // }
465
        //
466
        // aCopy.filter = this.filter;
467
        //
468
        // if (this.order != null) {
469
        // aCopy.order = this.order.getCopy();
470
        // }
471
        //
472
        // return aCopy;
473
    }
474

    
475
    @Override
476
    public String getFeatureTypeId() {
477
        return featureTypeId;
478
    }
479

    
480
    @Override
481
    public void setFeatureTypeId(String featureTypeId) {
482
        this.featureTypeId = featureTypeId;
483
    }
484

    
485
    @Override
486
    public void saveToState(PersistentState state) throws PersistenceException {
487
        // FIXME: falta por terminar de implementar
488
        state.set("queryParameters", this.queryParameters);
489
        state.set("featureTypeId", this.featureTypeId);
490
        state.set("attributeNames", this.attributeNames);
491
        // state.set("filter", this.filter);
492
        state.set("limit", this.limit);
493
        state.set("pageSize", this.pageSize);
494

    
495
    }
496

    
497
    @Override
498
    public void loadFromState(PersistentState state) throws PersistenceException {
499
        // FIXME: falta por terminar de implementar
500
        this.queryParameters = (Map) state.get("queryParameters");
501
        this.featureTypeId = state.getString("featureTypeId");
502
        this.attributeNames = state.getList("attributeNames");
503
        this.filter = (Evaluator) state.get("filter");
504
        this.limit = state.getLong("limit");
505
        this.pageSize = state.getLong("pageSize");
506

    
507
    }
508

    
509
    /**
510
     * Register the class on PersistenceManager
511
     *
512
     */
513
    public static void registerPersistent() {
514
        DynStruct definition =
515
            ToolsLocator.getPersistenceManager()
516
            .addDefinition(DefaultFeatureQuery.class,
517
                "DefaultFeatureQuery",
518
                "DefaultFeatureQuery Persistent definition",
519
                null,
520
                null);
521

    
522
        definition.addDynFieldMap("queryParameters")
523
        .setClassOfItems(Object.class)
524
        .setMandatory(true);
525

    
526
        definition.addDynFieldString("featureTypeId").setMandatory(false);
527

    
528
        definition.addDynFieldList("attributeNames")
529
        .setClassOfItems(String.class)
530
        .setMandatory(false);
531

    
532
        definition.addDynFieldObject("filter")
533
        .setClassOfValue(Evaluator.class)
534
        .setMandatory(false);
535

    
536
        definition.addDynFieldObject("order")
537
        .setClassOfValue(FeatureQueryOrder.class)
538
        .setMandatory(false);
539

    
540
        definition.addDynFieldLong("limit").setMandatory(false);
541

    
542
        definition.addDynFieldLong("pageSize").setMandatory(false);
543

    
544
    }
545

    
546
    @Override
547
    public long getLimit() {
548
        return limit;
549
    }
550

    
551
    @Override
552
    public long getPageSize() {
553
        return pageSize;
554
    }
555

    
556
    @Override
557
    public void setLimit(long limit) {
558
        this.limit = limit;
559
    }
560

    
561
    @Override
562
    public void setPageSize(long pageSize) {
563
        this.pageSize = pageSize;
564
    }
565

    
566
    @Override
567
    public String[] getConstantsAttributeNames() {
568
        return (String[])constantsAttributeNames.toArray(new String[constantsAttributeNames.size()]);
569
    }
570

    
571
    @Override
572
    public void setConstantsAttributeNames(String[] constantsAttributeNames) {
573
        this.constantsAttributeNames.clear();
574
        if (constantsAttributeNames != null){
575
            for (int i=0 ; i<constantsAttributeNames.length ; i++){
576
                this.constantsAttributeNames.add(constantsAttributeNames[i]);
577
            }
578
        }
579
    }
580

    
581
    @Override
582
    public void addConstantAttributeName(String attributeName) {
583
        //If the attribute exists finish the method
584
        for (int i=0 ; i<constantsAttributeNames.size() ; i++){
585
            if (constantsAttributeNames.get(i).equals(attributeName)){
586
                return;
587
            }
588
        }
589
        this.constantsAttributeNames.add(attributeName);
590
    }
591

    
592
    @Override
593
    public boolean hasConstantsAttributeNames() {
594
        return !this.constantsAttributeNames.isEmpty();
595
    }
596

    
597
    @Override
598
    public void clearConstantsAttributeNames() {
599
        this.constantsAttributeNames = new ArrayList();
600
    }
601

    
602
}