fix pagination

This commit is contained in:
Mykhaylo
2021-09-11 13:32:02 +02:00
parent bb020e623a
commit 25377a639b
6 changed files with 100 additions and 10 deletions

View File

@@ -711,8 +711,16 @@ public class RelationshipServiceImpl implements RelationshipService {
@Override @Override
public List<Relationship> findByItemAndRelationshipTypeAndList(Context context, UUID focusUUID, public List<Relationship> findByItemAndRelationshipTypeAndList(Context context, UUID focusUUID,
RelationshipType relationshipType, List<UUID> items, boolean isLeft) throws SQLException { RelationshipType relationshipType, List<UUID> items, boolean isLeft,
int offset, int limit) throws SQLException {
return relationshipDAO 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<UUID> items, boolean isLeft) throws SQLException {
return relationshipDAO
.countByItemAndRelationshipTypeAndList(context, focusUUID, relationshipType, items, isLeft);
} }
} }

View File

@@ -218,6 +218,28 @@ public interface RelationshipDAO extends GenericDAO<Relationship> {
throws SQLException; 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<Relationship> findByItemAndRelationshipTypeAndList(Context context, UUID focusUUID,
RelationshipType relationshipType, List<UUID> 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 context DSpace context object
* @param focusUUID UUID of Item that will match left side if the param isLeft is true otherwise right side * @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 relationshipType Relationship type to filter by
@@ -227,6 +249,6 @@ public interface RelationshipDAO extends GenericDAO<Relationship> {
* @return * @return
* @throws SQLException If database error * @throws SQLException If database error
*/ */
List<Relationship> findByItemAndRelationshipTypeAndList(Context context, UUID focusUUID, int countByItemAndRelationshipTypeAndList(Context context, UUID focusUUID, RelationshipType relationshipType,
RelationshipType relationshipType, List<UUID> items, boolean isLeft) throws SQLException; List<UUID> items, boolean isLeft) throws SQLException;
} }

View File

@@ -268,7 +268,8 @@ public class RelationshipDAOImpl extends AbstractHibernateDAO<Relationship> impl
@Override @Override
public List<Relationship> findByItemAndRelationshipTypeAndList(Context context, UUID focusUUID, public List<Relationship> findByItemAndRelationshipTypeAndList(Context context, UUID focusUUID,
RelationshipType relationshipType, List<UUID> items, boolean isLeft) throws SQLException { RelationshipType relationshipType, List<UUID> items, boolean isLeft,
int offset, int limit) throws SQLException {
String side = isLeft ? "left_id" : "right_id"; String side = isLeft ? "left_id" : "right_id";
String otherSide = !isLeft ? "left_id" : "right_id"; String otherSide = !isLeft ? "left_id" : "right_id";
Query query = createQuery(context, "FROM " + Relationship.class.getSimpleName() + Query query = createQuery(context, "FROM " + Relationship.class.getSimpleName() +
@@ -278,7 +279,23 @@ public class RelationshipDAOImpl extends AbstractHibernateDAO<Relationship> impl
query.setParameter("typeId", relationshipType.getID()); query.setParameter("typeId", relationshipType.getID());
query.setParameter("focusUUID", focusUUID); query.setParameter("focusUUID", focusUUID);
query.setParameter("list", items); query.setParameter("list", items);
return list(query); return list(query, limit, offset);
}
@Override
public int countByItemAndRelationshipTypeAndList(Context context, UUID focusUUID, RelationshipType relationshipType,
List<UUID> 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);
} }
} }

View File

@@ -338,6 +338,28 @@ public interface RelationshipService extends DSpaceCRUDService<Relationship> {
throws SQLException, AuthorizeException; 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<Relationship> findByItemAndRelationshipTypeAndList(Context context, UUID focusUUID,
RelationshipType relationshipType, List<UUID> 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 context DSpace context object
* @param focusUUID UUID of Item that will match left side if the param isLeft is true otherwise right side * @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 relationshipType Relationship type to filter by
@@ -347,7 +369,7 @@ public interface RelationshipService extends DSpaceCRUDService<Relationship> {
* @return * @return
* @throws SQLException If database error * @throws SQLException If database error
*/ */
public List<Relationship> findByItemAndRelationshipTypeAndList(Context context, UUID focusUUID, public int countByItemAndRelationshipTypeAndList(Context context, UUID focusUUID, RelationshipType relationshipType,
RelationshipType relationshipType, List<UUID> items, boolean isLeft) throws SQLException; List<UUID> items, boolean isLeft) throws SQLException;
} }

View File

@@ -204,6 +204,22 @@ public abstract class AbstractHibernateDAO<T> implements GenericDAO<T> {
return result; 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<T> list(Query query, int limit, int offset) {
query.setFirstResult(offset);
query.setMaxResults(limit);
@SuppressWarnings("unchecked")
List<T> result = (List<T>) query.getResultList();
return result;
}
/** /**
* Retrieve a unique result from the query. If multiple results CAN be * Retrieve a unique result from the query. If multiple results CAN be
* retrieved an exception will be thrown, so only use when the criteria * retrieved an exception will be thrown, so only use when the criteria

View File

@@ -379,6 +379,7 @@ public class RelationshipRestRepository extends DSpaceRestRepository<Relationshi
@Parameter(value = "relatedItem", required = true) Set<UUID> items, @Parameter(value = "relatedItem", required = true) Set<UUID> items,
Pageable pageable) throws SQLException { Pageable pageable) throws SQLException {
Context context = obtainContext(); Context context = obtainContext();
int total = 0;
List<Relationship> relationships = new LinkedList<>(); List<Relationship> relationships = new LinkedList<>();
RelationshipType relationshipType = relationshipTypeService.find(context, typeId); RelationshipType relationshipType = relationshipTypeService.find(context, typeId);
if (Objects.nonNull(relationshipType)) { if (Objects.nonNull(relationshipType)) {
@@ -388,10 +389,14 @@ public class RelationshipRestRepository extends DSpaceRestRepository<Relationshi
" , does not match any relation!"); " , does not match any relation!");
} }
relationships = relationshipService.findByItemAndRelationshipTypeAndList(context, focusUUID, relationships = relationshipService.findByItemAndRelationshipTypeAndList(context, focusUUID,
relationshipType, new ArrayList<UUID>(items), relationshipType.getLeftwardType().equals(label)); relationshipType, new ArrayList<UUID>(items), relationshipType.getLeftwardType().equals(label),
Math.toIntExact(pageable.getOffset()),
Math.toIntExact(pageable.getPageSize()));
total = relationshipService.countByItemAndRelationshipTypeAndList(context, focusUUID,
relationshipType, new ArrayList<UUID>(items), relationshipType.getLeftwardType().equals(label));
} }
return converter.toRestPage(relationships, pageable, utils.obtainProjection()); return converter.toRestPage(relationships, pageable, total, utils.obtainProjection());
} }
} }