From 0ee618385ba4514042b984e8bde7410ecf8ab1ff Mon Sep 17 00:00:00 2001 From: Raf Ponsaerts Date: Mon, 5 Nov 2018 09:25:56 +0100 Subject: [PATCH 01/14] [Task 56182] Added the endpoint to search across an items relationships by a label of the relationshiptype --- .../content/RelationshipServiceImpl.java | 14 ++++ .../content/RelationshipTypeServiceImpl.java | 4 + .../content/dao/RelationshipTypeDAO.java | 2 + .../dao/impl/RelationshipTypeDAOImpl.java | 15 ++++ .../content/service/RelationshipService.java | 5 ++ .../service/RelationshipTypeService.java | 11 +++ .../app/rest/RelationshipRestController.java | 80 +++++++++++++++++++ .../rest/model/RelationshipRestWrapper.java | 32 ++++++++ .../hateoas/RelationshipResourceWrapper.java | 25 ++++++ 9 files changed, 188 insertions(+) create mode 100644 dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java create mode 100644 dspace-spring-rest/src/main/java/org/dspace/app/rest/model/RelationshipRestWrapper.java create mode 100644 dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipResourceWrapper.java 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 6b563564d3..03851f2b22 100644 --- a/dspace-api/src/main/java/org/dspace/content/RelationshipServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/RelationshipServiceImpl.java @@ -286,4 +286,18 @@ public class RelationshipServiceImpl implements RelationshipService { } return listToReturn; } + + public List findByItemAndRelationshipType(Context context, Item item, + RelationshipType relationshipType) + + throws SQLException { + List list = this.findByItem(context, item); + List listToReturn = new LinkedList<>(); + for (Relationship relationship : list) { + if (relationship.getRelationshipType().equals(relationshipType)) { + listToReturn.add(relationship); + } + } + return listToReturn; + } } diff --git a/dspace-api/src/main/java/org/dspace/content/RelationshipTypeServiceImpl.java b/dspace-api/src/main/java/org/dspace/content/RelationshipTypeServiceImpl.java index a03eb02677..e149e81b6e 100644 --- a/dspace-api/src/main/java/org/dspace/content/RelationshipTypeServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/RelationshipTypeServiceImpl.java @@ -53,6 +53,10 @@ public class RelationshipTypeServiceImpl implements RelationshipTypeService { return relationshipTypeDAO.findAll(context, RelationshipType.class); } + public List findByLeftOrRightLabel(Context context, String label) throws SQLException { + return relationshipTypeDAO.findByLeftOrRightLabel(context, label); + } + public RelationshipType find(Context context,int id) throws SQLException { return relationshipTypeDAO.findByID(context, RelationshipType.class, id); } diff --git a/dspace-api/src/main/java/org/dspace/content/dao/RelationshipTypeDAO.java b/dspace-api/src/main/java/org/dspace/content/dao/RelationshipTypeDAO.java index 50dc422032..021a92217c 100644 --- a/dspace-api/src/main/java/org/dspace/content/dao/RelationshipTypeDAO.java +++ b/dspace-api/src/main/java/org/dspace/content/dao/RelationshipTypeDAO.java @@ -8,6 +8,7 @@ package org.dspace.content.dao; import java.sql.SQLException; +import java.util.List; import org.dspace.content.EntityType; import org.dspace.content.RelationshipType; @@ -26,4 +27,5 @@ public interface RelationshipTypeDAO extends GenericDAO { EntityType leftType,EntityType rightType,String leftLabel,String rightLabel) throws SQLException; + List findByLeftOrRightLabel(Context context, String label) throws SQLException; } diff --git a/dspace-api/src/main/java/org/dspace/content/dao/impl/RelationshipTypeDAOImpl.java b/dspace-api/src/main/java/org/dspace/content/dao/impl/RelationshipTypeDAOImpl.java index acf11bf743..8fe5d18c56 100644 --- a/dspace-api/src/main/java/org/dspace/content/dao/impl/RelationshipTypeDAOImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/dao/impl/RelationshipTypeDAOImpl.java @@ -8,6 +8,7 @@ package org.dspace.content.dao.impl; import java.sql.SQLException; +import java.util.List; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; @@ -37,4 +38,18 @@ public class RelationshipTypeDAOImpl extends AbstractHibernateDAO findByLeftOrRightLabel(Context context, String label) throws SQLException { + CriteriaBuilder criteriaBuilder = getCriteriaBuilder(context); + CriteriaQuery criteriaQuery = getCriteriaQuery(criteriaBuilder, RelationshipType.class); + Root relationshipTypeRoot = criteriaQuery.from(RelationshipType.class); + criteriaQuery.select(relationshipTypeRoot); + criteriaQuery.where( + criteriaBuilder.or( + criteriaBuilder.equal(relationshipTypeRoot.get(RelationshipType_.leftLabel), label), + criteriaBuilder.equal(relationshipTypeRoot.get(RelationshipType_.rightLabel), label) + ) + ); + return list(context, criteriaQuery, true, RelationshipType.class, -1, -1); + } + } 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 643a914f09..a8052e1e6f 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 @@ -13,6 +13,7 @@ import java.util.List; import org.dspace.authorize.AuthorizeException; import org.dspace.content.Item; import org.dspace.content.Relationship; +import org.dspace.content.RelationshipType; import org.dspace.core.Context; import org.dspace.service.DSpaceCRUDService; @@ -71,4 +72,8 @@ public interface RelationshipService extends DSpaceCRUDService { * @throws SQLException If something goes wrong */ int findRightPlaceByRightItem(Context context, Item item) throws SQLException; + + public List findByItemAndRelationshipType(Context context, Item item, + RelationshipType relationshipType) + throws SQLException; } \ No newline at end of file diff --git a/dspace-api/src/main/java/org/dspace/content/service/RelationshipTypeService.java b/dspace-api/src/main/java/org/dspace/content/service/RelationshipTypeService.java index a336878874..88ba200176 100644 --- a/dspace-api/src/main/java/org/dspace/content/service/RelationshipTypeService.java +++ b/dspace-api/src/main/java/org/dspace/content/service/RelationshipTypeService.java @@ -52,4 +52,15 @@ public interface RelationshipTypeService extends DSpaceCRUDService findAll(Context context) throws SQLException; + + /** + * Retrieves all RelationshipType objects that have a left or right label that is + * equal to the given String + * @param context The relevant DSpace context + * @param label The label that has to match + * @return The list of all RelationshipType objects that have a left or right label + * that is equal to the given label param + * @throws SQLException If something goes wrong + */ + List findByLeftOrRightLabel(Context context, String label) throws SQLException; } diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java new file mode 100644 index 0000000000..eb9ad84b7a --- /dev/null +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java @@ -0,0 +1,80 @@ +package org.dspace.app.rest; + +import java.sql.SQLException; +import java.util.LinkedList; +import java.util.List; +import java.util.UUID; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.dspace.app.rest.converter.RelationshipConverter; +import org.dspace.app.rest.model.RelationshipRest; +import org.dspace.app.rest.model.RelationshipRestWrapper; +import org.dspace.app.rest.model.hateoas.RelationshipResourceWrapper; +import org.dspace.app.rest.utils.ContextUtil; +import org.dspace.app.rest.utils.Utils; +import org.dspace.content.Item; +import org.dspace.content.Relationship; +import org.dspace.content.RelationshipType; +import org.dspace.content.service.ItemService; +import org.dspace.content.service.RelationshipService; +import org.dspace.content.service.RelationshipTypeService; +import org.dspace.core.Context; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api/core/items/" + + "{uuid:[0-9a-fxA-FX]{8}-[0-9a-fxA-FX]{4}-[0-9a-fxA-FX]{4}-[0-9a-fxA-FX]{4}-[0-9a-fxA-FX]{12}}/relationships") +public class RelationshipRestController { + + @Autowired + private RelationshipTypeService relationshipTypeService; + + @Autowired + private RelationshipService relationshipService; + + @Autowired + private ItemService itemService; + + @Autowired + private RelationshipConverter relationshipConverter; + + @Autowired + Utils utils; + + @RequestMapping(method = RequestMethod.GET, value = "/{label}") + public RelationshipResourceWrapper retrieveByLabel(@PathVariable UUID uuid, HttpServletResponse response, + HttpServletRequest request, @PathVariable String label) + throws SQLException { + + Context context = ContextUtil.obtainContext(request); + + List relationshipTypeList = relationshipTypeService.findByLeftOrRightLabel(context, label); + Item item = itemService.find(context, uuid); + + List relationships = new LinkedList<>(); + + for (RelationshipType relationshipType : relationshipTypeList) { + relationships.addAll(relationshipService.findByItemAndRelationshipType(context, item, relationshipType)); + } + + List relationshipRests = new LinkedList<>(); + for (Relationship relationship : relationships) { + relationshipRests.add(relationshipConverter.fromModel(relationship)); + } + + RelationshipRestWrapper relationshipRestWrapper = new RelationshipRestWrapper(); + relationshipRestWrapper.setRelationshipRestList(relationshipRests); + + RelationshipResourceWrapper relationshipResourceWrapper = new RelationshipResourceWrapper( + relationshipRestWrapper, utils); + + return relationshipResourceWrapper; + } + +} diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/RelationshipRestWrapper.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/RelationshipRestWrapper.java new file mode 100644 index 0000000000..bc72528d8a --- /dev/null +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/RelationshipRestWrapper.java @@ -0,0 +1,32 @@ +package org.dspace.app.rest.model; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import org.dspace.app.rest.RelationshipRestController; + +public class RelationshipRestWrapper implements RestAddressableModel { + + @JsonIgnore + private List relationshipRestList; + + public List getRelationshipRestList() { + return relationshipRestList; + } + + public void setRelationshipRestList(List relationshipRestList) { + this.relationshipRestList = relationshipRestList; + } + + public String getCategory() { + return "core"; + } + + public Class getController() { + return RelationshipRestController.class; + } + + public String getType() { + return "relationship"; + } +} diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipResourceWrapper.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipResourceWrapper.java new file mode 100644 index 0000000000..9a2aef494e --- /dev/null +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipResourceWrapper.java @@ -0,0 +1,25 @@ +package org.dspace.app.rest.model.hateoas; + +import java.util.LinkedList; +import java.util.List; + +import org.dspace.app.rest.model.RelationshipRest; +import org.dspace.app.rest.model.RelationshipRestWrapper; +import org.dspace.app.rest.utils.Utils; + +public class RelationshipResourceWrapper extends HALResource { + + public RelationshipResourceWrapper(RelationshipRestWrapper content, Utils utils) { + super(content); + addEmbeds(content, utils); + } + + private void addEmbeds(RelationshipRestWrapper content, Utils utils) { + List list = new LinkedList<>(); + for (RelationshipRest relationshipRest : content.getRelationshipRestList()) { + list.add(new RelationshipResource(relationshipRest, utils)); + } + + embedResource("relationships", list); + } +} From d90f4e63f5ad946cc5ee9628da1582e06f875c77 Mon Sep 17 00:00:00 2001 From: Raf Ponsaerts Date: Mon, 5 Nov 2018 14:02:44 +0100 Subject: [PATCH 02/14] [Task 56183] added the additional relationshiptype endpoint on the entitytype --- .../content/RelationshipTypeServiceImpl.java | 4 ++ .../content/dao/RelationshipTypeDAO.java | 2 + .../dao/impl/RelationshipTypeDAOImpl.java | 16 +++++ .../service/RelationshipTypeService.java | 2 + .../rest/RelationshipTypeRestController.java | 61 +++++++++++++++++++ .../model/RelationshipTypeRestWrapper.java | 33 ++++++++++ .../RelationshipTypeResourceWrapper.java | 25 ++++++++ 7 files changed, 143 insertions(+) create mode 100644 dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipTypeRestController.java create mode 100644 dspace-spring-rest/src/main/java/org/dspace/app/rest/model/RelationshipTypeRestWrapper.java create mode 100644 dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipTypeResourceWrapper.java diff --git a/dspace-api/src/main/java/org/dspace/content/RelationshipTypeServiceImpl.java b/dspace-api/src/main/java/org/dspace/content/RelationshipTypeServiceImpl.java index a03eb02677..3ef3b5bdd3 100644 --- a/dspace-api/src/main/java/org/dspace/content/RelationshipTypeServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/RelationshipTypeServiceImpl.java @@ -53,6 +53,10 @@ public class RelationshipTypeServiceImpl implements RelationshipTypeService { return relationshipTypeDAO.findAll(context, RelationshipType.class); } + public List findByEntityType(Context context, EntityType entityType) throws SQLException { + return relationshipTypeDAO.findByEntityType(context, entityType); + } + public RelationshipType find(Context context,int id) throws SQLException { return relationshipTypeDAO.findByID(context, RelationshipType.class, id); } diff --git a/dspace-api/src/main/java/org/dspace/content/dao/RelationshipTypeDAO.java b/dspace-api/src/main/java/org/dspace/content/dao/RelationshipTypeDAO.java index 50dc422032..96fe633543 100644 --- a/dspace-api/src/main/java/org/dspace/content/dao/RelationshipTypeDAO.java +++ b/dspace-api/src/main/java/org/dspace/content/dao/RelationshipTypeDAO.java @@ -8,6 +8,7 @@ package org.dspace.content.dao; import java.sql.SQLException; +import java.util.List; import org.dspace.content.EntityType; import org.dspace.content.RelationshipType; @@ -26,4 +27,5 @@ public interface RelationshipTypeDAO extends GenericDAO { EntityType leftType,EntityType rightType,String leftLabel,String rightLabel) throws SQLException; + List findByEntityType(Context context, EntityType entityType) throws SQLException; } diff --git a/dspace-api/src/main/java/org/dspace/content/dao/impl/RelationshipTypeDAOImpl.java b/dspace-api/src/main/java/org/dspace/content/dao/impl/RelationshipTypeDAOImpl.java index acf11bf743..9300adb602 100644 --- a/dspace-api/src/main/java/org/dspace/content/dao/impl/RelationshipTypeDAOImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/dao/impl/RelationshipTypeDAOImpl.java @@ -8,6 +8,7 @@ package org.dspace.content.dao.impl; import java.sql.SQLException; +import java.util.List; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; @@ -37,4 +38,19 @@ public class RelationshipTypeDAOImpl extends AbstractHibernateDAO findByEntityType(Context context, EntityType entityType) throws SQLException { + CriteriaBuilder criteriaBuilder = getCriteriaBuilder(context); + CriteriaQuery criteriaQuery = getCriteriaQuery(criteriaBuilder, RelationshipType.class); + Root relationshipTypeRoot = criteriaQuery.from(RelationshipType.class); + criteriaQuery.select(relationshipTypeRoot); + criteriaQuery.where( + criteriaBuilder.or(criteriaBuilder. + equal(relationshipTypeRoot.get(RelationshipType_.leftType), entityType), + criteriaBuilder + .equal(relationshipTypeRoot.get(RelationshipType_.rightType), entityType) + ) + ); + return list(context, criteriaQuery, false, RelationshipType.class, -1, -1); + } + } diff --git a/dspace-api/src/main/java/org/dspace/content/service/RelationshipTypeService.java b/dspace-api/src/main/java/org/dspace/content/service/RelationshipTypeService.java index a336878874..248fea9b13 100644 --- a/dspace-api/src/main/java/org/dspace/content/service/RelationshipTypeService.java +++ b/dspace-api/src/main/java/org/dspace/content/service/RelationshipTypeService.java @@ -52,4 +52,6 @@ public interface RelationshipTypeService extends DSpaceCRUDService findAll(Context context) throws SQLException; + + List findByEntityType(Context context, EntityType entityType) throws SQLException; } diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipTypeRestController.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipTypeRestController.java new file mode 100644 index 0000000000..6844d8c7d1 --- /dev/null +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipTypeRestController.java @@ -0,0 +1,61 @@ +package org.dspace.app.rest; + +import java.sql.SQLException; +import java.util.LinkedList; +import java.util.List; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.dspace.app.rest.converter.RelationshipTypeConverter; +import org.dspace.app.rest.model.RelationshipTypeRest; +import org.dspace.app.rest.model.RelationshipTypeRestWrapper; +import org.dspace.app.rest.model.hateoas.RelationshipTypeResourceWrapper; +import org.dspace.app.rest.utils.ContextUtil; +import org.dspace.app.rest.utils.Utils; +import org.dspace.content.EntityType; +import org.dspace.content.RelationshipType; +import org.dspace.content.service.EntityTypeService; +import org.dspace.content.service.RelationshipTypeService; +import org.dspace.core.Context; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api/core/entitytypes/{id}/relationshiptypes") +public class RelationshipTypeRestController { + + + @Autowired + private RelationshipTypeService relationshipTypeService; + + @Autowired + private EntityTypeService entityTypeService; + + @Autowired + private RelationshipTypeConverter relationshipTypeConverter; + + @Autowired + private Utils utils; + + @RequestMapping(method = RequestMethod.GET) + public RelationshipTypeResourceWrapper retrieve(@PathVariable Integer id, HttpServletResponse response, + HttpServletRequest request) throws SQLException { + Context context = ContextUtil.obtainContext(request); + EntityType entityType = entityTypeService.find(context, id); + List list = relationshipTypeService.findByEntityType(context, entityType); + + List relationshipTypeRests = new LinkedList<>(); + + for (RelationshipType relationshipType : list) { + relationshipTypeRests.add(relationshipTypeConverter.fromModel(relationshipType)); + } + + + RelationshipTypeRestWrapper relationshipTypeRestWrapper = new RelationshipTypeRestWrapper(); + relationshipTypeRestWrapper.setRelationshipTypeRestList(relationshipTypeRests); + return new RelationshipTypeResourceWrapper(relationshipTypeRestWrapper, utils); + } +} diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/RelationshipTypeRestWrapper.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/RelationshipTypeRestWrapper.java new file mode 100644 index 0000000000..e9bc758e14 --- /dev/null +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/RelationshipTypeRestWrapper.java @@ -0,0 +1,33 @@ +package org.dspace.app.rest.model; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import org.dspace.app.rest.RelationshipTypeRestController; + +public class RelationshipTypeRestWrapper implements RestAddressableModel { + + @JsonIgnore + private List relationshipTypeRestList; + + public List getRelationshipTypeRestList() { + return relationshipTypeRestList; + } + + public void setRelationshipTypeRestList( + List relationshipTypeRestList) { + this.relationshipTypeRestList = relationshipTypeRestList; + } + + public String getCategory() { + return "core"; + } + + public Class getController() { + return RelationshipTypeRestController.class; + } + + public String getType() { + return "relationshiptype"; + } +} diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipTypeResourceWrapper.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipTypeResourceWrapper.java new file mode 100644 index 0000000000..de65bcff31 --- /dev/null +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipTypeResourceWrapper.java @@ -0,0 +1,25 @@ +package org.dspace.app.rest.model.hateoas; + +import java.util.LinkedList; +import java.util.List; + +import org.dspace.app.rest.model.RelationshipTypeRest; +import org.dspace.app.rest.model.RelationshipTypeRestWrapper; +import org.dspace.app.rest.utils.Utils; + +public class RelationshipTypeResourceWrapper extends HALResource { + + public RelationshipTypeResourceWrapper(RelationshipTypeRestWrapper content, Utils utils) { + super(content); + addEmbeds(content, utils); + } + + private void addEmbeds(RelationshipTypeRestWrapper content, Utils utils) { + List list = new LinkedList<>(); + for (RelationshipTypeRest relationshipTypeRest : content.getRelationshipTypeRestList()) { + list.add(new RelationshipTypeResource(relationshipTypeRest, utils)); + } + + embedResource("relationshiptypes", list); + } +} From 925b83bc02b6c44970753f866c7847013513533a Mon Sep 17 00:00:00 2001 From: Raf Ponsaerts Date: Thu, 22 Nov 2018 16:05:27 +0100 Subject: [PATCH 03/14] [Task 57441] Implemented the requested endpoints, except for one todo --- .../app/rest/RelationshipRestController.java | 37 ++++++++++++++----- .../rest/RelationshipTypeRestController.java | 12 +++++- .../relation/EntityTypeHalLinkFactory.java | 25 +++++++++++++ ...tionshipResourceWrapperHalLinkFactory.java | 28 ++++++++++++++ ...shipTypeResourceWrapperHalLinkFactory.java | 27 ++++++++++++++ .../rest/model/RelationshipRestWrapper.java | 21 +++++++++++ .../model/RelationshipTypeRestWrapper.java | 21 +++++++++++ 7 files changed, 161 insertions(+), 10 deletions(-) create mode 100644 dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/EntityTypeHalLinkFactory.java create mode 100644 dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipResourceWrapperHalLinkFactory.java create mode 100644 dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipTypeResourceWrapperHalLinkFactory.java diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java index b2509f4e88..fadf7a6fc9 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java @@ -14,7 +14,9 @@ import java.util.UUID; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.apache.commons.lang.StringUtils; import org.dspace.app.rest.converter.RelationshipConverter; +import org.dspace.app.rest.link.HalLinkService; import org.dspace.app.rest.model.RelationshipRest; import org.dspace.app.rest.model.RelationshipRestWrapper; import org.dspace.app.rest.model.hateoas.RelationshipResourceWrapper; @@ -27,16 +29,17 @@ import org.dspace.content.service.ItemService; import org.dspace.content.service.RelationshipService; import org.dspace.content.service.RelationshipTypeService; import org.dspace.core.Context; +import org.dspace.util.UUIDUtils; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.data.rest.webmvc.ResourceNotFoundException; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController -@RequestMapping("/api/core/items/" + - "{uuid:[0-9a-fxA-FX]{8}-[0-9a-fxA-FX]{4}-[0-9a-fxA-FX]{4}-[0-9a-fxA-FX]{4}-[0-9a-fxA-FX]{12}}/relationships") +@RequestMapping("/api/core/relationships") public class RelationshipRestController { @Autowired @@ -54,20 +57,33 @@ public class RelationshipRestController { @Autowired Utils utils; + @Autowired + private HalLinkService halLinkService; + @RequestMapping(method = RequestMethod.GET, value = "/{label}") - public RelationshipResourceWrapper retrieveByLabel(@PathVariable UUID uuid, HttpServletResponse response, - HttpServletRequest request, @PathVariable String label) + public RelationshipResourceWrapper retrieveByLabel(HttpServletResponse response, + HttpServletRequest request, @PathVariable String label, + @RequestParam(name = "dso", required = false) String dsoId) throws SQLException { Context context = ContextUtil.obtainContext(request); List relationshipTypeList = relationshipTypeService.findByLeftOrRightLabel(context, label); - Item item = itemService.find(context, uuid); - List relationships = new LinkedList<>(); + if (StringUtils.isNotBlank(dsoId)) { - for (RelationshipType relationshipType : relationshipTypeList) { - relationships.addAll(relationshipService.findByItemAndRelationshipType(context, item, relationshipType)); + UUID uuid = UUIDUtils.fromString(dsoId); + Item item = itemService.find(context, uuid); + + if (item == null) { + throw new ResourceNotFoundException("The request DSO with id: " + dsoId + " was not found"); + } + for (RelationshipType relationshipType : relationshipTypeList) { + relationships.addAll(relationshipService.findByItemAndRelationshipType(context, item, relationshipType)); + } + } else { + //TODO Find by label + relationships = relationshipService.findAll(context); } List relationshipRests = new LinkedList<>(); @@ -76,11 +92,14 @@ public class RelationshipRestController { } RelationshipRestWrapper relationshipRestWrapper = new RelationshipRestWrapper(); + relationshipRestWrapper.setLabel(label); + relationshipRestWrapper.setDsoId(dsoId); relationshipRestWrapper.setRelationshipRestList(relationshipRests); RelationshipResourceWrapper relationshipResourceWrapper = new RelationshipResourceWrapper( relationshipRestWrapper, utils); + halLinkService.addLinks(relationshipResourceWrapper); return relationshipResourceWrapper; } diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipTypeRestController.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipTypeRestController.java index 1038864883..3e80426805 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipTypeRestController.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipTypeRestController.java @@ -14,6 +14,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.dspace.app.rest.converter.RelationshipTypeConverter; +import org.dspace.app.rest.link.HalLinkService; import org.dspace.app.rest.model.RelationshipTypeRest; import org.dspace.app.rest.model.RelationshipTypeRestWrapper; import org.dspace.app.rest.model.hateoas.RelationshipTypeResourceWrapper; @@ -47,6 +48,9 @@ public class RelationshipTypeRestController { @Autowired private Utils utils; + @Autowired + private HalLinkService halLinkService; + @RequestMapping(method = RequestMethod.GET) public RelationshipTypeResourceWrapper retrieve(@PathVariable Integer id, HttpServletResponse response, HttpServletRequest request) throws SQLException { @@ -62,7 +66,13 @@ public class RelationshipTypeRestController { RelationshipTypeRestWrapper relationshipTypeRestWrapper = new RelationshipTypeRestWrapper(); + relationshipTypeRestWrapper.setEntityTypeId(id); + relationshipTypeRestWrapper.setEntityTypeLabel(entityType.getLabel()); relationshipTypeRestWrapper.setRelationshipTypeRestList(relationshipTypeRests); - return new RelationshipTypeResourceWrapper(relationshipTypeRestWrapper, utils); + + RelationshipTypeResourceWrapper relationshipTypeResourceWrapper = new RelationshipTypeResourceWrapper( + relationshipTypeRestWrapper, utils); + halLinkService.addLinks(relationshipTypeResourceWrapper); + return relationshipTypeResourceWrapper; } } diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/EntityTypeHalLinkFactory.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/EntityTypeHalLinkFactory.java new file mode 100644 index 0000000000..a721dfbe42 --- /dev/null +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/EntityTypeHalLinkFactory.java @@ -0,0 +1,25 @@ +package org.dspace.app.rest.link.relation; + +import java.util.LinkedList; + +import org.dspace.app.rest.RelationshipTypeRestController; +import org.dspace.app.rest.link.HalLinkFactory; +import org.dspace.app.rest.model.hateoas.EntityTypeResource; +import org.springframework.data.domain.Pageable; +import org.springframework.hateoas.Link; +import org.springframework.stereotype.Component; + +@Component +public class EntityTypeHalLinkFactory extends HalLinkFactory { + protected void addLinks(EntityTypeResource halResource, Pageable pageable, LinkedList list) throws Exception { + list.add(buildLink("relationshiptypes", getMethodOn().retrieve(halResource.getContent().getId(), null, null))); + } + + protected Class getControllerClass() { + return RelationshipTypeRestController.class; + } + + protected Class getResourceClass() { + return EntityTypeResource.class; + } +} diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipResourceWrapperHalLinkFactory.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipResourceWrapperHalLinkFactory.java new file mode 100644 index 0000000000..57f6edb245 --- /dev/null +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipResourceWrapperHalLinkFactory.java @@ -0,0 +1,28 @@ +package org.dspace.app.rest.link.relation; + +import java.util.LinkedList; + +import org.dspace.app.rest.RelationshipRestController; +import org.dspace.app.rest.link.HalLinkFactory; +import org.dspace.app.rest.model.hateoas.RelationshipResourceWrapper; +import org.springframework.data.domain.Pageable; +import org.springframework.hateoas.Link; +import org.springframework.stereotype.Component; + +@Component +public class RelationshipResourceWrapperHalLinkFactory + extends HalLinkFactory { + protected void addLinks(RelationshipResourceWrapper halResource, Pageable pageable, LinkedList list) + throws Exception { + list.add(buildLink(Link.REL_SELF, getMethodOn() + .retrieveByLabel(null, null, halResource.getContent().getLabel(), halResource.getContent().getDsoId()))); + } + + protected Class getControllerClass() { + return RelationshipRestController.class; + } + + protected Class getResourceClass() { + return RelationshipResourceWrapper.class; + } +} diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipTypeResourceWrapperHalLinkFactory.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipTypeResourceWrapperHalLinkFactory.java new file mode 100644 index 0000000000..b4a59f7953 --- /dev/null +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipTypeResourceWrapperHalLinkFactory.java @@ -0,0 +1,27 @@ +package org.dspace.app.rest.link.relation; + +import java.util.LinkedList; + +import org.dspace.app.rest.RelationshipTypeRestController; +import org.dspace.app.rest.link.HalLinkFactory; +import org.dspace.app.rest.model.hateoas.RelationshipTypeResourceWrapper; +import org.springframework.data.domain.Pageable; +import org.springframework.hateoas.Link; +import org.springframework.stereotype.Component; + +@Component +public class RelationshipTypeResourceWrapperHalLinkFactory + extends HalLinkFactory { + protected void addLinks(RelationshipTypeResourceWrapper halResource, Pageable pageable, LinkedList list) + throws Exception { + list.add(buildLink(Link.REL_SELF, getMethodOn().retrieve(halResource.getContent().getEntityTypeId(), null, null))); + } + + protected Class getControllerClass() { + return RelationshipTypeRestController.class; + } + + protected Class getResourceClass() { + return RelationshipTypeResourceWrapper.class; + } +} diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/RelationshipRestWrapper.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/RelationshipRestWrapper.java index cf5804045c..43f0439a23 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/RelationshipRestWrapper.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/RelationshipRestWrapper.java @@ -17,6 +17,11 @@ public class RelationshipRestWrapper implements RestAddressableModel { @JsonIgnore private List relationshipRestList; + private String label; + private String dsoId; + + + public List getRelationshipRestList() { return relationshipRestList; } @@ -36,4 +41,20 @@ public class RelationshipRestWrapper implements RestAddressableModel { public String getType() { return "relationship"; } + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + + public String getDsoId() { + return dsoId; + } + + public void setDsoId(String dsoId) { + this.dsoId = dsoId; + } } diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/RelationshipTypeRestWrapper.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/RelationshipTypeRestWrapper.java index 577fc276be..3a6e22ebd2 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/RelationshipTypeRestWrapper.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/RelationshipTypeRestWrapper.java @@ -17,6 +17,11 @@ public class RelationshipTypeRestWrapper implements RestAddressableModel { @JsonIgnore private List relationshipTypeRestList; + private String entityTypeLabel; + private Integer entityTypeId; + + + public List getRelationshipTypeRestList() { return relationshipTypeRestList; } @@ -37,4 +42,20 @@ public class RelationshipTypeRestWrapper implements RestAddressableModel { public String getType() { return "relationshiptype"; } + + public String getEntityTypeLabel() { + return entityTypeLabel; + } + + public void setEntityTypeLabel(String label) { + this.entityTypeLabel = label; + } + + public Integer getEntityTypeId() { + return entityTypeId; + } + + public void setEntityTypeId(Integer entityTypeId) { + this.entityTypeId = entityTypeId; + } } From c6dfae90de2533f07076a92cfcea32bd5cfb6655 Mon Sep 17 00:00:00 2001 From: Raf Ponsaerts Date: Mon, 26 Nov 2018 09:02:30 +0100 Subject: [PATCH 04/14] intermediary commit --- .../dspace/content/RelationshipServiceImpl.java | 5 +++++ .../org/dspace/content/dao/RelationshipDAO.java | 3 +++ .../content/dao/impl/RelationshipDAOImpl.java | 14 ++++++++++++++ .../content/service/RelationshipService.java | 2 ++ .../app/rest/RelationshipRestController.java | 6 ++++-- 5 files changed, 28 insertions(+), 2 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 eecc80bc5d..3f5886e4bc 100644 --- a/dspace-api/src/main/java/org/dspace/content/RelationshipServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/RelationshipServiceImpl.java @@ -300,4 +300,9 @@ public class RelationshipServiceImpl implements RelationshipService { } return listToReturn; } + + public List findByRelationshipType(Context context, RelationshipType relationshipType) + throws SQLException { + return relationshipDAO.findByRelationshipType(context, relationshipType); + } } 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 9f532f4c01..bcf2ba8183 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 @@ -12,6 +12,7 @@ import java.util.List; import org.dspace.content.Item; import org.dspace.content.Relationship; +import org.dspace.content.RelationshipType; import org.dspace.core.Context; import org.dspace.core.GenericDAO; @@ -56,4 +57,6 @@ public interface RelationshipDAO extends GenericDAO { * @throws SQLException If something goes wrong */ int findRightPlaceByRightItem(Context context,Item item) throws SQLException; + + List findByRelationshipType(Context context, RelationshipType relationshipType) 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 5b3e33f5b5..b7fbb3710e 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 @@ -15,6 +15,7 @@ import javax.persistence.criteria.Root; import org.dspace.content.Item; import org.dspace.content.Relationship; +import org.dspace.content.RelationshipType; import org.dspace.content.Relationship_; import org.dspace.content.dao.RelationshipDAO; import org.dspace.core.AbstractHibernateDAO; @@ -62,4 +63,17 @@ public class RelationshipDAOImpl extends AbstractHibernateDAO impl return 1; } } + + public List findByRelationshipType(Context context, RelationshipType relationshipType) + throws SQLException { + CriteriaBuilder criteriaBuilder = getCriteriaBuilder(context); + CriteriaQuery criteriaQuery = getCriteriaQuery(criteriaBuilder, Relationship.class); + Root relationshipRoot = criteriaQuery.from(Relationship.class); + criteriaQuery.select(relationshipRoot); + criteriaQuery + .where(criteriaBuilder.equal(relationshipRoot.get(Relationship_.relationshipType), relationshipType)); + return list(context, criteriaQuery, true, Relationship.class, -1, -1); + } + + } 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 a8052e1e6f..feadaaaba3 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 @@ -76,4 +76,6 @@ public interface RelationshipService extends DSpaceCRUDService { public List findByItemAndRelationshipType(Context context, Item item, RelationshipType relationshipType) throws SQLException; + + List findByRelationshipType(Context context, RelationshipType relationshipType) throws SQLException; } \ No newline at end of file diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java index fadf7a6fc9..94b801ddc6 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java @@ -82,8 +82,10 @@ public class RelationshipRestController { relationships.addAll(relationshipService.findByItemAndRelationshipType(context, item, relationshipType)); } } else { - //TODO Find by label - relationships = relationshipService.findAll(context); + for (RelationshipType relationshipType : relationshipTypeList) { + relationships.addAll(relationshipService.findByRelationshipType(context, relationshipType)); + } +// relationships = relationshipService.findAll(context); } List relationshipRests = new LinkedList<>(); From 41ff37de1b0230f8e48cdc059a2331fb0f5f270a Mon Sep 17 00:00:00 2001 From: Raf Ponsaerts Date: Tue, 27 Nov 2018 08:33:10 +0100 Subject: [PATCH 05/14] [Task 57441] fixed pagination on the /label endpoint --- .../app/rest/RelationshipRestController.java | 10 +++-- ...tionshipResourceWrapperHalLinkFactory.java | 22 ++++++++++- .../hateoas/RelationshipResourceWrapper.java | 39 +++++++++++++++++-- 3 files changed, 61 insertions(+), 10 deletions(-) diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java index 94b801ddc6..74620a2358 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java @@ -31,6 +31,7 @@ import org.dspace.content.service.RelationshipTypeService; import org.dspace.core.Context; import org.dspace.util.UUIDUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Pageable; import org.springframework.data.rest.webmvc.ResourceNotFoundException; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @@ -63,8 +64,9 @@ public class RelationshipRestController { @RequestMapping(method = RequestMethod.GET, value = "/{label}") public RelationshipResourceWrapper retrieveByLabel(HttpServletResponse response, HttpServletRequest request, @PathVariable String label, - @RequestParam(name = "dso", required = false) String dsoId) - throws SQLException { + @RequestParam(name = "dso", required = false) String dsoId, + Pageable pageable) + throws Exception { Context context = ContextUtil.obtainContext(request); @@ -99,9 +101,9 @@ public class RelationshipRestController { relationshipRestWrapper.setRelationshipRestList(relationshipRests); RelationshipResourceWrapper relationshipResourceWrapper = new RelationshipResourceWrapper( - relationshipRestWrapper, utils); + relationshipRestWrapper, utils, relationshipRests.size(), pageable); - halLinkService.addLinks(relationshipResourceWrapper); + halLinkService.addLinks(relationshipResourceWrapper, pageable); return relationshipResourceWrapper; } diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipResourceWrapperHalLinkFactory.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipResourceWrapperHalLinkFactory.java index 57f6edb245..dd3dd90f07 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipResourceWrapperHalLinkFactory.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipResourceWrapperHalLinkFactory.java @@ -4,18 +4,36 @@ import java.util.LinkedList; import org.dspace.app.rest.RelationshipRestController; import org.dspace.app.rest.link.HalLinkFactory; +import org.dspace.app.rest.model.RelationshipRestWrapper; +import org.dspace.app.rest.model.hateoas.EmbeddedPageHeader; +import org.dspace.app.rest.model.hateoas.RelationshipResource; import org.dspace.app.rest.model.hateoas.RelationshipResourceWrapper; +import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; import org.springframework.hateoas.Link; import org.springframework.stereotype.Component; +import org.springframework.web.util.UriComponentsBuilder; @Component public class RelationshipResourceWrapperHalLinkFactory extends HalLinkFactory { protected void addLinks(RelationshipResourceWrapper halResource, Pageable pageable, LinkedList list) throws Exception { - list.add(buildLink(Link.REL_SELF, getMethodOn() - .retrieveByLabel(null, null, halResource.getContent().getLabel(), halResource.getContent().getDsoId()))); + + PageImpl page = new PageImpl<>(halResource.getFullList(), pageable, + halResource.getTotalElements()); + halResource.setPageHeader(new EmbeddedPageHeader(getSelfLink(halResource.getContent(), pageable), + page, true)); + } + + public String getSelfLink(RelationshipRestWrapper content, Pageable pageable) throws Exception { + if (content != null) { + UriComponentsBuilder uriBuilderSelfLink = uriBuilder(getMethodOn() + .retrieveByLabel(null, null, content.getLabel(), + content.getDsoId(), pageable)); + return uriBuilderSelfLink.build().toString(); + } + return null; } protected Class getControllerClass() { diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipResourceWrapper.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipResourceWrapper.java index 5262d23746..f4cbd1e102 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipResourceWrapper.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipResourceWrapper.java @@ -10,23 +10,54 @@ package org.dspace.app.rest.model.hateoas; import java.util.LinkedList; import java.util.List; +import com.fasterxml.jackson.annotation.JsonIgnore; import org.dspace.app.rest.model.RelationshipRest; import org.dspace.app.rest.model.RelationshipRestWrapper; import org.dspace.app.rest.utils.Utils; +import org.springframework.data.domain.Pageable; public class RelationshipResourceWrapper extends HALResource { - public RelationshipResourceWrapper(RelationshipRestWrapper content, Utils utils) { + + @JsonIgnore + private List list; + + @JsonIgnore + private List fullList; + + @JsonIgnore + private Integer totalElements; + + public RelationshipResourceWrapper(RelationshipRestWrapper content, Utils utils, Integer totalElements, + Pageable pageable) { super(content); - addEmbeds(content, utils); + this.totalElements = totalElements; + addEmbeds(content, utils, pageable); } - private void addEmbeds(RelationshipRestWrapper content, Utils utils) { + private void addEmbeds(RelationshipRestWrapper content, Utils utils, + Pageable pageable) { List list = new LinkedList<>(); for (RelationshipRest relationshipRest : content.getRelationshipRestList()) { list.add(new RelationshipResource(relationshipRest, utils)); } - + this.fullList = list; + int begin = pageable.getOffset(); + int end = (pageable.getOffset() + pageable.getPageSize()) > list.size() ? list.size() : pageable.getOffset() + pageable.getPageSize(); + list = list.subList(begin, end); + this.list = list; embedResource("relationships", list); } + + public List getList() { + return list; + } + + public Integer getTotalElements() { + return totalElements; + } + + public List getFullList() { + return fullList; + } } From 13fea6312e33ce2ff934468e2c7347dbee4818cf Mon Sep 17 00:00:00 2001 From: Raf Ponsaerts Date: Tue, 27 Nov 2018 09:37:17 +0100 Subject: [PATCH 06/14] Cleaned up and fix license headers --- .../app/rest/RelationshipRestController.java | 1 - .../relation/EntityTypeHalLinkFactory.java | 7 ++++++ ...tionshipResourceWrapperHalLinkFactory.java | 20 ++++++++++----- ...shipTypeResourceWrapperHalLinkFactory.java | 7 ++++++ .../hateoas/RelationshipResourceWrapper.java | 25 ------------------- 5 files changed, 28 insertions(+), 32 deletions(-) diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java index 74620a2358..b760c8d0ea 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java @@ -87,7 +87,6 @@ public class RelationshipRestController { for (RelationshipType relationshipType : relationshipTypeList) { relationships.addAll(relationshipService.findByRelationshipType(context, relationshipType)); } -// relationships = relationshipService.findAll(context); } List relationshipRests = new LinkedList<>(); diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/EntityTypeHalLinkFactory.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/EntityTypeHalLinkFactory.java index a721dfbe42..8aa4620617 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/EntityTypeHalLinkFactory.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/EntityTypeHalLinkFactory.java @@ -1,3 +1,10 @@ +/** + * 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.app.rest.link.relation; import java.util.LinkedList; diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipResourceWrapperHalLinkFactory.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipResourceWrapperHalLinkFactory.java index dd3dd90f07..8c05fdd1b0 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipResourceWrapperHalLinkFactory.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipResourceWrapperHalLinkFactory.java @@ -1,12 +1,19 @@ +/** + * 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.app.rest.link.relation; import java.util.LinkedList; import org.dspace.app.rest.RelationshipRestController; import org.dspace.app.rest.link.HalLinkFactory; +import org.dspace.app.rest.model.RelationshipRest; import org.dspace.app.rest.model.RelationshipRestWrapper; -import org.dspace.app.rest.model.hateoas.EmbeddedPageHeader; -import org.dspace.app.rest.model.hateoas.RelationshipResource; +import org.dspace.app.rest.model.hateoas.EmbeddedPage; import org.dspace.app.rest.model.hateoas.RelationshipResourceWrapper; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; @@ -20,10 +27,11 @@ public class RelationshipResourceWrapperHalLinkFactory protected void addLinks(RelationshipResourceWrapper halResource, Pageable pageable, LinkedList list) throws Exception { - PageImpl page = new PageImpl<>(halResource.getFullList(), pageable, - halResource.getTotalElements()); - halResource.setPageHeader(new EmbeddedPageHeader(getSelfLink(halResource.getContent(), pageable), - page, true)); + PageImpl page = new PageImpl<>(halResource.getContent().getRelationshipRestList(), pageable, + halResource.getContent().getRelationshipRestList().size()); + + halResource.setPageHeader(new EmbeddedPage(getSelfLink(halResource.getContent(), pageable), + page, halResource.getContent().getRelationshipRestList(), true, "relationships")); } public String getSelfLink(RelationshipRestWrapper content, Pageable pageable) throws Exception { diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipTypeResourceWrapperHalLinkFactory.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipTypeResourceWrapperHalLinkFactory.java index b4a59f7953..88b98af27f 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipTypeResourceWrapperHalLinkFactory.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipTypeResourceWrapperHalLinkFactory.java @@ -1,3 +1,10 @@ +/** + * 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.app.rest.link.relation; import java.util.LinkedList; diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipResourceWrapper.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipResourceWrapper.java index f4cbd1e102..0081638157 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipResourceWrapper.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipResourceWrapper.java @@ -10,7 +10,6 @@ package org.dspace.app.rest.model.hateoas; import java.util.LinkedList; import java.util.List; -import com.fasterxml.jackson.annotation.JsonIgnore; import org.dspace.app.rest.model.RelationshipRest; import org.dspace.app.rest.model.RelationshipRestWrapper; import org.dspace.app.rest.utils.Utils; @@ -18,20 +17,9 @@ import org.springframework.data.domain.Pageable; public class RelationshipResourceWrapper extends HALResource { - - @JsonIgnore - private List list; - - @JsonIgnore - private List fullList; - - @JsonIgnore - private Integer totalElements; - public RelationshipResourceWrapper(RelationshipRestWrapper content, Utils utils, Integer totalElements, Pageable pageable) { super(content); - this.totalElements = totalElements; addEmbeds(content, utils, pageable); } @@ -41,23 +29,10 @@ public class RelationshipResourceWrapper extends HALResource list.size() ? list.size() : pageable.getOffset() + pageable.getPageSize(); list = list.subList(begin, end); - this.list = list; embedResource("relationships", list); } - public List getList() { - return list; - } - - public Integer getTotalElements() { - return totalElements; - } - - public List getFullList() { - return fullList; - } } From 699fb42683f748fa17bb06729a8492485026620a Mon Sep 17 00:00:00 2001 From: Raf Ponsaerts Date: Thu, 29 Nov 2018 13:56:38 +0100 Subject: [PATCH 07/14] [Task 57441] fixed the label vs id conflicting issue --- .../org/dspace/app/rest/RelationshipRestController.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java index b760c8d0ea..dc9439166c 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java @@ -43,6 +43,13 @@ import org.springframework.web.bind.annotation.RestController; @RequestMapping("/api/core/relationships") public class RelationshipRestController { + /** + * Regular expression in the request mapping to accept a string as identifier but not the other kind of + * identifier (digits or uuid) + */ + private static final String REGEX_REQUESTMAPPING_LABEL = "/{label:^(?!^\\d+$)" + + "(?!^[0-9a-fxA-FX]{8}-[0-9a-fxA-FX]{4}-[0-9a-fxA-FX]{4}-[0-9a-fxA-FX]{4}-[0-9a-fxA-FX]{12}$)[\\w+\\-]+$+}"; + @Autowired private RelationshipTypeService relationshipTypeService; @@ -61,7 +68,7 @@ public class RelationshipRestController { @Autowired private HalLinkService halLinkService; - @RequestMapping(method = RequestMethod.GET, value = "/{label}") + @RequestMapping(method = RequestMethod.GET, value = REGEX_REQUESTMAPPING_LABEL) public RelationshipResourceWrapper retrieveByLabel(HttpServletResponse response, HttpServletRequest request, @PathVariable String label, @RequestParam(name = "dso", required = false) String dsoId, From e932075d039275430a866247965c381d9e548193 Mon Sep 17 00:00:00 2001 From: Raf Ponsaerts Date: Fri, 14 Dec 2018 13:26:16 +0100 Subject: [PATCH 08/14] Added documentation and @Override where applicable --- .../content/RelationshipServiceImpl.java | 7 +++++++ .../content/RelationshipTypeServiceImpl.java | 7 ++++++- .../dspace/content/dao/RelationshipDAO.java | 10 ++++++++++ .../content/dao/RelationshipTypeDAO.java | 18 ++++++++++++++++++ .../content/dao/impl/RelationshipDAOImpl.java | 4 ++++ .../dao/impl/RelationshipTypeDAOImpl.java | 3 +++ .../content/service/RelationshipService.java | 19 +++++++++++++++++++ .../service/RelationshipTypeService.java | 9 +++++++++ 8 files changed, 76 insertions(+), 1 deletion(-) 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 3f5886e4bc..46bfde95f0 100644 --- a/dspace-api/src/main/java/org/dspace/content/RelationshipServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/RelationshipServiceImpl.java @@ -48,6 +48,7 @@ public class RelationshipServiceImpl implements RelationshipService { return relationshipDAO.create(context, new Relationship()); } + @Override public Relationship create(Context context, Relationship relationship) throws SQLException, AuthorizeException { if (isRelationshipValidToCreate(context, relationship)) { if (!authorizeService.isAdmin(context)) { @@ -86,10 +87,12 @@ public class RelationshipServiceImpl implements RelationshipService { } } + @Override public int findLeftPlaceByLeftItem(Context context, Item item) throws SQLException { return relationshipDAO.findLeftPlaceByLeftItem(context, item); } + @Override public int findRightPlaceByRightItem(Context context, Item item) throws SQLException { return relationshipDAO.findRightPlaceByRightItem(context, item); } @@ -165,6 +168,7 @@ public class RelationshipServiceImpl implements RelationshipService { return relationship; } + @Override public List findByItem(Context context, Item item) throws SQLException { List list = relationshipDAO.findByItem(context, item); @@ -185,6 +189,7 @@ public class RelationshipServiceImpl implements RelationshipService { return list; } + @Override public List findAll(Context context) throws SQLException { return relationshipDAO.findAll(context, Relationship.class); } @@ -287,6 +292,7 @@ public class RelationshipServiceImpl implements RelationshipService { return listToReturn; } + @Override public List findByItemAndRelationshipType(Context context, Item item, RelationshipType relationshipType) @@ -301,6 +307,7 @@ public class RelationshipServiceImpl implements RelationshipService { return listToReturn; } + @Override public List findByRelationshipType(Context context, RelationshipType relationshipType) throws SQLException { return relationshipDAO.findByRelationshipType(context, relationshipType); diff --git a/dspace-api/src/main/java/org/dspace/content/RelationshipTypeServiceImpl.java b/dspace-api/src/main/java/org/dspace/content/RelationshipTypeServiceImpl.java index e2d0a82141..7863fef898 100644 --- a/dspace-api/src/main/java/org/dspace/content/RelationshipTypeServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/RelationshipTypeServiceImpl.java @@ -35,7 +35,8 @@ public class RelationshipTypeServiceImpl implements RelationshipTypeService { return relationshipTypeDAO.create(context, new RelationshipType()); } - public RelationshipType create(Context context, RelationshipType relationshipType) + @Override + public RelationshipType ecreate(Context context, RelationshipType relationshipType) throws SQLException, AuthorizeException { if (!authorizeService.isAdmin(context)) { throw new AuthorizeException( @@ -44,19 +45,23 @@ public class RelationshipTypeServiceImpl implements RelationshipTypeService { return relationshipTypeDAO.create(context, relationshipType); } + @Override public RelationshipType findbyTypesAndLabels(Context context,EntityType leftType,EntityType rightType, String leftLabel,String rightLabel) throws SQLException { return relationshipTypeDAO.findbyTypesAndLabels(context, leftType, rightType, leftLabel, rightLabel); } + @Override public List findAll(Context context) throws SQLException { return relationshipTypeDAO.findAll(context, RelationshipType.class); } + @Override public List findByLeftOrRightLabel(Context context, String label) throws SQLException { return relationshipTypeDAO.findByLeftOrRightLabel(context, label); } + @Override public List findByEntityType(Context context, EntityType entityType) throws SQLException { return relationshipTypeDAO.findByEntityType(context, entityType); } 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 bcf2ba8183..5352e39680 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 @@ -58,5 +58,15 @@ public interface RelationshipDAO extends GenericDAO { */ int findRightPlaceByRightItem(Context context,Item item) throws SQLException; + /** + * This method returns a list of Relationship objects for the given RelationshipType object. + * It will construct a list of all Relationship objects that have the given RelationshipType object + * as the relationshipType property + * @param context The relevant DSpace context + * @param relationshipType The RelationshipType object to be checked on + * @return A list of Relationship objects that have the given RelationshipType object as the + * relationshipType property + * @throws SQLException If something goes wrong + */ List findByRelationshipType(Context context, RelationshipType relationshipType) throws SQLException; } diff --git a/dspace-api/src/main/java/org/dspace/content/dao/RelationshipTypeDAO.java b/dspace-api/src/main/java/org/dspace/content/dao/RelationshipTypeDAO.java index 9da26f76fe..f3193594df 100644 --- a/dspace-api/src/main/java/org/dspace/content/dao/RelationshipTypeDAO.java +++ b/dspace-api/src/main/java/org/dspace/content/dao/RelationshipTypeDAO.java @@ -38,6 +38,24 @@ public interface RelationshipTypeDAO extends GenericDAO { EntityType leftType,EntityType rightType,String leftLabel,String rightLabel) throws SQLException; + /** + * This method will return a list of RelationshipType objects for which the given label is equal to + * either the leftLabel or rightLabel. + * @param context The relevant DSpace context + * @param label The label that will be used to check on + * @return A list of RelationshipType objects that have the given label as either the leftLabel or rightLabel + * @throws SQLException If something goes wrong + */ List findByLeftOrRightLabel(Context context, String label) throws SQLException; + + /** + * This method will return a list of RelationshipType objects for which the given EntityType object is equal + * to the leftType or rightType + * @param context The relevant DSpace context + * @param entityType The EntityType object that will be used to check on + * @return The list of RelationshipType objects that have the given EntityType object + * as either a leftType or rightType + * @throws SQLException If something goes wrong + */ List findByEntityType(Context context, EntityType entityType) 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 b7fbb3710e..d6e79e7ff4 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 @@ -23,6 +23,7 @@ import org.dspace.core.Context; public class RelationshipDAOImpl extends AbstractHibernateDAO implements RelationshipDAO { + @Override public List findByItem(Context context, Item item) throws SQLException { CriteriaBuilder criteriaBuilder = getCriteriaBuilder(context); CriteriaQuery criteriaQuery = getCriteriaQuery(criteriaBuilder, Relationship.class); @@ -34,6 +35,7 @@ public class RelationshipDAOImpl extends AbstractHibernateDAO impl return list(context, criteriaQuery, true, Relationship.class, -1, -1); } + @Override public int findLeftPlaceByLeftItem(Context context, Item item) throws SQLException { CriteriaBuilder criteriaBuilder = getCriteriaBuilder(context); CriteriaQuery criteriaQuery = getCriteriaQuery(criteriaBuilder, Relationship.class); @@ -49,6 +51,7 @@ public class RelationshipDAOImpl extends AbstractHibernateDAO impl } } + @Override public int findRightPlaceByRightItem(Context context, Item item) throws SQLException { CriteriaBuilder criteriaBuilder = getCriteriaBuilder(context); CriteriaQuery criteriaQuery = getCriteriaQuery(criteriaBuilder, Relationship.class); @@ -64,6 +67,7 @@ public class RelationshipDAOImpl extends AbstractHibernateDAO impl } } + @Override public List findByRelationshipType(Context context, RelationshipType relationshipType) throws SQLException { CriteriaBuilder criteriaBuilder = getCriteriaBuilder(context); diff --git a/dspace-api/src/main/java/org/dspace/content/dao/impl/RelationshipTypeDAOImpl.java b/dspace-api/src/main/java/org/dspace/content/dao/impl/RelationshipTypeDAOImpl.java index 79a5fa1ca6..7914040fa0 100644 --- a/dspace-api/src/main/java/org/dspace/content/dao/impl/RelationshipTypeDAOImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/dao/impl/RelationshipTypeDAOImpl.java @@ -22,6 +22,7 @@ import org.dspace.core.Context; public class RelationshipTypeDAOImpl extends AbstractHibernateDAO implements RelationshipTypeDAO { + @Override public RelationshipType findbyTypesAndLabels(Context context, EntityType leftType, EntityType rightType, String leftLabel, String rightLabel) throws SQLException { @@ -38,6 +39,7 @@ public class RelationshipTypeDAOImpl extends AbstractHibernateDAO findByLeftOrRightLabel(Context context, String label) throws SQLException { CriteriaBuilder criteriaBuilder = getCriteriaBuilder(context); CriteriaQuery criteriaQuery = getCriteriaQuery(criteriaBuilder, RelationshipType.class); @@ -52,6 +54,7 @@ public class RelationshipTypeDAOImpl extends AbstractHibernateDAO findByEntityType(Context context, EntityType entityType) throws SQLException { CriteriaBuilder criteriaBuilder = getCriteriaBuilder(context); CriteriaQuery criteriaQuery = getCriteriaQuery(criteriaBuilder, RelationshipType.class); 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 feadaaaba3..81f6c32c5c 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 @@ -73,9 +73,28 @@ public interface RelationshipService extends DSpaceCRUDService { */ int findRightPlaceByRightItem(Context context, Item item) throws SQLException; + /** + * This method returns a list of Relationships for which the leftItem or rightItem is equal to the given + * Item object and for which the RelationshipType object is equal to the relationshipType property + * @param context The relevant DSpace context + * @param item The Item object to be matched on the leftItem or rightItem for the relationship + * @param relationshipType The RelationshipType object that will be used to check the Relationship on + * @return The list of Relationship objects that have the given Item object as leftItem or rightItem and + * for which the relationshipType property is equal to the given RelationshipType + * @throws SQLException If something goes wrong + */ public List findByItemAndRelationshipType(Context context, Item item, RelationshipType relationshipType) throws SQLException; + /** + * This method returns a list of Relationship objets for which the relationshipType property is equal to the given + * RelationshipType object + * @param context The relevant DSpace context + * @param relationshipType The RelationshipType object that will be used to check the Relationship on + * @return The list of Relationship objects for which the given RelationshipType object is equal + * to the relationshipType property + * @throws SQLException If something goes wrong + */ List findByRelationshipType(Context context, RelationshipType relationshipType) throws SQLException; } \ No newline at end of file diff --git a/dspace-api/src/main/java/org/dspace/content/service/RelationshipTypeService.java b/dspace-api/src/main/java/org/dspace/content/service/RelationshipTypeService.java index f1abb734a8..fb199f39be 100644 --- a/dspace-api/src/main/java/org/dspace/content/service/RelationshipTypeService.java +++ b/dspace-api/src/main/java/org/dspace/content/service/RelationshipTypeService.java @@ -64,5 +64,14 @@ public interface RelationshipTypeService extends DSpaceCRUDService findByLeftOrRightLabel(Context context, String label) throws SQLException; + /** + * Returns a list of RelationshipType objects for which the given EntityType is equal to either the leftType + * or the rightType + * @param context The relevant DSpace context + * @param entityType The EntityType object used to check the leftType and rightType properties + * @return A list of RelationshipType objects for which the leftType or rightType property are equal to the + * given EntityType object + * @throws SQLException If something goes wrong + */ List findByEntityType(Context context, EntityType entityType) throws SQLException; } From 2f0fb32c8c23d25f9c7e92c49462253b8f8dd277 Mon Sep 17 00:00:00 2001 From: Raf Ponsaerts Date: Fri, 14 Dec 2018 13:27:29 +0100 Subject: [PATCH 09/14] Fixed typo --- .../java/org/dspace/content/RelationshipTypeServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dspace-api/src/main/java/org/dspace/content/RelationshipTypeServiceImpl.java b/dspace-api/src/main/java/org/dspace/content/RelationshipTypeServiceImpl.java index 7863fef898..401ade2d9e 100644 --- a/dspace-api/src/main/java/org/dspace/content/RelationshipTypeServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/RelationshipTypeServiceImpl.java @@ -36,7 +36,7 @@ public class RelationshipTypeServiceImpl implements RelationshipTypeService { } @Override - public RelationshipType ecreate(Context context, RelationshipType relationshipType) + public RelationshipType create(Context context, RelationshipType relationshipType) throws SQLException, AuthorizeException { if (!authorizeService.isAdmin(context)) { throw new AuthorizeException( From 240636e9783b431f807b31fdbd811731e75d9645 Mon Sep 17 00:00:00 2001 From: Raf Ponsaerts Date: Fri, 14 Dec 2018 14:53:11 +0100 Subject: [PATCH 10/14] Created new IT and fixed checkstyle --- .../config/entities/relationship-types.xml | 14 + .../app/rest/RelationshipRestController.java | 4 +- ...tionshipResourceWrapperHalLinkFactory.java | 6 +- ...shipTypeResourceWrapperHalLinkFactory.java | 3 +- .../hateoas/RelationshipResourceWrapper.java | 3 +- .../RelationshipTypeRestControllerIT.java | 268 ++++++++++++++++++ .../RelationshipTypeRestRepositoryIT.java | 7 +- 7 files changed, 296 insertions(+), 9 deletions(-) create mode 100644 dspace-spring-rest/src/test/java/org/dspace/app/rest/RelationshipTypeRestControllerIT.java diff --git a/dspace-api/src/test/data/dspaceFolder/config/entities/relationship-types.xml b/dspace-api/src/test/data/dspaceFolder/config/entities/relationship-types.xml index 23604d7c71..d3726994ff 100644 --- a/dspace-api/src/test/data/dspaceFolder/config/entities/relationship-types.xml +++ b/dspace-api/src/test/data/dspaceFolder/config/entities/relationship-types.xml @@ -109,4 +109,18 @@ 0 + + JournalIssue + Publication + isPublicationOfJournalIssue + isJournalIssueOfPublication + + 0 + 2147483647 + + + 0 + 1 + + \ No newline at end of file diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java index dc9439166c..310f0033ef 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java @@ -7,7 +7,6 @@ */ package org.dspace.app.rest; -import java.sql.SQLException; import java.util.LinkedList; import java.util.List; import java.util.UUID; @@ -88,7 +87,8 @@ public class RelationshipRestController { throw new ResourceNotFoundException("The request DSO with id: " + dsoId + " was not found"); } for (RelationshipType relationshipType : relationshipTypeList) { - relationships.addAll(relationshipService.findByItemAndRelationshipType(context, item, relationshipType)); + relationships.addAll(relationshipService.findByItemAndRelationshipType(context, + item, relationshipType)); } } else { for (RelationshipType relationshipType : relationshipTypeList) { diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipResourceWrapperHalLinkFactory.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipResourceWrapperHalLinkFactory.java index 8c05fdd1b0..4342e18d4b 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipResourceWrapperHalLinkFactory.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipResourceWrapperHalLinkFactory.java @@ -31,13 +31,15 @@ public class RelationshipResourceWrapperHalLinkFactory halResource.getContent().getRelationshipRestList().size()); halResource.setPageHeader(new EmbeddedPage(getSelfLink(halResource.getContent(), pageable), - page, halResource.getContent().getRelationshipRestList(), true, "relationships")); + page, halResource.getContent().getRelationshipRestList(), + true, "relationships")); } public String getSelfLink(RelationshipRestWrapper content, Pageable pageable) throws Exception { if (content != null) { UriComponentsBuilder uriBuilderSelfLink = uriBuilder(getMethodOn() - .retrieveByLabel(null, null, content.getLabel(), + .retrieveByLabel(null, null, + content.getLabel(), content.getDsoId(), pageable)); return uriBuilderSelfLink.build().toString(); } diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipTypeResourceWrapperHalLinkFactory.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipTypeResourceWrapperHalLinkFactory.java index 88b98af27f..9da0a39f32 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipTypeResourceWrapperHalLinkFactory.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipTypeResourceWrapperHalLinkFactory.java @@ -21,7 +21,8 @@ public class RelationshipTypeResourceWrapperHalLinkFactory extends HalLinkFactory { protected void addLinks(RelationshipTypeResourceWrapper halResource, Pageable pageable, LinkedList list) throws Exception { - list.add(buildLink(Link.REL_SELF, getMethodOn().retrieve(halResource.getContent().getEntityTypeId(), null, null))); + list.add(buildLink(Link.REL_SELF, getMethodOn() + .retrieve(halResource.getContent().getEntityTypeId(), null, null))); } protected Class getControllerClass() { diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipResourceWrapper.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipResourceWrapper.java index 0081638157..5851cbc65d 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipResourceWrapper.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipResourceWrapper.java @@ -30,7 +30,8 @@ public class RelationshipResourceWrapper extends HALResource list.size() ? list.size() : pageable.getOffset() + pageable.getPageSize(); + int end = (pageable.getOffset() + pageable.getPageSize()) > list.size() ? list.size() : + pageable.getOffset() + pageable.getPageSize(); list = list.subList(begin, end); embedResource("relationships", list); } diff --git a/dspace-spring-rest/src/test/java/org/dspace/app/rest/RelationshipTypeRestControllerIT.java b/dspace-spring-rest/src/test/java/org/dspace/app/rest/RelationshipTypeRestControllerIT.java new file mode 100644 index 0000000000..f32f8bffc7 --- /dev/null +++ b/dspace-spring-rest/src/test/java/org/dspace/app/rest/RelationshipTypeRestControllerIT.java @@ -0,0 +1,268 @@ +/** + * 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.app.rest; + +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.is; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.io.File; +import java.util.Iterator; +import java.util.List; + +import org.dspace.app.rest.builder.CollectionBuilder; +import org.dspace.app.rest.builder.CommunityBuilder; +import org.dspace.app.rest.builder.ItemBuilder; +import org.dspace.app.rest.builder.RelationshipBuilder; +import org.dspace.app.rest.matcher.EntityTypeMatcher; +import org.dspace.app.rest.matcher.PageMatcher; +import org.dspace.app.rest.matcher.RelationshipMatcher; +import org.dspace.app.rest.matcher.RelationshipTypeMatcher; +import org.dspace.app.rest.test.AbstractControllerIntegrationTest; +import org.dspace.content.Collection; +import org.dspace.content.Community; +import org.dspace.content.EntityType; +import org.dspace.content.Item; +import org.dspace.content.Relationship; +import org.dspace.content.RelationshipType; +import org.dspace.content.service.EntityTypeService; +import org.dspace.content.service.RelationshipService; +import org.dspace.content.service.RelationshipTypeService; +import org.dspace.services.ConfigurationService; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; + +public class RelationshipTypeRestControllerIT extends AbstractControllerIntegrationTest { + + @Autowired + private RelationshipTypeService relationshipTypeService; + + @Autowired + private EntityTypeService entityTypeService; + + @Autowired + private RelationshipService relationshipService; + + @Autowired + private ConfigurationService configurationService; + + @Before + public void setup() throws Exception { + + //Set up the database for the next test + String pathToFile = configurationService.getProperty("dspace.dir") + + File.separator + "config" + File.separator + "entities" + File.separator + "relationship-types.xml"; + runDSpaceScript("initialize-entities", "-f", pathToFile); + + } + + @After + public void destroy() throws Exception { + //Clean up the database for the next test + context.turnOffAuthorisationSystem(); + List relationshipTypeList = relationshipTypeService.findAll(context); + List entityTypeList = entityTypeService.findAll(context); + List relationships = relationshipService.findAll(context); + + Iterator relationshipIterator = relationships.iterator(); + while (relationshipIterator.hasNext()) { + Relationship relationship = relationshipIterator.next(); + relationshipIterator.remove(); + relationshipService.delete(context, relationship); + } + + Iterator relationshipTypeIterator = relationshipTypeList.iterator(); + while (relationshipTypeIterator.hasNext()) { + RelationshipType relationshipType = relationshipTypeIterator.next(); + relationshipTypeIterator.remove(); + relationshipTypeService.delete(context, relationshipType); + } + + Iterator entityTypeIterator = entityTypeList.iterator(); + while (entityTypeIterator.hasNext()) { + EntityType entityType = entityTypeIterator.next(); + entityTypeIterator.remove(); + entityTypeService.delete(context, entityType); + } + + super.destroy(); + } + + @Test + public void findAllEntityTypes() throws Exception { + + context.turnOffAuthorisationSystem(); + + getClient().perform(get("/api/core/entitytypes")) + + .andExpect(status().isOk()) + .andExpect(jsonPath("$.page", + is(PageMatcher.pageEntryWithTotalPagesAndElements(0, 20, 1, 7)))) + .andExpect(jsonPath("$._embedded.entitytypes", containsInAnyOrder( + EntityTypeMatcher.matchEntityTypeEntryForLabel("Publication"), + EntityTypeMatcher.matchEntityTypeEntryForLabel("Person"), + EntityTypeMatcher.matchEntityTypeEntryForLabel("Project"), + EntityTypeMatcher.matchEntityTypeEntryForLabel("OrgUnit"), + EntityTypeMatcher.matchEntityTypeEntryForLabel("Journal"), + EntityTypeMatcher.matchEntityTypeEntryForLabel("JournalVolume"), + EntityTypeMatcher.matchEntityTypeEntryForLabel("JournalIssue") + + ))) + ; + } + + @Test + public void findAllRelationshipTypesForPublications() throws Exception { + + context.turnOffAuthorisationSystem(); + EntityType publicationEntityType = entityTypeService.findByEntityType(context, "Publication"); + EntityType personEntityType = entityTypeService.findByEntityType(context, "Person"); + EntityType projectEntityType = entityTypeService.findByEntityType(context, "Project"); + EntityType orgunitEntityType = entityTypeService.findByEntityType(context, "OrgUnit"); + EntityType journalIssueEntityType = entityTypeService.findByEntityType(context, "journalIssue"); + + RelationshipType relationshipType1 = relationshipTypeService + .findbyTypesAndLabels(context, publicationEntityType, personEntityType, "isAuthorOfPublication", + "isPublicationOfAuthor"); + RelationshipType relationshipType2 = relationshipTypeService + .findbyTypesAndLabels(context, publicationEntityType, projectEntityType, "isProjectOfPublication", + "isPublicationOfProject"); + RelationshipType relationshipType3 = relationshipTypeService + .findbyTypesAndLabels(context, publicationEntityType, orgunitEntityType, "isOrgUnitOfPublication", + "isPublicationOfOrgUnit"); + RelationshipType relationshipType4 = relationshipTypeService + .findbyTypesAndLabels(context, journalIssueEntityType, publicationEntityType, "isPublicationOfJournalIssue", + "isJournalIssueOfPublication"); + RelationshipType relationshipType5 = relationshipTypeService + .findbyTypesAndLabels(context, publicationEntityType, orgunitEntityType, "isAuthorOfPublication", + "isPublicationOfAuthor"); + getClient().perform(get("/api/core/entitytypes/" + publicationEntityType.getID() + "/relationshiptypes")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$._embedded.relationshiptypes", containsInAnyOrder( + RelationshipTypeMatcher.matchRelationshipTypeEntry(relationshipType1), + RelationshipTypeMatcher.matchRelationshipTypeEntry(relationshipType2), + RelationshipTypeMatcher.matchRelationshipTypeEntry(relationshipType3), + RelationshipTypeMatcher.matchRelationshipTypeEntry(relationshipType4), + RelationshipTypeMatcher.matchRelationshipTypeEntry(relationshipType5) + ))); + } + + @Test + public void createAndFindRelationships() throws Exception { + + context.turnOffAuthorisationSystem(); + + + parentCommunity = CommunityBuilder.createCommunity(context) + .withName("Parent Community") + .build(); + Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity) + .withName("Sub Community") + .build(); + Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build(); + Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build(); + Collection col3 = CollectionBuilder.createCollection(context, child1).withName("OrgUnits").build(); + + Item author1 = ItemBuilder.createItem(context, col1) + .withTitle("Author1") + .withIssueDate("2017-10-17") + .withAuthor("Smith, Donald") + .withRelationshipType("Person") + .build(); + + Item author2 = ItemBuilder.createItem(context, col2) + .withTitle("Author2") + .withIssueDate("2016-02-13") + .withAuthor("Smith, Maria") + .withRelationshipType("Person") + .build(); + + Item author3 = ItemBuilder.createItem(context, col2) + .withTitle("Author3") + .withIssueDate("2016-02-13") + .withAuthor("Maybe, Maybe") + .withRelationshipType("Person") + .build(); + + Item orgUnit1 = ItemBuilder.createItem(context, col3) + .withTitle("OrgUnit1") + .withAuthor("Testy, TEst") + .withIssueDate("2015-01-01") + .withRelationshipType("OrgUnit") + .build(); + + Item project1 = ItemBuilder.createItem(context, col3) + .withTitle("Project1") + .withAuthor("Testy, TEst") + .withIssueDate("2015-01-01") + .withRelationshipType("Project") + .build(); + + Item publication = ItemBuilder.createItem(context, col3) + .withTitle("Publication1") + .withAuthor("Testy, TEst") + .withIssueDate("2015-01-01") + .withRelationshipType("Publication") + .build(); + + Item publication2 = ItemBuilder.createItem(context, col3) + .withTitle("Publication2") + .withIssueDate("2015-01-01") + .withRelationshipType("Publication") + .build(); + + RelationshipType isOrgUnitOfPersonRelationshipType = relationshipTypeService + .findbyTypesAndLabels(context, entityTypeService.findByEntityType(context, "Person"), + entityTypeService.findByEntityType(context, "OrgUnit"), + "isOrgUnitOfPerson", "isPersonOfOrgUnit"); + RelationshipType isOrgUnitOfProjectRelationshipType = relationshipTypeService + .findbyTypesAndLabels(context, entityTypeService.findByEntityType(context, "Project"), + entityTypeService.findByEntityType(context, "OrgUnit"), + "isOrgUnitOfProject", "isProjectOfOrgUnit"); + RelationshipType isAuthorOfPublicationRelationshipType = relationshipTypeService + .findbyTypesAndLabels(context, entityTypeService.findByEntityType(context, "Publication"), + entityTypeService.findByEntityType(context, "Person"), + "isAuthorOfPublication", "isPublicationOfAuthor"); + + Relationship relationship1 = RelationshipBuilder + .createRelationshipBuilder(context, publication, author1, isAuthorOfPublicationRelationshipType).build(); + Relationship relationship2 = RelationshipBuilder + .createRelationshipBuilder(context, publication, author2, isAuthorOfPublicationRelationshipType).build(); + Relationship relationship3 = RelationshipBuilder + .createRelationshipBuilder(context, publication2, author2, isAuthorOfPublicationRelationshipType).build(); + Relationship relationship4 = RelationshipBuilder + .createRelationshipBuilder(context, publication2, author3, isAuthorOfPublicationRelationshipType).build(); + + getClient().perform(get("/api/core/relationships/isAuthorOfPublication")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$._embedded.relationships", containsInAnyOrder( + RelationshipMatcher.matchRelationship(relationship1), + RelationshipMatcher.matchRelationship(relationship2), + RelationshipMatcher.matchRelationship(relationship3), + RelationshipMatcher.matchRelationship(relationship4) + ))); + + getClient().perform(get("/api/core/relationships/isAuthorOfPublication?dso=" + publication.getID())) + .andExpect(status().isOk()) + .andExpect(jsonPath("$._embedded.relationships", containsInAnyOrder( + RelationshipMatcher.matchRelationship(relationship1), + RelationshipMatcher.matchRelationship(relationship2) + ))); + + getClient().perform(get("/api/core/relationships/isAuthorOfPublication?dso=" + publication2.getID())) + .andExpect(status().isOk()) + .andExpect(jsonPath("$._embedded.relationships", containsInAnyOrder( + RelationshipMatcher.matchRelationship(relationship3), + RelationshipMatcher.matchRelationship(relationship4) + ))); + } +} diff --git a/dspace-spring-rest/src/test/java/org/dspace/app/rest/RelationshipTypeRestRepositoryIT.java b/dspace-spring-rest/src/test/java/org/dspace/app/rest/RelationshipTypeRestRepositoryIT.java index 3fafd8e1b9..e654ff6383 100644 --- a/dspace-spring-rest/src/test/java/org/dspace/app/rest/RelationshipTypeRestRepositoryIT.java +++ b/dspace-spring-rest/src/test/java/org/dspace/app/rest/RelationshipTypeRestRepositoryIT.java @@ -94,7 +94,7 @@ public class RelationshipTypeRestRepositoryIT extends AbstractControllerIntegrat @Test public void findAllRelationshipTypesTest() throws SQLException { - assertEquals(9, relationshipTypeService.findAll(context).size()); + assertEquals(10, relationshipTypeService.findAll(context).size()); } @Test @@ -194,7 +194,7 @@ public class RelationshipTypeRestRepositoryIT extends AbstractControllerIntegrat //We expect a 200 OK status .andExpect(status().isOk()) //The type has to be 'discover' - .andExpect(jsonPath("$.page.totalElements", is(9))) + .andExpect(jsonPath("$.page.totalElements", is(10))) //There needs to be a self link to this endpoint .andExpect(jsonPath("$._links.self.href", containsString("api/core/relationshiptypes"))) //We have 4 facets in the default configuration, they need to all be present in the embedded section @@ -207,7 +207,8 @@ public class RelationshipTypeRestRepositoryIT extends AbstractControllerIntegrat RelationshipTypeMatcher.matchRelationshipTypeEntry(relationshipTypes.get(5)), RelationshipTypeMatcher.matchRelationshipTypeEntry(relationshipTypes.get(6)), RelationshipTypeMatcher.matchRelationshipTypeEntry(relationshipTypes.get(7)), - RelationshipTypeMatcher.matchRelationshipTypeEntry(relationshipTypes.get(8))) + RelationshipTypeMatcher.matchRelationshipTypeEntry(relationshipTypes.get(8)), + RelationshipTypeMatcher.matchRelationshipTypeEntry(relationshipTypes.get(9))) )); } From 39bf016eb8627dbfea5f815a0a8f6da1a711e1fe Mon Sep 17 00:00:00 2001 From: Ben Bosman Date: Mon, 17 Dec 2018 13:45:04 +0100 Subject: [PATCH 11/14] Merging configurable_entities branch --- .../java/org/dspace/app/rest/RelationshipRestController.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java index 310f0033ef..f9c34ebf62 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java @@ -13,7 +13,8 @@ import java.util.UUID; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.logging.log4j.Logger; import org.dspace.app.rest.converter.RelationshipConverter; import org.dspace.app.rest.link.HalLinkService; import org.dspace.app.rest.model.RelationshipRest; From 32c9585edeb8f090123543c102a32b7992a421db Mon Sep 17 00:00:00 2001 From: Ben Bosman Date: Tue, 18 Dec 2018 10:55:22 +0100 Subject: [PATCH 12/14] checkstyle --- .../java/org/dspace/app/rest/RelationshipRestController.java | 1 - 1 file changed, 1 deletion(-) diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java index f9c34ebf62..6898eabe1c 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java @@ -14,7 +14,6 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; -import org.apache.logging.log4j.Logger; import org.dspace.app.rest.converter.RelationshipConverter; import org.dspace.app.rest.link.HalLinkService; import org.dspace.app.rest.model.RelationshipRest; From c9bbbfde1f62a03ff3e130711fb2aa8e72e54b29 Mon Sep 17 00:00:00 2001 From: Ben Bosman Date: Thu, 20 Dec 2018 11:26:05 +0100 Subject: [PATCH 13/14] merge conflict --- .../java/org/dspace/content/RelationshipTypeServiceImpl.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/content/RelationshipTypeServiceImpl.java b/dspace-api/src/main/java/org/dspace/content/RelationshipTypeServiceImpl.java index 5700a83c0c..401ade2d9e 100644 --- a/dspace-api/src/main/java/org/dspace/content/RelationshipTypeServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/RelationshipTypeServiceImpl.java @@ -97,9 +97,4 @@ public class RelationshipTypeServiceImpl implements RelationshipTypeService { } relationshipTypeDAO.delete(context, relationshipType); } - - @Override - public List findByLeftOrRightLabel(Context context, String label) throws SQLException { - return relationshipTypeDAO.findByLeftOrRightLabel(context, label); - } } From ae96aeafd4d44254ebc6096737a941bd1989155d Mon Sep 17 00:00:00 2001 From: Raf Ponsaerts Date: Tue, 15 Jan 2019 15:49:42 +0100 Subject: [PATCH 14/14] Applied community feedback, javadocs --- .../config/entities/relationship-types.xml | 1 - .../app/rest/RelationshipRestController.java | 17 +++++++++++++++++ .../rest/RelationshipTypeRestController.java | 15 +++++++++++++++ .../relation/EntityTypeHalLinkFactory.java | 10 ++++++++++ ...tionshipResourceWrapperHalLinkFactory.java | 19 +++++++++++++++++++ ...shipTypeResourceWrapperHalLinkFactory.java | 10 ++++++++++ .../rest/model/RelationshipRestWrapper.java | 5 +++++ .../model/RelationshipTypeRestWrapper.java | 6 ++++++ .../hateoas/RelationshipResourceWrapper.java | 12 ++++++++++++ .../RelationshipTypeResourceWrapper.java | 10 ++++++++++ 10 files changed, 104 insertions(+), 1 deletion(-) diff --git a/dspace-api/src/test/data/dspaceFolder/config/entities/relationship-types.xml b/dspace-api/src/test/data/dspaceFolder/config/entities/relationship-types.xml index d3726994ff..6f71b3e19c 100644 --- a/dspace-api/src/test/data/dspaceFolder/config/entities/relationship-types.xml +++ b/dspace-api/src/test/data/dspaceFolder/config/entities/relationship-types.xml @@ -116,7 +116,6 @@ isJournalIssueOfPublication 0 - 2147483647 0 diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java index 6898eabe1c..7f6a1f22cf 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipRestController.java @@ -38,6 +38,9 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +/** + * This will be the entry point for the api/core/relationships endpoint with additional paths to it + */ @RestController @RequestMapping("/api/core/relationships") public class RelationshipRestController { @@ -67,6 +70,20 @@ public class RelationshipRestController { @Autowired private HalLinkService halLinkService; + /** + * This method will retrieve all the Relationships that have a RelationshipType which has a left or right label + * equal to the one passed along in the pathvariable. + * This is further filtered by an optional dso parameter to filter on only the relationships for the given dso + * if this is applicable + * + * @param response The response object + * @param request The request object + * @param label The label on which the Relationship's RelationshipType will be matched + * @param dsoId The ID of the dso on which we'll search for relationships if applicable + * @param pageable The page object + * @return A Resource containing all the relationships that meet the criteria + * @throws Exception If something goes wrong + */ @RequestMapping(method = RequestMethod.GET, value = REGEX_REQUESTMAPPING_LABEL) public RelationshipResourceWrapper retrieveByLabel(HttpServletResponse response, HttpServletRequest request, @PathVariable String label, diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipTypeRestController.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipTypeRestController.java index 3e80426805..f6f7a28067 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipTypeRestController.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/RelationshipTypeRestController.java @@ -31,6 +31,11 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; +/** + * This controller will handle all the incoming calls on the api/core/entitytypes/{id}/relationshiptypes endpoint + * where the id parameter can be filled in to match a specific entityType and then get all the relationshipTypes + * for the given EntityType + */ @RestController @RequestMapping("/api/core/entitytypes/{id}/relationshiptypes") public class RelationshipTypeRestController { @@ -51,6 +56,16 @@ public class RelationshipTypeRestController { @Autowired private HalLinkService halLinkService; + /** + * This method will retrieve all the RelationshipTypes that conform to the given EntityType by the given ID and + * it will return this in a wrapped resource. + * + * @param id The ID of the EntityType objects that we'll use to retrieve the RelationshipTypes + * @param response The response object + * @param request The request object + * @return The wrapped resource containing the list of RelationshipType objects as defined above + * @throws SQLException If something goes wrong + */ @RequestMapping(method = RequestMethod.GET) public RelationshipTypeResourceWrapper retrieve(@PathVariable Integer id, HttpServletResponse response, HttpServletRequest request) throws SQLException { diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/EntityTypeHalLinkFactory.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/EntityTypeHalLinkFactory.java index 8aa4620617..99f738ba1e 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/EntityTypeHalLinkFactory.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/EntityTypeHalLinkFactory.java @@ -16,16 +16,26 @@ import org.springframework.data.domain.Pageable; import org.springframework.hateoas.Link; import org.springframework.stereotype.Component; +/** + * This class' purpose is to add the links to the EntityTypeResource. This function and class will be called + * and used + * when the HalLinkService addLinks methods is called as it'll iterate over all the different factories and check + * whether + * these are allowed to create links for said resource or not. + */ @Component public class EntityTypeHalLinkFactory extends HalLinkFactory { + @Override protected void addLinks(EntityTypeResource halResource, Pageable pageable, LinkedList list) throws Exception { list.add(buildLink("relationshiptypes", getMethodOn().retrieve(halResource.getContent().getId(), null, null))); } + @Override protected Class getControllerClass() { return RelationshipTypeRestController.class; } + @Override protected Class getResourceClass() { return EntityTypeResource.class; } diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipResourceWrapperHalLinkFactory.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipResourceWrapperHalLinkFactory.java index 4342e18d4b..36fb3e3f4d 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipResourceWrapperHalLinkFactory.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipResourceWrapperHalLinkFactory.java @@ -21,9 +21,17 @@ import org.springframework.hateoas.Link; import org.springframework.stereotype.Component; import org.springframework.web.util.UriComponentsBuilder; +/** + * This class' purpose is to add the links to the RelationshipResourceWrapper. This function and class will be called + * and used + * when the HalLinkService addLinks methods is called as it'll iterate over all the different factories and check + * whether + * these are allowed to create links for said resource or not. + */ @Component public class RelationshipResourceWrapperHalLinkFactory extends HalLinkFactory { + @Override protected void addLinks(RelationshipResourceWrapper halResource, Pageable pageable, LinkedList list) throws Exception { @@ -35,6 +43,15 @@ public class RelationshipResourceWrapperHalLinkFactory true, "relationships")); } + /** + * This method will construct a self link to the RelationshipRestController.retrieveByLabel method. + * This will be constructed so that the RelationshipResourceWrapper resource can contain this selflink + * and immediately point to the correct endpoint with it. + * @param content The RelationshipRestWrapper from which we'll retrieve variables to construct the link + * @param pageable The page object + * @return The String determining the link to the correct endpoint + * @throws Exception If something goes wrong + */ public String getSelfLink(RelationshipRestWrapper content, Pageable pageable) throws Exception { if (content != null) { UriComponentsBuilder uriBuilderSelfLink = uriBuilder(getMethodOn() @@ -46,10 +63,12 @@ public class RelationshipResourceWrapperHalLinkFactory return null; } + @Override protected Class getControllerClass() { return RelationshipRestController.class; } + @Override protected Class getResourceClass() { return RelationshipResourceWrapper.class; } diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipTypeResourceWrapperHalLinkFactory.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipTypeResourceWrapperHalLinkFactory.java index 9da0a39f32..f3dccbddbd 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipTypeResourceWrapperHalLinkFactory.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/link/relation/RelationshipTypeResourceWrapperHalLinkFactory.java @@ -16,19 +16,29 @@ import org.springframework.data.domain.Pageable; import org.springframework.hateoas.Link; import org.springframework.stereotype.Component; +/** + * This class' purpose is to add the links to the RelationshipTypeResourceWrapper. + * This function and class will be called and used + * when the HalLinkService addLinks methods is called as it'll iterate over all the different factories and check + * whether + * these are allowed to create links for said resource or not. + */ @Component public class RelationshipTypeResourceWrapperHalLinkFactory extends HalLinkFactory { + @Override protected void addLinks(RelationshipTypeResourceWrapper halResource, Pageable pageable, LinkedList list) throws Exception { list.add(buildLink(Link.REL_SELF, getMethodOn() .retrieve(halResource.getContent().getEntityTypeId(), null, null))); } + @Override protected Class getControllerClass() { return RelationshipTypeRestController.class; } + @Override protected Class getResourceClass() { return RelationshipTypeResourceWrapper.class; } diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/RelationshipRestWrapper.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/RelationshipRestWrapper.java index 43f0439a23..54cbfd7184 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/RelationshipRestWrapper.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/RelationshipRestWrapper.java @@ -12,6 +12,11 @@ import java.util.List; import com.fasterxml.jackson.annotation.JsonIgnore; import org.dspace.app.rest.RelationshipRestController; +/** + * This is the RestWrapper object for the RelationshipRestResource class. This will contain all the data that is + * used in that resource and more specifically, the label, dsoid and list of RelationshipRest objects + * The other methods are generic getters and setters + */ public class RelationshipRestWrapper implements RestAddressableModel { @JsonIgnore diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/RelationshipTypeRestWrapper.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/RelationshipTypeRestWrapper.java index 3a6e22ebd2..4a3ffbb6ab 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/RelationshipTypeRestWrapper.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/RelationshipTypeRestWrapper.java @@ -12,6 +12,12 @@ import java.util.List; import com.fasterxml.jackson.annotation.JsonIgnore; import org.dspace.app.rest.RelationshipTypeRestController; +/** + * This is the RestWrapper object for the RelationshipTypeRestResource class. This will contain all the data that is + * used in that resource and more specifically, the entityTypeLabel, entityTypeId and list of + * RelationshipTypeRest objects + * The other methods are generic getters and setters + */ public class RelationshipTypeRestWrapper implements RestAddressableModel { @JsonIgnore diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipResourceWrapper.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipResourceWrapper.java index 5851cbc65d..0594eeb52c 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipResourceWrapper.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipResourceWrapper.java @@ -15,8 +15,20 @@ import org.dspace.app.rest.model.RelationshipRestWrapper; import org.dspace.app.rest.utils.Utils; import org.springframework.data.domain.Pageable; +/** + * This is the RelationshipResourceWrapper class which will take the RelationshipRestWrapper's data and transform + * this into a resource with the data, embeds and links. + */ public class RelationshipResourceWrapper extends HALResource { + /** + * The constructor for the RelationshipResourceWrapper + * This will call the HALResource constructor and additionally add embeds to the resource + * @param content The RelationshipRestWrapper object that contains the data + * @param utils The Util object + * @param totalElements The total amount of elements to be included in the list + * @param pageable The pageable object + */ public RelationshipResourceWrapper(RelationshipRestWrapper content, Utils utils, Integer totalElements, Pageable pageable) { super(content); diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipTypeResourceWrapper.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipTypeResourceWrapper.java index dbe5483acc..6045946634 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipTypeResourceWrapper.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/model/hateoas/RelationshipTypeResourceWrapper.java @@ -14,8 +14,18 @@ import org.dspace.app.rest.model.RelationshipTypeRest; import org.dspace.app.rest.model.RelationshipTypeRestWrapper; import org.dspace.app.rest.utils.Utils; +/** + * This is the RelationshipTypeResourceWrapper class which will take the + * RelationshipTypeRestWrapper's data and transform this into a resource with the data, embeds and links. + */ public class RelationshipTypeResourceWrapper extends HALResource { + /** + * The constructor for the RelationshipTypeResourceWrapper + * This will call the HALResource constructor and additionally add embeds to the resource + * @param content The RelationshipTypeRestWrapper object that contains the data + * @param utils The Util object + */ public RelationshipTypeResourceWrapper(RelationshipTypeRestWrapper content, Utils utils) { super(content); addEmbeds(content, utils);