cleaned up task list + started implementation of deleteTask

This commit is contained in:
julius.lautz 2021-11-01 22:07:21 +01:00
parent d08a6d0b67
commit 3184ab3389
16 changed files with 178 additions and 107 deletions

View File

@ -75,6 +75,12 @@
<artifactId>org.eclipse.paho.client.mqttv3</artifactId> <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.0</version> <version>1.2.0</version>
</dependency> </dependency>
<dependency>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
<version>0.0.20131108.vaadin1</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -88,7 +88,7 @@ final public class TaskJsonRepresentation {
this.taskId = task.getTaskId().getValue(); this.taskId = task.getTaskId().getValue();
this.taskStatus = task.getTaskStatus().getValue().name(); this.taskStatus = task.getTaskStatus().getValue().name();
this.originalTaskUri = (task.getOriginalTaskUri() == null) ? this.originalTaskUri = (task. getOriginalTaskUri() == null) ?
null : task.getOriginalTaskUri().getValue(); null : task.getOriginalTaskUri().getValue();
this.serviceProvider = (task.getProvider() == null) ? null : task.getProvider().getValue(); this.serviceProvider = (task.getProvider() == null) ? null : task.getProvider().getValue();

View File

@ -1,8 +1,10 @@
package ch.unisg.tapastasks.tasks.adapter.in.web; 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.CompleteTaskCommand;
import ch.unisg.tapastasks.tasks.application.port.in.CompleteTaskUseCase; import ch.unisg.tapastasks.tasks.application.port.in.CompleteTaskUseCase;
import ch.unisg.tapastasks.tasks.domain.Task; import ch.unisg.tapastasks.tasks.domain.Task;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -21,7 +23,7 @@ public class CompleteTaskWebController {
this.completeTaskUseCase = completeTaskUseCase; this.completeTaskUseCase = completeTaskUseCase;
} }
@PostMapping(path = "/tasks/completeTask", consumes = {TaskMediaType.TASK_MEDIA_TYPE}) @PostMapping(path = "/tasks/completeTask", consumes = {TaskJsonRepresentation.MEDIA_TYPE})
public ResponseEntity<String> completeTask (@RequestBody Task task){ public ResponseEntity<String> completeTask (@RequestBody Task task){
try { try {
@ -32,10 +34,12 @@ public class CompleteTaskWebController {
Task updateATask = completeTaskUseCase.completeTask(command); Task updateATask = completeTaskUseCase.completeTask(command);
HttpHeaders responseHeaders = new HttpHeaders(); 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); return new ResponseEntity<>(TaskJsonRepresentation.serialize(updateATask), responseHeaders, HttpStatus.ACCEPTED);
} catch(ConstraintViolationException e){ } catch (JsonProcessingException e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
} catch (ConstraintViolationException e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
} }
} }

View File

@ -1,9 +1,11 @@
package ch.unisg.tapastasks.tasks.adapter.in.web; 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.DeleteTaskCommand;
import ch.unisg.tapastasks.tasks.application.port.in.DeleteTaskUseCase; import ch.unisg.tapastasks.tasks.application.port.in.DeleteTaskUseCase;
import ch.unisg.tapastasks.tasks.domain.Task; import ch.unisg.tapastasks.tasks.domain.Task;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -23,26 +25,27 @@ public class DeleteTaskWebController {
this.deleteClassUseCase = deleteClassUseCase; this.deleteClassUseCase = deleteClassUseCase;
} }
@PostMapping(path="/tasks/deleteTask", consumes = {TaskMediaType.TASK_MEDIA_TYPE}) @PostMapping(path="/tasks/deleteTask", consumes = {TaskJsonRepresentation.MEDIA_TYPE})
public ResponseEntity<String> deleteTask (@RequestBody Task task){ public ResponseEntity<String> deleteTask (@RequestBody Task task){
try { try {
DeleteTaskCommand command = new DeleteTaskCommand(task.getTaskId()); DeleteTaskCommand command = new DeleteTaskCommand(task.getTaskId(), task.getOriginalTaskUri());
Optional<Task> deleteATask = deleteClassUseCase.deleteTask(command); Optional<Task> deleteATask = deleteClassUseCase.deleteTask(command);
// Check if the task with the given identifier exists // Check if the task with the given identifier exists
if (deleteATask.isEmpty()) { if (deleteATask.isEmpty()) {
// If not, through a 404 Not Found status code // If not, through a 404 Not Found status code
throw new ResponseStatusException(HttpStatus.NOT_FOUND); throw new ResponseStatusException(HttpStatus.NOT_FOUND);
} }
HttpHeaders responseHeaders = new HttpHeaders(); 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); return new ResponseEntity<>(TaskJsonRepresentation.serialize(deleteATask.get()), responseHeaders, HttpStatus.ACCEPTED);
} catch(ConstraintViolationException e){ } catch (JsonProcessingException e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
} catch (ConstraintViolationException e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
} }
} }

