From 1c34927e9201cbc82938a1002c7ee6bcb78384e6 Mon Sep 17 00:00:00 2001 From: Jens Pelzetter Date: Wed, 5 Aug 2020 17:46:24 +0200 Subject: [PATCH] Added methods with limit and offset parameter to page models repo --- .../pagemodel/PageModelRepository.java | 97 ++++++++++++++++--- 1 file changed, 82 insertions(+), 15 deletions(-) diff --git a/ccm-core/src/main/java/org/libreccm/pagemodel/PageModelRepository.java b/ccm-core/src/main/java/org/libreccm/pagemodel/PageModelRepository.java index bbcb46ac0..c94b2c492 100644 --- a/ccm-core/src/main/java/org/libreccm/pagemodel/PageModelRepository.java +++ b/ccm-core/src/main/java/org/libreccm/pagemodel/PageModelRepository.java @@ -58,7 +58,7 @@ public class PageModelRepository extends AbstractEntityRepository findAllDraftModels() { + return getEntityManager() + .createNamedQuery("PageModel.findAllDraftModels", PageModel.class) + .getResultList(); + } - final TypedQuery query = getEntityManager() - .createNamedQuery("PageModel.findAllDraftModels", PageModel.class); - - return query.getResultList(); + /** + * Find all draft versions of {@link PageModel}s. + * + * @param limit + * @param offset + * + * @return A list with all draft versions of {@link PageModel}s. + */ + @AuthorizationRequired + @RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN) + @Transactional(Transactional.TxType.REQUIRED) + public List findAllDraftModels( + final int limit, final int offset + ) { + return getEntityManager() + .createNamedQuery("PageModel.findAllDraftModels", PageModel.class) + .setMaxResults(limit) + .setFirstResult(offset) + .getResultList(); } /** @@ -120,11 +138,27 @@ public class PageModelRepository extends AbstractEntityRepository findAllLiveModels() { + return getEntityManager() + .createNamedQuery("PageModel.findAllLiveModels", PageModel.class) + .getResultList(); + } - final TypedQuery query = getEntityManager() - .createNamedQuery("PageModel.findAllLiveModels", PageModel.class); - - return query.getResultList(); + /** + * Find all live versions of {@link PageModel}s. + * + * @param limit + * @param offset + * @return A list with all draft versions of {@link PageModel}s. + */ + @Transactional(Transactional.TxType.REQUIRED) + public List findAllLiveModels( + final int limit, final int offset + ) { + return getEntityManager() + .createNamedQuery("PageModel.findAllLiveModels", PageModel.class) + .setMaxResults(limit) + .setFirstResult(offset) + .getResultList(); } /** @@ -141,17 +175,50 @@ public class PageModelRepository extends AbstractEntityRepository findDraftByApplication( - final CcmApplication application) { + final CcmApplication application + ) { Objects.requireNonNull(application, "Can't find page models for application null"); - final TypedQuery query = getEntityManager() + return getEntityManager() .createNamedQuery("PageModel.findDraftByApplication", - PageModel.class); - query.setParameter("application", application); + PageModel.class) + .setParameter("application", application) + .getResultList(); + } + + /** + * Finds the draft versions of all {@link PageModel}s for the provided + * application. + * + * @param application The application for which the {@link PageModel}s are + * retrieved. + * @param limit + * @param offset + * + * @return A list of the {@link PageModel}s defined for the provided + * {@code application}. + */ + @Transactional(Transactional.TxType.REQUIRED) + @AuthorizationRequired + @RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN) + public List findDraftByApplication( + final CcmApplication application, + final int limit, + final int offset + ) { - return query.getResultList(); + Objects.requireNonNull(application, + "Can't find page models for application null"); + + return getEntityManager() + .createNamedQuery("PageModel.findDraftByApplication", + PageModel.class) + .setParameter("application", application) + .setMaxResults(limit) + .setFirstResult(offset) + .getResultList(); } /**