Project

General

Profile

Revision 51ee66dc

ID51ee66dc3a639019afbb58811e32c98e0f186b12
Parent 52ecbe10
Child 86257e3b

Added by Andreas Kohlbecker almost 2 years ago

fix #7527 making page by parameter with criteria and restrictions available in the service layer

View differences:

cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/common/ICdmEntityDao.java
15 15
import java.util.Set;
16 16
import java.util.UUID;
17 17

  
18
import org.hibernate.Criteria;
18 19
import org.hibernate.LockOptions;
19 20
import org.hibernate.Session;
21
import org.hibernate.criterion.Criterion;
22
import org.hibernate.criterion.Projections;
23
import org.hibernate.criterion.Restrictions;
20 24
import org.springframework.dao.DataAccessException;
21 25

  
22 26
import eu.etaxonomy.cdm.model.common.CdmBase;
......
450 454
     */
451 455
    public List<T> list(T example, Set<String> includeProperties, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths);
452 456

  
457
    List<T> findByParamWithRestrictions(Class<? extends T> clazz, String param, String queryString, MatchMode matchmode, List<Restriction<?>> restrictions, Integer pageSize,
458
            Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths);
459

  
460
    long countByParamWithRestrictions(Class<? extends T> clazz, String param, String queryString, MatchMode matchmode, List<Restriction<?>> restrictions);
461

  
462
    long countByParam(Class<? extends T> clazz, String param, String queryString, MatchMode matchmode, List<Criterion> criterion);
463

  
464
    List<T> findByParam(Class<? extends T> clazz, String param, String queryString, MatchMode matchmode, List<Criterion> criterion, Integer pageSize, Integer pageNumber,
465
            List<OrderHint> orderHints, List<String> propertyPaths);
466

  
453 467
}
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/hibernate/common/CdmEntityDaoBase.java
975 975
     * @param orderHints
976 976
     * @param propertyPaths
977 977
     * @return
978
     *
