From ad2bbc44497b8ee3f7098a7b06445408f1016edb Mon Sep 17 00:00:00 2001 From: Marcel Lengl <52546181+LenglBoy@users.noreply.github.com> Date: Wed, 21 Feb 2018 12:39:46 +0100 Subject: [PATCH] TSK-323: REST WB-Resources and key-filtering applied. --- .../pro/taskana/rest/RestApplication.java | 12 ++++ .../taskana/rest/WorkbasketController.java | 67 +++++++++++++------ .../WorkbasketAccessItemResource.java | 62 +++++++++++++++++ .../rest/resource/WorkbasketResource.java | 62 +++++++++++++++++ .../resource/WorkbasketSummaryResource.java | 1 - .../mapper/WorkbasketAccessItemMapper.java | 29 ++++++++ .../resource/mapper/WorkbasketMapper.java | 23 +++++++ 7 files changed, 233 insertions(+), 23 deletions(-) create mode 100644 rest/src/main/java/pro/taskana/rest/resource/WorkbasketAccessItemResource.java create mode 100644 rest/src/main/java/pro/taskana/rest/resource/WorkbasketResource.java create mode 100644 rest/src/main/java/pro/taskana/rest/resource/mapper/WorkbasketAccessItemMapper.java create mode 100644 rest/src/main/java/pro/taskana/rest/resource/mapper/WorkbasketMapper.java diff --git a/rest/src/main/java/pro/taskana/rest/RestApplication.java b/rest/src/main/java/pro/taskana/rest/RestApplication.java index 50938294f..1201be77f 100644 --- a/rest/src/main/java/pro/taskana/rest/RestApplication.java +++ b/rest/src/main/java/pro/taskana/rest/RestApplication.java @@ -24,7 +24,9 @@ import pro.taskana.TaskanaEngine; import pro.taskana.WorkbasketService; import pro.taskana.configuration.TaskanaEngineConfiguration; import pro.taskana.rest.resource.mapper.ClassificationMapper; +import pro.taskana.rest.resource.mapper.WorkbasketAccessItemMapper; import pro.taskana.rest.resource.mapper.WorkbasketDefinitionMapper; +import pro.taskana.rest.resource.mapper.WorkbasketMapper; import pro.taskana.rest.resource.mapper.WorkbasketSummaryMapper; import pro.taskana.sampledata.SampleDataGenerator; @@ -62,6 +64,16 @@ public class RestApplication { return new WorkbasketSummaryMapper(); } + @Bean + public WorkbasketMapper getWorkbasketMapper() { + return new WorkbasketMapper(); + } + + @Bean + public WorkbasketAccessItemMapper getWorkbasketAccessItemMapper() { + return new WorkbasketAccessItemMapper(); + } + @Bean public WorkbasketDefinitionMapper getWorkbasketDefinitionMapper() { return new WorkbasketDefinitionMapper(); diff --git a/rest/src/main/java/pro/taskana/rest/WorkbasketController.java b/rest/src/main/java/pro/taskana/rest/WorkbasketController.java index 17856dbf2..68288e3cb 100644 --- a/rest/src/main/java/pro/taskana/rest/WorkbasketController.java +++ b/rest/src/main/java/pro/taskana/rest/WorkbasketController.java @@ -28,7 +28,11 @@ import pro.taskana.exceptions.NotAuthorizedException; import pro.taskana.exceptions.WorkbasketNotFoundException; import pro.taskana.impl.WorkbasketAuthorization; import pro.taskana.impl.WorkbasketType; +import pro.taskana.rest.resource.WorkbasketAccessItemResource; +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.WorkbasketMapper; import pro.taskana.rest.resource.mapper.WorkbasketSummaryMapper; @RestController @@ -49,12 +53,20 @@ public class WorkbasketController { @Autowired private WorkbasketSummaryMapper workbasketSummaryMapper; + @Autowired + private WorkbasketMapper workbasketMapper; + + @Autowired + private WorkbasketAccessItemMapper workbasketAccessItemMapper; + @RequestMapping(method = RequestMethod.GET) public ResponseEntity> 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, @RequestParam(value = "nameLike", required = false) String nameLike, + @RequestParam(value = "key", required = false) String key, + @RequestParam(value = "keyLike", required = false) String keyLike, @RequestParam(value = "descLike", required = false) String descLike, @RequestParam(value = "owner", required = false) String owner, @RequestParam(value = "ownerLike", required = false) String ownerLike, @@ -65,7 +77,7 @@ public class WorkbasketController { WorkbasketQuery query = workbasketService.createWorkbasketQuery(); try { addSortingToQuery(query, sortBy, order); - addAttributeFilter(query, name, nameLike, descLike, owner, ownerLike, type); + addAttributeFilter(query, name, nameLike, key, keyLike, descLike, owner, ownerLike, type); addAuthorizationFilter(query, requiredPermission); workbasketsSummary = query.list(); } catch (InvalidArgumentException e) { @@ -79,11 +91,11 @@ public class WorkbasketController { .collect(Collectors.toList()), HttpStatus.OK); } - @RequestMapping(value = "/{workbasketid}") - public ResponseEntity getWorkbasket(@PathVariable(value = "workbasketid") String workbasketId) { + @RequestMapping(value = "/{workbasketId}") + public ResponseEntity getWorkbasket(@PathVariable(value = "workbasketId") String workbasketId) { try { Workbasket workbasket = workbasketService.getWorkbasket(workbasketId); - return new ResponseEntity<>(workbasket, HttpStatus.OK); + return new ResponseEntity<>(workbasketMapper.toResource(workbasket), HttpStatus.OK); } catch (WorkbasketNotFoundException e) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } catch (NotAuthorizedException e) { @@ -92,22 +104,23 @@ public class WorkbasketController { } @RequestMapping(method = RequestMethod.POST) - public ResponseEntity createWorkbasket(@RequestBody Workbasket workbasket) { + public ResponseEntity createWorkbasket(@RequestBody Workbasket workbasket) { Workbasket createdWorkbasket; try { createdWorkbasket = workbasketService.createWorkbasket(workbasket); - return new ResponseEntity<>(createdWorkbasket, HttpStatus.CREATED); + return new ResponseEntity<>(workbasketMapper.toResource(createdWorkbasket), HttpStatus.CREATED); } catch (InvalidWorkbasketException e) { return new ResponseEntity<>(HttpStatus.CONFLICT); } } - @RequestMapping(value = "/{workbasketkey}", method = RequestMethod.PUT) - public ResponseEntity updateWorkbasket(@PathVariable(value = "workbasketkey") String workbasketKey, + @RequestMapping(value = "/{workbasketKey}", method = RequestMethod.PUT) + public ResponseEntity updateWorkbasket( + @PathVariable(value = "workbasketKey") String workbasketKey, @RequestBody Workbasket workbasket) { try { Workbasket updatedWorkbasket = workbasketService.updateWorkbasket(workbasket); - return new ResponseEntity<>(updatedWorkbasket, HttpStatus.OK); + return new ResponseEntity<>(workbasketMapper.toResource(updatedWorkbasket), HttpStatus.OK); } catch (InvalidWorkbasketException e) { return new ResponseEntity<>(HttpStatus.CONFLICT); } catch (WorkbasketNotFoundException e) { @@ -117,30 +130,32 @@ public class WorkbasketController { } } - @RequestMapping(value = "/{workbasketkey}/authorizations", method = RequestMethod.GET) - public ResponseEntity> getWorkbasketAuthorizations( - @PathVariable(value = "workbasketkey") String workbasketKey) { + @RequestMapping(value = "/{workbasketKey}/authorizations", method = RequestMethod.GET) + public ResponseEntity> getWorkbasketAuthorizations( + @PathVariable(value = "workbasketKey") String workbasketKey) { List wbAuthorizations = workbasketService.getWorkbasketAuthorizations(workbasketKey); - return new ResponseEntity<>(wbAuthorizations, HttpStatus.OK); + return new ResponseEntity<>(wbAuthorizations.stream() + .map(accItem -> workbasketAccessItemMapper.toResource(accItem)) + .collect(Collectors.toList()), HttpStatus.OK); } @RequestMapping(value = "/authorizations", method = RequestMethod.POST) - public ResponseEntity createWorkbasketAuthorization( + public ResponseEntity createWorkbasketAuthorization( @RequestBody WorkbasketAccessItem workbasketAccessItem) { workbasketAccessItem = workbasketService.createWorkbasketAuthorization(workbasketAccessItem); - return new ResponseEntity<>(workbasketAccessItem, HttpStatus.OK); + return new ResponseEntity<>(workbasketAccessItemMapper.toResource(workbasketAccessItem), HttpStatus.OK); } - @RequestMapping(value = "/authorizations/{authid}", method = RequestMethod.PUT) - public ResponseEntity updateWorkbasketAuthorization( - @PathVariable(value = "authid") String authId, + @RequestMapping(value = "/authorizations/{authId}", method = RequestMethod.PUT) + public ResponseEntity updateWorkbasketAuthorization( + @PathVariable(value = "authId") String authId, @RequestBody WorkbasketAccessItem workbasketAccessItem) throws InvalidArgumentException { workbasketAccessItem = workbasketService.updateWorkbasketAuthorization(workbasketAccessItem); - return new ResponseEntity<>(workbasketAccessItem, HttpStatus.OK); + return new ResponseEntity<>(workbasketAccessItemMapper.toResource(workbasketAccessItem), HttpStatus.OK); } - @RequestMapping(value = "/authorizations/{authid}", method = RequestMethod.DELETE) - public ResponseEntity deleteWorkbasketAuthorization(@PathVariable(value = "authid") String authId) { + @RequestMapping(value = "/authorizations/{authId}", method = RequestMethod.DELETE) + public ResponseEntity deleteWorkbasketAuthorization(@PathVariable(value = "authId") String authId) { workbasketService.deleteWorkbasketAuthorization(authId); return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); } @@ -257,12 +272,17 @@ public class WorkbasketController { private void addAttributeFilter(WorkbasketQuery query, String name, String nameLike, + String key, String keyLike, String descLike, String owner, String ownerLike, String type) throws InvalidArgumentException { if (name != null) query.nameIn(name); if (nameLike != null) query.nameLike(LIKE + nameLike + LIKE); + if (key != null) + query.keyIn(key); + if (keyLike != null) + query.keyLike(LIKE + keyLike + LIKE); if (owner != null) query.ownerIn(owner); if (ownerLike != null) @@ -273,14 +293,17 @@ public class WorkbasketController { switch (type) { case "PERSONAL": query.typeIn(WorkbasketType.PERSONAL); + break; case "GROUP": query.typeIn(WorkbasketType.GROUP); + break; case "CLEARANCE": query.typeIn(WorkbasketType.CLEARANCE); + break; case "TOPIC": query.typeIn(WorkbasketType.TOPIC); + break; } } } - } diff --git a/rest/src/main/java/pro/taskana/rest/resource/WorkbasketAccessItemResource.java b/rest/src/main/java/pro/taskana/rest/resource/WorkbasketAccessItemResource.java new file mode 100644 index 000000000..4f26e9556 --- /dev/null +++ b/rest/src/main/java/pro/taskana/rest/resource/WorkbasketAccessItemResource.java @@ -0,0 +1,62 @@ +package pro.taskana.rest.resource; + +import javax.validation.constraints.NotNull; + +import org.springframework.hateoas.ResourceSupport; + +public class WorkbasketAccessItemResource extends ResourceSupport { + + public String id; + + @NotNull + public String workbasketKey; + + @NotNull + public String accessId; + + public boolean permRead; + public boolean permOpen; + public boolean permAppend; + public boolean permTransfer; + public boolean permDistribute; + public boolean permCustom1; + public boolean permCustom2; + public boolean permCustom3; + public boolean permCustom4; + public boolean permCustom5; + public boolean permCustom6; + public boolean permCustom7; + public boolean permCustom8; + public boolean permCustom9; + public boolean permCustom10; + public boolean permCustom11; + public boolean permCustom12; + + public WorkbasketAccessItemResource(String id, String workbasketKey, String accessId, boolean permRead, + boolean permOpen, boolean permAppend, boolean permTransfer, boolean permDistribute, boolean permCustom1, + boolean permCustom2, boolean permCustom3, boolean permCustom4, boolean permCustom5, boolean permCustom6, + boolean permCustom7, boolean permCustom8, boolean permCustom9, boolean permCustom10, boolean permCustom11, + boolean permCustom12) { + super(); + this.id = id; + this.workbasketKey = workbasketKey; + this.accessId = accessId; + this.permRead = permRead; + this.permOpen = permOpen; + this.permAppend = permAppend; + this.permTransfer = permTransfer; + this.permDistribute = permDistribute; + this.permCustom1 = permCustom1; + this.permCustom2 = permCustom2; + this.permCustom3 = permCustom3; + this.permCustom4 = permCustom4; + this.permCustom5 = permCustom5; + this.permCustom6 = permCustom6; + this.permCustom7 = permCustom7; + this.permCustom8 = permCustom8; + this.permCustom9 = permCustom9; + this.permCustom10 = permCustom10; + this.permCustom11 = permCustom11; + this.permCustom12 = permCustom12; + } +} diff --git a/rest/src/main/java/pro/taskana/rest/resource/WorkbasketResource.java b/rest/src/main/java/pro/taskana/rest/resource/WorkbasketResource.java new file mode 100644 index 000000000..a042825fe --- /dev/null +++ b/rest/src/main/java/pro/taskana/rest/resource/WorkbasketResource.java @@ -0,0 +1,62 @@ +package pro.taskana.rest.resource; + +import java.time.Instant; + +import javax.validation.constraints.NotNull; + +import org.springframework.hateoas.ResourceSupport; + +import pro.taskana.impl.WorkbasketType; + +public class WorkbasketResource extends ResourceSupport { + + public String id; + + @NotNull + public String key; + + @NotNull + public String name; + + @NotNull + public String domain; + + @NotNull + public WorkbasketType type; + + public Instant created; + public Instant modified; + public String description; + public String owner; + public String custom1; + public String custom2; + public String custom3; + public String custom4; + public String orgLevel1; + public String orgLevel2; + public String orgLevel3; + public String orgLevel4; + + public WorkbasketResource(String id, String key, String name, String domain, WorkbasketType type, Instant created, + Instant modified, String description, String owner, String custom1, String custom2, String custom3, + String custom4, String orgLevel1, String orgLevel2, String orgLevel3, String orgLevel4) { + super(); + this.id = id; + this.key = key; + this.name = name; + this.domain = domain; + this.type = type; + this.created = created; + this.modified = modified; + this.description = description; + this.owner = owner; + this.custom1 = custom1; + this.custom2 = custom2; + this.custom3 = custom3; + this.custom4 = custom4; + this.orgLevel1 = orgLevel1; + this.orgLevel2 = orgLevel2; + this.orgLevel3 = orgLevel3; + this.orgLevel4 = orgLevel4; + } +} diff --git a/rest/src/main/java/pro/taskana/rest/resource/WorkbasketSummaryResource.java b/rest/src/main/java/pro/taskana/rest/resource/WorkbasketSummaryResource.java index cb3d08be3..e195550db 100644 --- a/rest/src/main/java/pro/taskana/rest/resource/WorkbasketSummaryResource.java +++ b/rest/src/main/java/pro/taskana/rest/resource/WorkbasketSummaryResource.java @@ -44,5 +44,4 @@ public class WorkbasketSummaryResource extends ResourceSupport { this.orgLevel3 = orgLevel3; this.orgLevel4 = orgLevel4; } - } diff --git a/rest/src/main/java/pro/taskana/rest/resource/mapper/WorkbasketAccessItemMapper.java b/rest/src/main/java/pro/taskana/rest/resource/mapper/WorkbasketAccessItemMapper.java new file mode 100644 index 000000000..5c6a7516a --- /dev/null +++ b/rest/src/main/java/pro/taskana/rest/resource/mapper/WorkbasketAccessItemMapper.java @@ -0,0 +1,29 @@ +package pro.taskana.rest.resource.mapper; + +import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; +import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn; + +import pro.taskana.WorkbasketAccessItem; +import pro.taskana.rest.WorkbasketController; +import pro.taskana.rest.resource.WorkbasketAccessItemResource; + +public class WorkbasketAccessItemMapper { + + public WorkbasketAccessItemResource toResource(WorkbasketAccessItem wbAccItem) { + WorkbasketAccessItemResource resource = new WorkbasketAccessItemResource(wbAccItem.getId(), + wbAccItem.getWorkbasketKey(), + wbAccItem.getAccessId(), wbAccItem.isPermRead(), wbAccItem.isPermOpen(), wbAccItem.isPermAppend(), + wbAccItem.isPermTransfer(), + wbAccItem.isPermDistribute(), wbAccItem.isPermCustom1(), wbAccItem.isPermCustom2(), + wbAccItem.isPermCustom3(), wbAccItem.isPermCustom4(), + wbAccItem.isPermCustom5(), wbAccItem.isPermCustom6(), wbAccItem.isPermCustom7(), wbAccItem.isPermCustom8(), + wbAccItem.isPermCustom9(), + wbAccItem.isPermCustom10(), wbAccItem.isPermCustom11(), wbAccItem.isPermCustom12()); + + // Add self-decription link to hateoas + resource.add( + linkTo(methodOn(WorkbasketController.class).getWorkbasketAuthorizations(wbAccItem.getWorkbasketKey())) + .withSelfRel()); + return resource; + } +} diff --git a/rest/src/main/java/pro/taskana/rest/resource/mapper/WorkbasketMapper.java b/rest/src/main/java/pro/taskana/rest/resource/mapper/WorkbasketMapper.java new file mode 100644 index 000000000..38fbe0fb0 --- /dev/null +++ b/rest/src/main/java/pro/taskana/rest/resource/mapper/WorkbasketMapper.java @@ -0,0 +1,23 @@ +package pro.taskana.rest.resource.mapper; + +import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; +import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn; + +import pro.taskana.Workbasket; +import pro.taskana.rest.WorkbasketController; +import pro.taskana.rest.resource.WorkbasketResource; + +public class WorkbasketMapper { + + public WorkbasketResource toResource(Workbasket wb) { + WorkbasketResource resource = new WorkbasketResource(wb.getId(), wb.getKey(), wb.getName(), wb.getDomain(), + wb.getType(), wb.getCreated(), + wb.getModified(), wb.getDescription(), wb.getOwner(), wb.getCustom1(), wb.getCustom2(), wb.getCustom3(), + wb.getCustom4(), + wb.getOrgLevel1(), wb.getOrgLevel2(), wb.getOrgLevel3(), wb.getOrgLevel4()); + + // Add self-decription link to hateoas + resource.add(linkTo(methodOn(WorkbasketController.class).getWorkbasket(wb.getId())).withSelfRel()); + return resource; + } +}