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

History | View | Annotate | Download (26.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
        
479
        if (groupByColumns!=null) {
480
            clone.groupByColumns = new ArrayList<String>();
481
            for (String value : groupByColumns) {
482
                clone.groupByColumns.add(value);
483
            }
484
        } else {
485
            clone.groupByColumns = null;
486
        }
487
                
488
                
489
        if (aggregateFunctions!=null) {
490
            clone.aggregateFunctions = new HashMap<String, String>();
491
            for (String key : aggregateFunctions.keySet()) {
492
                clone.aggregateFunctions.put(key, aggregateFunctions.get(key));
493
            }
494
        } else {
495
            clone.aggregateFunctions = null;
496
        }
497
        
498
        
499
        return clone;
500
    }
501

    
502
    @Override
503
    public FeatureQuery getCopy() {
504
        try {
505
            return (FeatureQuery) clone();
506
        } catch (CloneNotSupportedException e) {
507
            // TODO Auto-generated catch block
508
            e.printStackTrace();
509
            return null;
510
        }
511
        // DefaultFeatureQuery aCopy = new DefaultFeatureQuery();
512
        //
513
        // aCopy.featureTypeId = this.featureTypeId;
514
        //
515
        // if (this.attributeNames != null) {
516
        // aCopy.attributeNames = (String[]) Arrays
517
        // .asList(this.attributeNames).toArray(new String[0]);
518
        // }
519
        //
520
        // aCopy.filter = this.filter;
521
        //
522
        // if (this.order != null) {
523
        // aCopy.order = this.order.getCopy();
524
        // }
525
        //
526
        // return aCopy;
527
    }
528

    
529
    @Override
530
    public String getFeatureTypeId() {
531
        return featureTypeId;
532
    }
533

    
534
    @Override
535
    public void setFeatureTypeId(String featureTypeId) {
536
        this.featureTypeId = featureTypeId;
537
    }
538

    
539
    @Override
540
    public void saveToState(PersistentState state) throws PersistenceException {
541
        // FIXME: falta por terminar de implementar
542
        state.set("queryParameters", this.queryParameters);
543
        state.set("featureTypeId", this.featureTypeId);
544
        state.set("attributeNames", this.attributeNames);
545
        
546
        ArrayList<Expression> filterList = new ArrayList<>();
547
        if (this.filter instanceof DefaultExpressionEvaluator) {
548
            DefaultExpressionEvaluator filterExpression = (DefaultExpressionEvaluator) this.filter;
549
           filterList.add(filterExpression.getExpression());
550
        } else if (this.filter instanceof AndEvaluator) {
551
            AndEvaluator filterAnd = (AndEvaluator) this.filter;
552
            List<Evaluator> evaluators = filterAnd.getEvaluators();
553
            for (Evaluator evaluator : evaluators) {
554
                if (evaluator instanceof DefaultExpressionEvaluator) {
555
                    DefaultExpressionEvaluator expressionEvaluator = (DefaultExpressionEvaluator) evaluator;
556
                    filterList.add(expressionEvaluator.getExpression());
557
                } else {
558
                    filterList.clear();
559
                    LOGGER.warn(StringUtils.join("Filters in this FeatureQuery will not persist:", this.toString()));
560
                    break;
561
                }
562
            }
563
        } else {
564
            filterList.clear();
565
            LOGGER.warn(StringUtils.join("Filters in this FeatureQuery will not persist:", this.toString()));
566
        }
567
        
568
        state.set("filter", filterList);
569
        state.set("limit", this.limit);
570
        state.set("pageSize", this.pageSize);
571
        
572
        state.set("order", this.order);
573
        state.set("groupByColumns", this.groupByColumns);
574
        state.set("aggregateFunctions", this.aggregateFunctions);
575
        state.set("extraColumn", this.extraColumn);
576
        
577

    
578
    }
579
    
580

    
581
    @Override