979
     * @deprecated consider using {@link #findByParamWithRestrictions(Class, String, String, MatchMode, List, Integer, Integer, List, List))
980 978
     */
981
    @Deprecated
982
    protected List<T> findByParam(Class<? extends T> clazz, String param, String queryString, MatchMode matchmode,
979
    @Override
980
    public List<T> findByParam(Class<? extends T> clazz, String param, String queryString, MatchMode matchmode,
983 981
            List<Criterion> criterion, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,
984 982
            List<String> propertyPaths) {
985 983

  
......
1026 1024
     * @param matchmode
1027 1025
     * @param criterion
1028 1026
     * @return
1029
     *
1030
     * @deprecated consider using {@link #countByParamWithRestrictions(Class, String, String, MatchMode, List)}
1031 1027
     */
1032
    @Deprecated
1033
    protected long countByParam(Class<? extends T> clazz, String param, String queryString, MatchMode matchmode,
1028
    @Override
1029
    public long countByParam(Class<? extends T> clazz, String param, String queryString, MatchMode matchmode,
1034 1030
            List<Criterion> criterion) {
1035 1031

  
1036 1032
        Criteria criteria = null;
......
1124 1120
        return criteria;
1125 1121
    }
1126 1122

  
1127
    protected List<T> findByParamWithRestrictions(Class<? extends T> clazz, String param, String queryString,
1123

  
1124

  
1125
    @Override
1126
    public List<T> findByParamWithRestrictions(Class<? extends T> clazz, String param, String queryString,
1128 1127
            MatchMode matchmode, List<Restriction<?>> restrictions, Integer pageSize, Integer pageNumber,
1129 1128
            List<OrderHint> orderHints, List<String> propertyPaths) {
1130 1129

  
......
1148 1147

  
1149 1148
    }
1150 1149

  
1151
    protected long countByParamWithRestrictions(Class<? extends T> clazz, String param, String queryString,
1150
    @Override
1151
    public long countByParamWithRestrictions(Class<? extends T> clazz, String param, String queryString,
1152 1152
            MatchMode matchmode, List<Restriction<?>> restrictions) {
1153 1153

  
1154 1154
        Criteria criteria = createCriteria(clazz, param, queryString, matchmode, restrictions, true);
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/hibernate/common/DaoBase.java
114 114
        return result;
115 115
    }
116 116

  
117

  
118

  
117 119
    // -------------- hql, query and criteria helper methods -------------- //
118 120

  
119 121
    protected void addFieldPredicate(StringBuilder hql, String field, Optional<String> value) {
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/hibernate/common/VersionableDaoBase.java
75 75
	}
76 76

  
77 77
    @Override
78
	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) {
78
    public 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) {
79 79
    	checkNotInPriorView("IdentifiableDaoBase.findByParam(Class<? extends T> clazz, String queryString, MatchMode matchmode, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths)");
80 80
    	return super.findByParam(clazz, param, queryString, matchmode, criterion, pageSize, pageNumber, orderHints, propertyPaths);
81 81
    }
82 82

  
83 83
    @Override
84
    protected List<T> findByParamWithRestrictions(Class<? extends T> clazz, String param, String queryString, MatchMode matchmode, List<Restriction<?>> restrictions, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
84
    public List<T> findByParamWithRestrictions(Class<? extends T> clazz, String param, String queryString, MatchMode matchmode, List<Restriction<?>> restrictions, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
85 85
            checkNotInPriorView("IdentifiableDaoBase.findByParam(Class<? extends T> clazz, String queryString, MatchMode matchmode, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths)");
86 86
        return super.findByParamWithRestrictions(clazz, param, queryString, matchmode, restrictions, pageSize, pageNumber, orderHints, propertyPaths);
87 87
    }
......
383 383
	}
384 384

  
385 385
	@Override
386
	protected long countByParam(Class<? extends T> clazz, String param, String queryString, MatchMode matchmode, List<Criterion> criterion) {
386
	public long countByParam(Class<? extends T> clazz, String param, String queryString, MatchMode matchmode, List<Criterion> criterion) {
387 387
    	checkNotInPriorView("IdentifiableDaoBase.findByParam(Class<? extends T> clazz, String queryString, MatchMode matchmode, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths)");
388 388
    	return super.countByParam(clazz, param, queryString, matchmode, criterion);
389 389
	}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/IService.java
18 18

  
19 19
import org.hibernate.LockOptions;
20 20
import org.hibernate.Session;
21
import org.hibernate.event.spi.MergeEvent;
21
import org.hibernate.criterion.Criterion;
22 22

  
23 23
import eu.etaxonomy.cdm.api.service.pager.Pager;
24 24
import eu.etaxonomy.cdm.model.common.ICdmBase;
25
import eu.etaxonomy.cdm.persistence.dao.initializer.IBeanInitializer;
25
import eu.etaxonomy.cdm.persistence.dao.common.Restriction;
26 26
import eu.etaxonomy.cdm.persistence.dto.MergeResult;
27
import eu.etaxonomy.cdm.persistence.hibernate.PostMergeEntityListener;
28 27
import eu.etaxonomy.cdm.persistence.query.Grouping;
28
import eu.etaxonomy.cdm.persistence.query.MatchMode;
29 29
import eu.etaxonomy.cdm.persistence.query.OrderHint;
30 30

  
31 31

  
......
433 433
     */
434 434
    public MergeResult<T> merge(T newInstance, boolean returnTransientEntity);
435 435

  
436
    public Pager<T> page(Class<? extends T> clazz, String param, String queryString, MatchMode matchmode, List<Criterion> criteria, Integer pageSize, Integer pageIndex, List<OrderHint> orderHints, List<String> propertyPaths);
437

  
438
    public Pager<T> pageByRestrictions(Class<? extends T> clazz, String param, String queryString, MatchMode matchmode, List<Restriction<?>> restrictions, Integer pageSize, Integer pageIndex, List<OrderHint> orderHints,
439
            List<String> propertyPaths);
440

  
436 441
}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/ServiceBase.java
19 19
import org.apache.log4j.Logger;
20 20
import org.hibernate.LockOptions;
21 21
import org.hibernate.Session;
22
import org.hibernate.criterion.Criterion;
22 23
import org.springframework.context.ApplicationContext;
23 24
import org.springframework.context.ApplicationContextAware;
24 25
import org.springframework.dao.DataAccessException;
25 26
import org.springframework.transaction.annotation.Transactional;
26 27

  
27 28
import eu.etaxonomy.cdm.api.service.pager.Pager;
29
import eu.etaxonomy.cdm.api.service.pager.impl.AbstractPagerImpl;
28 30
import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
29 31
import eu.etaxonomy.cdm.exception.UnpublishedException;
30 32
import eu.etaxonomy.cdm.model.common.CdmBase;
31 33
import eu.etaxonomy.cdm.model.common.IPublishable;
32 34
import eu.etaxonomy.cdm.persistence.dao.common.ICdmEntityDao;
35
import eu.etaxonomy.cdm.persistence.dao.common.Restriction;
33 36
import eu.etaxonomy.cdm.persistence.dao.hibernate.common.DaoBase;
34 37
import eu.etaxonomy.cdm.persistence.dto.MergeResult;
35 38
import eu.etaxonomy.cdm.persistence.query.Grouping;
39
import eu.etaxonomy.cdm.persistence.query.MatchMode;
36 40
import eu.etaxonomy.cdm.persistence.query.OrderHint;
37 41

  
38 42
public abstract class ServiceBase<T extends CdmBase, DAO extends ICdmEntityDao<T>>
......
300 304
        return dao.list(example, includeProperties, limit, start, orderHints, propertyPaths);
301 305
    }
302 306

  
307
    @Override
308
    public Pager<T> page(Class<? extends T> clazz, String param, String queryString, MatchMode matchmode, List<Criterion> criteria, Integer pageSize, Integer pageIndex, List<OrderHint> orderHints, List<String> propertyPaths){
309

  
310
        List<T> records;
311
        long resultSize = dao.countByParam(clazz, param, queryString, matchmode, criteria);
312
        if(AbstractPagerImpl.hasResultsInRange(resultSize, pageIndex, pageSize)){
313
            records = dao.findByParam(clazz, param, queryString, matchmode, criteria, pageSize, pageIndex, orderHints, propertyPaths);
314
        } else {
315
            records = new ArrayList<>();
316
        }
317
        Pager<T> pager = new DefaultPagerImpl<>(pageIndex, resultSize, pageSize, records);
318
        return pager;
319
    }
320

  
321

  
322
    @Override
323
    public Pager<T> pageByRestrictions(Class<? extends T> clazz, String param, String queryString, MatchMode matchmode, List<Restriction<?>> restrictions, Integer pageSize, Integer pageIndex, List<OrderHint> orderHints, List<String> propertyPaths){
324

  
325
        List<T> records;
326
        long resultSize = dao.countByParamWithRestrictions(clazz, param, queryString, matchmode, restrictions);
327
        if(AbstractPagerImpl.hasResultsInRange(resultSize, pageIndex, pageSize)){
328
            records = dao.findByParamWithRestrictions(clazz, param, queryString, matchmode, restrictions, pageSize, pageIndex, orderHints, propertyPaths);
329
        } else {
330
            records = new ArrayList<>();
331
        }
332
        Pager<T> pager = new DefaultPagerImpl<>(pageIndex, resultSize, pageSize, records);
333
        return pager;
334
    }
335

  
303 336

  
304 337
    /**
305 338
     * Throws an exception if the publishable entity should not be published.

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)