View File

@ -1,8 +1,10 @@
package ch.unisg.tapastasks.tasks.adapter.in.web; 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.TaskAssignedCommand;
import ch.unisg.tapastasks.tasks.application.port.in.TaskAssignedUseCase; import ch.unisg.tapastasks.tasks.application.port.in.TaskAssignedUseCase;
import ch.unisg.tapastasks.tasks.domain.Task; import ch.unisg.tapastasks.tasks.domain.Task;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -21,7 +23,7 @@ public class TaskAssignedWebController {
this.taskAssignedUseCase = taskAssignedUseCase; this.taskAssignedUseCase = taskAssignedUseCase;
} }
@PostMapping(path="/tasks/assignTask", consumes= {TaskMediaType.TASK_MEDIA_TYPE}) @PostMapping(path="/tasks/assignTask", consumes= {TaskJsonRepresentation.MEDIA_TYPE})
public ResponseEntity<String> assignTask(@RequestBody Task task){ public ResponseEntity<String> assignTask(@RequestBody Task task){
try{ try{
TaskAssignedCommand command = new TaskAssignedCommand( TaskAssignedCommand command = new TaskAssignedCommand(
@ -31,10 +33,12 @@ public class TaskAssignedWebController {
Task updateATask = taskAssignedUseCase.assignTask(command); Task updateATask = taskAssignedUseCase.assignTask(command);
HttpHeaders responseHeaders = new HttpHeaders(); 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); return new ResponseEntity<>(TaskJsonRepresentation.serialize(updateATask), responseHeaders, HttpStatus.ACCEPTED);
} catch (ConstraintViolationException e){ } catch (JsonProcessingException e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
} catch (ConstraintViolationException e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
} }
} }

View File

@ -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() { }
}

View File

@ -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<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException e){
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

View File

@ -42,7 +42,7 @@ public class PublishNewTaskAddedEventWebAdapter implements NewTaskAddedEventPort
HttpClient client = HttpClient.newHttpClient(); HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder() HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(server+"/task")) .uri(URI.create(server+"/task"))
.header("Content-Type", "application/json") .header("Content-Type", "application/task+json")
.POST(HttpRequest.BodyPublishers.ofString(requestBody)) .POST(HttpRequest.BodyPublishers.ofString(requestBody))
.build(); .build();

View File

@ -2,6 +2,7 @@ package ch.unisg.tapastasks.tasks.application.port.in;
import ch.unisg.tapastasks.common.SelfValidating; import ch.unisg.tapastasks.common.SelfValidating;
import ch.unisg.tapastasks.tasks.domain.Task.TaskId; import ch.unisg.tapastasks.tasks.domain.Task.TaskId;
import ch.unisg.tapastasks.tasks.domain.Task.OriginalTaskUri;
import lombok.Value; import lombok.Value;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
@ -11,8 +12,12 @@ public class DeleteTaskCommand extends SelfValidating<DeleteTaskCommand> {
@NotNull @NotNull
private final TaskId taskId; private final TaskId taskId;
public DeleteTaskCommand(TaskId taskId){ @NotNull
private final OriginalTaskUri taskUri;
public DeleteTaskCommand(TaskId taskId, OriginalTaskUri taskUri){
this.taskId=taskId; this.taskId=taskId;
this.taskUri = taskUri;
this.validateSelf(); this.validateSelf();
} }
} }

