Project

General

Profile

Revision 92d26f65

ID92d26f65a20deef9da0ab1ad9622c55e0de36729
Parent 7a56adaa
Child 30812b93

Added by Andreas Kohlbecker about 4 years ago

ref #7130 enhancing filter capabilities of RegistrationService
- extending Restrictions by support for property paths

View differences:

cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/hibernate/common/CdmEntityDaoBase.java
472 472
     * @param criteria
473 473
     */
474 474
    private void addRestrictions(List<Restriction<?>> restrictions, Criteria criteria) {
475

  
475 476
        List<Criterion> perProperty = new ArrayList<>(restrictions.size());
477
        Map<String, String> aliases = new HashMap<>();
478

  
476 479
        for(Restriction<?> propMatchMode : restrictions){
477 480
            Collection<? extends Object> values = propMatchMode.getValues();
478 481
            if(values != null && !values.isEmpty()){
479 482
                Criterion[] predicates = new Criterion[values.size()];
480 483
                int i = 0;
481 484
                for(Object v : values){
482
                    predicates[i++] = createRestriction(propMatchMode.getPropertyName(), v, propMatchMode.getMatchMode());
485
                    String propertyPath = propMatchMode.getPropertyName();
486
                    // create aliases if the propertyName is a dot separated property path
487
                    String[] props =  propertyPath.split("\\.");
488
                    String aliasKey = "";
489
                    String alias = null;
490
                    for(int p = 0; p < props.length -1; p++){
491
                        if(!aliases.containsKey(aliasKey + "_" + props[p])){
492
                            alias = props[p] + aliases.size();
493
                            aliases.put(aliasKey, alias);
494
                            criteria.createAlias(props[p], alias);
495
                        }
496
                    }
497

  
498
                    String propertyName = alias != null ? alias + "." + props[props.length -1] : propertyPath;
499
                    predicates[i++] = createRestriction(propertyName, v, propMatchMode.getMatchMode());
483 500
                }
484 501
                perProperty.add(Restrictions.or(predicates));
485 502
            }
486 503
        }
504

  
487 505
        if(!perProperty.isEmpty()){
488 506
            criteria.add(Restrictions.and(perProperty.toArray(new Criterion[perProperty.size()])));
489 507
        }
......
497 515
     * @return
498 516
     */
499 517
    private Criterion createRestriction(String propertyName, Object value, MatchMode matchMode) {
518

  
500 519
        Criterion restriction;
501 520
        if(matchMode == null) {
502 521
            restriction = Restrictions.eq(propertyName, value);
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/IRegistrationService.java
12 12
import java.util.List;
13 13
import java.util.Optional;
14 14

  
15
import org.springframework.dao.DataAccessException;
16

  
17 15
import eu.etaxonomy.cdm.api.service.pager.Pager;
18 16
import eu.etaxonomy.cdm.model.common.User;
19 17
import eu.etaxonomy.cdm.model.name.Registration;
20 18
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
21 19
import eu.etaxonomy.cdm.model.reference.Reference;
22
import eu.etaxonomy.cdm.persistence.dao.initializer.IBeanInitializer;
23 20
import eu.etaxonomy.cdm.persistence.query.OrderHint;
24 21

  
25 22
/**
......
91 88
     * @throws DataAccessException
92 89
     */
93 90
    public Pager<Registration> page(User submitter, Collection<RegistrationStatus> includedStatus,
91
            String identifierFilterPattern, String taxonNameFilterPattern,
94 92
            Integer pageSize, Integer pageIndex, List<OrderHint> orderHints, List<String> propertyPaths);
95 93

  
96 94

  
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/RegistrationServiceImpl.java
70 70
     */
71 71
    @Override
72 72
    public Pager<Registration> page(User submitter, Collection<RegistrationStatus> includedStatus,
73
            String identifierFilterPattern, String taxonNameFilterPattern,
73 74
            Integer pageSize, Integer pageIndex, List<OrderHint> orderHints, List<String> propertyPaths) {
74 75

  
75 76
        List<Restriction<? extends Object>> restrictions = new ArrayList<>();
77

  
76 78
        if(submitter != null){
77 79
            restrictions.add(new Restriction<>("submitter", MatchMode.EXACT, submitter));
78 80
        }
79 81
        if(includedStatus != null && !includedStatus.isEmpty()){
80 82
            restrictions.add(new Restriction<>("status", MatchMode.EXACT, includedStatus.toArray(new RegistrationStatus[includedStatus.size()])));
81 83
        }
84
        if(identifierFilterPattern != null){
85
            restrictions.add(new Restriction<>("identifier", MatchMode.LIKE, identifierFilterPattern));
86
        }
87
        if(taxonNameFilterPattern != null){
88
            restrictions.add(new Restriction<>("name.titleCache", MatchMode.LIKE, taxonNameFilterPattern));
89
        }
90

  
82 91

  
83 92
        long numberOfResults = dao.count(Registration.class, restrictions);
84 93

  

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)