Project

General

Profile

Revision 76d829dc

ID76d829dc6040ac2edec7be4f2033d82f44131c84
Parent bd1fbf01
Child 440d74a5

Added by Andreas Kohlbecker about 3 years ago

ref #6612 PropertyNameMatchMode to Restriction class which contains the values to be tested

View differences:

cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/common/ICdmEntityDao.java
227 227
     *          all objects of type T or subclasses
228 228
     * @param restrictions
229 229
     *      This defines a filter for multiple properties represented by the map keys. Sine the keys are of the type
230
     *      {@link PropertyNameMatchMode} for each property a single MatchMode is defined. Multiple alternative values
230
     *      {@link Restriction} for each property a single MatchMode is defined. Multiple alternative values
231 231
     *      can be supplied per property, that is the values per property are combined with OR. The per property
232 232
     *      restrictions are combined with AND. </br>
233 233
     *      <b>NOTE:</b> For non string type properties you must use
......
246 246
     * @return
247 247
     * @throws DataAccessException
248 248
     */
249
    public List<T> list(Class<? extends T> type, Map<PropertyNameMatchMode,  Collection<? extends Object>> restrictions, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths);
249
    public List<T> list(Class<? extends T> type, List<Restriction<?>> restrictions, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths);
250 250

  
251 251
    /**
252 252
     * Counts the Cdm entities matching the restrictions defined by
......
257 257
     *          all objects of type T or subclasses
258 258
     * @param restrictions
259 259
     *      This defines a filter for multiple properties represented by the map keys. Sine the keys are of the type
260
     *      {@link PropertyNameMatchMode} for each property a single MatchMode is defined. Multiple alternative values
260
     *      {@link Restriction} for each property a single MatchMode is defined. Multiple alternative values
261 261
     *      can be supplied per property, that is the values per property are combined with OR. The per property
262 262
     *      restrictions are combined with AND. </br>
263 263
     *      <b>NOTE:</b> For non string type properties you must use
......
268 268
     *
269 269
     * @return
270 270
     */
271
    public int count(Class<? extends T> type, Map<PropertyNameMatchMode,  Collection<? extends Object>> restrictions);
271
    public int count(Class<? extends T> type, List<Restriction<?>> restrictions);
272 272

  
273 273
    /**
274 274
     * Returns a sublist of CdmBase instances of type <TYPE> stored in the database.
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/common/PropertyNameMatchMode.java
1
/**
2
* Copyright (C) 2017 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.persistence.dao.common;
10

  
11
import eu.etaxonomy.cdm.persistence.query.MatchMode;
12

  
13
/**
14
 * @author a.kohlbecker
15
 * @since May 8, 2017
16
 *
17
 */
18
public class PropertyNameMatchMode {
19

  
20
    private String propertyName;
21

  
22
    private MatchMode matchMode;
23

  
24

  
25

  
26
    /**
27
     * @param propertyName
28
     * @param matchMode
29
     */
30
    public PropertyNameMatchMode(String propertyName, MatchMode matchMode) {
31
        this.propertyName = propertyName;
32
        this.matchMode = matchMode;
33
    }
34

  
35
    /**
36
     * @return the propertyName
37
     */
38
    public String getPropertyName() {
39
        return propertyName;
40
    }
41

  
42
    /**
43
     * @param propertyName the propertyName to set
44
     */
45
    public void setPropertyName(String propertyName) {
46
        this.propertyName = propertyName;
47
    }
48

  
49
    /**
50
     * @return the matchMode
51
     */
52
    public MatchMode getMatchMode() {
53
        return matchMode;
54
    }
55

  
56
    /**
57
     * @param matchMode the matchMode to set
58
     */
59
    public void setMatchMode(MatchMode matchMode) {
60
        this.matchMode = matchMode;
61
    }
62

  
63

  
64
}
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/common/Restriction.java
1
/**
2
* Copyright (C) 2017 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.persistence.dao.common;
10

  
11
import java.util.ArrayList;
12
import java.util.Arrays;
13
import java.util.List;
14

  
15
import eu.etaxonomy.cdm.persistence.query.MatchMode;
16

  
17
/**
18
 *
19
 * @author a.kohlbecker
20
 * @since May 8, 2017
21
 *
22
 */