582
    public void loadFromState(PersistentState state) throws PersistenceException {
583
        // FIXME: falta por terminar de implementar
584
        this.queryParameters = (Map) state.get("queryParameters");
585
        this.featureTypeId = state.getString("featureTypeId");
586
        this.attributeNames = state.getList("attributeNames");
587
        List<Expression> filterList = state.getList("filter");
588
        String stateFilter = "";
589
        DataManager dataManager = DALLocator.getDataManager();
590
        if (filterList.size() == 0) {
591
            this.filter = null;
592
        } else if (filterList.size() == 1) {
593
            Expression expression = filterList.get(0);
594
            Evaluator evaluator;
595
            try {
596
                evaluator = dataManager.createFilter(expression);
597
            } catch (InitializeException ex) {
598
                LOGGER.warn("Can't create evaluator", ex);
599
                evaluator = null;
600
            }
601
            this.filter = evaluator;
602
        } else {
603
            AndEvaluator andEvaluator = null;
604
            for (Expression expression : filterList) {
605
                Evaluator evaluator;
606
                try {
607
                    evaluator = dataManager.createFilter(expression);
608

    
609
                    if (andEvaluator == null) {
610
                        andEvaluator = new AndEvaluator(evaluator);
611
                    } else {
612
                        andEvaluator.addEvaluator(evaluator);
613
                    }
614
                } catch (InitializeException ex) {
615
                    LOGGER.warn("Can't create AndEvaluator", ex);//TODO evaluator a null
616
                    break;
617
                }
618
                this.filter = evaluator;
619

    
620
            }
621
        }
622
        this.limit = state.getLong("limit");
623
        this.pageSize = state.getLong("pageSize");
624
        
625
        
626
        this.order = (FeatureQueryOrder) state.get("order");
627
        List asListGroupByColumns = (List) state.getList("groupByColumns");
628
        if (asListGroupByColumns!=null) {
629
            this.groupByColumns = new ArrayList<>(asListGroupByColumns);
630
        } else {
631
            this.groupByColumns = null;
632
        }
633
        Map asMapAggregateFunctions = (Map) state.getMap("aggregateFunctions");
634
        if (asMapAggregateFunctions!=null) {
635
            this.aggregateFunctions = new HashMap<>(asMapAggregateFunctions);
636
        } else {
637
            this.aggregateFunctions = null;
638
        }
639
        this.extraColumn = (FeatureExtraColumns) state.get("extraColumn");
640

    
641
    
642
    }
643

    
644
    /**
645
     * Register the class on PersistenceManager
646
     *
647
     */
648
    public static void registerPersistent() {
649
        DynStruct definition =
650
            ToolsLocator.getPersistenceManager()
651
            .addDefinition(DefaultFeatureQuery.class,
652
                "DefaultFeatureQuery",
653
                "DefaultFeatureQuery Persistent definition",
654
                null,
655
                null);
656

    
657
        definition.addDynFieldMap("queryParameters")
658
                .setClassOfItems(Object.class)
659
                .setMandatory(true);
660

    
661
        definition.addDynFieldString("featureTypeId").setMandatory(false);
662

    
663
        definition.addDynFieldList("attributeNames")
664
                .setClassOfItems(String.class)
665
                .setMandatory(false);
666

    
667
        definition.addDynFieldList("filter")
668
                .setClassOfItems(Expression.class)
669
                .setMandatory(false);
670

    
671
        definition.addDynFieldObject("order")
672
                .setClassOfValue(FeatureQueryOrder.class)
673
                .setMandatory(false);
674

    
675
        definition.addDynFieldLong("limit").setMandatory(false);
676

    
677
        definition.addDynFieldLong("pageSize").setMandatory(false);
678
        
679
       
680
        definition.addDynFieldList("groupByColumns")
681
                .setClassOfItems(String.class);
682

    
683
        definition.addDynFieldMap("aggregateFunctions")
684
                .setClassOfItems(String.class)
685
                .setClassOfValue(String.class);
686
    
687
        definition.addDynFieldObject("extraColumn")
688
                .setClassOfValue(DefaultFeatureExtraColumns.class);
689
                        
690

    
691
    }
692

    
693
    @Override
694
    public long getLimit() {
695
        return limit;
696
    }
697

    
698
    @Override
699
    public long getPageSize() {
700
        return pageSize;
701
    }
702

    
703
    @Override
704
    public void setLimit(long limit) {
705
        this.limit = limit;
706
    }
707

    
708
    @Override
709
    public void setPageSize(long pageSize) {
710
        this.pageSize = pageSize;
711
    }
712

    
713
    @Override
714
    public String[] getConstantsAttributeNames() {
715
        return (String[])constantsAttributeNames.toArray(new String[constantsAttributeNames.size()]);
716
    }
717

    
718
    @Override
719
    public void setConstantsAttributeNames(String[] constantsAttributeNames) {
720
        this.constantsAttributeNames.clear();
721
        if (constantsAttributeNames != null){
722
            for (int i=0 ; i<constantsAttributeNames.length ; i++){
723
                this.constantsAttributeNames.add(constantsAttributeNames[i]);
724
            }
725
        }
726
    }
