From 25377a639b5047f89facec85b4f520024bb4f86d Mon Sep 17 00:00:00 2001 From: Mykhaylo Date: Sat, 11 Sep 2021 13:32:02 +0200 Subject: [PATCH] fix pagination --- .../content/RelationshipServiceImpl.java | 12 +++++++-- .../dspace/content/dao/RelationshipDAO.java | 26 +++++++++++++++++-- .../content/dao/impl/RelationshipDAOImpl.java | 21 +++++++++++++-- .../content/service/RelationshipService.java | 26 +++++++++++++++++-- .../org/dspace/core/AbstractHibernateDAO.java | 16 ++++++++++++ .../RelationshipRestRepository.java | 9 +++++-- 6 files changed, 100 insertions(+), 10 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/content/RelationshipServiceImpl.java b/dspace-api/src/main/java/org/dspace/content/RelationshipServiceImpl.java index c7971716a4..a0878f49c2 100644 --- a/dspace-api/src/main/java/org/dspace/content/RelationshipServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/RelationshipServiceImpl.java @@ -711,8 +711,16 @@ public class RelationshipServiceImpl implements RelationshipService { @Override public List findByItemAndRelationshipTypeAndList(Context context, UUID focusUUID, - RelationshipType relationshipType, List items, boolean isLeft) throws SQLException { + RelationshipType relationshipType, List items, boolean isLeft, + int offset, int limit) throws SQLException { return relationshipDAO - .findByItemAndRelationshipTypeAndList(context, focusUUID, relationshipType, items, isLeft); + .findByItemAndRelationshipTypeAndList(context, focusUUID, relationshipType, items, isLeft, offset,limit); + } + + @Override + public int countByItemAndRelationshipTypeAndList(Context context, UUID focusUUID, RelationshipType relationshipType, + List items, boolean isLeft) throws SQLException { + return relationshipDAO + .countByItemAndRelationshipTypeAndList(context, focusUUID, relationshipType, items, isLeft); } } diff --git a/dspace-api/src/main/java/org/dspace/content/dao/RelationshipDAO.java b/dspace-api/src/main/java/org/dspace/content/dao/RelationshipDAO.java index 0e877d9913..57b950a36b 100644 --- a/dspace-api/src/main/java/org/dspace/content/dao/RelationshipDAO.java +++ b/dspace-api/src/main/java/org/dspace/content/dao/RelationshipDAO.java @@ -218,6 +218,28 @@ public interface RelationshipDAO extends GenericDAO { throws SQLException; /** + * This method is used to retrieve relationships that match focusItem + * on the one hand and matches list of related items elsewhere. + * + * @param context DSpace context object + * @param focusUUID UUID of Item that will match left side if the param isLeft is true otherwise right side + * @param relationshipType Relationship type to filter by + * @param items List of UUID that will use to filter other side respect the focusUUID + * @param isLeft Indicating whether the counted Relationships should have + * the given Item on the left side or not + * @param limit paging limit + * @param offset paging offset + * @return + * @throws SQLException If database error + */ + List findByItemAndRelationshipTypeAndList(Context context, UUID focusUUID, + RelationshipType relationshipType, List items, boolean isLeft, + int offset, int limit) throws SQLException; + + /** + * Count total number of relationships that match focusItem + * on the one hand and matches list of related items elsewhere. + * * @param context DSpace context object * @param focusUUID UUID of Item that will match left side if the param isLeft is true otherwise right side * @param relationshipType Relationship type to filter by @@ -227,6 +249,6 @@ public interface RelationshipDAO extends GenericDAO { * @return * @throws SQLException If database error */ - List findByItemAndRelationshipTypeAndList(Context context, UUID focusUUID, - RelationshipType relationshipType, List items, boolean isLeft) throws SQLException; + int countByItemAndRelationshipTypeAndList(Context context, UUID focusUUID, RelationshipType relationshipType, + List items, boolean isLeft) throws SQLException; } diff --git a/dspace-api/src/main/java/org/dspace/content/dao/impl/RelationshipDAOImpl.java b/dspace-api/src/main/java/org/dspace/content/dao/impl/RelationshipDAOImpl.java index e9bffc0d13..318ee63701 100644 --- a/dspace-api/src/main/java/org/dspace/content/dao/impl/RelationshipDAOImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/dao/impl/RelationshipDAOImpl.java @@ -268,7 +268,8 @@ public class RelationshipDAOImpl extends AbstractHibernateDAO impl @Override public List findByItemAndRelationshipTypeAndList(Context context, UUID focusUUID, - RelationshipType relationshipType, List items, boolean isLeft) throws SQLException { + RelationshipType relationshipType, List items, boolean isLeft, + int offset, int limit) throws SQLException { String side = isLeft ? "left_id" : "right_id"; String otherSide = !isLeft ? "left_id" : "right_id"; Query query = createQuery(context, "FROM " + Relationship.class.getSimpleName() + @@ -278,7 +279,23 @@ public class RelationshipDAOImpl extends AbstractHibernateDAO impl query.setParameter("typeId", relationshipType.getID()); query.setParameter("focusUUID", focusUUID); query.setParameter("list", items); - return list(query); + return list(query, limit, offset); + } + + @Override + public int countByItemAndRelationshipTypeAndList(Context context, UUID focusUUID, RelationshipType relationshipType, + List items, boolean isLeft) throws SQLException { + String side = isLeft ? "left_id" : "right_id"; + String otherSide = !isLeft ? "left_id" : "right_id"; + Query query = createQuery(context, "SELECT count(*) " + + "FROM " + Relationship.class.getSimpleName() + + " WHERE type_id = (:typeId) " + + "AND " + side + " = (:focusUUID) " + + "AND " + otherSide + " in (:list)"); + query.setParameter("typeId", relationshipType.getID()); + query.setParameter("focusUUID", focusUUID); + query.setParameter("list", items); + return count(query); } } diff --git a/dspace-api/src/main/java/org/dspace/content/service/RelationshipService.java b/dspace-api/src/main/java/org/dspace/content/service/RelationshipService.java index 9223fe17fc..6a100e2a3d 100644 --- a/dspace-api/src/main/java/org/dspace/content/service/RelationshipService.java +++ b/dspace-api/src/main/java/org/dspace/content/service/RelationshipService.java @@ -338,6 +338,28 @@ public interface RelationshipService extends DSpaceCRUDService { throws SQLException, AuthorizeException; /** + * This method is used to retrieve relationships that match focusItem + * on the one hand and matches list of related items elsewhere. + * + * @param context DSpace context object + * @param focusUUID UUID of Item that will match left side if the param isLeft is true otherwise right side + * @param relationshipType Relationship type to filter by + * @param items List of UUID that will use to filter other side respect the focusUUID + * @param isLeft Indicating whether the counted Relationships should have + * the given Item on the left side or not + * @param limit paging limit + * @param offset paging offset + * @return + * @throws SQLException If database error + */ + public List findByItemAndRelationshipTypeAndList(Context context, UUID focusUUID, + RelationshipType relationshipType, List items, boolean isLeft, + int offset, int limit) throws SQLException; + + /** + * Count total number of relationships that match focusItem + * on the one hand and matches list of related items elsewhere. + * * @param context DSpace context object * @param focusUUID UUID of Item that will match left side if the param isLeft is true otherwise right side * @param relationshipType Relationship type to filter by @@ -347,7 +369,7 @@ public interface RelationshipService extends DSpaceCRUDService { * @return * @throws SQLException If database error */ - public List findByItemAndRelationshipTypeAndList(Context context, UUID focusUUID, - RelationshipType relationshipType, List items, boolean isLeft) throws SQLException; + public int countByItemAndRelationshipTypeAndList(Context context, UUID focusUUID, RelationshipType relationshipType, + List items, boolean isLeft) throws SQLException; } diff --git a/dspace-api/src/main/java/org/dspace/core/AbstractHibernateDAO.java b/dspace-api/src/main/java/org/dspace/core/AbstractHibernateDAO.java index 9f97dce1ce..adeb4d09d8 100644 --- a/dspace-api/src/main/java/org/dspace/core/AbstractHibernateDAO.java +++ b/dspace-api/src/main/java/org/dspace/core/AbstractHibernateDAO.java @@ -204,6 +204,22 @@ public abstract class AbstractHibernateDAO implements GenericDAO { return result; } + /** + * This method will return a list of results for the given Query and parameters + * + * @param query The query for which the resulting list will be returned + * @param limit The maximum amount of results to be returned + * @param offset The offset to be used for the Query + * @return A list of results determined by the Query and parameters + */ + public List list(Query query, int limit, int offset) { + query.setFirstResult(offset); + query.setMaxResults(limit); + @SuppressWarnings("unchecked") + List result = (List) query.getResultList(); + return result; + } + /** * Retrieve a unique result from the query. If multiple results CAN be * retrieved an exception will be thrown, so only use when the criteria diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/RelationshipRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/RelationshipRestRepository.java index 3bcb57bd74..0fdf8eb7e2 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/RelationshipRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/RelationshipRestRepository.java @@ -379,6 +379,7 @@ public class RelationshipRestRepository extends DSpaceRestRepository items, Pageable pageable) throws SQLException { Context context = obtainContext(); + int total = 0; List relationships = new LinkedList<>(); RelationshipType relationshipType = relationshipTypeService.find(context, typeId); if (Objects.nonNull(relationshipType)) { @@ -388,10 +389,14 @@ public class RelationshipRestRepository extends DSpaceRestRepository(items), relationshipType.getLeftwardType().equals(label)); + relationshipType, new ArrayList(items), relationshipType.getLeftwardType().equals(label), + Math.toIntExact(pageable.getOffset()), + Math.toIntExact(pageable.getPageSize())); + total = relationshipService.countByItemAndRelationshipTypeAndList(context, focusUUID, + relationshipType, new ArrayList(items), relationshipType.getLeftwardType().equals(label)); } - return converter.toRestPage(relationships, pageable, utils.obtainProjection()); + return converter.toRestPage(relationships, pageable, total, utils.obtainProjection()); } }