23
public class Restriction<T extends Object> {
24

  
25
    private String propertyName;
26

  
27
    private MatchMode matchMode;
28

  
29
    private List<T> values = null;
30

  
31
    /**
32
     * @param propertyName
33
     * @param matchMode
34
     */
35
    public Restriction(String propertyName, MatchMode matchMode, T ... values ) {
36
        this.propertyName = propertyName;
37
        this.matchMode = matchMode;
38
        if(values.length > 0){
39
            this.setValues(Arrays.asList(values));
40
        }
41
    }
42

  
43
    /**
44
     * @return the propertyName
45
     */
46
    public String getPropertyName() {
47
        return propertyName;
48
    }
49

  
50
    /**
51
     * @param propertyName the propertyName to set
52
     */
53
    public void setPropertyName(String propertyName) {
54
        this.propertyName = propertyName;
55
    }
56

  
57
    /**
58
     * @return the matchMode
59
     */
60
    public MatchMode getMatchMode() {
61
        return matchMode;
62
    }
63

  
64
    /**
65
     * @param matchMode the matchMode to set
66
     */
67
    public void setMatchMode(MatchMode matchMode) {
68
        this.matchMode = matchMode;
69
    }
70

  
71
    /**
72
     * @return the values, never <code>null</code>
73
     */
74
    public List<T> getValues() {
75
        if(values == null){
76
            values = new ArrayList<>();
77
        }
78
        return values;
79
    }
80

  
81
    /**
82
     * @param values the values to set
83
     */
84
    public void setValues(List<T> values) {
85
        this.values = values;
86
    }
87

  
88
    /**
89
     *
90
     * @param value
91
     */
92
    public void addValue(T value){
93
        getValues().add(value);
94
    }
95

  
96

  
97
}
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/hibernate/common/CdmEntityDaoBase.java
55 55
import eu.etaxonomy.cdm.model.common.User;
56 56
import eu.etaxonomy.cdm.model.common.VersionableEntity;
57 57
import eu.etaxonomy.cdm.persistence.dao.common.ICdmEntityDao;
58
import eu.etaxonomy.cdm.persistence.dao.common.PropertyNameMatchMode;
58
import eu.etaxonomy.cdm.persistence.dao.common.Restriction;
59 59
import eu.etaxonomy.cdm.persistence.dao.initializer.IBeanInitializer;
60 60
import eu.etaxonomy.cdm.persistence.dto.MergeResult;
61 61
import eu.etaxonomy.cdm.persistence.hibernate.PostMergeEntityListener;
......
453 453
     * {@inheritDoc}
454 454
     */
455 455
    @Override
