[DSC-183] new paramater of type of dso in findall and pagination usage in query

This commit is contained in:
Mykhaylo
2022-12-01 18:54:35 +01:00
parent 141cde9899
commit 3ac40b9761
8 changed files with 80 additions and 30 deletions

View File

@@ -93,8 +93,12 @@ public class SubscribeCLITool {
IOException {
// Grab the subscriptions
List<Subscription> subscriptions = subscribeService.findAll(context);
List<Subscription> subscriptions = new ArrayList<>();
try {
subscriptions = subscribeService.findAll(context, null, -1, -1);
} catch (Exception e) {
log.error(e.getMessage());
}
EPerson currentEPerson = null;
List<Collection> collections = null; // List of Collections

View File

@@ -46,8 +46,18 @@ public class SubscribeServiceImpl implements SubscribeService {
}
@Override
public List<Subscription> findAll(Context context) throws SQLException {
public List<Subscription> findAll(Context context, String resourceType,
Integer limit, Integer offset) throws Exception {
if (resourceType == null) {
return subscriptionDAO.findAllOrderedByEPerson(context);
} else {
if (resourceType.equals("Item") || resourceType.equals("Collection") || resourceType.equals("Community")) {
return subscriptionDAO.findAllOrderedByEPersonAndResourceType(context, resourceType, limit, offset);
} else {
log.error("Resource type must be Item, Collection or Community");
throw new Exception("Resource type must be Item, Collection or Community");
}
}
}
@Override
@@ -96,16 +106,17 @@ public class SubscribeServiceImpl implements SubscribeService {
}
@Override
public List<Subscription> getSubscriptionsByEPerson(Context context, EPerson eperson)
public List<Subscription> getSubscriptionsByEPerson(Context context, EPerson eperson, Integer limit, Integer offset)
throws SQLException {
return subscriptionDAO.findByEPerson(context, eperson);
return subscriptionDAO.findByEPerson(context, eperson, limit, offset);
}
@Override
public List<Subscription> getSubscriptionsByEPersonAndDso(Context context,
EPerson eperson, DSpaceObject dSpaceObject)
EPerson eperson, DSpaceObject dSpaceObject,
Integer limit, Integer offset)
throws SQLException {
return subscriptionDAO.findByEPerson(context, eperson);
return subscriptionDAO.findByEPersonAndDso(context, eperson, dSpaceObject, limit, offset);
}
@Override
@@ -122,14 +133,13 @@ public class SubscribeServiceImpl implements SubscribeService {
context.setCurrentUser(eperson);
}
collections = collectionService.findAuthorized(context, null, Constants.ADD);
return collections;
}
@Override
public boolean isSubscribed(Context context, EPerson eperson,
DSpaceObject dSpaceObject) throws SQLException {
return subscriptionDAO.findByEPersonAndDso(context, eperson, dSpaceObject) != null;
return subscriptionDAO.findByEPersonAndDso(context, eperson, dSpaceObject, -1, -1) != null;
}
@Override

View File

@@ -2,7 +2,6 @@
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.eperson;

View File

@@ -2,7 +2,6 @@
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.eperson;

View File

@@ -27,15 +27,20 @@ public interface SubscriptionDAO extends GenericDAO<Subscription> {
public void deleteByDspaceObject(Context context, DSpaceObject dSpaceObject) throws SQLException;
public List<Subscription> findByEPerson(Context context, EPerson eperson) throws SQLException;
public List<Subscription> findByEPerson(Context context,
EPerson eperson, Integer limit, Integer offset) throws SQLException;
public List<Subscription> findByEPersonAndDso(Context context,
EPerson eperson, DSpaceObject dSpaceObject) throws SQLException;
EPerson eperson, DSpaceObject dSpaceObject,
Integer limit, Integer offset) throws SQLException;
public void deleteByEPerson(Context context, EPerson eperson) throws SQLException;
public void deleteByDSOAndEPerson(Context context, DSpaceObject dSpaceObject, EPerson eperson)
throws SQLException;
public List<Subscription> findAllOrderedByEPersonAndResourceType(Context context, String resourceType,
Integer limit, Integer offset) throws SQLException;
public List<Subscription> findAllOrderedByEPerson(Context context) throws SQLException;
}

View File

