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

History | View | Annotate | Download (25.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 org.apache.commons.lang3.ArrayUtils;
31
import org.apache.commons.lang3.StringUtils;
32
import org.gvsig.expressionevaluator.Expression;
33
import org.gvsig.expressionevaluator.ExpressionUtils;
34
import org.gvsig.fmap.dal.DALLocator;
35
import org.gvsig.fmap.dal.DataManager;
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.expressionevaluator.ExpressionEvaluator;
40
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
41
import org.gvsig.fmap.dal.feature.FeatureQuery;
42
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
43
import org.gvsig.fmap.dal.feature.FeatureStore;
44
import org.gvsig.fmap.dal.feature.FeatureType;
45
import org.gvsig.fmap.dal.impl.expressionevaluator.DefaultExpressionEvaluator;
46
import org.gvsig.tools.ToolsLocator;
47
import org.gvsig.tools.dynobject.DynStruct;
48
import org.gvsig.tools.evaluator.AndEvaluator;
49
import org.gvsig.tools.evaluator.Evaluator;
50
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo;
51
import org.gvsig.tools.persistence.PersistentState;
52
import org.gvsig.tools.persistence.exception.PersistenceException;
53
import org.slf4j.Logger;
54
import org.slf4j.LoggerFactory;
55
import org.gvsig.fmap.dal.feature.FeatureExtraColumns;
56

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

    
91
    public static final String SCALE_PARAM_NAME = "Scale";
92

    
93
    private Map<String,Object> queryParameters = new HashMap();
94

    
95
    private String featureTypeId = null;
96

    
97
    private List<String> attributeNames = new ArrayList();
98

    
99
    private List<String> constantsAttributeNames = new ArrayList();
100

    
101
    private Evaluator filter;
102

    
103
    private FeatureQueryOrder order = new FeatureQueryOrder();
104

    
105
    private long limit;
106

    
107
    private long pageSize;
108

    
109
    private List<String> groupByColumns;
110
    
111
    private Map<String,String> aggregateFunctions;
112
    
113
    private FeatureExtraColumns extraColumn = new DefaultFeatureExtraColumns();
114
    
115
    /**
116
     * Creates a FeatureQuery which will load all available Features of a type.
117
     *
118
     */
119
    public DefaultFeatureQuery() {
120
        super();
121
    }
122

    
123
    /**
124
     * Creates a FeatureQuery which will load all available Features of a type.
125
     *
126
     * @param featureType
127
     *            the type of Features of the query
128
     */
129
    public DefaultFeatureQuery(FeatureType featureType) {
130
        super();
131
        this.setFeatureType(featureType);
132
    }
133

    
134
    /**
135
     * Creates a FeatureQuery with the type of features, a filter and the order
136
     * for the FeatureCollection.
137
     *
138
     * @param featureType
139
     *            the type of Features of the query
140
     * @param filter
141
     *            based on the properties of the Features
142
     */
143
    public DefaultFeatureQuery(FeatureType featureType, Evaluator filter) {
144
        super();
145
        this.setFeatureType(featureType);
146
        this.filter = filter;
147
    }
148

    
149
    /**
150
     * Creates a FeatureQuery with the type of features, a filter, the order for
151
     * the FeatureCollection and the view scale.
152
     *
153
     * @param featureType
154
     *            the type of Features of the query
155
     * @param filter
156
     *            based on the properties of the Features
157
     * @param scale
158
     *            to view the Features.
159
     */
160
    public DefaultFeatureQuery(FeatureType featureType, Evaluator filter,
161
        double scale) {
162
        this.setFeatureType(featureType);
163
        this.filter = filter;
164
        this.setScale(scale);
165
    }
166

    
167
    /**
168
     * Creates a FeatureQuery which will load a list of attribute names of all
169
     * available Features.
170
     *
171
     * @param attributeNames
172
     *            the list of attribute names to load
173
     */
174
    public DefaultFeatureQuery(String[] attributeNames) {
175
        super();
176
        setAttributeNames(attributeNames);
177
    }
178

    
179
    /**
180
     * Creates a FeatureQuery with the list of attribute names of feature, a
181
     * filter and the order for the FeatureCollection.
182
     *
183
     * @param attributeNames
184
     *            the list of attribute names to load
185
     * @param filter
186
     *            based on the properties of the Features
187
     */
188
    public DefaultFeatureQuery(String[] attributeNames, Evaluator filter) {
189
        super();
190
        setAttributeNames(attributeNames);
191
        this.filter = filter;
192
    }
193

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

    
212
    @Override
213
    public double getScale() {
214
        Double scale = (Double) this.getQueryParameter(SCALE_PARAM_NAME);
215
        if (scale == null) {
216
            return 0;
217
        }
218
        return scale;
219
    }
220

    
221
    @Override
222
    public void setScale(double scale) {
223
        this.setQueryParameter(SCALE_PARAM_NAME, scale);
224
    }
225

    
226
    @Override
227
    public Object getQueryParameter(String name) {
228
        return queryParameters.get(name);
229
    }
230

    
231
    @Override
232
    public void setQueryParameter(String name, Object value) {
233
        queryParameters.put(name, value);
234
    }
235

    
236
    @Override
237
    public void setFeatureType(FeatureType featureType) {
238
        this.featureTypeId = featureType.getId();
239
    }
240

    
241
    @Override
242
    public String[] getAttributeNames() {
243
        return (String[])attributeNames.toArray(new String[attributeNames.size()]);
244
    }
245

    
246
    @Override
247
    public void setAttributeNames(String[] attributeNames) {
248
        this.attributeNames.clear();
249
        if (attributeNames != null){
250
            for (int i=0 ; i<attributeNames.length ; i++){
251
                this.attributeNames.add(attributeNames[i]);
252
            }
253
        }
254
    }
255

    
256
    @Override
257
    public void retrievesAllAttributes() {
258
        this.attributeNames.clear();
259
    } 
260
    
261
    @Override
262
    public void addAttributeName(String attributeName){
263
        //If the attribute exists finish the method
264
        for (int i=0 ; i<attributeNames.size() ; i++){
265
            if (attributeNames.get(i).equals(attributeName)){
266
                return;
267
            }
268
        }
269
        this.attributeNames.add(attributeName);
270
    }
271

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

    
323
    @Override
324
    public boolean hasAttributeNames() {
325
        return !this.attributeNames.isEmpty();
326
    }
327

    
328
    @Override
329
    public void clearAttributeNames() {
330
        this.attributeNames = new ArrayList();
331
    }
332

    
333
    @Override
334
    public Evaluator getFilter() {
335
        return filter;
336
    }
337

    
338
    @Override
339
    public Expression getExpressionFilter() {
340
      if( this.filter instanceof ExpressionEvaluator ) {
341
        return ((ExpressionEvaluator)this.filter).getExpression();
342
      }
343
      return null;
344
    }
345

    
346
    @Override
347
    public void setFilter(Expression filter) {
348
        if( filter == null ) {
349
            this.clearFilter();
350
            return;
351
        }
352
        Evaluator x = new DefaultExpressionEvaluator(filter);
353
        this.setFilter(x);
354
    }
355

    
356
   @Override
357
    public void setFilter(String filter) {
358
        if( StringUtils.isEmpty(filter) ) {
359
            this.clearFilter();
360
            return;
361
        }
362
        try {
363
            this.setFilter(ExpressionUtils.createExpression(filter));
364
        } catch (Exception ex) {
365
            throw new RuntimeException("Can't create filter from '"+filter+"'",ex);
366
        }
367
    }
368

    
369
    @Override
370
    public void setFilter(Evaluator filter) {
371
        if( filter == null ) {
372
            this.clearFilter();
373
            return;
374
        }        
375
        this.filter = filter;
376
        addFilterAttributes(filter);
377
    }
378

    
379
    @Override
380
    public void addFilter(String filter) {
381
        if( StringUtils.isEmpty(filter) ) {
382
            return;
383
        }
384
        this.addFilter(ExpressionUtils.createExpression(filter));
385
    }
386

    
387
    @Override
388
    public void addFilter(Expression filter) {
389
        Evaluator x = new DefaultExpressionEvaluator(filter);
390
        this.addFilter(x);
391
    }
392

    
393
    @Override
394
    public void addFilter(Evaluator evaluator) {
395
        if (evaluator == null) {
396
            return;
397
        }
398
        if (this.filter == null) {
399
            this.filter = evaluator;
400
        } else {
401
            if (this.filter instanceof AndEvaluator) {
402
                ((AndEvaluator) this.filter).addEvaluator(evaluator);
403
            } else {
404
                this.filter = new AndEvaluator(this.filter);
405
                ((AndEvaluator) this.filter).addEvaluator(evaluator);
406
            }
407
        }
408
        addFilterAttributes(evaluator);
409
    }
410

    
411
    @Override
412
    public void clearFilter() {
413
      this.filter = null;
414
    }
415

    
416
    private void addFilterAttributes(Evaluator evaluator){
417
        if (evaluator != null){
418
            EvaluatorFieldsInfo fieldsInfo = evaluator.getFieldsInfo();
419
            if (fieldsInfo == null){
420
                // FieldsInfo is not available in this evaluator
421
                return;
422
            }
423
            String[] fieldNames = fieldsInfo.getFieldNames();
424
            if (fieldNames== null){
425
                // fieldNames is not available in this evaluator
426
                return;
427
            }
428

    
429
            for (int i=0 ; i<fieldNames.length ; i++){
430
                addAttributeName(fieldNames[i]);
431
            }
432
        }
433
    }
434

    
435
    @Override
436
    public FeatureQueryOrder getOrder() {
437
        return order;
438
    }
439

    
440
    @Override
441
    public void setOrder(FeatureQueryOrder order) {
442
        this.order = order;
443
    }
444

    
445
    @Override
446
    public boolean hasFilter() {
447
        return this.filter != null;
448
    }
449

    
450
    @Override
451
    public boolean hasOrder() {
452
        return this.order != null && this.order.size() > 0;
453
    }
454

    
455
    @Override
456
    public Object clone() throws CloneNotSupportedException {
457
        DefaultFeatureQuery clone = (DefaultFeatureQuery) super.clone();
458

    
459
        // Clone attribute names array
460
        if (attributeNames != null) {
461
            clone.attributeNames = new ArrayList();
462
            for (int i=0 ; i<attributeNames.size() ; i++){
463
                clone.attributeNames.add(attributeNames.get(i));
464
            }
465
        }
466

    
467
        // Clone order
468
        if (order != null) {
469
            clone.order = (FeatureQueryOrder) order.clone();
470
        }
471
        
472
        clone.extraColumn = extraColumn.getCopy();
473
        
474
        if( this.filter instanceof ExpressionEvaluator ) {
475
            Expression exp = ((ExpressionEvaluator)this.filter).getExpression();
476
            clone.filter =  new DefaultExpressionEvaluator(exp);
477
        }
478
        return clone;
479
    }
480

    
481
    @Override
482
    public FeatureQuery getCopy() {
483
        try {
484
            return (FeatureQuery) clone();
485
        } catch (CloneNotSupportedException e) {
486
            // TODO Auto-generated catch block
487
            e.printStackTrace();
488
            return null;
489
        }
490
        // DefaultFeatureQuery aCopy = new DefaultFeatureQuery();
491
        //
492
        // aCopy.featureTypeId = this.featureTypeId;
493
        //
494
        // if (this.attributeNames != null) {
495
        // aCopy.attributeNames = (String[]) Arrays
496
        // .asList(this.attributeNames).toArray(new String[0]);
497
        // }
498
        //
499
        // aCopy.filter = this.filter;
500
        //
501
        // if (this.order != null) {
502
        // aCopy.order = this.order.getCopy();
503
        // }
504
        //
505
        // return aCopy;
506
    }
507

    
508
    @Override
509
    public String getFeatureTypeId() {
510
        return featureTypeId;
511
    }
512

    
513
    @Override
514
    public void setFeatureTypeId(String featureTypeId) {
515
        this.featureTypeId = featureTypeId;
516
    }
517

    
518
    @Override
519
    public void saveToState(PersistentState state) throws PersistenceException {
520
        // FIXME: falta por terminar de implementar
521
        state.set("queryParameters", this.queryParameters);
522
        state.set("featureTypeId", this.featureTypeId);
523
        state.set("attributeNames", this.attributeNames);
524
        
525
        ArrayList<Expression> filterList = new ArrayList<>();
526
        if (this.filter instanceof DefaultExpressionEvaluator) {
527
            DefaultExpressionEvaluator filterExpression = (DefaultExpressionEvaluator) this.filter;
528
           filterList.add(filterExpression.getExpression());
529
        } else if (this.filter instanceof AndEvaluator) {
530
            AndEvaluator filterAnd = (AndEvaluator) this.filter;
531
            List<Evaluator> evaluators = filterAnd.getEvaluators();
532
            for (Evaluator evaluator : evaluators) {
533
                if (evaluator instanceof DefaultExpressionEvaluator) {
534
                    DefaultExpressionEvaluator expressionEvaluator = (DefaultExpressionEvaluator) evaluator;
535
                    filterList.add(expressionEvaluator.getExpression());
536
                } else {
537
                    filterList.clear();
538
                    LOGGER.warn(StringUtils.join("Filters in this FeatureQuery will not persist:", this.toString()));
539
                    break;
540
                }
541
            }
542
        } else {
543
            filterList.clear();
544
            LOGGER.warn(StringUtils.join("Filters in this FeatureQuery will not persist:", this.toString()));
545
        }
546
        
547
        state.set("filter", filterList);
548
        state.set("limit", this.limit);
549
        state.set("pageSize", this.pageSize);
550
        
551
        state.set("order", this.order);
552
        state.set("groupByColumns", this.groupByColumns);
553
        state.set("aggregateFunctions", this.aggregateFunctions);
554
        state.set("extraColumn", this.extraColumn);
555
        
556

    
557
    }
558
    
559

    
560
    @Override
561
    public void loadFromState(PersistentState state) throws PersistenceException {
562
        // FIXME: falta por terminar de implementar
563
        this.queryParameters = (Map) state.get("queryParameters");
564
        this.featureTypeId = state.getString("featureTypeId");
565
        this.attributeNames = state.getList("attributeNames");
566
        List<Expression> filterList = state.getList("filter");
567
        String stateFilter = "";
568
        DataManager dataManager = DALLocator.getDataManager();
569
        if (filterList.size() == 0) {
570
            this.filter = null;
571
        } else if (filterList.size() == 1) {
572
            Expression expression = filterList.get(0);
573
            Evaluator evaluator;
574
            try {
575
                evaluator = dataManager.createFilter(expression);
576
            } catch (InitializeException ex) {
577
                LOGGER.warn("Can't create evaluator", ex);
578
                evaluator = null;
579
            }
580
            this.filter = evaluator;
581
        } else {
582
            AndEvaluator andEvaluator = null;
583
            for (Expression expression : filterList) {
584
                Evaluator evaluator;
585
                try {
586
                    evaluator = dataManager.createFilter(expression);
587

    
588
                    if (andEvaluator == null) {
589
                        andEvaluator = new AndEvaluator(evaluator);
590
                    } else {
591
                        andEvaluator.addEvaluator(evaluator);
592
                    }
593
                } catch (InitializeException ex) {
594
                    LOGGER.warn("Can't create AndEvaluator", ex);//TODO evaluator a null
595
                    break;
596
                }
597
                this.filter = evaluator;
598

    
599
            }
600
        }
601
        this.limit = state.getLong("limit");
602
        this.pageSize = state.getLong("pageSize");
603
        
604
        
605
        this.order = (FeatureQueryOrder) state.get("order");
606
        this.groupByColumns = (List) state.getList("groupByColumns");
607
        this.aggregateFunctions = (Map) state.getMap("aggregateFunctions");
608
        this.extraColumn = (FeatureExtraColumns) state.get("extraColumn");
609

    
610
    
611
    }
612

    
613
    /**
614
     * Register the class on PersistenceManager
615
     *
616
     */
617
    public static void registerPersistent() {
618
        DynStruct definition =
619
            ToolsLocator.getPersistenceManager()
620
            .addDefinition(DefaultFeatureQuery.class,
621
                "DefaultFeatureQuery",
622
                "DefaultFeatureQuery Persistent definition",
623
                null,
624
                null);
625

    
626
        definition.addDynFieldMap("queryParameters")
627
                .setClassOfItems(Object.class)
628
                .setMandatory(true);
629

    
630
        definition.addDynFieldString("featureTypeId").setMandatory(false);
631

    
632
        definition.addDynFieldList("attributeNames")
633
                .setClassOfItems(String.class)
634
                .setMandatory(false);
635

    
636
        definition.addDynFieldList("filter")
637
                .setClassOfItems(Expression.class)
638
                .setMandatory(false);
639

    
640
        definition.addDynFieldObject("order")
641
                .setClassOfValue(FeatureQueryOrder.class)
642
                .setMandatory(false);
643

    
644
        definition.addDynFieldLong("limit").setMandatory(false);
645

    
646
        definition.addDynFieldLong("pageSize").setMandatory(false);
647
        
648
       
649
        definition.addDynFieldList("groupByColumns")
650
                .setClassOfItems(String.class);
651

    
652
        definition.addDynFieldMap("aggregateFunctions")
653
                .setClassOfItems(String.class)
654
                .setClassOfValue(String.class);
655
    
656
        definition.addDynFieldObject("extraColumn")
657
                .setClassOfValue(DefaultFeatureExtraColumns.class);
658
                        
659

    
660
    }
661

    
662
    @Override
663
    public long getLimit() {
664
        return limit;
665
    }
666

    
667
    @Override
668
    public long getPageSize() {
669
        return pageSize;
670
    }
671

    
672
    @Override
673
    public void setLimit(long limit) {
674
        this.limit = limit;
675
    }
676

    
677
    @Override
678
    public void setPageSize(long pageSize) {
679
        this.pageSize = pageSize;
680
    }
681

    
682
    @Override
683
    public String[] getConstantsAttributeNames() {
684
        return (String[])constantsAttributeNames.toArray(new String[constantsAttributeNames.size()]);
685
    }
686

    
687
    @Override
688
    public void setConstantsAttributeNames(String[] constantsAttributeNames) {
689
        this.constantsAttributeNames.clear();
690
        if (constantsAttributeNames != null){
691
            for (int i=0 ; i<constantsAttributeNames.length ; i++){
692
                this.constantsAttributeNames.add(constantsAttributeNames[i]);
693
            }
694
        }
695
    }
696

    
697
    @Override
698
    public void addConstantAttributeName(String attributeName) {
699
        //If the attribute exists finish the method
700
        for (int i=0 ; i<constantsAttributeNames.size() ; i++){
701
            if (constantsAttributeNames.get(i).equals(attributeName)){
702
                return;
703
            }
704
        }
705
        this.constantsAttributeNames.add(attributeName);
706
    }
707

    
708
    @Override
709
    public boolean hasConstantsAttributeNames() {
710
        return !this.constantsAttributeNames.isEmpty();
711
    }
712

    
713
    @Override
714
    public void clearConstantsAttributeNames() {
715
        this.constantsAttributeNames = new ArrayList();
716
    }
717

    
718
  @Override
719
  public boolean isAGroupByColumn(String name) {
720
    for (String columnName : groupByColumns) {
721
      if( StringUtils.equalsIgnoreCase(name, columnName) ) {
722
        return true;
723
      }
724
    }
725
    return false;
726
  }
727

    
728
  @Override
729
  public List<String> getGroupByColumns() {
730
    if( this.groupByColumns == null ) {
731
      this.groupByColumns = new ArrayList<>();
732
    }
733
    return this.groupByColumns;
734
  }
735

    
736
  @Override
737
  public Map<String, String> getAggregateFunctions() {
738
    if( this.aggregateFunctions == null ) {
739
      this.aggregateFunctions = new HashMap<>();
740
    }
741
    return this.aggregateFunctions;
742
  }
743

    
744
  @Override
745
  public String getAggregate(String name) {    
746
      String fn = this.aggregateFunctions.get(name);
747
      if( StringUtils.isAlphanumeric(fn) ) {
748
        return fn + "(\""+ name + "\")";
749
      }
750
      return fn;
751
  }
752
  
753
  @Override
754
  public String getAggregate(String tableName, String name) {    
755
      String fn = this.aggregateFunctions.get(name);
756
      if (!tableName.startsWith("\"")) {
757
         tableName = "\""+tableName+"\"";
758
      }
759
      if( StringUtils.isAlphanumeric(fn) ) {
760
        return fn + "("+tableName+".\""+ name + "\")";
761
      }
762
      return fn;
763
  }
764
  
765
  @Override
766
  public boolean hasAggregateFunctions() {
767
    return this.aggregateFunctions == null || this.aggregateFunctions.isEmpty();
768
  }
769

    
770
  @Override
771
  public boolean hasGroupByColumns() {
772
    return this.groupByColumns != null && !this.groupByColumns.isEmpty();
773
  }
774

    
775
  @Override
776
  public void copyFrom(FeatureQuery query) {
777
    DefaultFeatureQuery other = (DefaultFeatureQuery) query;
778
    this.queryParameters = new HashMap();
779
    this.queryParameters.putAll(other.queryParameters);
780
    
781
    this.featureTypeId = other.featureTypeId;
782

    
783
    this.attributeNames.clear();
784
    this.attributeNames.addAll(other.attributeNames);
785

    
786
    this.constantsAttributeNames.clear();
787
    this.constantsAttributeNames.addAll(other.constantsAttributeNames);
788

    
789
    this.filter = other.filter;
790

    
791
    this.order.copyFrom(other.order);
792

    
793
    this.limit = other.limit;
794

    
795
    this.pageSize = other.pageSize;
796

    
797
    if( this.groupByColumns!=null && other.groupByColumns!=null ) {
798
      this.groupByColumns.clear();
799
      this.groupByColumns.addAll(other.groupByColumns);
800
    } else if( other.groupByColumns!=null ) {
801
      this.groupByColumns = new ArrayList<>();
802
      this.groupByColumns.addAll(other.groupByColumns);
803
    } else if( this.groupByColumns!=null ) {
804
      this.groupByColumns = null;
805
    }
806
    
807
    if( this.aggregateFunctions!=null && other.aggregateFunctions!=null ) {
808
      this.aggregateFunctions.clear();
809
      this.aggregateFunctions.putAll(other.aggregateFunctions);
810
    } else if( other.aggregateFunctions!=null ) {
811
      this.aggregateFunctions = new HashMap<>();
812
      this.aggregateFunctions.putAll(other.aggregateFunctions);
813
    } else if( this.aggregateFunctions!=null ) {
814
      this.aggregateFunctions=null;
815
    }
816
    this.extraColumn.copyFrom(other.extraColumn);
817
  }
818

    
819
  public FeatureExtraColumns getExtraColumn() {
820
      return this.extraColumn;
821
  }
822
  
823
}