View File

@ -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);
}

View File

@ -19,17 +19,13 @@ public class CompleteTaskService implements CompleteTaskUseCase {
@Override @Override
public Task completeTask(CompleteTaskCommand command){ public Task completeTask(CompleteTaskCommand command){
// TODO Retrieve the task based on ID
TaskList taskList = TaskList.getTapasTaskList(); TaskList taskList = TaskList.getTapasTaskList();
Optional<Task> updatedTask = taskList.retrieveTaskById(command.getTaskId()); Optional<Task> updatedTask = taskList.retrieveTaskById(command.getTaskId());
// TODO Update the status and result (and save?)
Task newTask = updatedTask.get(); Task newTask = updatedTask.get();
newTask.taskResult = new TaskResult(command.getTaskResult().getValue()); 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; return newTask;
} }
} }

View File

@ -19,10 +19,15 @@ public class DeleteTaskService implements DeleteTaskUseCase {
@Override @Override
public Optional<Task> deleteTask(DeleteTaskCommand command){ public Optional<Task> deleteTask(DeleteTaskCommand command){
// TODO check with assignment service if we can delte
TaskList taskList = TaskList.getTapasTaskList(); TaskList taskList = TaskList.getTapasTaskList();
Optional<Task> 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()); return taskList.deleteTaskById(command.getTaskId());
} else {
/*send message back to TaskList that the task cannot be deleted*/
}
} }
} }

View File

@ -24,7 +24,7 @@ public class TaskAssignedService implements TaskAssignedUseCase {
// update the status to assigned // update the status to assigned
Task updatedTask = task.get(); Task updatedTask = task.get();
updatedTask.taskState = new TaskState(State.ASSIGNED); updatedTask.taskStatus = new TaskStatus(Status.ASSIGNED);
return updatedTask; return updatedTask;
} }

View File

@ -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;
}
}

View File

