Project

General

Profile

Revision 49532c71

ID49532c714277d9298c5c8b8231d2b825fdd0688f
Parent 9866eb9a
Child 89c17754, ff614835

Added by Andreas Kohlbecker over 3 years ago

ref #6612 new dao method list entities filtered by a parameter value

View differences:

cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/common/ICdmEntityDao.java
23 23
import eu.etaxonomy.cdm.persistence.dao.initializer.IBeanInitializer;
24 24
import eu.etaxonomy.cdm.persistence.dto.MergeResult;
25 25
import eu.etaxonomy.cdm.persistence.query.Grouping;
26
import eu.etaxonomy.cdm.persistence.query.MatchMode;
26 27
import eu.etaxonomy.cdm.persistence.query.OrderHint;
27 28

  
28 29
/**
......
191 192
     * The bean properties specified by the parameter <code>propertyPaths</code>
192 193
     * and recursively initialized for each of the entities in the resultset
193 194
     *
194
     * For detailed description and examples redarding
195
     * For detailed description and examples regarding
195 196
     * <code>propertyPaths</code> <b>please refer to:</b>
196 197
     * {@link IBeanInitializer#initialize(Object, List)}
197 198
     *
......
210 211
    public List<T> list(Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths);
211 212

  
212 213
    /**
214
     * Returns a list of Cdm entities stored in the database filtered by the restrictions defined by
215
     * the <code>parameters</code> <code>propertyName</code>, value and <code>matchMode</code>
216
     * A maximum
217
     * of 'limit' objects are returned, starting at object with index 'start'.
218
     * The bean properties specified by the parameter <code>propertyPaths</code>
219
     * and recursively initialized for each of the entities in the resultset
220
     *
221
     * For detailed description and examples regarding
222
     * <code>propertyPaths</code> <b>please refer to:</b>
223
     * {@link IBeanInitializer#initialize(Object, List)}
224
     *
225
     * @param type
226
     *          Restrict the query to objects of a certain class, or null for
227
     *          all objects of type T or subclasses
228
     * @param propertyName
229
     *      The name of a entity property.
230
     * @param value
231
     *      The value for the comparison with the entity property.
232
     * @param matchMode
233
     *      The comparison method to use. <b>NOTE:</b> For non string type properties you must use
234
     *      {@link MatchMode#EXACT}. If set <code>null</code> {@link MatchMode#EXACT} will be used
235
     *      as default.
236
     * @param limit
237
     *         the maximum number of entities returned (can be null to return
238
     *         all entities)
239
     * @param start
240
     *       The list of criterion objects representing the restriction to be applied.
241
     * @param orderHints
242
     *            Supports path like <code>orderHints.propertyNames</code> which
243
     *            include *-to-one properties like createdBy.username or
244
     *            authorTeam.persistentTitleCache
245
     * @param propertyPaths
246
     * @return
247
     * @throws DataAccessException
248
     */
249
    public List<T> list(Class<? extends T> type, String propertyName, Object value, MatchMode matchMode, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths);
250

  
251
    /**
252
     * Counts the Cdm entities matching the restrictions defined by
253
     * the <code>parameters</code> <code>propertyName</code>, value and <code>matchMode</code>.
254
     *
255
     * @param type
256
     *          Restrict the query to objects of a certain class, or null for
257
     *          all objects of type T or subclasses
258
     * @param propertyName
259
     *      The name of a entity property.
260
     * @param value
261
     *      The value for the comparison with the entity property.
262
     * @param matchMode
263
     *      The comparison method to use. <b>NOTE:</b> For non string type properties you must use
264
     *      {@link MatchMode#EXACT}. If set <code>null</code> {@link MatchMode#EXACT} will be used
265
     *      as default.
266
     * @param criteria
267
     *       The list of criterion objects representing the restriction to be applied.
268
     *
269
     * @return
270
     */
271
    public int count(Class<? extends T> type, String propertyName, Object value, MatchMode matchMode);
272

  
273
    /**
213 274
     * Returns a sublist of CdmBase instances of type <TYPE> stored in the database.
214 275
     * A maximum of 'limit' objects are returned, starting at object with index 'start'.
215 276
     * @param limit the maximum number of entities returned (can be null to return all entities)
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/hibernate/common/CdmEntityDaoBase.java
71 71
 */
72 72
@Repository
73 73
public abstract class CdmEntityDaoBase<T extends CdmBase> extends DaoBase implements ICdmEntityDao<T> {
74

  
74 75
    private static final Logger logger = Logger.getLogger(CdmEntityDaoBase.class);
75 76

  
76 77
    protected int flushAfterNo = 1000; //large numbers may cause synchronisation errors when commiting the session !!
......
448 449
    }
449 450

  
450 451
    /**
452
     * {@inheritDoc}
453
     */
454
    @Override
455
    public List<T> list(Class<? extends T> type, String propertyName, Object value, MatchMode matchMode,
456
            Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
457

  
458
        Criteria criteria = criterionForType(type);
459

  
460
        if (propertyName != null) {
461
            addRestriction(propertyName, value, matchMode, criteria);
462
        }
463

  
464
        addLimitAndStart(limit, start, criteria);
465
        addOrder(criteria, orderHints);
466

  
467
        @SuppressWarnings("unchecked")
468
        List<T> result = criteria.list();
469
        defaultBeanInitializer.initializeAll(result, propertyPaths);
470
        return result;
471
    }
