fixes
This commit is contained in:
@@ -55,16 +55,15 @@ public class AddNewTaskToTaskListWebController {
|
||||
(payload.getOriginalTaskUri() == null) ? Optional.empty()
|
||||
: Optional.of(new Task.OriginalTaskUri(payload.getOriginalTaskUri()));
|
||||
|
||||
Optional<Task.InputData> inputData =
|
||||
(payload.getInputData() == null) ? Optional.empty()
|
||||
: Optional.of(new Task.InputData(payload.getInputData()));
|
||||
|
||||
AddNewTaskToTaskListCommand command = new AddNewTaskToTaskListCommand(taskName, taskType,
|
||||
originalTaskUriOptional);
|
||||
originalTaskUriOptional, inputData);
|
||||
|
||||
Task createdTask = addNewTaskToTaskListUseCase.addNewTaskToTaskList(command);
|
||||
|
||||
// When creating a task, the task's representation may include optional input data
|
||||
if (payload.getInputData() != null) {
|
||||
createdTask.setInputData(new Task.InputData(payload.getInputData()));
|
||||
}
|
||||
|
||||
// Add the content type as a response header
|
||||
HttpHeaders responseHeaders = new HttpHeaders();
|
||||
responseHeaders.add(HttpHeaders.CONTENT_TYPE, TaskJsonRepresentation.MEDIA_TYPE);
|
||||
|
@@ -8,8 +8,11 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
@@ -23,12 +26,17 @@ public class CompleteTaskWebController {
|
||||
this.completeTaskUseCase = completeTaskUseCase;
|
||||
}
|
||||
|
||||
@PostMapping(path = "/tasks/completeTask", consumes = {TaskJsonRepresentation.MEDIA_TYPE})
|
||||
public ResponseEntity<String> completeTask (@RequestBody Task task){
|
||||
@GetMapping(path = "/tasks/completeTask/{taskId}")
|
||||
public ResponseEntity<String> completeTask (@PathVariable("taskId") String taskId){
|
||||
|
||||
System.out.println("completeTask");
|
||||
System.out.println(taskId);
|
||||
|
||||
String taskResult = "0";
|
||||
|
||||
try {
|
||||
CompleteTaskCommand command = new CompleteTaskCommand(
|
||||
task.getTaskId(), task.getTaskResult()
|
||||
new Task.TaskId(taskId), new Task.OutputData(taskResult)
|
||||
);
|
||||
|
||||
Task updateATask = completeTaskUseCase.completeTask(command);
|
||||
|
@@ -0,0 +1,74 @@
|
||||
package ch.unisg.tapastasks.tasks.adapter.out.web;
|
||||
|
||||
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.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.github.fge.jsonpatch.JsonPatch;
|
||||
import com.github.fge.jsonpatch.JsonPatchOperation;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import ch.unisg.tapastasks.tasks.adapter.in.formats.TaskJsonPatchRepresentation;
|
||||
import ch.unisg.tapastasks.tasks.application.port.out.ExternalTaskExecutedEvent;
|
||||
import ch.unisg.tapastasks.tasks.application.port.out.ExternalTaskExecutedEventHandler;
|
||||
|
||||
@Component
|
||||
@Primary
|
||||
public class ExternalTaskExecutedWebAdapter implements ExternalTaskExecutedEventHandler {
|
||||
|
||||
Logger logger = Logger.getLogger(ExternalTaskExecutedWebAdapter.class.getName());
|
||||
|
||||
/**
|
||||
* Updates an external task which got executed in our system.
|
||||
**/
|
||||
@Override
|
||||
public void handleEvent(ExternalTaskExecutedEvent externalTaskExecutedEvent) {
|
||||
|
||||
JSONObject op1;
|
||||
JSONObject op2;
|
||||
try {
|
||||
op1 = new JSONObject()
|
||||
.put("op", "replace")
|
||||
.put("path", "/taskStatus")
|
||||
.put("value", "EXECUTED");
|
||||
|
||||
op2 = new JSONObject()
|
||||
.put("op", "add")
|
||||
.put("path", "/outputData")
|
||||
.put("value", "0");
|
||||
} catch (JSONException e) {
|
||||
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
|
||||
return;
|
||||
}
|
||||
|
||||
String body = new JSONArray().put(op1).put(op2).toString();
|
||||
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(externalTaskExecutedEvent.getOriginalTaskUri().getValue()))
|
||||
.header("Content-Type", "application/json")
|
||||
.method("PATCH", HttpRequest.BodyPublishers.ofString(body))
|
||||
.build();
|
||||
|
||||
|
||||
try {
|
||||
client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
} catch (InterruptedException e) {
|
||||
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (IOException e) {
|
||||
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -29,6 +29,7 @@ public class PublishNewTaskAddedEventWebAdapter implements NewTaskAddedEventPort
|
||||
var values = new HashMap<String, String>() {{
|
||||
put("taskID", event.taskId);
|
||||
put("taskType", event.taskType);
|
||||
put("inputData", event.inputData);
|
||||
}};
|
||||
|
||||
var objectMapper = new ObjectMapper();
|
||||
|
@@ -19,11 +19,19 @@ public class AddNewTaskToTaskListCommand extends SelfValidating<AddNewTaskToTask
|
||||
@Getter
|
||||
private final Optional<Task.OriginalTaskUri> originalTaskUri;
|
||||
|
||||
public AddNewTaskToTaskListCommand(Task.TaskName taskName, Task.TaskType taskType,
|
||||
Optional<Task.OriginalTaskUri> originalTaskUri) {
|
||||
@Getter
|
||||
private final Optional<Task.InputData> inputData;
|
||||
|
||||
public AddNewTaskToTaskListCommand(
|
||||
Task.TaskName taskName,
|
||||
Task.TaskType taskType,
|
||||
Optional<Task.OriginalTaskUri> originalTaskUri,
|
||||
Optional<Task.InputData> inputData
|
||||
) {
|
||||
this.taskName = taskName;
|
||||
this.taskType = taskType;
|
||||
this.originalTaskUri = originalTaskUri;
|
||||
this.inputData = inputData;
|
||||
|
||||
this.validateSelf();
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package ch.unisg.tapastasks.tasks.application.port.in;
|
||||
|
||||
import ch.unisg.tapastasks.common.SelfValidating;
|
||||
import ch.unisg.tapastasks.tasks.domain.Task.OutputData;
|
||||
import ch.unisg.tapastasks.tasks.domain.Task.TaskId;
|
||||
import ch.unisg.tapastasks.tasks.domain.Task.TaskResult;
|
||||
import lombok.Value;
|
||||
@@ -13,11 +14,11 @@ public class CompleteTaskCommand extends SelfValidating<CompleteTaskCommand> {
|
||||
private final TaskId taskId;
|
||||
|
||||
@NotNull
|
||||
private final TaskResult taskResult;
|
||||
private final OutputData outputData;
|
||||
|
||||
public CompleteTaskCommand(TaskId taskId, TaskResult taskResult){
|
||||
public CompleteTaskCommand(TaskId taskId, OutputData outputData){
|
||||
this.taskId = taskId;
|
||||
this.taskResult = taskResult;
|
||||
this.outputData = outputData;
|
||||
this.validateSelf();
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,28 @@
|
||||
package ch.unisg.tapastasks.tasks.application.port.out;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import ch.unisg.tapastasks.common.SelfValidating;
|
||||
import ch.unisg.tapastasks.tasks.domain.Task;
|
||||
import lombok.Getter;
|
||||
import lombok.Value;
|
||||
|
||||
@Value
|
||||
public class ExternalTaskExecutedEvent extends SelfValidating<ExternalTaskExecutedEvent> {
|
||||
@NotNull
|
||||
private final Task.TaskId taskId;
|
||||
|
||||
@Getter
|
||||
private final Task.OriginalTaskUri originalTaskUri;
|
||||
|
||||
@Getter
|
||||
private final Task.OutputData outputData;
|
||||
|
||||
public ExternalTaskExecutedEvent(Task.TaskId taskId, Task.OriginalTaskUri originalTaskUri, Task.OutputData outputData) {
|
||||
this.taskId = taskId;
|
||||
this.originalTaskUri = originalTaskUri;
|
||||
this.outputData = outputData;
|
||||
|
||||
this.validateSelf();
|
||||
}
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
package ch.unisg.tapastasks.tasks.application.port.out;
|
||||
|
||||
public interface ExternalTaskExecutedEventHandler {
|
||||
void handleEvent(ExternalTaskExecutedEvent externalTaskExecutedEvent);
|
||||
}
|
@@ -22,12 +22,26 @@ public class AddNewTaskToTaskListService implements AddNewTaskToTaskListUseCase
|
||||
public Task addNewTaskToTaskList(AddNewTaskToTaskListCommand command) {
|
||||
TaskList taskList = TaskList.getTapasTaskList();
|
||||
|
||||
Task newTask = (command.getOriginalTaskUri().isPresent()) ?
|
||||
// Create a delegated task that points back to the original task
|
||||
taskList.addNewTaskWithNameAndTypeAndOriginalTaskUri(command.getTaskName(),
|
||||
command.getTaskType(), command.getOriginalTaskUri().get())
|
||||
// Create an original task
|
||||
: taskList.addNewTaskWithNameAndType(command.getTaskName(), command.getTaskType());
|
||||
Task newTask;
|
||||
|
||||
System.out.println("TEST:");
|
||||
System.out.println(command.getInputData().get());
|
||||
|
||||
if (command.getOriginalTaskUri().isPresent() && command.getInputData().isPresent()) {
|
||||
System.out.println("TEST2:");
|
||||
newTask = taskList.addNewTaskWithNameAndTypeAndOriginalTaskUriAndInputData(command.getTaskName(),
|
||||
command.getTaskType(), command.getOriginalTaskUri().get(), command.getInputData().get());
|
||||
} else if (command.getOriginalTaskUri().isPresent()) {
|
||||
newTask = taskList.addNewTaskWithNameAndTypeAndOriginalTaskUri(command.getTaskName(),
|
||||
command.getTaskType(), command.getOriginalTaskUri().get());
|
||||
} else if (command.getOriginalTaskUri().isPresent()) {
|
||||
newTask = null;
|
||||
} else {
|
||||
newTask = taskList.addNewTaskWithNameAndType(command.getTaskName(), command.getTaskType());
|
||||
}
|
||||
|
||||
System.out.println("TEST");
|
||||
System.out.println(newTask.getInputData());
|
||||
|
||||
//Here we are using the application service to emit the domain event to the outside of the bounded context.
|
||||
//This event should be considered as a light-weight "integration event" to communicate with other services.
|
||||
@@ -35,8 +49,13 @@ public class AddNewTaskToTaskListService implements AddNewTaskToTaskListUseCase
|
||||
//not recommended to emit a domain event via an application service! You should first emit the domain event in
|
||||
//the core and then the integration event in the application layer.
|
||||
if (newTask != null) {
|
||||
NewTaskAddedEvent newTaskAdded = new NewTaskAddedEvent(newTask.getTaskName().getValue(),
|
||||
taskList.getTaskListName().getValue(), newTask.getTaskId().getValue(), newTask.getTaskType().getValue());
|
||||
NewTaskAddedEvent newTaskAdded = new NewTaskAddedEvent(
|
||||
newTask.getTaskName().getValue(),
|
||||
taskList.getTaskListName().getValue(),
|
||||
newTask.getTaskId().getValue(),
|
||||
newTask.getTaskType().getValue(),
|
||||
newTask.getInputData().getValue()
|
||||
);
|
||||
newTaskAddedEventPort.publishNewTaskAddedEvent(newTaskAdded);
|
||||
}
|
||||
|
||||
|
@@ -2,10 +2,11 @@ package ch.unisg.tapastasks.tasks.application.service;
|
||||
|
||||
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.out.ExternalTaskExecutedEvent;
|
||||
import ch.unisg.tapastasks.tasks.application.port.out.ExternalTaskExecutedEventHandler;
|
||||
import ch.unisg.tapastasks.tasks.domain.Task;
|
||||
import ch.unisg.tapastasks.tasks.domain.Task.*;
|
||||
import ch.unisg.tapastasks.tasks.domain.TaskList;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -17,15 +18,26 @@ import java.util.Optional;
|
||||
@Transactional
|
||||
public class CompleteTaskService implements CompleteTaskUseCase {
|
||||
|
||||
private final ExternalTaskExecutedEventHandler externalTaskExecutedEventHandler;
|
||||
|
||||
@Override
|
||||
public Task completeTask(CompleteTaskCommand command){
|
||||
TaskList taskList = TaskList.getTapasTaskList();
|
||||
Optional<Task> updatedTask = taskList.retrieveTaskById(command.getTaskId());
|
||||
|
||||
Task newTask = updatedTask.get();
|
||||
newTask.taskResult = new TaskResult(command.getTaskResult().getValue());
|
||||
newTask.taskResult = new TaskResult(command.getOutputData().getValue());
|
||||
newTask.taskStatus = new TaskStatus(Task.Status.EXECUTED);
|
||||
|
||||
if (!newTask.getOriginalTaskUri().getValue().equalsIgnoreCase("")) {
|
||||
ExternalTaskExecutedEvent event = new ExternalTaskExecutedEvent(
|
||||
newTask.getTaskId(),
|
||||
newTask.getOriginalTaskUri(),
|
||||
newTask.getOutputData()
|
||||
);
|
||||
externalTaskExecutedEventHandler.handleEvent(event);
|
||||
}
|
||||
|
||||
return newTask;
|
||||
}
|
||||
}
|
||||
|
@@ -6,12 +6,14 @@ public class NewTaskAddedEvent {
|
||||
public String taskListName;
|
||||
public String taskId;
|
||||
public String taskType;
|
||||
public String inputData;
|
||||
|
||||
public NewTaskAddedEvent(String taskName, String taskListName, String taskId, String taskType) {
|
||||
public NewTaskAddedEvent(String taskName, String taskListName, String taskId, String taskType, String inputData) {
|
||||
this.taskName = taskName;
|
||||
this.taskListName = taskListName;
|
||||
this.taskId = taskId;
|
||||
this.taskType = taskType;
|
||||
this.inputData = inputData;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -51,6 +51,18 @@ public class Task {
|
||||
this.outputData = null;
|
||||
}
|
||||
|
||||
public Task(TaskName taskName, TaskType taskType, OriginalTaskUri taskUri, InputData inputData) {
|
||||
this.taskName = taskName;
|
||||
this.taskType = taskType;
|
||||
this.taskStatus = new TaskStatus(Status.OPEN);
|
||||
this.taskId = new TaskId(UUID.randomUUID().toString());
|
||||
this.taskResult = new TaskResult("");
|
||||
this.originalTaskUri = taskUri;
|
||||
|
||||
this.inputData = inputData;
|
||||
this.outputData = null;
|
||||
}
|
||||
|
||||
protected static Task createTaskWithNameAndType(TaskName name, TaskType type) {
|
||||
//This is a simple debug message to see that the request has reached the right method in the core
|
||||
System.out.println("New Task: " + name.getValue() + " " + type.getValue());
|
||||
@@ -62,6 +74,11 @@ public class Task {
|
||||
return new Task(name, type, originalTaskUri);
|
||||
}
|
||||
|
||||
protected static Task createTaskWithNameAndTypeAndOriginalTaskUriAndInputData(TaskName name, TaskType type,
|
||||
OriginalTaskUri originalTaskUri, InputData inputData) {
|
||||
return new Task(name, type, originalTaskUri, inputData);
|
||||
}
|
||||
|
||||
@Value
|
||||
public static class TaskId {
|
||||
String value;
|
||||
|
@@ -48,6 +48,15 @@ public class TaskList {
|
||||
return newTask;
|
||||
}
|
||||
|
||||
public Task addNewTaskWithNameAndTypeAndOriginalTaskUriAndInputData(Task.TaskName name, Task.TaskType type,
|
||||
Task.OriginalTaskUri originalTaskUri, Task.InputData inputData) {
|
||||
Task newTask = Task.createTaskWithNameAndTypeAndOriginalTaskUriAndInputData(name, type, originalTaskUri, inputData);
|
||||
this.addNewTaskToList(newTask);
|
||||
|
||||
return newTask;
|
||||
}
|
||||
|
||||
|
||||
private void addNewTaskToList(Task newTask) {
|
||||
//Here we would also publish a domain event to other entities in the core interested in this event.
|
||||
//However, we skip this here as it makes the core even more complex (e.g., we have to implement a light-weight
|
||||
|
Reference in New Issue
Block a user