TSK-373: added HAL format to workbasket list.

This commit is contained in:
Holger Hagen 2018-03-07 11:55:56 +01:00 committed by Martin Rojas Miguel Angel
parent e323ea3e6e
commit c958e8f1d4
6 changed files with 167 additions and 26 deletions

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pro.taskana</groupId>
@ -37,7 +38,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
@ -161,6 +162,11 @@
<artifactId>spring-hateoas</artifactId>
<version>0.24.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.plugin</groupId>
<artifactId>spring-plugin-core</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>

View File

@ -6,6 +6,8 @@ import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.config.EnableHypermediaSupport;
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
@ -35,9 +37,11 @@ import pro.taskana.exceptions.NotAuthorizedException;
import pro.taskana.exceptions.WorkbasketInUseException;
import pro.taskana.exceptions.WorkbasketNotFoundException;
import pro.taskana.rest.resource.WorkbasketAccessItemResource;
import pro.taskana.rest.resource.WorkbasketListResource;
import pro.taskana.rest.resource.WorkbasketResource;
import pro.taskana.rest.resource.WorkbasketSummaryResource;
import pro.taskana.rest.resource.mapper.WorkbasketAccessItemMapper;
import pro.taskana.rest.resource.mapper.WorkbasketListMapper;
import pro.taskana.rest.resource.mapper.WorkbasketMapper;
import pro.taskana.rest.resource.mapper.WorkbasketSummaryMapper;
@ -45,6 +49,7 @@ import pro.taskana.rest.resource.mapper.WorkbasketSummaryMapper;
* Controller for all {@link Workbasket} related endpoints.
*/
@RestController
@EnableHypermediaSupport(type = HypermediaType.HAL)
@RequestMapping(path = "/v1/workbaskets", produces = {MediaType.APPLICATION_JSON_VALUE})
public class WorkbasketController {
@ -65,12 +70,15 @@ public class WorkbasketController {
@Autowired
private WorkbasketMapper workbasketMapper;
@Autowired
private WorkbasketListMapper workbasketListMapper;
@Autowired
private WorkbasketAccessItemMapper workbasketAccessItemMapper;
@GetMapping
@Transactional(readOnly = true, rollbackFor = Exception.class)
public ResponseEntity<List<WorkbasketSummaryResource>> getWorkbaskets(
public ResponseEntity<WorkbasketListResource> getWorkbaskets(
@RequestParam(value = "sortBy", defaultValue = "name", required = false) String sortBy,
@RequestParam(value = "order", defaultValue = "asc", required = false) String order,
@RequestParam(value = "name", required = false) String name,
@ -83,15 +91,13 @@ public class WorkbasketController {
@RequestParam(value = "type", required = false) String type,
@RequestParam(value = "requiredPermission", required = false) String requiredPermission) {
try {
List<WorkbasketSummary> workbasketsSummary;
WorkbasketQuery query = workbasketService.createWorkbasketQuery();
addSortingToQuery(query, sortBy, order);
addAttributeFilter(query, name, nameLike, key, keyLike, descLike, owner, ownerLike, type);
addAuthorizationFilter(query, requiredPermission);
workbasketsSummary = query.list();
return new ResponseEntity<>(workbasketsSummary.stream()
.map(workbasket -> workbasketSummaryMapper.toResource(workbasket))
.collect(Collectors.toList()), HttpStatus.OK);
List<WorkbasketSummary> workbasketSummaries = query.list();
WorkbasketListResource workbasketListResource = workbasketListMapper.toResource(workbasketSummaries);
return new ResponseEntity<>(workbasketListResource, HttpStatus.OK);
} catch (InvalidArgumentException ex) {
return new ResponseEntity<>(HttpStatus.PRECONDITION_FAILED);
}

View File

@ -0,0 +1,62 @@
package pro.taskana.rest.resource;
import org.springframework.hateoas.ResourceSupport;
/**
* Base class for page list resources.
*/
public class AbstractPageableListResource extends ResourceSupport {
private final int pageNumber;
private final int pageSize;
private final int totalPages;
private final long totalElements;
/**
* Creates new instance of {@link AbstractPageableListResource}.
*
* @param pageNumber
* actual page of the collection.
* @param pageSize
* number of elements per pages of the collection.
* @param totalPages
* total number of pages of the collection.
* @param totalElements
* total number of elements of the collection.
*/
public AbstractPageableListResource(int pageNumber, int pageSize, int totalPages, long totalElements) {
this.pageNumber = pageNumber;
this.pageSize = pageSize;
this.totalPages = totalPages;
this.totalElements = totalElements;
}
/**
* Default constructor for a list without paging.
*/
public AbstractPageableListResource() {
this.pageNumber = -1;
this.pageSize = -1;
this.totalPages = -1;
this.totalElements = -1;
}
public int getPageNumber() {
return pageNumber;
}
public int getPageSize() {
return pageSize;
}
public int getTotalPages() {
return totalPages;
}
public long getTotalElements() {
return totalElements;
}
}

View File

@ -0,0 +1,47 @@
package pro.taskana.rest.resource;
import java.util.Collection;
/**
* Resource for workbasket lists.
*/
public class WorkbasketListResource extends AbstractPageableListResource {
private final Collection<WorkbasketSummaryResource> workbasketSummaries;
/**
* Create a paged list of workbaskets.
*
* @param workbasketSummaries
* the workbasket summaries
* @param pageNumber
* the current page number
* @param pageSize
* the size of the pages
* @param totalPages
* the total number of pages
* @param totalElements
* the total number of elements
*/
public WorkbasketListResource(Collection<WorkbasketSummaryResource> workbasketSummaries, int pageNumber,
int pageSize,
int totalPages, long totalElements) {
super(pageNumber, pageSize, totalPages, totalElements);
this.workbasketSummaries = workbasketSummaries;
}
/**
* Create a unpaged list of workbaskets.
*
* @param workbasketSummaries
* the workbasket summaries
*/
public WorkbasketListResource(Collection<WorkbasketSummaryResource> workbasketSummaries) {
this.workbasketSummaries = workbasketSummaries;
}
public Collection<WorkbasketSummaryResource> getWorkbasketSummaries() {
return workbasketSummaries;
}
}

View File

@ -3,8 +3,6 @@ package pro.taskana.rest.resource.mapper;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;
import java.util.Collections;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -46,22 +44,10 @@ public class WorkbasketAccessItemMapper {
private WorkbasketAccessItemResource addLinks(WorkbasketAccessItemResource resource, WorkbasketAccessItem wbAccItem)
throws NotAuthorizedException {
resource.add(
linkTo(methodOn(WorkbasketController.class).getWorkbasketAccessItems(wbAccItem.getWorkbasketId()))
.withRel("getWorkbasketAccessItems"));
resource.add(
linkTo(methodOn(WorkbasketController.class).createWorkbasketAccessItem(resource))
.withRel("createWorkbasketAccessItem"));
resource.add(
linkTo(methodOn(WorkbasketController.class).updateWorkbasketAccessItem(wbAccItem.getId(), resource))
.withRel("updateWorkbasketAccessItem"));
resource.add(
linkTo(methodOn(WorkbasketController.class).setWorkbasketAccessItems(wbAccItem.getWorkbasketId(),
Collections.singletonList(resource)))
.withRel("setWorkbasketAccessItems"));
resource.add(
linkTo(methodOn(WorkbasketController.class).deleteWorkbasketAccessItem(wbAccItem.getId()))
.withRel("deleteWorkbasketAccessItem"));
linkTo(methodOn(WorkbasketController.class).getWorkbasket(wbAccItem.getWorkbasketId()))
.withRel("workbasket"));
return resource;
}
}

View File

@ -0,0 +1,34 @@
package pro.taskana.rest.resource.mapper;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import java.util.Collection;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import pro.taskana.WorkbasketSummary;
import pro.taskana.rest.WorkbasketController;
import pro.taskana.rest.resource.WorkbasketListResource;
/**
* Mapper to convert from {@link Collection WorkbasketSummary} to {@link WorkbasketListResource}.
*/
@Component
public class WorkbasketListMapper {
@Autowired
private WorkbasketSummaryMapper workbasketSummaryMapper;
public WorkbasketListResource toResource(Collection<WorkbasketSummary> workbasketSummaries) {
WorkbasketListResource workbasketListResource = new WorkbasketListResource(workbasketSummaries.stream()
.map(workbasket -> workbasketSummaryMapper.toResource(workbasket))
.collect(Collectors.toList()));
workbasketListResource.add(linkTo(WorkbasketController.class).withSelfRel());
return workbasketListResource;
}
}