472

  
473
    /**
474
     * @param propertyName
475
     * @param value
476
     * @param matchMode
477
     * @param criteria
478
     */
479
    private void addRestriction(String propertyName, Object value, MatchMode matchMode, Criteria criteria) {
480
        Criterion restriction;
481
        if(matchMode == null) {
482
            restriction = Restrictions.eq(propertyName, value);
483
        } else if(value == null) {
484
            restriction = Restrictions.isNull(propertyName);
485
        } else if(!(value instanceof String)) {
486
            restriction = Restrictions.eq(propertyName, value);
487
        } else {
488
            String queryString = (String)value;
489
            if(matchMode == MatchMode.BEGINNING) {
490
                restriction = Restrictions.ilike(propertyName, queryString, org.hibernate.criterion.MatchMode.START);
491
            } else if(matchMode == MatchMode.END) {
492
                restriction = Restrictions.ilike(propertyName, queryString, org.hibernate.criterion.MatchMode.END);
493
            } else if(matchMode == MatchMode.EXACT) {
494
                restriction = Restrictions.ilike(propertyName, queryString, org.hibernate.criterion.MatchMode.EXACT);
495
            } else {
496
                restriction = Restrictions.ilike(propertyName, queryString, org.hibernate.criterion.MatchMode.ANYWHERE);
497
            }
498
        }
499
        criteria.add(restriction);
500
    }
501

  
502
    /**
503
     * {@inheritDoc}
504
     */
505
    @Override
506
    public int count(Class<? extends T> type, String propertyName, Object value, MatchMode matchMode) {
507

  
508
        Criteria criteria = criterionForType(type);
509

  
510
        if (propertyName != null) {
511
            addRestriction(propertyName, value, matchMode, criteria);
512
        }
513

  
514
        criteria.setProjection(Projections.projectionList().add(Projections.rowCount()));
515

  
516
        //since hibernate 4 (or so) uniqueResult returns Long, not Integer, therefore needs
517
        //to be casted. Think about returning long rather then int!
518
        return ((Number) criteria.uniqueResult()).intValue();
519

  
520
    }
521

  
522
    /**
451 523
     * @param uuids
452 524
     * @param pageSize
453 525
     * @param pageNumber
......
475 547
        return criteria;
476 548
    }
477 549

  
478

  
550
    /**
551
     *
552
     * NOTE: We can't reuse {@link #list(Class, String, Object, MatchMode, Integer, Integer, List, List)
553
     * here due to different default behavior of the <code>matchmode</code> parameter.
554
     *
555
     * @param clazz
556
     * @param param
557
     * @param queryString
558
     * @param matchmode
559
     * @param criterion
560
     * @param pageSize
561
     * @param pageNumber
562
     * @param orderHints
563
     * @param propertyPaths
564
     * @return
565
     */