456
    public List<T> list(Class<? extends T> type, Map<PropertyNameMatchMode,  Collection<? extends Object>> restrictions,
456
    public  List<T> list(Class<? extends T> type, List<Restriction<?>> restrictions,
457 457
            Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
458 458

  
459 459
        Criteria criteria = criterionForType(type);
......
473 473
     * @param restrictions
474 474
     * @param criteria
475 475
     */
476
    private void addRestrictions(Map<PropertyNameMatchMode, Collection<? extends Object>> restrictions, Criteria criteria) {
476
    private void addRestrictions(List<Restriction<?>> restrictions, Criteria criteria) {
477 477
        List<Criterion> perProperty = new ArrayList<>(restrictions.size());
478
        for(PropertyNameMatchMode propMatchMode : restrictions.keySet()){
479
            Collection<? extends Object> values = restrictions.get(propMatchMode);
478
        for(Restriction<?> propMatchMode : restrictions){
479
            Collection<? extends Object> values = propMatchMode.getValues();
480 480
            if(values != null && !values.isEmpty()){
481 481
                Criterion[] predicates = new Criterion[values.size()];
482 482
                int i = 0;
......
525 525
     * {@inheritDoc}
526 526
     */
527 527
    @Override
528
    public int count(Class<? extends T> type, Map<PropertyNameMatchMode,  Collection<? extends Object>> restrictions) {
528
    public int count(Class<? extends T> type, List<Restriction<?>> restrictions) {
529 529

  
530 530
        Criteria criteria = criterionForType(type);
531 531

  
cdmlib-persistence/src/test/java/eu/etaxonomy/cdm/persistence/dao/hibernate/agent/AgentDaoImplTest.java
15 15

  
16 16
import java.io.FileNotFoundException;
17 17
import java.util.ArrayList;
18
import java.util.Collection;
19
import java.util.HashMap;
20 18
import java.util.List;
21
import java.util.Map;
22 19
import java.util.UUID;
23 20

  
24 21
import org.junit.After;
......
36 33
import eu.etaxonomy.cdm.model.view.AuditEvent;
37 34
import eu.etaxonomy.cdm.model.view.context.AuditEventContextHolder;
38 35
import eu.etaxonomy.cdm.persistence.dao.agent.IAgentDao;
39
import eu.etaxonomy.cdm.persistence.dao.common.PropertyNameMatchMode;
36
import eu.etaxonomy.cdm.persistence.dao.common.Restriction;
40 37
import eu.etaxonomy.cdm.persistence.dao.reference.IReferenceDao;
41 38
import eu.etaxonomy.cdm.persistence.query.MatchMode;
42 39
import eu.etaxonomy.cdm.persistence.query.OrderHint;
......
262 259
    @DataSet("AgentDaoImplTest.xml")
263 260
    public void testListPeopleFiltered() {
264 261

  
265
        Map<PropertyNameMatchMode,  Collection<? extends Object>> restrictions = new HashMap<>();
262
        List<Restriction<?>> restrictions = new ArrayList<>();
266 263
        List<AgentBase> result = agentDao.list(null, restrictions, (Integer)null, (Integer)null, null, null);
267 264
        Assert.assertNotNull("list() should return a list",result);
268 265
        Assert.assertEquals("list() should return 9 AgentBase entities in the current view", 9 ,result.size());
......
270 267
        List<AgentBase>  personResults = agentDao.list(Person.class, restrictions, (Integer)null, (Integer)null, null, null);
271 268
        Assert.assertEquals("list() should return 5 Persons entities", 5, personResults.size());
272 269

  
273
        Collection<String> values = new ArrayList<>();
274
        PropertyNameMatchMode firstNameExact = new PropertyNameMatchMode("firstname", MatchMode.EXACT);
275
        restrictions.put(firstNameExact,  values);
270
        Restriction<String> firstNameExact = new Restriction<>("firstname", MatchMode.EXACT);
271
        restrictions.add(firstNameExact);
276 272

  
277 273
        personResults = agentDao.list(Person.class, restrictions, (Integer)null, (Integer)null, null, null);
278 274
        Assert.assertEquals("list() empty value lists should be ignored", 5, personResults.size());
279 275

  
280
        values.add("Ben");
281
        restrictions.put(firstNameExact, values);
276
        firstNameExact.addValue("Ben");
277
        restrictions.clear();
278
        restrictions.add(firstNameExact);
282 279
        personResults = agentDao.list(Person.class, restrictions, (Integer)null, (Integer)null, null, null);
283 280
        Assert.assertEquals("list() should return 1 AgentBase entity having the firstname 'Ben'", 1 ,personResults.size());
284 281
    }
......
287 284
    @DataSet("AgentDaoImplTest.xml")
288 285
    public void testCountPeopleFiltered() {
289 286

  
290
        Map<PropertyNameMatchMode,  Collection<? extends Object>> restrictions = new HashMap<>();
287
        List<Restriction<?>> restrictions = new ArrayList<>();
291 288
        Assert.assertEquals("count() should return 9 AgentBase entities", 9 , agentDao.count(null, restrictions));
292 289

  
293 290
        Assert.assertEquals("count() should return 5 Persons entities", 5, agentDao.count(Person.class, restrictions));
294 291

  
295
        Collection<String> values = new ArrayList<>();
296
        PropertyNameMatchMode firstNameExact = new PropertyNameMatchMode("firstname", MatchMode.EXACT);
297
        restrictions.put(firstNameExact,  values);
292
        Restriction<String> firstNameExact = new Restriction<>("firstname", MatchMode.EXACT);
293
        restrictions.add(firstNameExact);
294

  
298 295
        Assert.assertEquals("count() empty value lists should be ignored", 5, agentDao.count(Person.class, restrictions));
299 296

  
300
        values.add("Ben");
301
        restrictions.put(firstNameExact, values);
297
        firstNameExact.addValue("Ben");
298
        restrictions.clear();
299
        restrictions.add(firstNameExact);
302 300
        Assert.assertEquals("count() should return 1 Persons entity having the firstname 'Ben'", 1 , agentDao.count(Person.class, restrictions));
303 301
    }
304 302

  
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/RegistrationServiceImpl.java
9 9
package eu.etaxonomy.cdm.api.service;
10 10

  
11 11
import java.util.ArrayList;
12
import java.util.Arrays;
13 12
import java.util.Collection;
14
import java.util.HashMap;
15 13
import java.util.List;
16
import java.util.Map;
17 14
import java.util.Optional;
18 15

  
19 16
import org.springframework.beans.factory.annotation.Autowired;
......
27 24
import eu.etaxonomy.cdm.model.name.Registration;
28 25
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
29 26
import eu.etaxonomy.cdm.model.reference.Reference;
30
import eu.etaxonomy.cdm.persistence.dao.common.PropertyNameMatchMode;
27
import eu.etaxonomy.cdm.persistence.dao.common.Restriction;
31 28
import eu.etaxonomy.cdm.persistence.dao.name.IRegistrationDao;
32 29
import eu.etaxonomy.cdm.persistence.query.MatchMode;
33 30
import eu.etaxonomy.cdm.persistence.query.OrderHint;
......
75 72
    public Pager<Registration> page(User submitter, Collection<RegistrationStatus> includedStatus,
76 73
            Integer pageSize, Integer pageIndex, List<OrderHint> orderHints, List<String> propertyPaths) {
77 74

  
78
        Map<PropertyNameMatchMode,  Collection<? extends Object>> restrictions = new HashMap<>();
75
        List<Restriction<? extends Object>> restrictions = new ArrayList<>();
79 76
        if(submitter != null){
80
            restrictions.put(new PropertyNameMatchMode("submitter", MatchMode.EXACT), Arrays.asList(submitter));
77
            restrictions.add(new Restriction<User>("submitter", MatchMode.EXACT, submitter));
81 78
        }
82 79
        if(includedStatus != null && !includedStatus.isEmpty()){
83
            restrictions.put(new PropertyNameMatchMode("status", MatchMode.EXACT), includedStatus);
80
            restrictions.add(new Restriction<RegistrationStatus>("status", MatchMode.EXACT, includedStatus.toArray(new RegistrationStatus[includedStatus.size()])));
84 81
        }
85 82

  
86 83
        long numberOfResults = dao.count(Registration.class, restrictions);

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)