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

History | View | Annotate | Download (42.7 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.Arrays;
28
import java.util.Collection;
29
import java.util.HashMap;
30
import java.util.Iterator;
31
import java.util.List;
32
import java.util.Map;
33
import java.util.logging.Level;
34
import javax.json.JsonObject;
35
import org.apache.commons.lang3.ArrayUtils;
36
import org.apache.commons.lang3.StringUtils;
37
import org.apache.commons.lang3.builder.ToStringBuilder;
38
import org.apache.commons.lang3.mutable.MutableBoolean;
39
import org.gvsig.expressionevaluator.Code;
40
import org.gvsig.expressionevaluator.CodeBuilder;
41
import org.gvsig.expressionevaluator.Codes;
42
import org.gvsig.expressionevaluator.Expression;
43
import org.gvsig.expressionevaluator.ExpressionEvaluator;
44
import org.gvsig.expressionevaluator.ExpressionUtils;
45
import org.gvsig.expressionevaluator.MutableCodes;
46
import org.gvsig.expressionevaluator.MutableSymbolTable;
47
import org.gvsig.fmap.dal.DALLocator;
48
import org.gvsig.fmap.dal.DataManager;
49
import org.gvsig.fmap.dal.DataTypes;
50
import org.gvsig.fmap.dal.exception.DataException;
51
import org.gvsig.fmap.dal.exception.InitializeException;
52
import org.gvsig.fmap.dal.expressionevaluator.FeatureAttributeEmulatorExpression;
53
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
54
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
55
import org.gvsig.fmap.dal.feature.FeatureExtraColumns;
56
import org.gvsig.fmap.dal.feature.FeatureQuery;
57
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
58
import org.gvsig.fmap.dal.feature.FeatureStore;
59
import org.gvsig.fmap.dal.feature.FeatureType;
60
import org.gvsig.fmap.dal.impl.expressionevaluator.DefaultFeatureExpressionEvaluator;
61
import org.gvsig.json.Json;
62
import org.gvsig.json.JsonArrayBuilder;
63
import org.gvsig.json.JsonObjectBuilder;
64
import org.gvsig.json.SupportJson;
65
import org.gvsig.json.SupportToJson;
66
import org.gvsig.tools.ToolsLocator;
67
import org.gvsig.tools.dynobject.DynStruct;
68
import org.gvsig.tools.evaluator.AndEvaluator;
69
import org.gvsig.tools.evaluator.Evaluator;
70
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo;
71
import org.gvsig.tools.exception.BaseException;
72
import org.gvsig.tools.packageutils.Version;
73
import org.gvsig.tools.packageutils.impl.DefaultVersion;
74
import org.gvsig.tools.persistence.PersistentState;
75
import org.gvsig.tools.persistence.exception.PersistenceException;
76
import org.gvsig.tools.visitor.VisitCanceledException;
77
import org.gvsig.tools.visitor.Visitor;
78
import org.slf4j.Logger;
79
import org.slf4j.LoggerFactory;
80

    
81
/**
82
 * Defines the properties of a collection of Features, as a result of a query
83
 * through a FeatureStore.
84
 * 
85
 * A FeatureQuery is always defined by a FeatureType, or by the list of
86
 * attribute names of the FeatureStore to return.
87
 * 
88
 * The filter allows to select Features whose properties have values with the
89
 * characteristics defined by the filter.
90
 * 
91
 * The order is used to set the order of the result FeatureCollection, based on
92
 * the values of the properties of the Features.
93
 * 
94
 * The scale parameter can be used by the FeatureStore as a hint about the
95
 * quality or resolution of the data needed to view or operate with the data
96
 * returned. As an example, the FeatureStore may use the scale to return only a
97
 * representative subset of the data, or maybe to return Features with less
98
 * detail, like a point or a line instead of a polygon.
99
 * 
100
 * If an implementation of FeatureStore is able to get other parameters to
101
 * customize the behavior of the getDataCollection methods, there is an option
102
 * to set more parameters through the setAttribute method.
103
 */
104
@SuppressWarnings("UseSpecificCatch")
105
public class DefaultFeatureQuery implements FeatureQuery {
106
    
107
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureQuery.class);
108

    
109
    public static final String SCALE_PARAM_NAME = "Scale";
110
    
111
    private static final Version VERSION_2_6_0 = ToolsLocator.getPackageManager().createVersion("2.6.0");
112
    private static final Version VERSION_2_6_0_1 = ToolsLocator.getPackageManager().createVersion("2.6.0-1");
113

    
114
    private Map<String,Object> queryParameters = new HashMap();
115

    
116
    private String featureTypeId = null;
117

    
118
    private List<String> attributeNames = new ArrayList();
119

    
120
    private List<String> constantsAttributeNames = new ArrayList();
121

    
122
    private Evaluator filter;
123

    
124
    private FeatureQueryOrder order = new DefaultFeatureQueryOrder();
125

    
126
    private long limit;
127

    
128
    private long pageSize;
129

    
130
    private List<String> groupByColumns;
131
    
132
    private Map<String,String> aggregateFunctions;
133
    
134
    private FeatureExtraColumns extraColumns = new DefaultFeatureExtraColumns();
135
    
136
    private MutableSymbolTable symbolTable;
137
    
138
    private String storeName;
139
    
140
    private boolean useSubquery;
141
    
142
    /**
143
     * Creates a FeatureQuery which will load all available Features of a type.
144
     *
145
     */
146
    public DefaultFeatureQuery() {
147
        super();
148
        this.useSubquery = true; // true for backwards compatibility. 
149
        this.limit = NO_LIMIT;
150
        this.pageSize = 0;
151
    }
152
        
153
    public DefaultFeatureQuery(String storeName) {
154
        this();
155
        this.storeName = storeName;
156
                
157
    }
158

    
159
    /**
160
     * Creates a FeatureQuery which will load all available Features of a type.
161
     *
162
     * @param featureType
163
     *            the type of Features of the query
164
     */
165
    public DefaultFeatureQuery(FeatureType featureType) {
166
        this();
167
        this.setFeatureType(featureType);
168
    }
169

    
170
    /**
171
     * Creates a FeatureQuery with the type of features, a filter and the order
172
     * for the FeatureCollection.
173
     *
174
     * @param featureType
175
     *            the type of Features of the query
176
     * @param filter
177
     *            based on the properties of the Features
178
     */
179
    public DefaultFeatureQuery(FeatureType featureType, Evaluator filter) {
180
        this();
181
        this.setFeatureType(featureType);
182
        this.filter = filter;
183
    }
184

    
185
    /**
186
     * Creates a FeatureQuery with the type of features, a filter, the order for
187
     * the FeatureCollection and the view scale.
188
     *
189
     * @param featureType
190
     *            the type of Features of the query
191
     * @param filter
192
     *            based on the properties of the Features
193
     * @param scale
194
     *            to view the Features.
195
     */
196
    public DefaultFeatureQuery(FeatureType featureType, Evaluator filter,
197
        double scale) {
198
        this();
199
        this.setFeatureType(featureType);
200
        this.filter = filter;
201
        this.setScale(scale);
202
    }
203

    
204
    /**
205
     * Creates a FeatureQuery which will load a list of attribute names of all
206
     * available Features.
207
     *
208
     * @param attributeNames
209
     *            the list of attribute names to load
210
     */
211
    public DefaultFeatureQuery(String[] attributeNames) {
212
        this();
213
        setAttributeNames(attributeNames);
214
    }
215

    
216
    /**
217
     * Creates a FeatureQuery with the list of attribute names of feature, a
218
     * filter and the order for the FeatureCollection.
219
     *
220
     * @param attributeNames
221
     *            the list of attribute names to load
222
     * @param filter
223
     *            based on the properties of the Features
224
     */
225
    public DefaultFeatureQuery(String[] attributeNames, Evaluator filter) {
226
        this();
227
        setAttributeNames(attributeNames);
228
        this.filter = filter;
229
    }
230

    
231
    /**
232
     * Creates a FeatureQuery with the list of attribute names of feature, a
233
     * filter, the order for the FeatureCollection and the view scale.
234
     *
235
     * @param attributeNames
236
     *            the list of attribute names to load
237
     * @param filter
238
     *            based on the properties of the Features
239
     * @param scale
240
     *            to view the Features.
241
     */
242
    public DefaultFeatureQuery(String[] attributeNames, Evaluator filter,
243
        double scale) {
244
        this();
245
        setAttributeNames(attributeNames);
246
        this.filter = filter;
247
        this.setScale(scale);
248
    }
249

    
250
    @Override
251
    public double getScale() {
252
        Double scale = (Double) this.getQueryParameter(SCALE_PARAM_NAME);
253
        if (scale == null) {
254
            return 0;
255
        }
256
        return scale;
257
    }
258

    
259
    @Override
260
    public final void setScale(double scale) {
261
        this.setQueryParameter(SCALE_PARAM_NAME, scale);
262
    }
263

    
264
    @Override
265
    public Object getQueryParameter(String name) {
266
        return queryParameters.get(name);
267
    }
268

    
269
    @Override
270
    public void setQueryParameter(String name, Object value) {
271
        queryParameters.put(name, value);
272
    }
273

    
274
    @Override
275
    public final void setFeatureType(FeatureType featureType) {
276
        this.featureTypeId = featureType.getId();
277
    }
278

    
279
    @Override
280
    public String[] getAttributeNames() {
281
        if (this.hasExtraColumnDeclaredAsGroupByField()) {
282
            this.retrievesAllAttributes();
283
        }
284
        return (String[])attributeNames.toArray(new String[attributeNames.size()]);
285
    }
286
    
287
    private boolean hasExtraColumnDeclaredAsGroupByField() {
288
        // indica si un campo de agrupaciones es una columna calculada
289
        if (this.hasGroupByColumns()) {
290
            for (String groupByColumn : groupByColumns) {
291
                if (this.extraColumns.get(groupByColumn)!=null) {
292
                    return true;
293
                }
294
            }
295
        }
296
        return false;
297
    }
298

    
299
    @Override
300
    public final void setAttributeNames(String[] attributeNames) {
301
        this.attributeNames.clear();
302
        if (attributeNames != null){
303
            this.attributeNames.addAll(Arrays.asList(attributeNames));
304
        }
305
    }
306

    
307
    @Override
308
    public void retrievesAllAttributes() {
309
        this.attributeNames.clear();
310
    } 
311
    
312
    @Override
313
    public void addAttributeName(String attributeName){
314
        //If the attribute exists finish the method
315
        for (int i=0 ; i<attributeNames.size() ; i++){
316
            if (attributeNames.get(i).equals(attributeName)){
317
                return;
318
            }
319
        }
320
        this.attributeNames.add(attributeName);
321
    }
322

    
323
    @Override
324
    public void addEssentialAttributeNames(FeatureStore store) {
325
        FeatureType storeType;
326
        try {
327
            storeType = store.getDefaultFeatureType();
328
        } catch (DataException ex) {
329
            throw new RuntimeException("Can't access to the default feature type of tghe store", ex);
330
        }
331
        FeatureAttributeDescriptor[] pks = storeType.getPrimaryKey();
332
        if( storeType.hasOID() || ArrayUtils.isEmpty(pks) ) {
333
            FeatureAttributeDescriptor attrInt = null;
334
            FeatureAttributeDescriptor attrStr = null;
335
            FeatureAttributeDescriptor attrNotGeom = null;
336
            for (FeatureAttributeDescriptor attr : storeType) {
337
                if( attrInt == null && (attr.getType()==DataTypes.INT || attr.getType()==DataTypes.LONG) ) {
338
                    attrInt = attr;
339
                } else if( attrStr == null && attr.getType()==DataTypes.STRING ) {
340
                    attrStr = attr;
341
                } else if( attrNotGeom == null && attr.getType()!=DataTypes.GEOMETRY ) {
342
                    attrNotGeom = attr;
343
                }
344
            }
345
            if( attrInt != null ) {
346
                this.addAttributeName(attrInt.getName());
347
            } else if( attrStr != null ) {
348
                this.addAttributeName(attrStr.getName());
349
            } else if( attrNotGeom != null ) {
350
                this.addAttributeName(attrNotGeom.getName());
351
            } else {
352
                this.addAttributeName(storeType.getAttributeDescriptor(0).getName());
353
            }
354
        } else {
355
            for(FeatureAttributeDescriptor attr : pks ) {
356
                this.addAttributeName(attr.getName());
357
            }
358
        }
359
    }
360
    
361
    @Override
362
    public void addPrimaryKeyAttributeNames(FeatureStore store) {
363
        FeatureType storeType;
364
        try {
365
            storeType = store.getDefaultFeatureType();
366
        } catch (DataException ex) {
367
            throw new RuntimeException("Can't access to the default feature type of tghe store", ex);
368
        }
369
        for(FeatureAttributeDescriptor attr : storeType.getPrimaryKey() ) {
370
            this.addAttributeName(attr.getName());
371
        }
372
    }
373

    
374
    @Override
375
    public boolean hasAttributeNames() {
376
        if (hasExtraColumnDeclaredAsGroupByField()) {
377
            return true;
378
        }
379
        return !this.attributeNames.isEmpty();
380
    }
381

    
382
    @Override
383
    public void clearAttributeNames() {
384
        this.attributeNames = new ArrayList();
385
    }
386

    
387
    @Override
388
    public Evaluator getFilter() {
389
      if( this.filter instanceof ExpressionEvaluator ) {
390
        ExpressionEvaluator eefilter = (ExpressionEvaluator) this.filter;
391
        if( symbolTable != null ) {
392
            eefilter.addSymbolTable(symbolTable);
393
        }
394
      }
395
      return filter;
396
    }
397

    
398
    @Override
399
    public Expression getExpressionFilter() {
400
      if( this.filter instanceof ExpressionEvaluator ) {
401
        ExpressionEvaluator eefilter = (ExpressionEvaluator) this.filter;
402
        if( symbolTable != null ) {
403
            eefilter.addSymbolTable(symbolTable);
404
        }
405
        return eefilter.toExpression();
406
      }
407
      return null;
408
    }
409

    
410
    @Override
411
    public void setFilter(Expression filter) {
412
        if( filter == null ) {
413
            this.clearFilter();
414
            return;
415
        }
416
        Evaluator x = new DefaultFeatureExpressionEvaluator(storeName, filter);
417
        this.setFilter(x);
418
    }
419

    
420
   @Override
421
    public void setFilter(String filter) {
422
        if( StringUtils.isEmpty(filter) ) {
423
            this.clearFilter();
424
            return;
425
        }
426
        try {
427
            this.setFilter(ExpressionUtils.createExpression(filter));
428
        } catch (Exception ex) {
429
            throw new RuntimeException("Can't create filter from '"+filter+"'",ex);
430
        }
431
    }
432

    
433
    @Override
434
    public void setFilter(Evaluator filter) {
435
        if( filter == null ) {
436
            this.clearFilter();
437
            return;
438
        }        
439
        this.filter = filter;
440
        addFilterAttributes(filter);
441
    }
442

    
443
    @Override
444
    public void addFilter(String filter) {
445
        if( StringUtils.isEmpty(filter) ) {
446
            return;
447
        }
448
        this.addFilter(ExpressionUtils.createExpression(filter));
449
    }
450

    
451
    @Override
452
    public void addFilter(Expression filter) {
453
        Evaluator x = new DefaultFeatureExpressionEvaluator(filter);
454
        this.addFilter(x);
455
    }
456

    
457
    @Override
458
    public void addFilter(Evaluator evaluator) {
459
        if (evaluator == null) {
460
            return;
461
        }
462
        if (this.filter == null) {
463
            this.filter = evaluator;
464
        } else {
465
            if (this.filter instanceof AndEvaluator) {
466
                ((AndEvaluator) this.filter).addEvaluator(evaluator);
467
            } else {
468
                this.filter = new AndEvaluator(this.filter);
469
                ((AndEvaluator) this.filter).addEvaluator(evaluator);
470
            }
471
        }
472
        addFilterAttributes(evaluator);
473
    }
474

    
475
    @Override
476
    public void clearFilter() {
477
      this.filter = null;
478
    }
479

    
480
    private void addFilterAttributes(Evaluator evaluator){
481
        if (evaluator != null){
482
            EvaluatorFieldsInfo fieldsInfo = evaluator.getFieldsInfo();
483
            if (fieldsInfo == null){
484
                // FieldsInfo is not available in this evaluator
485
                return;
486
            }
487
            String[] fieldNames = fieldsInfo.getFieldNames();
488
            if (fieldNames== null){
489
                // fieldNames is not available in this evaluator
490
                return;
491
            }
492

    
493
            for (String fieldName : fieldNames) {
494
                addAttributeName(fieldName);
495
            }
496
        }
497
    }
498

    
499
    @Override
500
    public FeatureQueryOrder getOrder() {
501
        return order;
502
    }
503

    
504
    @Override
505
    public void setOrder(FeatureQueryOrder order) {
506
        if(order == null){
507
            this.order = new DefaultFeatureQueryOrder();
508
        } else {
509
            this.order = order;
510
        }
511
    }
512

    
513
    @Override
514
    public boolean hasFilter() {
515
        return this.filter != null;
516
    }
517

    
518
    @Override
519
    public boolean hasLimit() {
520
        return this.limit != NO_LIMIT;
521
    }
522

    
523
    @Override
524
    public boolean hasOrder() {
525
        return this.order != null && this.order.size() > 0;
526
    }
527

    
528
    @Override
529
    public Object clone() throws CloneNotSupportedException {
530
        DefaultFeatureQuery clone = (DefaultFeatureQuery) super.clone();
531

    
532
        // Clone attribute names array
533
        if (attributeNames != null) {
534
            clone.attributeNames = new ArrayList();
535
            for (int i=0 ; i<attributeNames.size() ; i++){
536
                clone.attributeNames.add(attributeNames.get(i));
537
            }
538
        }
539

    
540
        // Clone order
541
        if (order != null) {
542
            clone.order = (FeatureQueryOrder) order.clone();
543
        }
544
        
545
        clone.extraColumns = extraColumns.getCopy();
546
        
547
        if( this.filter instanceof ExpressionEvaluator ) {
548
            Expression exp = ((ExpressionEvaluator)this.filter).toExpression();
549
            clone.filter =  new DefaultFeatureExpressionEvaluator(exp);
550
        }
551
        
552
        if (groupByColumns!=null) {
553
            clone.groupByColumns = new ArrayList<>();
554
            for (String value : groupByColumns) {
555
                clone.groupByColumns.add(value);
556
            }
557
        } else {
558
            clone.groupByColumns = null;
559
        }
560
                
561
                
562
        if (aggregateFunctions!=null) {
563
            clone.aggregateFunctions = new HashMap<>();
564
            for (String key : aggregateFunctions.keySet()) {
565
                clone.aggregateFunctions.put(key, aggregateFunctions.get(key));
566
            }
567
        } else {
568
            clone.aggregateFunctions = null;
569
        }
570
        if( this.symbolTable!=null ) {
571
            clone.symbolTable = this.symbolTable.clone();
572
        }
573
        
574
        return clone;
575
    }
576

    
577
    @Override
578
    public FeatureQuery getCopy() {
579
        try {
580
            return (FeatureQuery) clone();
581
        } catch (CloneNotSupportedException e) {
582
            LOGGER.debug("Can't clone feature query",e);
583
            return null;
584
        }
585
        // DefaultFeatureQuery aCopy = new DefaultFeatureQuery();
586
        //
587
        // aCopy.featureTypeId = this.featureTypeId;
588
        //
589
        // if (this.attributeNames != null) {
590
        // aCopy.attributeNames = (String[]) Arrays
591
        // .asList(this.attributeNames).toArray(new String[0]);
592
        // }
593
        //
594
        // aCopy.filter = this.filter;
595
        //
596
        // if (this.order != null) {
597
        // aCopy.order = this.order.getCopy();
598
        // }
599
        //
600
        // return aCopy;
601
    }
602

    
603
    @Override
604
    public String getFeatureTypeId() {
605
        return featureTypeId;
606
    }
607

    
608
    @Override
609
    public void setFeatureTypeId(String featureTypeId) {
610
        this.featureTypeId = featureTypeId;
611
    }
612

    
613
    @Override
614
    public void saveToState(PersistentState state) throws PersistenceException {
615
        // FIXME: falta por terminar de implementar
616
        state.set("version", VERSION_2_6_0_1);
617
        state.set("queryParameters", this.queryParameters);
618
        state.set("featureTypeId", this.featureTypeId);
619
        state.set("attributeNames", this.attributeNames);
620
        
621
        ArrayList<Expression> filterList = new ArrayList<>();
622
        if (this.filter instanceof DefaultFeatureExpressionEvaluator) {
623
            DefaultFeatureExpressionEvaluator filterExpression = (DefaultFeatureExpressionEvaluator) this.filter;
624
           filterList.add(filterExpression.toExpression());
625
        } else if (this.filter instanceof AndEvaluator) {
626
            AndEvaluator filterAnd = (AndEvaluator) this.filter;
627
            List<Evaluator> evaluators = filterAnd.getEvaluators();
628
            for (Evaluator evaluator : evaluators) {
629
                if (evaluator instanceof DefaultFeatureExpressionEvaluator) {
630
                    DefaultFeatureExpressionEvaluator expressionEvaluator = (DefaultFeatureExpressionEvaluator) evaluator;
631
                    filterList.add(expressionEvaluator.toExpression());
632
                } else {
633
                    filterList.clear();
634
                    LOGGER.warn(StringUtils.join("Filters in this FeatureQuery will not persist:", this.toString()));
635
                    break;
636
                }
637
            }
638
        } else {
639
            filterList.clear();
640
            if( this.filter!=null ) {
641
                LOGGER.warn(StringUtils.join("Filters in this FeatureQuery will not persist:", this.toString()));
642
            }
643
        }
644
        
645
        state.set("filter", filterList);
646
        state.set("limit", this.limit);
647
        state.set("pageSize", this.pageSize);
648
        state.set("useSubquery", this.useSubquery);
649
        
650
        state.set("order", this.order);
651
        state.set("groupByColumns", this.groupByColumns);
652
        state.set("aggregateFunctions", this.aggregateFunctions);
653
        state.set("extraColumn", this.extraColumns);
654
        state.set("storeName", this.storeName);
655
        
656

    
657
    }
658
    
659

    
660
    @Override
661
    public void loadFromState(PersistentState state) throws PersistenceException {
662
        // FIXME: falta por terminar de implementar
663
        Version version = (Version) state.get("version");
664
        this.queryParameters = (Map) state.get("queryParameters");
665
        this.featureTypeId = state.getString("featureTypeId");
666
        this.attributeNames = state.getList("attributeNames");
667
        List<Expression> filterList = state.getList("filter");
668
        String stateFilter = "";
669
        DataManager dataManager = DALLocator.getDataManager();
670
        if (filterList.isEmpty()) {
671
            this.filter = null;
672
        } else if (filterList.size() == 1) {
673
            Expression expression = filterList.get(0);
674
            Evaluator evaluator;
675
            try {
676
                evaluator = dataManager.createFilter(expression);
677
            } catch (InitializeException ex) {
678
                LOGGER.warn("Can't create evaluator", ex);
679
                evaluator = null;
680
            }
681
            this.filter = evaluator;
682
        } else {
683
            AndEvaluator andEvaluator = null;
684
            for (Expression expression : filterList) {
685
                Evaluator evaluator;
686
                try {
687
                    evaluator = dataManager.createFilter(expression);
688

    
689
                    if (andEvaluator == null) {
690
                        andEvaluator = new AndEvaluator(evaluator);
691
                    } else {
692
                        andEvaluator.addEvaluator(evaluator);
693
                    }
694
                } catch (InitializeException ex) {
695
                    LOGGER.warn("Can't create AndEvaluator", ex);//TODO evaluator a null
696
                    break;
697
                }
698
                this.filter = evaluator;
699

    
700
            }
701
        }
702
        this.limit = state.getLong("limit");
703
        
704
        this.pageSize = state.getLong("pageSize");
705
        this.useSubquery = state.getBoolean("useSubquery",true);
706
        this.storeName = state.getString("storeName");
707
        
708
        
709
        this.order = (FeatureQueryOrder) state.get("order");
710
        List asListGroupByColumns = (List) state.getList("groupByColumns");
711
        if (asListGroupByColumns!=null) {
712
            this.groupByColumns = new ArrayList<>(asListGroupByColumns);
713
        } else {
714
            this.groupByColumns = null;
715
        }
716
        Map asMapAggregateFunctions = (Map) state.getMap("aggregateFunctions");
717
        if (asMapAggregateFunctions!=null) {
718
            this.aggregateFunctions = new HashMap<>(asMapAggregateFunctions);
719
        } else {
720
            this.aggregateFunctions = null;
721
        }
722
        this.extraColumns = (FeatureExtraColumns) state.get("extraColumn");
723

    
724
        //
725
        // -------------------------------------------------------------
726
        // Correcciones por perdida de compatibilidad entre versiones
727
        //
728
        
729
        // Ver TC13-AC Listado peatones USUARIOS 2021.....
730
        
731
        if(version == null || version.compareTo(VERSION_2_6_0)<0){
732
            if(this.limit == 0) {
733
                this.clearLimit();
734
            }
735
            Expression exp = this.getExpressionFilter();
736
            fixOldExistsSelect(exp);
737
            for (EditableFeatureAttributeDescriptor extraColumn : this.extraColumns) {
738
                if(extraColumn.getFeatureAttributeEmulator() instanceof FeatureAttributeEmulatorExpression) {
739
                    exp = ((FeatureAttributeEmulatorExpression)extraColumn.getFeatureAttributeEmulator()).getExpression();
740
                    fixOldExistsSelect(exp);
741
                }
742
            }
743
        }
744
    }
745

    
746
    public static void selfRegister() {
747
        registerPersistent();
748
        Json.registerSerializer(DefaultFeatureQuery.class);            
749
    }
750
    
751
    /**
752
     * Register the class on PersistenceManager
753
     *
754
     */
755
    private static void registerPersistent() {
756
        DynStruct definition =
757
            ToolsLocator.getPersistenceManager()
758
            .addDefinition(DefaultFeatureQuery.class,
759
                "DefaultFeatureQuery",
760
                "DefaultFeatureQuery Persistent definition",
761
                null,
762
                null);
763

    
764
        
765
        definition.addDynFieldObject("version")
766
                .setClassOfValue(DefaultVersion.class)
767
                .setMandatory(false);
768

    
769
        definition.addDynFieldMap("queryParameters")
770
                .setClassOfItems(Object.class)
771
                .setMandatory(true);
772

    
773
        definition.addDynFieldString("featureTypeId").setMandatory(false);
774

    
775
        definition.addDynFieldList("attributeNames")
776
                .setClassOfItems(String.class)
777
                .setMandatory(false);
778

    
779
        definition.addDynFieldList("filter")
780
                .setClassOfItems(Expression.class)
781
                .setMandatory(false);
782

    
783
        definition.addDynFieldObject("order")
784
                .setClassOfValue(FeatureQueryOrder.class)
785
                .setMandatory(false);
786

    
787
        definition.addDynFieldLong("limit").setMandatory(false);
788

    
789
        definition.addDynFieldLong("pageSize").setMandatory(false);
790
        
791
        definition.addDynFieldBoolean("useSubquery").setMandatory(false);
792
       
793
        definition.addDynFieldList("groupByColumns")
794
                .setClassOfItems(String.class);
795

    
796
        definition.addDynFieldMap("aggregateFunctions")
797
                .setClassOfItems(String.class)
798
                .setClassOfValue(String.class);
799
    
800
        definition.addDynFieldObject("extraColumn")
801
                .setClassOfValue(DefaultFeatureExtraColumns.class);
802
                        
803
                definition.addDynFieldString("storeName").setMandatory(false);
804

    
805
    }
806

    
807
    @Override
808
    public long getLimit() {
809
        return limit;
810
    }
811

    
812
    @Override
813
    public long getPageSize() {
814
        return pageSize;
815
    }
816

    
817
    @Override
818
    public void setLimit(long limit) {
819
        this.limit = limit;
820
    }
821

    
822
    @Override
823
    public void clearLimit() {
824
        this.limit = NO_LIMIT;
825
    }
826
    
827
    @Override
828
    public void setPageSize(long pageSize) {
829
        this.pageSize = pageSize;
830
    }
831

    
832
    @Override
833
    public String[] getConstantsAttributeNames() {
834
        return (String[])constantsAttributeNames.toArray(new String[constantsAttributeNames.size()]);
835
    }
836

    
837
    @Override
838
    public void setConstantsAttributeNames(String[] constantsAttributeNames) {
839
        this.constantsAttributeNames.clear();
840
        if (constantsAttributeNames != null){
841
            this.constantsAttributeNames.addAll(Arrays.asList(constantsAttributeNames));
842
        }
843
    }
844

    
845
    @Override
846
    public void addConstantAttributeName(String attributeName) {
847
        //If the attribute exists finish the method
848
        for (int i=0 ; i<constantsAttributeNames.size() ; i++){
849
            if (constantsAttributeNames.get(i).equals(attributeName)){
850
                return;
851
            }
852
        }
853
        this.constantsAttributeNames.add(attributeName);
854
    }
855

    
856
    @Override
857
    public boolean hasConstantsAttributeNames() {
858
        return !this.constantsAttributeNames.isEmpty();
859
    }
860

    
861
    @Override
862
    public void clearConstantsAttributeNames() {
863
        this.constantsAttributeNames = new ArrayList();
864
    }
865

    
866
  @Override
867
  public boolean isAGroupByColumn(String name) {
868
    if( groupByColumns==null ) {
869
        return false;
870
    }
871
    for (String columnName : groupByColumns) {
872
      if( StringUtils.equalsIgnoreCase(name, columnName) ) {
873
        return true;
874
      }
875
    }
876
    return false;
877
  }
878

    
879
  @Override
880
  public List<String> getGroupByColumns() {
881
    if( this.groupByColumns == null ) {
882
      this.groupByColumns = new ArrayList<>();
883
    }
884
    return this.groupByColumns;
885
  }
886

    
887
  @Override
888
  public void removeGroupByColumn(String colname) {
889
    if( this.groupByColumns == null ) {
890
      return;
891
    }
892
    this.groupByColumns.remove(colname);
893
  }
894
  
895
  @Override
896
  public void addAggregate(String funcName, String columnName) {
897
        Map<String, String> aggregateds = this.getAggregateFunctions();
898
        aggregateds.put(columnName, funcName);
899
  }
900

    
901
  @Override
902
  public Map<String, String> getAggregateFunctions() {
903
    if( this.aggregateFunctions == null ) {
904
      this.aggregateFunctions = new HashMap<>();
905
    }
906
    return this.aggregateFunctions;
907
  }
908

    
909
  @Override
910
  public void removeAggregateFunction(String colname) {
911
    if( this.aggregateFunctions == null ) {
912
      return;
913
    }
914
    for (Iterator<Map.Entry<String, String>> iterator = this.getAggregateFunctions().entrySet().iterator(); iterator.hasNext();) {
915
        Map.Entry<String, String> entry = iterator.next();
916
        String attrName = entry.getKey();
917
        String function = entry.getValue();
918
        if(StringUtils.equalsIgnoreCase(colname, attrName)){
919
            iterator.remove();
920
            return;
921
        }
922
    }
923
  }
924
  
925
  @Override
926
  public String getAggregateFunction(String name){
927
      for (Map.Entry<String, String> entry : this.getAggregateFunctions().entrySet()) {
928
          String attrName = entry.getKey();
929
          String function = entry.getValue();
930
          if(StringUtils.equalsIgnoreCase(name, attrName)){
931
              return function;
932
          }
933
      }
934
      return null;
935
  }
936
  
937
  @Override
938
  public String getAggregate(String name) {    
939
      String fn = this.getAggregateFunction(name);
940
      if( StringUtils.isAlphanumeric(fn) ) {
941
        return fn + "(\""+ name + "\")";
942
      }
943
      return fn;
944
  }
945
  
946
  @Override
947
  public String getAggregate(String tableName, String name) {    
948
      String fn = this.getAggregateFunction(name);
949
      if (!tableName.startsWith("\"")) {
950
         tableName = "\""+tableName+"\"";
951
      }
952
      if( StringUtils.isAlphanumeric(fn) ) {
953
        return fn + "("+tableName+".\""+ name + "\")";
954
      }
955
      return fn;
956
  }
957

    
958
    @Override
959
    public boolean isAggregate(String name) {
960
        return this.getAggregateFunction(name) != null;
961
    }
962

    
963
  @Override
964
  public boolean hasAggregateFunctions() {
965
    return this.aggregateFunctions != null && !this.aggregateFunctions.isEmpty();
966
  }
967

    
968
  @Override
969
  public boolean hasGroupByColumns() {
970
    return this.groupByColumns != null && !this.groupByColumns.isEmpty();
971
  }
972

    
973
  private void clear() {
974
        this.queryParameters = new HashMap();
975
        
976
//        this.featureTypeId = other.featureTypeId;
977
//        this.storeName = other.storeName;
978

    
979
        this.clearConstantsAttributeNames();
980
        this.clearAttributeNames();
981
        this.clearFilter();
982
        this.clearLimit();
983
        this.setOrder(null);
984
        this.useSubquery = true; // true for backwards compatibility. 
985
        this.limit = NO_LIMIT;
986
        this.pageSize = 0;
987
        this.groupByColumns = null;
988
        this.aggregateFunctions=null;
989
        this.extraColumns = new DefaultFeatureExtraColumns();
990
        this.symbolTable = null;
991

    
992
  }
993
  
994
  @Override
995
  public void copyFrom(FeatureQuery query) {
996
    if( query == null ) {
997
        this.clear();
998
        return;
999
    }
1000
    DefaultFeatureQuery other = (DefaultFeatureQuery) query;
1001
    this.queryParameters = new HashMap();
1002
    this.queryParameters.putAll(other.queryParameters);
1003
    
1004
    this.featureTypeId = other.featureTypeId;
1005

    
1006
    this.attributeNames.clear();
1007
    this.attributeNames.addAll(other.attributeNames);
1008

    
1009
    this.constantsAttributeNames.clear();
1010
    this.constantsAttributeNames.addAll(other.constantsAttributeNames);
1011

    
1012
    this.filter = other.filter;
1013

    
1014
    this.order.copyFrom(other.order);
1015

    
1016
    this.limit = other.limit;
1017

    
1018
    this.pageSize = other.pageSize;
1019
    this.useSubquery = other.useSubquery;
1020

    
1021
    if( this.groupByColumns!=null && other.groupByColumns!=null ) {
1022
      this.groupByColumns.clear();
1023
      this.groupByColumns.addAll(other.groupByColumns);
1024
    } else if( other.groupByColumns!=null ) {
1025
      this.groupByColumns = new ArrayList<>();
1026
      this.groupByColumns.addAll(other.groupByColumns);
1027
    } else if( this.groupByColumns!=null ) {
1028
      this.groupByColumns = null;
1029
    }
1030
    
1031
    if( this.aggregateFunctions!=null && other.aggregateFunctions!=null ) {
1032
      this.aggregateFunctions.clear();
1033
      this.aggregateFunctions.putAll(other.aggregateFunctions);
1034
    } else if( other.aggregateFunctions!=null ) {
1035
      this.aggregateFunctions = new HashMap<>(other.aggregateFunctions);
1036
    } else if( this.aggregateFunctions!=null ) {
1037
      this.aggregateFunctions=null;
1038
    }
1039
    this.extraColumns.copyFrom(other.extraColumns);
1040
    if( other.symbolTable!=null ) {
1041
        try {
1042
            this.symbolTable = other.symbolTable.clone();
1043
        } catch (CloneNotSupportedException ex) {
1044
            LOGGER.debug("Can't clone symbol table",ex);
1045
        }
1046
    }
1047
        this.storeName = other.storeName;
1048
  }
1049

    
1050
    @Override
1051
    public FeatureExtraColumns getExtraColumns() {
1052
        return this.extraColumns;
1053
    }
1054

    
1055
    @Override
1056
    @Deprecated
1057
    public FeatureExtraColumns getExtraColumn() {
1058
        return this.extraColumns;
1059
    }
1060

    
1061
    @Override
1062
    public MutableSymbolTable getSymbolTable() {
1063
        return symbolTable;
1064
    }
1065

    
1066
    @Override
1067
    public void setSymbolTable(MutableSymbolTable symbolTable) {
1068
        this.symbolTable = symbolTable;
1069
    }
1070

    
1071
    @Override
1072
    public void setVar(String name, Object value) {
1073
        if( this.symbolTable==null ) {
1074
            this.symbolTable = ExpressionUtils.createSymbolTable();
1075
        }
1076
        this.symbolTable.setVar(name, value);
1077
    }
1078

    
1079
    @Override
1080
    public boolean isUseSubquery() {
1081
        return useSubquery;
1082
    }
1083

    
1084
    @Override
1085
    public void setUseSubquery(boolean useSubquery) {
1086
        this.useSubquery = useSubquery;
1087
    }
1088

    
1089
    @Override
1090
    public void fromJson(JsonObject json) {
1091
        DataManager dataManager = DALLocator.getDataManager();
1092

    
1093
        String s = json.getString("version",null);
1094
        Version version = s==null?null:Version.valueOf(s);
1095
        
1096
        this.queryParameters = Json.toMap(json, "queryParameters");
1097
        this.featureTypeId = json.getString("featureTypeId", null);
1098

    
1099
        this.attributeNames = new ArrayList<>();
1100
        Collection theAttributeNames = Json.toCollection(json,"attributeNames");
1101
        if( theAttributeNames!=null ) {
1102
            this.attributeNames.addAll(theAttributeNames);
1103
        }
1104
        
1105
        List<Expression> filterList = null;
1106
        Collection theFilter = Json.toCollection(json, "filter");
1107
        if( theFilter!=null ) {
1108
            filterList = new ArrayList<>(theFilter);
1109
        }
1110
        if (filterList==null || filterList.isEmpty()) {
1111
            this.filter = null;
1112
        } else if (filterList.size() == 1) {
1113
            Expression expression = filterList.get(0);
1114
            Evaluator evaluator;
1115
            try {
1116
                evaluator = dataManager.createFilter(expression);
1117
            } catch (InitializeException ex) {
1118
                LOGGER.warn("Can't create evaluator", ex);
1119
                evaluator = null;
1120
            }
1121
            this.filter = evaluator;
1122
        } else {
1123
            AndEvaluator andEvaluator = null;
1124
            for (Expression expression : filterList) {
1125
                Evaluator evaluator;
1126
                try {
1127
                    evaluator = dataManager.createFilter(expression);
1128

    
1129
                    if (andEvaluator == null) {
1130
                        andEvaluator = new AndEvaluator(evaluator);
1131
                    } else {
1132
                        andEvaluator.addEvaluator(evaluator);
1133
                    }
1134
                } catch (InitializeException ex) {
1135
                    LOGGER.warn("Can't create AndEvaluator", ex);//TODO evaluator a null
1136
                    break;
1137
                }
1138
                this.filter = evaluator;
1139

    
1140
            }
1141
        }
1142
        this.limit = json.getInt("limit");
1143
        if(version == null || version.compareTo(VERSION_2_6_0)<0){
1144
            if(this.limit == 0) {
1145
                this.clearLimit();
1146
            }
1147
        }
1148
        this.pageSize = json.getInt("pageSize");
1149
        this.useSubquery = json.getBoolean("useSubquery",true);
1150
        this.storeName = json.getString("storeName");
1151
        
1152
        
1153
        this.order = (FeatureQueryOrder) Json.toObject(json, "order");
1154
        
1155
        Collection<String> theGroupByColumns = Json.toCollection(json.getJsonArray("groupByColumns"));
1156
        if (theGroupByColumns!=null) {
1157
            this.groupByColumns = new ArrayList<>(theGroupByColumns);
1158
        } else {
1159
            this.groupByColumns = null;
1160
        }
1161
        
1162
        
1163
        Map asMapAggregateFunctions = Json.toMap(json,"aggregateFunctions");
1164
        if (asMapAggregateFunctions!=null) {
1165
            this.aggregateFunctions = new HashMap<>(asMapAggregateFunctions);
1166
        } else {
1167
            this.aggregateFunctions = null;
1168
        }
1169
        this.extraColumns = (FeatureExtraColumns) Json.toObject(json,"extraColumn");
1170
    }
1171

    
1172
    @Override
1173
    public JsonObjectBuilder toJsonBuilder() {
1174
        JsonObjectBuilder state = Json.createObjectBuilder();
1175
        state.add("version", VERSION_2_6_0.toString());
1176
        state.add("queryParameters", this.queryParameters);
1177
        state.add("featureTypeId", this.featureTypeId);
1178
        state.add("attributeNames", this.attributeNames);
1179
        
1180
        JsonArrayBuilder filterList = Json.createArrayBuilder();
1181
        if (this.filter instanceof DefaultFeatureExpressionEvaluator) {
1182
            DefaultFeatureExpressionEvaluator filterExpression = (DefaultFeatureExpressionEvaluator) this.filter;
1183
           filterList.add(filterExpression.toExpression());
1184
        } else if (this.filter instanceof AndEvaluator) {
1185
            AndEvaluator filterAnd = (AndEvaluator) this.filter;
1186
            List<Evaluator> evaluators = filterAnd.getEvaluators();
1187
            for (Evaluator evaluator : evaluators) {
1188
                if (evaluator instanceof DefaultFeatureExpressionEvaluator) {
1189
                    DefaultFeatureExpressionEvaluator expressionEvaluator = (DefaultFeatureExpressionEvaluator) evaluator;
1190
                    filterList.add(expressionEvaluator.toExpression());
1191
                } else {
1192
                    filterList = Json.createArrayBuilder();
1193
                    LOGGER.warn(StringUtils.join("Filters in this FeatureQuery will not persist:", this.toString()));
1194
                    break;
1195
                }
1196
            }
1197
        } else {
1198
            filterList = Json.createArrayBuilder();
1199
            if( this.filter!=null ) {
1200
                LOGGER.warn(StringUtils.join("Filters in this FeatureQuery will not persist:", this.toString()));
1201
            }
1202
        }
1203
        
1204
        state.add("filter", filterList);
1205
        state.add("limit", this.limit);
1206
        state.add("pageSize", this.pageSize);
1207
        state.add("useSubquery", this.useSubquery);
1208
        
1209
        state.add("order", (SupportJson) this.order); 
1210
        state.add("groupByColumns", this.groupByColumns); 
1211
        state.add("aggregateFunctions", this.aggregateFunctions);
1212
        state.add("extraColumn", (SupportToJson) this.extraColumns); 
1213
        state.add("storeName", this.storeName);
1214
        
1215
        return state;
1216
    }
1217

    
1218
    @Override
1219
    public String toString() {
1220
        try {
1221
            ToStringBuilder builder = new ToStringBuilder(this);
1222
            builder.append("storeName", this.storeName);
1223
            builder.append("filter", this.filter, true);
1224
            builder.append("order", this.order, true); 
1225
            builder.append("limit", this.limit);
1226
            builder.append("attributeNames", this.attributeNames, true);
1227
            builder.append("queryParameters", this.queryParameters, true);
1228
            builder.append("pageSize", this.pageSize);
1229
            builder.append("useSubquery", this.useSubquery);
1230
            builder.append("groupByColumns", this.groupByColumns); 
1231
            builder.append("aggregateFunctions", this.aggregateFunctions);
1232
            builder.append("featureTypeId", this.featureTypeId);
1233
            builder.append("extraColumn", this.extraColumns); 
1234

    
1235
            return builder.toString();
1236
        } catch (Exception e) {
1237
            return super.toString();
1238
        }
1239
    }
1240

    
1241
    private void fixOldExistsSelect(Expression exp) {
1242
        try {
1243
            if(exp == null || !StringUtils.containsIgnoreCase(exp.getPhrase(), "SELECT")
1244
                    || !StringUtils.containsIgnoreCase(exp.getPhrase(), "EXISTS")){
1245
                return;
1246
            }
1247

    
1248
            MutableBoolean modified = new MutableBoolean(false);
1249
            Code code = exp.getCode();
1250
            code.accept(new Visitor() {
1251
                CodeBuilder codeBuilder = null;
1252
                @Override
1253
                public void visit(Object obj) throws VisitCanceledException, BaseException {
1254
                    if(Code.isFunction((Code) obj, "EXISTS")){
1255
                        Code.Callable fnExists = (Code.Callable) obj;
1256
                        if(Code.isFunction(fnExists.parameters().get(0), "SELECT")){
1257
                            Code.Callable fnSelect = (Code.Callable) fnExists.parameters().get(0);
1258
                            MutableCodes params = (MutableCodes) fnSelect.parameters();
1259
                            if(Code.isFunction(params.get(0), "GETATTR")){
1260
                                if(codeBuilder == null){
1261
                                    codeBuilder = ExpressionUtils.createCodeBuilder();
1262
                                }
1263
                                
1264
                                Code value = codeBuilder.tuple(codeBuilder.constant(1));
1265
                                params.set(0, value);
1266
                                modified.setTrue();
1267
                            }
1268
                        }
1269
                    }
1270
                }
1271
            });
1272
            if(modified.isTrue()){
1273
                code.link();
1274
                exp.setPhrase(code.toString());
1275
            }
1276
        } catch (Exception ex) {
1277
            LOGGER.debug("Can't fix old query with exists and select.", ex);
1278
        }
1279
        
1280
    }
1281
    
1282
}