@@ -2,7 +2,6 @@
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.eperson.dao.impl;
@@ -36,18 +35,21 @@ public class SubscriptionDAOImpl extends AbstractHibernateDAO<Subscription> impl
}
@Override
public List<Subscription> findByEPerson(Context context, EPerson eperson) throws SQLException {
public List<Subscription> findByEPerson(Context context, EPerson eperson,
Integer limit, Integer offset) throws SQLException {
CriteriaBuilder criteriaBuilder = getCriteriaBuilder(context);
javax.persistence.criteria.CriteriaQuery criteriaQuery = getCriteriaQuery(criteriaBuilder, Subscription.class);
Root<Subscription> subscriptionRoot = criteriaQuery.from(Subscription.class);
criteriaQuery.select(subscriptionRoot);
criteriaQuery.where(criteriaBuilder.equal(subscriptionRoot.get(Subscription_.ePerson), eperson));
return list(context, criteriaQuery, false, Subscription.class, -1, -1);
return list(context, criteriaQuery, false, Subscription.class, limit, offset);
}
@Override
public List<Subscription> findByEPersonAndDso(Context context, EPerson eperson,
DSpaceObject dSpaceObject) throws SQLException {
DSpaceObject dSpaceObject,
Integer limit, Integer offset) throws SQLException {
CriteriaBuilder criteriaBuilder = getCriteriaBuilder(context);
javax.persistence.criteria.CriteriaQuery criteriaQuery =
getCriteriaQuery(criteriaBuilder, Subscription.class);
@@ -59,7 +61,7 @@ public class SubscriptionDAOImpl extends AbstractHibernateDAO<Subscription> impl
subscriptionRoot.get(Subscription_.ePerson), eperson),
criteriaBuilder.equal(subscriptionRoot.get(Subscription_.dSpaceObject), dSpaceObject)
));
return list(context, criteriaQuery, false, Subscription.class, -1, -1);
return list(context, criteriaQuery, false, Subscription.class, limit, offset);
}
@@ -89,6 +91,23 @@ public class SubscriptionDAOImpl extends AbstractHibernateDAO<Subscription> impl
query.executeUpdate();
}
@Override
public List<Subscription> findAllOrderedByEPersonAndResourceType(Context context, String resourceType,
Integer limit, Integer offset) throws SQLException {
String hqlQuery = "select s from Subscription s join %s dso ON dso.id = s.dSpaceObject ORDER BY eperson_id";
if (resourceType != null) {
hqlQuery = String.format(hqlQuery, resourceType);
}
Query query = createQuery(context, hqlQuery);
if (limit != -1) {
query.setMaxResults(limit);
}
if (offset != -1) {
query.setFirstResult(offset);
}
query.setHint("org.hibernate.cacheable", false);
return query.getResultList();
}
@Override
public List<Subscription> findAllOrderedByEPerson(Context context) throws SQLException {
CriteriaBuilder criteriaBuilder = getCriteriaBuilder(context);

View File

@@ -2,7 +2,6 @@
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.eperson.service;
@@ -33,10 +32,13 @@ public interface SubscribeService {
* new item appears in the collection.
*
* @param context DSpace context
* @param limit Number of subscriptions to return
* @param offset Offset number
* @return list of Subscription objects
* @throws SQLException An exception that provides information on a database access error or other errors.
*/
public List<Subscription> findAll(Context context) throws SQLException;
public List<Subscription> findAll(Context context, String resourceType,
Integer limit, Integer offset) throws Exception;
/**
* Subscribe an e-person to a collection. An e-mail will be sent every day a
@@ -72,10 +74,13 @@ public interface SubscribeService {
*
* @param context DSpace context
* @param eperson EPerson
* @param limit Number of subscriptions to return
* @param offset Offset number
* @return array of collections e-person is subscribed to
* @throws SQLException An exception that provides information on a database access error or other errors.
*/
public List<Subscription> getSubscriptionsByEPerson(Context context, EPerson eperson) throws SQLException;
public List<Subscription> getSubscriptionsByEPerson(Context context, EPerson eperson,
Integer limit, Integer offset) throws SQLException;
/**
* Find out which collections an e-person is subscribed to and related with dso
@@ -83,12 +88,16 @@ public interface SubscribeService {
* @param context DSpace context
* @param eperson EPerson
* @param dSpaceObject DSpaceObject
* @param limit Number of subscriptions to return
* @param offset Offset number
* @return array of collections e-person is subscribed to and related with dso
* @throws SQLException An exception that provides information on a database access error or other errors.
*/
public List<Subscription> getSubscriptionsByEPersonAndDso(Context context,
EPerson eperson,
DSpaceObject dSpaceObject) throws SQLException;
DSpaceObject dSpaceObject,
Integer limit,
Integer offset) throws SQLException;
/**
* Find out which collections the currently logged in e-person can subscribe to

View File

@@ -90,10 +90,13 @@ public class SubscriptionRestRepository extends DSpaceRestRepository
@PreAuthorize("hasAuthority('ADMIN')")
public Page<SubscriptionRest> findAll(Context context, Pageable pageable) {
try {
List<Subscription> subscriptionList = subscribeService.findAll(context);
HttpServletRequest req = getRequestService().getCurrentRequest().getHttpServletRequest();
String resourceType = req.getParameter("resourceType");
List<Subscription> subscriptionList = subscribeService.findAll(context, resourceType,
pageable.getPageSize(), Math.toIntExact(pageable.getOffset()));
return converter.toRestPage(subscriptionList, pageable, utils.obtainProjection());
} catch (SQLException sqlException) {
throw new RuntimeException(sqlException.getMessage(), sqlException);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
@@ -104,7 +107,8 @@ public class SubscriptionRestRepository extends DSpaceRestRepository
EPerson ePerson = personService.findByIdOrLegacyId(context, id);
if (context.getCurrentUser().equals(ePerson)
|| authorizeService.isAdmin(context, context.getCurrentUser())) {
List<Subscription> subscriptionList = subscribeService.getSubscriptionsByEPerson(context, ePerson);
List<Subscription> subscriptionList = subscribeService.getSubscriptionsByEPerson(context,
ePerson, pageable.getPageSize(), Math.toIntExact(pageable.getOffset()));
return converter.toRestPage(subscriptionList, pageable, utils.obtainProjection());
} else {
throw new AuthorizeException("Only admin or e-person themselves can search for it's subscription");
@@ -130,7 +134,8 @@ public class SubscriptionRestRepository extends DSpaceRestRepository
if (context.getCurrentUser().equals(ePerson)
|| authorizeService.isAdmin(context, context.getCurrentUser())) {
List<Subscription> subscriptionList =
subscribeService.getSubscriptionsByEPersonAndDso(context, ePerson, dSpaceObject);
subscribeService.getSubscriptionsByEPersonAndDso(context, ePerson, dSpaceObject,
pageable.getPageSize(), Math.toIntExact(pageable.getOffset()));
return converter.toRestPage(subscriptionList, pageable, subscriptionList.size(),
utils.obtainProjection());
} else {