Project

General

Profile

Revision d01b74ef

IDd01b74ef078bb3c957ec83e956f1a0da45f1250a
Parent 024d9b21
Child 8a81795d

Added by Andreas Kohlbecker over 2 years ago

ref #7525 ref #7523 allowing to use Restriction as exclude filter

View differences:

cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/common/Restriction.java
27 27

  
28 28
    private MatchMode matchMode;
29 29

  
30
    private boolean not = false;
31

  
30 32
    private List<T> values = null;
31 33

  
32 34
    /**
......
34 36
     * @param matchMode is only applied if the <code>value</code> is a <code>String</code> object
35 37
     */
36 38
    public Restriction(String propertyName, MatchMode matchMode, T ... values ) {
39
        this(propertyName, false, matchMode, values);
40
    }
41

  
42
    public Restriction(String propertyName, boolean not , MatchMode matchMode, T ... values ) {
37 43
        this.propertyName = propertyName;
44
        this.not = not;
38 45
        if(values.length > 0){
39 46
            this.setValues(Arrays.asList(values));
40 47
            if(values[0] != null && values[0] instanceof String){
......
96 103
        getValues().add(value);
97 104
    }
98 105

  
106
    /**
107
     *
108
     * @return the not
109
     */
110
    public boolean isNot() {
111
        return not;
112
    }
113

  
114
    /**
115
     * @param not the not to set
116
     */
117
    public void setNot(boolean not) {
118
        this.not = not;
119
    }
120

  
99 121

  
100 122
}
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/hibernate/common/CdmEntityDaoBase.java
41 41
import org.hibernate.envers.AuditReaderFactory;
42 42
import org.hibernate.envers.query.AuditQuery;
43 43
import org.hibernate.metadata.ClassMetadata;
44
import org.hibernate.sql.JoinType;
44 45
import org.hibernate.type.Type;
45 46
import org.joda.time.DateTime;
46 47
import org.springframework.beans.factory.annotation.Autowired;
......
523 524

  
524 525
        for(Restriction<?> propMatchMode : restrictions){
525 526
            Collection<? extends Object> values = propMatchMode.getValues();
527
            JoinType jointype = propMatchMode.isNot() ? JoinType.LEFT_OUTER_JOIN : JoinType.INNER_JOIN;
526 528
            if(values != null && !values.isEmpty()){
527 529
                // ---
528 530
                String propertyPath = propMatchMode.getPropertyName();
......
533 535
                    propertyName = propertyPath;
534 536
                } else {
535 537
                    // create aliases if the propertyName is a dot separated property path
536
                    String aĺiasKey = "";
538
                    String aĺiasKey = jointype.name() + "_";
537 539
                    String aliasedProperty = null;
538 540
                    String alias = "";
539 541
                    for(int p = 0; p < props.length -1; p++){
......
542 544
                        if(!aliases.containsKey(aliasedProperty)){
543 545
                            alias = alias + (alias.isEmpty() ? "" : "_" ) + props[p];
544 546
                            aliases.put(aĺiasKey, alias);
545
                            criteria.createAlias(aliasedProperty, alias);
547
                            criteria.createAlias(aliasedProperty, alias, jointype);
546 548
                            if(logger.isDebugEnabled()){
547 549
                                logger.debug("addRestrictions() alias created with aliasKey " + aĺiasKey + " => " + aliasedProperty + " as " + alias);
548 550
                            }
......
554 556
                Criterion[] predicates = new Criterion[values.size()];
555 557
                int i = 0;
556 558
                for(Object v : values){
557
                    predicates[i++] = createRestriction(propertyName, v, propMatchMode.getMatchMode());
559
                    Criterion criterion = createRestriction(propertyName, v, propMatchMode.getMatchMode());
560
                    if(propMatchMode.isNot()){
561
                        if(props.length > 1){
562
                            criterion = Restrictions.or(Restrictions.not(criterion), Restrictions.isNull(propertyName));
563
                        } else {
564
                            criterion = Restrictions.not(criterion);
565
                        }
566
                    }
567
                    predicates[i++] = criterion;
558 568
                    if(logger.isDebugEnabled()){
559 569
                        logger.debug("addRestrictions() predicate with " + propertyName + " " + (propMatchMode.getMatchMode() == null ? "=" : propMatchMode.getMatchMode().name()) + " " + v.toString());
560 570
                    }

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)