mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-07 01:54:22 +00:00
converted RelationshipTypeRestController in link repository
This commit is contained in:
@@ -174,4 +174,9 @@ public class RelationshipTypeServiceImpl implements RelationshipTypeService {
|
||||
}
|
||||
relationshipTypeDAO.delete(context, relationshipType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int countByEntityType(Context context, EntityType entityType) throws SQLException {
|
||||
return relationshipTypeDAO.countByEntityType(context, entityType);
|
||||
}
|
||||
}
|
||||
|
@@ -120,4 +120,16 @@ public interface RelationshipTypeDAO extends GenericDAO<RelationshipType> {
|
||||
List<RelationshipType> findByEntityType(Context context, EntityType entityType, Boolean isLeft,
|
||||
Integer limit, Integer offset)
|
||||
throws SQLException;
|
||||
|
||||
/**
|
||||
* Count all RelationshipType objects for which the given EntityType
|
||||
* is equal to either the leftType or the rightType
|
||||
*
|
||||
* @param context DSpace context object
|
||||
* @param entityType The EntityType object used to check the leftType and rightType properties
|
||||
* @return Total RelationshipType objects
|
||||
* @throws SQLException If database error
|
||||
*/
|
||||
public int countByEntityType(Context context, EntityType entityType) throws SQLException;
|
||||
|
||||
}
|
||||
|
@@ -8,6 +8,7 @@
|
||||
package org.dspace.content.dao.impl;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
@@ -92,6 +93,9 @@ public class RelationshipTypeDAOImpl extends AbstractHibernateDAO<RelationshipTy
|
||||
.equal(relationshipTypeRoot.get(RelationshipType_.rightType), entityType)
|
||||
)
|
||||
);
|
||||
List<javax.persistence.criteria.Order> orderList = new LinkedList<>();
|
||||
orderList.add(criteriaBuilder.asc(relationshipTypeRoot.get(RelationshipType_.ID)));
|
||||
criteriaQuery.orderBy(orderList);
|
||||
return list(context, criteriaQuery, false, RelationshipType.class, limit, offset);
|
||||
}
|
||||
|
||||
@@ -120,4 +124,18 @@ public class RelationshipTypeDAOImpl extends AbstractHibernateDAO<RelationshipTy
|
||||
}
|
||||
return list(context, criteriaQuery, false, RelationshipType.class, limit, offset);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int countByEntityType(Context context, EntityType entityType) throws SQLException {
|
||||
CriteriaBuilder criteriaBuilder = getCriteriaBuilder(context);
|
||||
CriteriaQuery criteriaQuery = getCriteriaQuery(criteriaBuilder, RelationshipType.class);
|
||||
Root<RelationshipType> 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 count(context, criteriaQuery, criteriaBuilder, relationshipTypeRoot);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -103,6 +103,16 @@ public interface RelationshipTypeService extends DSpaceCRUDService<RelationshipT
|
||||
List<RelationshipType> findByEntityType(Context context, EntityType entityType, Integer limit, Integer offset)
|
||||
throws SQLException;
|
||||
|
||||
/**
|
||||
* Count all RelationshipType objects for which the given EntityType
|
||||
* is equal to either the leftType or the rightType
|
||||
*
|
||||
* @param context DSpace context object
|
||||
* @param entityType The EntityType object used to check the leftType and rightType properties
|
||||
* @return Total RelationshipType objects
|
||||
* @throws SQLException If database error
|
||||
*/
|
||||
public int countByEntityType(Context context, EntityType entityType) throws SQLException;
|
||||
|
||||
/**
|
||||
* This method will return a list of RelationshipType objects for which the given EntityType object is equal
|
||||
|
@@ -1,94 +0,0 @@
|
||||
/**
|
||||
* 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 java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.dspace.app.rest.converter.ConverterService;
|
||||
import org.dspace.app.rest.link.HalLinkService;
|
||||
import org.dspace.app.rest.model.RelationshipTypeRest;
|
||||
import org.dspace.app.rest.model.hateoas.RelationshipTypeResource;
|
||||
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.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.web.PagedResourcesAssembler;
|
||||
import org.springframework.hateoas.PagedModel;
|
||||
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;
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
|
||||
|
||||
@Autowired
|
||||
private RelationshipTypeService relationshipTypeService;
|
||||
|
||||
@Autowired
|
||||
private EntityTypeService entityTypeService;
|
||||
|
||||
@Autowired
|
||||
private ConverterService converter;
|
||||
|
||||
@Autowired
|
||||
private Utils utils;
|
||||
|
||||
@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
|
||||
* @param pageable The pagination object
|
||||
* @param assembler The assembler 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 PagedModel<RelationshipTypeResource> retrieve(@PathVariable Integer id,
|
||||
HttpServletResponse response,
|
||||
HttpServletRequest request,
|
||||
Pageable pageable,
|
||||
PagedResourcesAssembler assembler) throws SQLException {
|
||||
Context context = ContextUtil.obtainContext(request);
|
||||
EntityType entityType = entityTypeService.find(context, id);
|
||||
List<RelationshipType> list = relationshipTypeService.findByEntityType(context, entityType, -1, -1);
|
||||
|
||||
Page<RelationshipTypeRest> relationshipTypeRestPage = converter
|
||||
.toRestPage(list, pageable, utils.obtainProjection());
|
||||
|
||||
Page<RelationshipTypeResource> relationshipTypeResources = relationshipTypeRestPage
|
||||
.map(relationshipTypeRest -> new RelationshipTypeResource(relationshipTypeRest, utils));
|
||||
relationshipTypeResources.forEach(halLinkService::addLinks);
|
||||
PagedModel<RelationshipTypeResource> result = assembler.toModel(relationshipTypeResources);
|
||||
return result;
|
||||
|
||||
|
||||
}
|
||||
}
|
@@ -1,43 +0,0 @@
|
||||
/**
|
||||
* 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.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;
|
||||
|
||||
/**
|
||||
* 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<EntityTypeResource, RelationshipTypeRestController> {
|
||||
@Override
|
||||
protected void addLinks(EntityTypeResource halResource, Pageable pageable, LinkedList<Link> list) throws Exception {
|
||||
list.add(buildLink("relationshiptypes", getMethodOn().retrieve(
|
||||
halResource.getContent().getId(), null, null, null, null)));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<RelationshipTypeRestController> getControllerClass() {
|
||||
return RelationshipTypeRestController.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<EntityTypeResource> getResourceClass() {
|
||||
return EntityTypeResource.class;
|
||||
}
|
||||
}
|
@@ -14,11 +14,20 @@ import org.dspace.app.rest.RestResourceController;
|
||||
* for the EntityTypeResource class.
|
||||
* Refer to {@link org.dspace.content.EntityType} for explanation of the properties
|
||||
*/
|
||||
@LinksRest(links = {
|
||||
@LinkRest(
|
||||
name = EntityTypeRest.RELATION_SHIP_TYPES,
|
||||
method = "getEntityTypeRelationship"
|
||||
)
|
||||
})
|
||||
public class EntityTypeRest extends BaseObjectRest<Integer> {
|
||||
|
||||
private static final long serialVersionUID = 8166078961459192770L;
|
||||
|
||||
public static final String NAME = "entitytype";
|
||||
public static final String NAME_PLURAL = "entitytypes";
|
||||
public static final String CATEGORY = "core";
|
||||
public static final String RELATION_SHIP_TYPES = "relationshipTypes";
|
||||
|
||||
public String getCategory() {
|
||||
return CATEGORY;
|
||||
|
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* 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.repository;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.dspace.app.rest.model.EntityTypeRest;
|
||||
import org.dspace.app.rest.model.RelationshipTypeRest;
|
||||
import org.dspace.app.rest.projection.Projection;
|
||||
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.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Link repository for "relationships" subresource of an individual EntityType
|
||||
*
|
||||
* @author Mykhaylo Boychuk (mykhaylo.boychuk at 4science.it)
|
||||
*/
|
||||
@Component(EntityTypeRest.CATEGORY + "." + EntityTypeRest.NAME + "." + EntityTypeRest.RELATION_SHIP_TYPES)
|
||||
public class EntityTypeRelationshipLinkRepository extends AbstractDSpaceRestRepository
|
||||
implements LinkRestRepository {
|
||||
|
||||
@Autowired
|
||||
private EntityTypeService entityTypeService;
|
||||
@Autowired
|
||||
private RelationshipTypeService relationshipTypeService;
|
||||
|
||||
/**
|
||||
* 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 request The request object
|
||||
* @param id The ID of the EntityType objects that we'll use to retrieve the RelationshipTypes
|
||||
* @param pageable The pagination object
|
||||
* @param projection
|
||||
* @return List of RelationshipType objects as defined above
|
||||
*/
|
||||
public Page<RelationshipTypeRest> getEntityTypeRelationship(@Nullable HttpServletRequest request,
|
||||
Integer id,
|
||||
@Nullable Pageable optionalPageable,
|
||||
Projection projection) {
|
||||
try {
|
||||
Context context = obtainContext();
|
||||
Pageable pageable = utils.getPageable(optionalPageable);
|
||||
EntityType entityType = entityTypeService.find(context, id);
|
||||
if (Objects.isNull(entityType)) {
|
||||
throw new ResourceNotFoundException("No such EntityType: " + id);
|
||||
}
|
||||
int total = relationshipTypeService.countByEntityType(context, entityType);
|
||||
List<RelationshipType> list = relationshipTypeService.findByEntityType(context, entityType,
|
||||
pageable.getPageSize(), Math.toIntExact(pageable.getOffset()));
|
||||
return converter.toRestPage(list, pageable, total, projection);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user