Revision 46505 trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.swing/org.gvsig.fmap.dal.swing.impl/src/main/java/org/gvsig/fmap/dal/swing/impl/searchpanel/DefaultSearchParameters.java

View differences:

DefaultSearchParameters.java
14 14
import javax.json.Json;
15 15
import javax.json.JsonObject;
16 16
import javax.json.JsonReader;
17
import org.apache.commons.collections4.MapUtils;
17
import org.apache.commons.lang3.StringUtils;
18
import org.gvsig.expressionevaluator.Code;
18 19
import org.gvsig.expressionevaluator.Expression;
20
import org.gvsig.expressionevaluator.ExpressionUtils;
19 21
import org.gvsig.fmap.dal.exception.DataException;
20 22
import org.gvsig.fmap.dal.expressionevaluator.FeatureAttributeEmulatorExpression;
21 23
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
24
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
22 25
import org.gvsig.fmap.dal.feature.FeatureQuery;
23 26
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
24 27
import org.gvsig.fmap.dal.feature.FeatureSet;
......
31 34
import org.gvsig.tools.persistence.PersistenceManager;
32 35
import org.gvsig.tools.persistence.PersistentState;
33 36
import org.gvsig.tools.persistence.exception.PersistenceException;
37
import org.gvsig.tools.visitor.FilteredVisitable;
34 38

  
35 39
/**
36 40
 *
......
136 140
        }
137 141
    }
138 142
    
139
    private boolean isColumn(String name) {
140
        // FIXME: no tenemos el featuretype (?)
143
    private boolean isColumn(String name, FeatureType type) {
144
        if(type != null) {
145
            FeatureAttributeDescriptor attr = type.getAttributeDescriptorFromAll(name);
146
            if(attr != null){
147
                return true;
148
            }
149
        }
141 150
        if( this.query!=null ) {
142 151
            EditableFeatureAttributeDescriptor x = this.query.getExtraColumn().get(name);
143 152
            return x!=null;
......
184 193
                    builder.append("- ");
185 194
                    builder.append(function);
186 195
                    builder.append("(");
187
                    if( this.isColumn(exp) ) {
196
                    if( this.isColumn(exp, null) ) {
188 197
                        builder.append("\"");
189 198
                        builder.append(exp);
190 199
                        builder.append("\"");
......
220 229
                    } else {
221 230
                        builder.append(", ");
222 231
                    }
223
                    if( isColumn(groupByColumn) ) {
232
                    if( isColumn(groupByColumn, null) ) {
224 233
                        builder.append("\"");
225 234
                        builder.append(groupByColumn);
226 235
                        builder.append("\"");
......
374 383
    }
375 384

  
376 385
    void fix(FeatureType featureType) {
377
        List<String> toRemoveAggregateColumns = new ArrayList<>();
378
        Map<String, String> aggregateFunctions = this.query.getAggregateFunctions();
379
        for (Map.Entry<String, String> aggregate : aggregateFunctions.entrySet()) {
380
            String colname = aggregate.getKey();
381
            String funcname = aggregate.getValue();
382
            if( this.query.getExtraColumn().get(colname)==null && featureType.get(colname)==null ) {
383
                toRemoveAggregateColumns.add(colname);
386
        fix(featureType, false);
387
    }
388
    
389
    void fix(FeatureType featureType, boolean onlyEssentials) {
390
        if(!onlyEssentials){
391
            List<String> toRemoveAggregateColumns = new ArrayList<>();
392
            Map<String, String> aggregateFunctions = this.query.getAggregateFunctions();
393
            for (Map.Entry<String, String> aggregate : aggregateFunctions.entrySet()) {
394
                String colname = aggregate.getKey();
395
                String funcname = aggregate.getValue();
396
                if( this.query.getExtraColumn().get(colname)==null && featureType.get(colname)==null ) {
397
                    toRemoveAggregateColumns.add(colname);
398
                }
384 399
            }
400
            for (String toRemoveAggregateColumn : toRemoveAggregateColumns) {
401
                this.query.removeAggregateFunction(toRemoveAggregateColumn);
402
            }
385 403
        }
386
        for (String toRemoveAggregateColumn : toRemoveAggregateColumns) {
387
            this.query.removeAggregateFunction(toRemoveAggregateColumn);
388
        }
389 404
        if( this.query.getLimit()==0 ) {
390 405
            // Esto es por compatibilidad con querys guardados con antelacion
391 406
            // al cambio para hacer valido el limit 0.
392 407
            this.query.clearLimit();
393 408
        }
394 409
    }
410
    
411
    public boolean isValid(FeatureType type, StringBuilder errMessage){
412
        StringBuilder message;
413
        if(errMessage == null){
414
            message = new StringBuilder();
415
        } else {
416
            message = errMessage;
417
        }
418
        for (String attributeName : this.getResultColumnNames()) {
419
            if(!isColumn(attributeName, type)){
420
                message.append("Column \"");
421
                message.append(attributeName);
422
                message.append("\" not defined.\n");
423
            }
424
        }                
425
        
426
        if (this.query != null) { 
427
            try {
428
                for (Map.Entry<String, String> aggregate : this.getAggregateFunctions().entrySet()) {
429
                    String exp = aggregate.getKey();
430
                    String function = aggregate.getValue();
431
                    if(!isColumn(exp, type)){
432
                        Code code = null;
433
                        try {
434
                            code = ExpressionUtils.compile(exp);
435
                        } catch(Throwable t){
436
                            //TODO
437
                        }
438
                        if(code == null) {
439
                            message.append("Invalid expression in aggregate function ");
440
                            message.append(function);
441
                            message.append("(");
442
                            message.append(exp);
443
                            message.append(")\n");
444
                        }
445
                    }
446
                }
447
            } catch(Exception ex) {
448
                message.append("ERROR: ");
449
                message.append(ex.getMessage());
450
                message.append("\n");
451
            }
452

  
453
            try {
454
                Expression exp = this.query.getExpressionFilter();
455
                if (exp != null) {
456
                    Code code = null;
457
                    try {
458
                        code = exp.getCode();
459
                        code.accept((Object obj) -> {
460
                            String name1 = ((Code.Identifier)obj).name();
461
                            if (!isColumn(name1, type)) {
462
                                    message.append("Column \"");
463
                                    message.append(name1);
464
                                    message.append("\" not defined in filter.\n");
465
                            }
466
                        }, (FilteredVisitable t) -> (t instanceof Code.Identifier));
467
                    } catch (Throwable t) {
468
                        //TODO
469
                    }
470
                    if (code == null) {
471
                        message.append("Incorrect filter ");
472
                        message.append(exp.getPhrase());
473
                        message.append("\n");
474
                    }
475
                }
476
            } catch(Exception ex) {
477
                message.append("ERROR: ");
478
                message.append(ex.getMessage());
479
                message.append("\n");
480
            }
481

  
482
            try {
483
                for (String groupByColumn : this.query.getGroupByColumns()) {
484
                    if(!isColumn(groupByColumn, type)){
485
                        Code code = null;
486
                        try {
487
                            code = ExpressionUtils.compile(groupByColumn);
488
                        } catch(Throwable t){
489
                            //TODO
490
                        }
491
                        if(code == null) {
492
                            message.append("Group expression ");
493
                            message.append(groupByColumn);
494
                            message.append(" not valid \n");
495
                        }
496
                    }
497
                    
498
                }
499
            } catch(Exception ex) {
500
                message.append("ERROR: ");
501
                message.append(ex.getMessage());
502
                message.append("\n");
503
            }
504

  
505
            try {
506
                for (FeatureQueryOrder.FeatureQueryOrderMember member : this.query.getOrder().members()) {
507
                    if(!isColumn(member.getAttributeName(), type)){
508
                        Code code = null;
509
                        try {
510
                            code = ExpressionUtils.compile(member.getAttributeName());
511
                        } catch(Throwable t){
512
                            //Do nothing
513
                        }
514
                        if(code == null) {
515
                            message.append("Order expression ");
516
                            message.append(member.getAttributeName());
517
                            message.append(" not valid \n");
518
                        }
519
                    }
520

  
521
                }
522
            } catch(Exception ex) {
523
                message.append("ERROR: ");
524
                message.append(ex.getMessage());
525
                message.append("\n");
526
            }
527

  
528
            try {
529
                for (EditableFeatureAttributeDescriptor col : this.query.getExtraColumn().getColumns()) {
530
                    if( col.getFeatureAttributeEmulator() instanceof FeatureAttributeEmulatorExpression ) {
531
                        Expression exp = ((FeatureAttributeEmulatorExpression)col.getFeatureAttributeEmulator()).getExpression();
532
                        if( exp == null) {
533
                        } else {
534
                            Code code = null;
535
                            try {
536
                                code = exp.getCode();
537
                                code.accept((Object obj) -> {
538
                                    String name1 = ((Code.Identifier) obj).name();
539
                                    if (!isColumn(name1, type)) {
540
                                        message.append("\"");
541
                                        message.append(col.getName());
542
                                        message.append("\" refers to the nonexistent column \"");
543
                                        message.append(name1);
544
                                        message.append("\"");
545
                                    }
546
                                }, (FilteredVisitable t) -> (t instanceof Code.Identifier));
547
                            } catch (Throwable t) {
548
                                //Do nothing
549
                            }
550
                            if (code == null) {
551
                                message.append("Incorrect calculate column \"");
552
                                message.append(col.getName());
553
                                message.append("\"\n");
554
                            }
555
                        }
556
                    }
557
                }
558
            } catch(Exception ex) {
559
                message.append("ERROR: ");
560
                message.append(ex.getMessage());
561
                message.append("\n");
562
            }
563
        }
564
        if(StringUtils.isNotBlank(message)){
565
            return false;
566
        }
567
        
568
//        try {
569
//            for (Map.Entry<String, JsonObject> value : this.getValues().entrySet()) {
570
//                String key = value.getKey();
571
//                JsonObject data = value.getValue();
572
//            }
573
//        } catch(Exception ex) {
574
//                errMessage.append("ERROR: ");
575
//                errMessage.append(ex.getMessage());
576
//                errMessage.append("\n");
577
//        }
578
        return true;
579

  
580
    }
395 581
}
396 582

  

Also available in: Unified diff