727

    
728
    @Override
729
    public void addConstantAttributeName(String attributeName) {
730
        //If the attribute exists finish the method
731
        for (int i=0 ; i<constantsAttributeNames.size() ; i++){
732
            if (constantsAttributeNames.get(i).equals(attributeName)){
733
                return;
734
            }
735
        }
736
        this.constantsAttributeNames.add(attributeName);
737
    }
738

    
739
    @Override
740
    public boolean hasConstantsAttributeNames() {
741
        return !this.constantsAttributeNames.isEmpty();
742
    }
743

    
744
    @Override
745
    public void clearConstantsAttributeNames() {
746
        this.constantsAttributeNames = new ArrayList();
747
    }
748

    
749
  @Override
750
  public boolean isAGroupByColumn(String name) {
751
    for (String columnName : groupByColumns) {
752
      if( StringUtils.equalsIgnoreCase(name, columnName) ) {
753
        return true;
754
      }
755
    }
756
    return false;
757
  }
758

    
759
  @Override
760
  public List<String> getGroupByColumns() {
761
    if( this.groupByColumns == null ) {
762
      this.groupByColumns = new ArrayList<>();
763
    }
764
    return this.groupByColumns;
765
  }
766

    
767
  @Override
768
  public Map<String, String> getAggregateFunctions() {
769
    if( this.aggregateFunctions == null ) {
770
      this.aggregateFunctions = new HashMap<>();
771
    }
772
    return this.aggregateFunctions;
773
  }
774

    
775
  @Override
776
  public String getAggregate(String name) {    
777
      String fn = this.getAggregateFunctions().get(name);
778
      if( StringUtils.isAlphanumeric(fn) ) {
779
        return fn + "(\""+ name + "\")";
780
      }
781
      return fn;
782
  }
783
  
784
  @Override
785
  public String getAggregate(String tableName, String name) {    
786
      String fn = this.getAggregateFunctions().get(name);
787
      if (!tableName.startsWith("\"")) {
788
         tableName = "\""+tableName+"\"";
789
      }
790
      if( StringUtils.isAlphanumeric(fn) ) {
791
        return fn + "("+tableName+".\""+ name + "\")";
792
      }
793
      return fn;
794
  }
795
  
796
  @Override
797
  public boolean hasAggregateFunctions() {
798
    return this.aggregateFunctions != null && !this.aggregateFunctions.isEmpty();
799
  }
800

    
801
  @Override
802
  public boolean hasGroupByColumns() {
803
    return this.groupByColumns != null && !this.groupByColumns.isEmpty();
804
  }
805

    
806
  @Override
807
  public void copyFrom(FeatureQuery query) {
808
    DefaultFeatureQuery other = (DefaultFeatureQuery) query;
809
    this.queryParameters = new HashMap();
810
    this.queryParameters.putAll(other.queryParameters);
811
    
812
    this.featureTypeId = other.featureTypeId;
813

    
814
    this.attributeNames.clear();
815
    this.attributeNames.addAll(other.attributeNames);
816

    
817
    this.constantsAttributeNames.clear();
818
    this.constantsAttributeNames.addAll(other.constantsAttributeNames);
819

    
820
    this.filter = other.filter;
821

    
822
    this.order.copyFrom(other.order);
823

    
824
    this.limit = other.limit;
825

    
826
    this.pageSize = other.pageSize;
827

    
828
    if( this.groupByColumns!=null && other.groupByColumns!=null ) {
829
      this.groupByColumns.clear();
830
      this.groupByColumns.addAll(other.groupByColumns);
831
    } else if( other.groupByColumns!=null ) {
832
      this.groupByColumns = new ArrayList<>();
833
      this.groupByColumns.addAll(other.groupByColumns);
834
    } else if( this.groupByColumns!=null ) {
835
      this.groupByColumns = null;
836
    }
837
    
838
    if( this.aggregateFunctions!=null && other.aggregateFunctions!=null ) {
839
      this.aggregateFunctions.clear();
840
      this.aggregateFunctions.putAll(other.aggregateFunctions);
841
    } else if( other.aggregateFunctions!=null ) {
842
      this.aggregateFunctions = new HashMap<>(other.aggregateFunctions);
843
    } else if( this.aggregateFunctions!=null ) {
844
      this.aggregateFunctions=null;
845
    }
846
    this.extraColumn.copyFrom(other.extraColumn);
847
  }
848

    
849
  public FeatureExtraColumns getExtraColumn() {
850
      return this.extraColumn;
851
  }
852
  
853
}