From 3184ab3389e14d20a5a26d11fb6f0b504b45935f Mon Sep 17 00:00:00 2001 From: "julius.lautz" Date: Mon, 1 Nov 2021 22:07:21 +0100 Subject: [PATCH 1/2] cleaned up task list + started implementation of deleteTask --- tapas-tasks/pom.xml | 6 ++ .../in/formats/TaskJsonRepresentation.java | 2 +- .../in/web/CompleteTaskWebController.java | 12 ++- .../in/web/DeleteTaskWebController.java | 15 ++-- .../in/web/TaskAssignedWebController.java | 12 ++- .../tasks/adapter/in/web/TaskMediaType.java | 23 ------ .../out/web/CanTaskBeDeletedWebAdapter.java | 59 +++++++++++++++ .../PublishNewTaskAddedEventWebAdapter.java | 2 +- .../port/in/DeleteTaskCommand.java | 7 +- .../port/out/CanTaskBeDeletedPort.java | 7 ++ .../service/CompleteTaskService.java | 6 +- .../service/DeleteTaskService.java | 11 ++- .../service/TaskAssignedService.java | 2 +- .../tasks/domain/DeleteTaskEvent.java | 11 +++ .../unisg/tapastasks/tasks/domain/Task.java | 35 ++++----- .../tapastasks/tasks/domain/TaskList.java | 75 ++++++++++--------- 16 files changed, 178 insertions(+), 107 deletions(-) delete mode 100644 tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/TaskMediaType.java create mode 100644 tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/out/web/CanTaskBeDeletedWebAdapter.java create mode 100644 tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/out/CanTaskBeDeletedPort.java create mode 100644 tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/domain/DeleteTaskEvent.java diff --git a/tapas-tasks/pom.xml b/tapas-tasks/pom.xml index 0118cf9..a815cef 100644 --- a/tapas-tasks/pom.xml +++ b/tapas-tasks/pom.xml @@ -75,6 +75,12 @@ org.eclipse.paho.client.mqttv3 1.2.0 + + com.vaadin.external.google + android-json + 0.0.20131108.vaadin1 + compile + diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/formats/TaskJsonRepresentation.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/formats/TaskJsonRepresentation.java index eb89415..ff8158a 100644 --- a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/formats/TaskJsonRepresentation.java +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/formats/TaskJsonRepresentation.java @@ -88,7 +88,7 @@ final public class TaskJsonRepresentation { this.taskId = task.getTaskId().getValue(); this.taskStatus = task.getTaskStatus().getValue().name(); - this.originalTaskUri = (task.getOriginalTaskUri() == null) ? + this.originalTaskUri = (task. getOriginalTaskUri() == null) ? null : task.getOriginalTaskUri().getValue(); this.serviceProvider = (task.getProvider() == null) ? null : task.getProvider().getValue(); diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/CompleteTaskWebController.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/CompleteTaskWebController.java index 536b72c..ec2b7b0 100644 --- a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/CompleteTaskWebController.java +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/CompleteTaskWebController.java @@ -1,8 +1,10 @@ package ch.unisg.tapastasks.tasks.adapter.in.web; +import ch.unisg.tapastasks.tasks.adapter.in.formats.TaskJsonRepresentation; import ch.unisg.tapastasks.tasks.application.port.in.CompleteTaskCommand; import ch.unisg.tapastasks.tasks.application.port.in.CompleteTaskUseCase; import ch.unisg.tapastasks.tasks.domain.Task; +import com.fasterxml.jackson.core.JsonProcessingException; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -21,7 +23,7 @@ public class CompleteTaskWebController { this.completeTaskUseCase = completeTaskUseCase; } - @PostMapping(path = "/tasks/completeTask", consumes = {TaskMediaType.TASK_MEDIA_TYPE}) + @PostMapping(path = "/tasks/completeTask", consumes = {TaskJsonRepresentation.MEDIA_TYPE}) public ResponseEntity completeTask (@RequestBody Task task){ try { @@ -32,10 +34,12 @@ public class CompleteTaskWebController { Task updateATask = completeTaskUseCase.completeTask(command); HttpHeaders responseHeaders = new HttpHeaders(); - responseHeaders.add(HttpHeaders.CONTENT_TYPE, TaskMediaType.TASK_MEDIA_TYPE); + responseHeaders.add(HttpHeaders.CONTENT_TYPE, TaskJsonRepresentation.MEDIA_TYPE); - return new ResponseEntity<>(TaskMediaType.serialize(updateATask), responseHeaders, HttpStatus.ACCEPTED); - } catch(ConstraintViolationException e){ + return new ResponseEntity<>(TaskJsonRepresentation.serialize(updateATask), responseHeaders, HttpStatus.ACCEPTED); + } catch (JsonProcessingException e) { + throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage()); + } catch (ConstraintViolationException e) { throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); } } diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/DeleteTaskWebController.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/DeleteTaskWebController.java index af721d1..ef79e6a 100644 --- a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/DeleteTaskWebController.java +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/DeleteTaskWebController.java @@ -1,9 +1,11 @@ package ch.unisg.tapastasks.tasks.adapter.in.web; +import ch.unisg.tapastasks.tasks.adapter.in.formats.TaskJsonRepresentation; import ch.unisg.tapastasks.tasks.application.port.in.DeleteTaskCommand; import ch.unisg.tapastasks.tasks.application.port.in.DeleteTaskUseCase; import ch.unisg.tapastasks.tasks.domain.Task; +import com.fasterxml.jackson.core.JsonProcessingException; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -23,26 +25,27 @@ public class DeleteTaskWebController { this.deleteClassUseCase = deleteClassUseCase; } - @PostMapping(path="/tasks/deleteTask", consumes = {TaskMediaType.TASK_MEDIA_TYPE}) + @PostMapping(path="/tasks/deleteTask", consumes = {TaskJsonRepresentation.MEDIA_TYPE}) public ResponseEntity deleteTask (@RequestBody Task task){ try { - DeleteTaskCommand command = new DeleteTaskCommand(task.getTaskId()); + DeleteTaskCommand command = new DeleteTaskCommand(task.getTaskId(), task.getOriginalTaskUri()); Optional deleteATask = deleteClassUseCase.deleteTask(command); // Check if the task with the given identifier exists if (deleteATask.isEmpty()) { - // If not, through a 404 Not Found status code throw new ResponseStatusException(HttpStatus.NOT_FOUND); } HttpHeaders responseHeaders = new HttpHeaders(); - responseHeaders.add(HttpHeaders.CONTENT_TYPE, TaskMediaType.TASK_MEDIA_TYPE); + responseHeaders.add(HttpHeaders.CONTENT_TYPE, TaskJsonRepresentation.MEDIA_TYPE); - return new ResponseEntity<>(TaskMediaType.serialize(deleteATask.get()), responseHeaders, HttpStatus.ACCEPTED); - } catch(ConstraintViolationException e){ + return new ResponseEntity<>(TaskJsonRepresentation.serialize(deleteATask.get()), responseHeaders, HttpStatus.ACCEPTED); + } catch (JsonProcessingException e) { + throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage()); + } catch (ConstraintViolationException e) { throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); } } diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/TaskAssignedWebController.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/TaskAssignedWebController.java index 9dfa6a2..b58d159 100644 --- a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/TaskAssignedWebController.java +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/TaskAssignedWebController.java @@ -1,8 +1,10 @@ package ch.unisg.tapastasks.tasks.adapter.in.web; +import ch.unisg.tapastasks.tasks.adapter.in.formats.TaskJsonRepresentation; import ch.unisg.tapastasks.tasks.application.port.in.TaskAssignedCommand; import ch.unisg.tapastasks.tasks.application.port.in.TaskAssignedUseCase; import ch.unisg.tapastasks.tasks.domain.Task; +import com.fasterxml.jackson.core.JsonProcessingException; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -21,7 +23,7 @@ public class TaskAssignedWebController { this.taskAssignedUseCase = taskAssignedUseCase; } - @PostMapping(path="/tasks/assignTask", consumes= {TaskMediaType.TASK_MEDIA_TYPE}) + @PostMapping(path="/tasks/assignTask", consumes= {TaskJsonRepresentation.MEDIA_TYPE}) public ResponseEntity assignTask(@RequestBody Task task){ try{ TaskAssignedCommand command = new TaskAssignedCommand( @@ -31,10 +33,12 @@ public class TaskAssignedWebController { Task updateATask = taskAssignedUseCase.assignTask(command); HttpHeaders responseHeaders = new HttpHeaders(); - responseHeaders.add(HttpHeaders.CONTENT_TYPE, TaskMediaType.TASK_MEDIA_TYPE); + responseHeaders.add(HttpHeaders.CONTENT_TYPE, TaskJsonRepresentation.MEDIA_TYPE); - return new ResponseEntity<>(TaskMediaType.serialize(updateATask), responseHeaders, HttpStatus.ACCEPTED); - } catch (ConstraintViolationException e){ + return new ResponseEntity<>(TaskJsonRepresentation.serialize(updateATask), responseHeaders, HttpStatus.ACCEPTED); + } catch (JsonProcessingException e) { + throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage()); + } catch (ConstraintViolationException e) { throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); } } diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/TaskMediaType.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/TaskMediaType.java deleted file mode 100644 index d9a0a46..0000000 --- a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/TaskMediaType.java +++ /dev/null @@ -1,23 +0,0 @@ -package ch.unisg.tapastasks.tasks.adapter.in.web; - -import ch.unisg.tapastasks.tasks.domain.Task; -import ch.unisg.tapastasks.tasks.domain.TaskList; -import org.json.JSONObject; - -final public class TaskMediaType { - public static final String TASK_MEDIA_TYPE = "application/json"; - - public static String serialize(Task task) { - JSONObject payload = new JSONObject(); - - payload.put("taskId", task.getTaskId().getValue()); - payload.put("taskName", task.getTaskName().getValue()); - payload.put("taskType", task.getTaskType().getValue()); - payload.put("taskState", task.getTaskState().getValue()); - payload.put("taskListName", TaskList.getTapasTaskList().getTaskListName().getValue()); - payload.put("taskResult", task.getTaskResult().getValue()); - return payload.toString(); - } - - private TaskMediaType() { } -} diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/out/web/CanTaskBeDeletedWebAdapter.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/out/web/CanTaskBeDeletedWebAdapter.java new file mode 100644 index 0000000..5061e3d --- /dev/null +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/out/web/CanTaskBeDeletedWebAdapter.java @@ -0,0 +1,59 @@ +package ch.unisg.tapastasks.tasks.adapter.out.web; + + +import ch.unisg.tapastasks.tasks.application.port.out.CanTaskBeDeletedPort; +import ch.unisg.tapastasks.tasks.domain.DeleteTaskEvent; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.context.annotation.Primary; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.util.HashMap; + +@Component +@Primary +public class CanTaskBeDeletedWebAdapter implements CanTaskBeDeletedPort { + + // Base URI of the service interested in this event + //Todo: Add the right IP address + String server = null; + + @Override + public void canTaskBeDeletedEvent(DeleteTaskEvent event){ + + var values = new HashMap<> () {{ + put("taskId", event.taskId); + put("taskUri", event.taskUri); + }}; + + var objectMapper = new ObjectMapper(); + String requestBody = null; + try { + requestBody = objectMapper.writeValueAsString(values); + } catch (JsonProcessingException e){ + e.printStackTrace(); + } + + //Todo: Question: How do we include the URI from the DeleteTaskEvent? Do we even need it? + HttpClient client = HttpClient.newHttpClient(); + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(server+"task")) + .header("Content-Type", "application/task+json") + .POST(HttpRequest.BodyPublishers.ofString(requestBody)) + .build(); + + //Todo: The following parameters probably need to be changed to get the right error code + try { + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + } catch (IOException e){ + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } +} diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/out/web/PublishNewTaskAddedEventWebAdapter.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/out/web/PublishNewTaskAddedEventWebAdapter.java index d642eca..569b1e9 100644 --- a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/out/web/PublishNewTaskAddedEventWebAdapter.java +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/out/web/PublishNewTaskAddedEventWebAdapter.java @@ -42,7 +42,7 @@ public class PublishNewTaskAddedEventWebAdapter implements NewTaskAddedEventPort HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(server+"/task")) - .header("Content-Type", "application/json") + .header("Content-Type", "application/task+json") .POST(HttpRequest.BodyPublishers.ofString(requestBody)) .build(); diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/in/DeleteTaskCommand.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/in/DeleteTaskCommand.java index 24acbb8..b57c719 100644 --- a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/in/DeleteTaskCommand.java +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/in/DeleteTaskCommand.java @@ -2,6 +2,7 @@ package ch.unisg.tapastasks.tasks.application.port.in; import ch.unisg.tapastasks.common.SelfValidating; import ch.unisg.tapastasks.tasks.domain.Task.TaskId; +import ch.unisg.tapastasks.tasks.domain.Task.OriginalTaskUri; import lombok.Value; import javax.validation.constraints.NotNull; @@ -11,8 +12,12 @@ public class DeleteTaskCommand extends SelfValidating { @NotNull private final TaskId taskId; - public DeleteTaskCommand(TaskId taskId){ + @NotNull + private final OriginalTaskUri taskUri; + + public DeleteTaskCommand(TaskId taskId, OriginalTaskUri taskUri){ this.taskId=taskId; + this.taskUri = taskUri; this.validateSelf(); } } diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/out/CanTaskBeDeletedPort.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/out/CanTaskBeDeletedPort.java new file mode 100644 index 0000000..67bde16 --- /dev/null +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/out/CanTaskBeDeletedPort.java @@ -0,0 +1,7 @@ +package ch.unisg.tapastasks.tasks.application.port.out; + +import ch.unisg.tapastasks.tasks.domain.DeleteTaskEvent; + +public interface CanTaskBeDeletedPort { + void canTaskBeDeletedEvent(DeleteTaskEvent event); +} diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/CompleteTaskService.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/CompleteTaskService.java index bade832..0e7f817 100644 --- a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/CompleteTaskService.java +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/CompleteTaskService.java @@ -19,17 +19,13 @@ public class CompleteTaskService implements CompleteTaskUseCase { @Override public Task completeTask(CompleteTaskCommand command){ - // TODO Retrieve the task based on ID TaskList taskList = TaskList.getTapasTaskList(); Optional updatedTask = taskList.retrieveTaskById(command.getTaskId()); - // TODO Update the status and result (and save?) Task newTask = updatedTask.get(); newTask.taskResult = new TaskResult(command.getTaskResult().getValue()); - newTask.taskState = new TaskState(Task.State.EXECUTED); + newTask.taskStatus = new TaskStatus(Task.Status.EXECUTED); - - // TODO return the updated task return newTask; } } diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/DeleteTaskService.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/DeleteTaskService.java index cfebcd6..f865f4c 100644 --- a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/DeleteTaskService.java +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/DeleteTaskService.java @@ -19,10 +19,15 @@ public class DeleteTaskService implements DeleteTaskUseCase { @Override public Optional deleteTask(DeleteTaskCommand command){ - // TODO check with assignment service if we can delte - TaskList taskList = TaskList.getTapasTaskList(); - return taskList.deleteTaskById(command.getTaskId()); + Optional updatedTask = taskList.retrieveTaskById(command.getTaskId()); + Task newTask = updatedTask.get(); + // TODO: Fill in the right condition into the if-statement and the else-statement + if (/*the task can be deleted*/){ + return taskList.deleteTaskById(command.getTaskId()); + } else { + /*send message back to TaskList that the task cannot be deleted*/ + } } } diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/TaskAssignedService.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/TaskAssignedService.java index baa6059..6c580e4 100644 --- a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/TaskAssignedService.java +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/TaskAssignedService.java @@ -24,7 +24,7 @@ public class TaskAssignedService implements TaskAssignedUseCase { // update the status to assigned Task updatedTask = task.get(); - updatedTask.taskState = new TaskState(State.ASSIGNED); + updatedTask.taskStatus = new TaskStatus(Status.ASSIGNED); return updatedTask; } diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/domain/DeleteTaskEvent.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/domain/DeleteTaskEvent.java new file mode 100644 index 0000000..16e803b --- /dev/null +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/domain/DeleteTaskEvent.java @@ -0,0 +1,11 @@ +package ch.unisg.tapastasks.tasks.domain; + +public class DeleteTaskEvent { + public String taskId; + public String taskUri; + + public DeleteTaskEvent(String taskId, String taskUri){ + this.taskId = taskId; + this.taskUri = taskUri; + } +} diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/domain/Task.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/domain/Task.java index ebe9d1c..4444beb 100644 --- a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/domain/Task.java +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/domain/Task.java @@ -21,40 +21,33 @@ public class Task { @Getter private final TaskType taskType; - @Getter - public TaskState taskState; // had to make public for CompleteTaskService + @Getter @Setter + public TaskStatus taskStatus; // had to make public for CompleteTaskService @Getter public TaskResult taskResult; // same as above - // private final OriginalTaskUri originalTaskUri; + @Getter + private final OriginalTaskUri originalTaskUri; - // @Getter @Setter - // private TaskStatus taskStatus; + @Getter @Setter + private ServiceProvider provider; - // @Getter @Setter - // private ServiceProvider provider; + @Getter @Setter + private InputData inputData; - // @Getter @Setter - // private InputData inputData; - - // @Getter @Setter - // private OutputData outputData; + @Getter @Setter + private OutputData outputData; public Task(TaskName taskName, TaskType taskType, OriginalTaskUri taskUri) { - this.taskId = new TaskId(UUID.randomUUID().toString()); - this.taskName = taskName; this.taskType = taskType; - this.taskState = new TaskState(State.OPEN); + this.taskStatus = new TaskStatus(Status.OPEN); this.taskId = new TaskId(UUID.randomUUID().toString()); this.taskResult = new TaskResult(""); - // this.originalTaskUri = taskUri; - - // this.taskStatus = new TaskStatus(Status.OPEN); - - // this.inputData = null; - // this.outputData = null; + this.originalTaskUri = taskUri; + this.inputData = null; + this.outputData = null; } protected static Task createTaskWithNameAndType(TaskName name, TaskType type) { diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/domain/TaskList.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/domain/TaskList.java index 7a4e70f..e07bcd8 100644 --- a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/domain/TaskList.java +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/domain/TaskList.java @@ -67,50 +67,51 @@ public class TaskList { } public Optional deleteTaskById(Task.TaskId id) { - for (Task task: listOfTasks.value){ - if(task.getTaskId().getValue().equalsIgnoreCase(id.getValue())){ + for (Task task : listOfTasks.value) { + if (task.getTaskId().getValue().equalsIgnoreCase(id.getValue())) { listOfTasks.value.remove(task); return Optional.of(task); } } return Optional.empty(); - // public Task changeTaskStatusToAssigned(Task.TaskId id, Optional serviceProvider) - // throws TaskNotFoundException { - // return changeTaskStatus(id, new Task.TaskStatus(Task.Status.ASSIGNED), serviceProvider, Optional.empty()); - // } - - // public Task changeTaskStatusToRunning(Task.TaskId id, Optional serviceProvider) - // throws TaskNotFoundException { - // return changeTaskStatus(id, new Task.TaskStatus(Task.Status.RUNNING), serviceProvider, Optional.empty()); - // } - - // public Task changeTaskStatusToExecuted(Task.TaskId id, Optional serviceProvider, - // Optional outputData) throws TaskNotFoundException { - // return changeTaskStatus(id, new Task.TaskStatus(Task.Status.EXECUTED), serviceProvider, outputData); - // } - - // private Task changeTaskStatus(Task.TaskId id, Task.TaskStatus status, Optional serviceProvider, - // Optional outputData) { - // Optional taskOpt = retrieveTaskById(id); - - // if (taskOpt.isEmpty()) { - // throw new TaskNotFoundException(); - // } - - // Task task = taskOpt.get(); - // task.setTaskStatus(status); - - // if (serviceProvider.isPresent()) { - // task.setProvider(serviceProvider.get()); - // } - - // if (outputData.isPresent()) { - // task.setOutputData(outputData.get()); - // } - - // return task; } + public Task changeTaskStatusToAssigned(Task.TaskId id, Optional serviceProvider) + throws TaskNotFoundException { + return changeTaskStatus(id, new Task.TaskStatus(Task.Status.ASSIGNED), serviceProvider, Optional.empty()); + } + + public Task changeTaskStatusToRunning(Task.TaskId id, Optional serviceProvider) + throws TaskNotFoundException { + return changeTaskStatus(id, new Task.TaskStatus(Task.Status.RUNNING), serviceProvider, Optional.empty()); + } + + public Task changeTaskStatusToExecuted(Task.TaskId id, Optional serviceProvider, + Optional outputData) throws TaskNotFoundException { + return changeTaskStatus(id, new Task.TaskStatus(Task.Status.EXECUTED), serviceProvider, outputData); + } + + private Task changeTaskStatus(Task.TaskId id, Task.TaskStatus status, Optional serviceProvider, + Optional outputData) { + Optional taskOpt = retrieveTaskById(id); + + if (taskOpt.isEmpty()) { + throw new TaskNotFoundException(); + } + + Task task = taskOpt.get(); + task.setTaskStatus(status); + + if (serviceProvider.isPresent()) { + task.setProvider(serviceProvider.get()); + } + + if (outputData.isPresent()) { + task.setOutputData(outputData.get()); + } + + return task; + } @Value public static class TaskListName { -- 2.45.1 From 7af2b6df66cf177e75c441fbf0abaaae6ad10b10 Mon Sep 17 00:00:00 2001 From: "julius.lautz" Date: Mon, 1 Nov 2021 22:18:00 +0100 Subject: [PATCH 2/2] cleaned up task list + started implementation of deleteTask --- .../tasks/adapter/in/formats/TaskJsonRepresentation.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/formats/TaskJsonRepresentation.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/formats/TaskJsonRepresentation.java index ff8158a..eb89415 100644 --- a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/formats/TaskJsonRepresentation.java +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/formats/TaskJsonRepresentation.java @@ -88,7 +88,7 @@ final public class TaskJsonRepresentation { this.taskId = task.getTaskId().getValue(); this.taskStatus = task.getTaskStatus().getValue().name(); - this.originalTaskUri = (task. getOriginalTaskUri() == null) ? + this.originalTaskUri = (task.getOriginalTaskUri() == null) ? null : task.getOriginalTaskUri().getValue(); this.serviceProvider = (task.getProvider() == null) ? null : task.getProvider().getValue(); -- 2.45.1