@ -21,40 +21,33 @@ public class Task {
@Getter @Getter
private final TaskType taskType; private final TaskType taskType;
@Getter @Getter @Setter
public TaskState taskState; // had to make public for CompleteTaskService public TaskStatus taskStatus; // had to make public for CompleteTaskService
@Getter @Getter
public TaskResult taskResult; // same as above public TaskResult taskResult; // same as above
// private final OriginalTaskUri originalTaskUri; @Getter
private final OriginalTaskUri originalTaskUri;
// @Getter @Setter @Getter @Setter
// private TaskStatus taskStatus; private ServiceProvider provider;
// @Getter @Setter @Getter @Setter
// private ServiceProvider provider; private InputData inputData;
// @Getter @Setter @Getter @Setter
// private InputData inputData; private OutputData outputData;
// @Getter @Setter
// private OutputData outputData;
public Task(TaskName taskName, TaskType taskType, OriginalTaskUri taskUri) { public Task(TaskName taskName, TaskType taskType, OriginalTaskUri taskUri) {
this.taskId = new TaskId(UUID.randomUUID().toString());
this.taskName = taskName; this.taskName = taskName;
this.taskType = taskType; this.taskType = taskType;
this.taskState = new TaskState(State.OPEN); this.taskStatus = new TaskStatus(Status.OPEN);
this.taskId = new TaskId(UUID.randomUUID().toString()); this.taskId = new TaskId(UUID.randomUUID().toString());
this.taskResult = new TaskResult(""); this.taskResult = new TaskResult("");
// this.originalTaskUri = taskUri; this.originalTaskUri = taskUri;
this.inputData = null;
// this.taskStatus = new TaskStatus(Status.OPEN); this.outputData = null;
// this.inputData = null;
// this.outputData = null;
} }
protected static Task createTaskWithNameAndType(TaskName name, TaskType type) { protected static Task createTaskWithNameAndType(TaskName name, TaskType type) {

View File

@ -67,49 +67,50 @@ public class TaskList {
} }
public Optional<Task> deleteTaskById(Task.TaskId id) { public Optional<Task> deleteTaskById(Task.TaskId id) {
for (Task task: listOfTasks.value){ for (Task task : listOfTasks.value) {
if(task.getTaskId().getValue().equalsIgnoreCase(id.getValue())){ if (task.getTaskId().getValue().equalsIgnoreCase(id.getValue())) {
listOfTasks.value.remove(task); listOfTasks.value.remove(task);
return Optional.of(task); return Optional.of(task);
} }
} }
return Optional.empty(); return Optional.empty();
// public Task changeTaskStatusToAssigned(Task.TaskId id, Optional<Task.ServiceProvider> serviceProvider) }
// throws TaskNotFoundException { public Task changeTaskStatusToAssigned(Task.TaskId id, Optional<Task.ServiceProvider> serviceProvider)
// return changeTaskStatus(id, new Task.TaskStatus(Task.Status.ASSIGNED), serviceProvider, Optional.empty()); throws TaskNotFoundException {
// } return changeTaskStatus(id, new Task.TaskStatus(Task.Status.ASSIGNED), serviceProvider, Optional.empty());
}
// public Task changeTaskStatusToRunning(Task.TaskId id, Optional<Task.ServiceProvider> serviceProvider) public Task changeTaskStatusToRunning(Task.TaskId id, Optional<Task.ServiceProvider> serviceProvider)
// throws TaskNotFoundException { throws TaskNotFoundException {
// return changeTaskStatus(id, new Task.TaskStatus(Task.Status.RUNNING), serviceProvider, Optional.empty()); return changeTaskStatus(id, new Task.TaskStatus(Task.Status.RUNNING), serviceProvider, Optional.empty());
// } }
// public Task changeTaskStatusToExecuted(Task.TaskId id, Optional<Task.ServiceProvider> serviceProvider, public Task changeTaskStatusToExecuted(Task.TaskId id, Optional<Task.ServiceProvider> serviceProvider,
// Optional<Task.OutputData> outputData) throws TaskNotFoundException { Optional<Task.OutputData> outputData) throws TaskNotFoundException {
// return changeTaskStatus(id, new Task.TaskStatus(Task.Status.EXECUTED), serviceProvider, outputData); return changeTaskStatus(id, new Task.TaskStatus(Task.Status.EXECUTED), serviceProvider, outputData);
// } }
// private Task changeTaskStatus(Task.TaskId id, Task.TaskStatus status, Optional<Task.ServiceProvider> serviceProvider, private Task changeTaskStatus(Task.TaskId id, Task.TaskStatus status, Optional<Task.ServiceProvider> serviceProvider,
// Optional<Task.OutputData> outputData) { Optional<Task.OutputData> outputData) {
// Optional<Task> taskOpt = retrieveTaskById(id); Optional<Task> taskOpt = retrieveTaskById(id);
// if (taskOpt.isEmpty()) { if (taskOpt.isEmpty()) {
// throw new TaskNotFoundException(); throw new TaskNotFoundException();
// } }
// Task task = taskOpt.get(); Task task = taskOpt.get();
// task.setTaskStatus(status); task.setTaskStatus(status);
// if (serviceProvider.isPresent()) { if (serviceProvider.isPresent()) {
// task.setProvider(serviceProvider.get()); task.setProvider(serviceProvider.get());
// } }
// if (outputData.isPresent()) { if (outputData.isPresent()) {
// task.setOutputData(outputData.get()); task.setOutputData(outputData.get());
// } }
// return task; return task;
} }
@Value @Value