479 566
    protected List<T> findByParam(Class<? extends T> clazz, String param, String queryString, MatchMode matchmode, List<Criterion> criterion, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
480
        Criteria criteria = null;
481 567

  
482
        if(clazz == null) {
483
            criteria = getSession().createCriteria(type);
484
        } else {
485
            criteria = getSession().createCriteria(clazz);
486
        }
568
        Criteria criteria = criterionForType(clazz);
487 569

  
488 570
        if (queryString != null) {
489 571
            if(matchmode == null) {
......
518 600
        return result;
519 601
    }
520 602

  
603
    /**
604
     * @param clazz
605
     * @return
606
     */
607
    private Criteria criterionForType(Class<? extends T> clazz) {
608
        Criteria criteria;
609
        if(clazz == null) {
610
            criteria = getSession().createCriteria(type);
611
        } else {
612
            criteria = getSession().createCriteria(clazz);
613
        }
614
        return criteria;
615
    }
616

  
521 617
    @Override
522 618
    public T load(UUID uuid) {
523 619
        T bean = findByUuid(uuid);
......
589 685
    public List<Object[]> group(Class<? extends T> clazz,Integer limit, Integer start, List<Grouping> groups, List<String> propertyPaths) {
590 686

  
591 687
        Criteria criteria = null;
592
        if(clazz == null){
593
            criteria = getSession().createCriteria(type);
594
        } else {
595
            criteria = getSession().createCriteria(clazz);
596
        }
688
        criteria = criterionForType(clazz);
597 689

  
598 690
        addGroups(criteria,groups);
599 691

  
......
726 818
        return results;
727 819
    }
728 820

  
821

  
729 822
    @Override
730 823
    public <S extends T> List<S> list(Class<S> clazz, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
731 824
        Criteria criteria = null;
......
735 828
            criteria = getSession().createCriteria(clazz);
736 829
        }
737 830

  
738
        if(limit != null) {
739
            if(start != null) {
740
                criteria.setFirstResult(start);
741
            } else {
742
                criteria.setFirstResult(0);
743
            }
744
            criteria.setMaxResults(limit);
745
        }
831
        addLimitAndStart(limit, start, criteria);
746 832

  
747 833
        addOrder(criteria, orderHints);
748 834

  
......
753 839
        return results;
754 840
    }
755 841

  
842
    /**
843
     * @param limit
844
     * @param start
845
     * @param criteria
846
     */
847
    private void addLimitAndStart(Integer limit, Integer start, Criteria criteria) {
848
        if(limit != null) {
849
            if(start != null) {
850
                criteria.setFirstResult(start);
851
            } else {
852
                criteria.setFirstResult(0);
853
            }
854
            criteria.setMaxResults(limit);
855
        }
856
    }
857

  
756 858

  
757 859
    public <S extends T> List<S> list(Class<S> type, Integer limit, Integer start, List<OrderHint> orderHints) {
758 860
        return list(type,limit,start,orderHints,null);
......
846 948
    protected long countByParam(Class<? extends T> clazz, String param, String queryString, MatchMode matchmode, List<Criterion> criterion) {
847 949
        Criteria criteria = null;
848 950

  
849
        if(clazz == null) {
850
            criteria = getSession().createCriteria(type);
851
        } else {
852
            criteria = getSession().createCriteria(clazz);
853
        }
951
        criteria = criterionForType(clazz);
854 952

  
855 953
        if (queryString != null) {
856 954
            if(matchmode == null) {
......
879 977
        Criteria criteria = getSession().createCriteria(example.getClass());
880 978
        addExample(criteria,example,includeProperties);
881 979

  
882
        if(limit != null) {
883
            if(start != null) {
884
                criteria.setFirstResult(start);
885
            } else {
886
                criteria.setFirstResult(0);
887
            }
888
            criteria.setMaxResults(limit);
889
        }
980
        addLimitAndStart(limit, start, criteria);
890 981

  
891 982
        addOrder(criteria,orderHints);
892 983

  
cdmlib-persistence/src/test/java/eu/etaxonomy/cdm/persistence/dao/hibernate/agent/AgentDaoImplTest.java
18 18
import java.util.List;
19 19
import java.util.UUID;
20 20

  
21
import org.hibernate.criterion.Criterion;
22
import org.hibernate.criterion.Restrictions;
21 23
import org.junit.After;
22 24
import org.junit.Assert;
23 25
import org.junit.Before;
......
34 36
import eu.etaxonomy.cdm.model.view.context.AuditEventContextHolder;
35 37
import eu.etaxonomy.cdm.persistence.dao.agent.IAgentDao;
36 38
import eu.etaxonomy.cdm.persistence.dao.reference.IReferenceDao;
39
import eu.etaxonomy.cdm.persistence.query.MatchMode;
37 40
import eu.etaxonomy.cdm.persistence.query.OrderHint;
38 41
import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
39 42
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
......
254 257
    }
255 258

  
256 259
    @Test
260
    @DataSet("AgentDaoImplTest.xml")
261
    public void testListPeopleFiltered() {
262

  
263
        List<AgentBase> result = agentDao.list(null, (String)null, null, (MatchMode)null, (Integer)null, (Integer)null, null, null);
264
        Assert.assertNotNull("list() should return a list",result);
265
        Assert.assertEquals("list() should return 9 AgentBase entities in the current view", 9 ,result.size());
266

  
267
        List<AgentBase>  personResults = agentDao.list(Person.class, (String)null, null, (MatchMode)null, (Integer)null, (Integer)null, null, null);
268
        Assert.assertEquals("list() should return 5 Persons entities", 5, personResults.size());
269

  
270
        personResults = agentDao.list(Person.class, "firstname", "Ben", MatchMode.EXACT, (Integer)null, (Integer)null, null, null);
271
        Assert.assertEquals("list() should return 1 AgentBase entity having the firstname 'Ben'", 1 ,personResults.size());
272
    }
273

  
274
    @Test
275
    @DataSet("AgentDaoImplTest.xml")
276
    public void testCountPeopleFiltered() {
277
        List<Criterion> restrictions = new ArrayList<Criterion>();
278

  
279
        Assert.assertEquals("count() should return 9 AgentBase entities", 9 , agentDao.count(null, (String)null, null, (MatchMode)null));
280

  
281
        Assert.assertEquals("count() should return 5 Persons entities", 5, agentDao.count(Person.class, (String)null, null, (MatchMode)null));
282

  
283
        restrictions.add(Restrictions.eq("firstname", "Ben"));
284
        Assert.assertEquals("count() should return 1 Persons entity having the firstname 'Ben'", 1 , agentDao.count(Person.class, "firstname", "Ben", MatchMode.EXACT));
285
    }
286

  
287
    @Test
257 288
    @DataSet("AgentDaoImplTest.testExists.xml")
258 289
    public void testListPeopleInPreviousView() {
259 290
        AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
260
        List<Person> result = agentDao.list(Person.class,null, null);
291
        List<Person> result = agentDao.list(Person.class, null, null);
261 292
        Assert.assertNotNull("list() should return a list",result);
262 293
        Assert.assertEquals("list() should return five agents in the current view",result.size(),5);
263 294
    }

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)