Merging dev to main #107

Merged
Maece97 merged 52 commits from dev into main 2021-12-23 16:12:19 +00:00
28 changed files with 194 additions and 283 deletions
Showing only changes of commit 2279ebb89c - Show all commits

View File

@ -7,6 +7,7 @@ import ch.unisg.tapastasks.tasks.application.port.in.TaskAssignedEventHandler;
import ch.unisg.tapastasks.tasks.domain.Task;
import ch.unisg.tapastasks.tasks.domain.Task.TaskId;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.RequiredArgsConstructor;
import java.util.Optional;
@ -18,8 +19,11 @@ import java.util.Optional;
*
* See also {@link TaskAssignedEvent}, {@link Task}, and {@link TaskEventHttpDispatcher}.
*/
@RequiredArgsConstructor
public class TaskAssignedEventListenerHttpAdapter extends TaskEventListener {
private final TaskAssignedEventHandler taskAssignedEventHandler;
/**
* Handles the task assigned event.
*
@ -32,7 +36,6 @@ public class TaskAssignedEventListenerHttpAdapter extends TaskEventListener {
Optional<Task.ServiceProvider> serviceProvider = representation.extractFirstServiceProviderChange();
TaskAssignedEvent taskAssignedEvent = new TaskAssignedEvent(new TaskId(taskId), serviceProvider);
TaskAssignedEventHandler taskAssignedEventHandler = new TaskAssignedHandler();
return taskAssignedEventHandler.handleTaskAssigned(taskAssignedEvent);
}

View File

@ -3,10 +3,14 @@ package ch.unisg.tapastasks.tasks.adapter.in.messaging.http;
import ch.unisg.tapastasks.tasks.adapter.in.formats.TaskJsonPatchRepresentation;
import ch.unisg.tapastasks.tasks.adapter.in.formats.TaskJsonRepresentation;
import ch.unisg.tapastasks.tasks.adapter.in.messaging.UnknownEventException;
import ch.unisg.tapastasks.tasks.application.port.in.TaskAssignedEventHandler;
import ch.unisg.tapastasks.tasks.application.port.in.TaskExecutedEventHandler;
import ch.unisg.tapastasks.tasks.application.port.in.TaskStartedEventHandler;
import ch.unisg.tapastasks.tasks.domain.Task;
import ch.unisg.tapastasks.tasks.domain.TaskNotFoundException;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jsonpatch.JsonPatch;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@ -37,6 +41,7 @@ import java.util.logging.Logger;
* For some sample HTTP requests, see the README.
*/
@RestController
@RequiredArgsConstructor
public class TaskEventHttpDispatcher {
// The standard media type for JSON Patch registered with IANA
// See: https://www.iana.org/assignments/media-types/application/json-patch+json
@ -44,6 +49,9 @@ public class TaskEventHttpDispatcher {
Logger logger = Logger.getLogger(TaskEventHttpDispatcher.class.getName());
private final TaskStartedEventHandler taskStartedEventHandler;
private final TaskAssignedEventHandler taskAssignedEventHandler;
private final TaskExecutedEventHandler taskExecutedEventHandler;
/**
* Handles HTTP PATCH requests with a JSON Patch payload. Routes the requests based on the
* the operations requested in the patch. In this implementation, one HTTP Patch request is
@ -58,6 +66,7 @@ public class TaskEventHttpDispatcher {
@PatchMapping(path = "/tasks/{taskId}", consumes = {JSON_PATCH_MEDIA_TYPE})
public ResponseEntity<String> dispatchTaskEvents(@PathVariable("taskId") String taskId,
@RequestBody JsonNode payload) {
logger.info("TaskList | Incoming patch task request for task: " + taskId);
try {
// Throw an exception if the JSON Patch format is invalid. This call is only used to
@ -74,13 +83,13 @@ public class TaskEventHttpDispatcher {
if (status.isPresent()) {
switch (status.get()) {
case ASSIGNED:
listener = new TaskAssignedEventListenerHttpAdapter();
listener = new TaskAssignedEventListenerHttpAdapter(taskAssignedEventHandler);
break;
case RUNNING:
listener = new TaskStartedEventListenerHttpAdapter();
listener = new TaskStartedEventListenerHttpAdapter(taskStartedEventHandler);
break;
case EXECUTED:
listener = new TaskExecutedEventListenerHttpAdapter();
listener = new TaskExecutedEventListenerHttpAdapter(taskExecutedEventHandler);
break;
default:
break;

View File

@ -6,6 +6,7 @@ import ch.unisg.tapastasks.tasks.application.port.in.TaskExecutedEvent;
import ch.unisg.tapastasks.tasks.application.port.in.TaskExecutedEventHandler;
import ch.unisg.tapastasks.tasks.domain.Task;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.RequiredArgsConstructor;
import java.util.Optional;
@ -17,8 +18,11 @@ import java.util.Optional;
*
* See also {@link TaskExecutedEvent}, {@link Task}, and {@link TaskEventHttpDispatcher}.
*/
@RequiredArgsConstructor
public class TaskExecutedEventListenerHttpAdapter extends TaskEventListener {
private final TaskExecutedEventHandler taskExecutedEventHandler;
public Task handleTaskEvent(String taskId, JsonNode payload) {
System.out.println(payload);
TaskJsonPatchRepresentation representation = new TaskJsonPatchRepresentation(payload);
@ -30,7 +34,6 @@ public class TaskExecutedEventListenerHttpAdapter extends TaskEventListener {
TaskExecutedEvent taskExecutedEvent = new TaskExecutedEvent(new Task.TaskId(taskId),
serviceProvider, outputData);
TaskExecutedEventHandler taskExecutedEventHandler = new TaskExecutedHandler();
return taskExecutedEventHandler.handleTaskExecuted(taskExecutedEvent);
}

View File

@ -7,6 +7,7 @@ import ch.unisg.tapastasks.tasks.application.port.in.TaskStartedEventHandler;
import ch.unisg.tapastasks.tasks.domain.Task;
import ch.unisg.tapastasks.tasks.domain.Task.TaskId;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.RequiredArgsConstructor;
import java.util.Optional;
@ -18,14 +19,16 @@ import java.util.Optional;
*
* See also {@link TaskStartedEvent}, {@link Task}, and {@link TaskEventHttpDispatcher}.
*/
@RequiredArgsConstructor
public class TaskStartedEventListenerHttpAdapter extends TaskEventListener {
private final TaskStartedEventHandler taskStartedEventHandler;
public Task handleTaskEvent(String taskId, JsonNode payload) {
TaskJsonPatchRepresentation representation = new TaskJsonPatchRepresentation(payload);
Optional<Task.ServiceProvider> serviceProvider = representation.extractFirstServiceProviderChange();
TaskStartedEvent taskStartedEvent = new TaskStartedEvent(new TaskId(taskId), serviceProvider);
TaskStartedEventHandler taskStartedEventHandler = new TaskStartedHandler();
return taskStartedEventHandler.handleTaskStarted(taskStartedEvent);
}

View File

@ -9,9 +9,7 @@ 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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import javax.validation.ConstraintViolationException;
@ -26,10 +24,10 @@ public class DeleteTaskWebController {
}
// TODO change to DELETE and why are we using task URI here?
@PostMapping(path="/tasks/deleteTask", consumes = {TaskJsonRepresentation.MEDIA_TYPE})
public ResponseEntity<String> deleteTask (@RequestBody Task task){
@DeleteMapping(path="/tasks/{taskId}")
public ResponseEntity<String> deleteTask (@PathVariable String taskId){
try {
DeleteTaskCommand command = new DeleteTaskCommand(task.getTaskId(), task.getOriginalTaskUri());
DeleteTaskCommand command = new DeleteTaskCommand(new Task.TaskId(taskId));
Optional<Task> deleteATask = deleteClassUseCase.deleteTask(command);

View File

@ -1,7 +1,6 @@
package ch.unisg.tapastasks.tasks.adapter.out.persistence.mongodb;
import ch.unisg.tapastasks.tasks.domain.Task;
import ch.unisg.tapastasks.tasks.domain.TaskList;
import java.util.Objects;
@ -22,6 +21,7 @@ class TaskMapper {
);
}
// TODO stupid task list name
MongoTaskDocument mapToMongoDocument(Task task) {
return new MongoTaskDocument(
task.getTaskId().getValue(),
@ -31,7 +31,7 @@ class TaskMapper {
task.getTaskStatus().getValue().toString(),
Objects.isNull(task.getInputData()) ? null : task.getInputData().getValue(),
Objects.isNull(task.getOutputData()) ? null : task.getOutputData().getValue(),
TaskList.getTapasTaskList().getTaskListName().getValue()
"task-list-name"
);
}
}

View File

@ -1,9 +1,9 @@
package ch.unisg.tapastasks.tasks.adapter.out.persistence.mongodb;
import ch.unisg.tapastasks.tasks.application.port.out.AddTaskPort;
import ch.unisg.tapastasks.tasks.application.port.out.DeleteTaskPort;
import ch.unisg.tapastasks.tasks.application.port.out.LoadTaskPort;
import ch.unisg.tapastasks.tasks.domain.Task;
import ch.unisg.tapastasks.tasks.domain.TaskList;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -12,7 +12,8 @@ import org.springframework.stereotype.Component;
@RequiredArgsConstructor
public class TaskPersistenceAdapter implements
AddTaskPort,
LoadTaskPort {
LoadTaskPort,
DeleteTaskPort {
@Autowired
private final TaskRepository taskRepository;
@ -26,8 +27,13 @@ public class TaskPersistenceAdapter implements
}
@Override
public Task loadTask(Task.TaskId taskId, TaskList.TaskListName taskListName) {
MongoTaskDocument mongoTaskDocument = taskRepository.findByTaskId(taskId.getValue(),taskListName.getValue());
public Task loadTask(Task.TaskId taskId) {
MongoTaskDocument mongoTaskDocument = taskRepository.findByTaskId(taskId.getValue());
return taskMapper.mapToDomainEntity(mongoTaskDocument);
}
@Override
public void deleteTask(Task.TaskId taskId) {
taskRepository.deleteByTaskId(taskId.getValue());
}
}

View File

@ -8,7 +8,9 @@ import java.util.List;
@Repository
public interface TaskRepository extends MongoRepository<MongoTaskDocument,String> {
public MongoTaskDocument findByTaskId(String taskId, String taskListName);
public MongoTaskDocument findByTaskId(String taskId);
public List<MongoTaskDocument> findByTaskListName(String taskListName);
public List<MongoTaskDocument> getAllBy();
public void deleteByTaskId(String taskId);
}

View File

@ -2,18 +2,36 @@ package ch.unisg.tapastasks.tasks.application.handler;
import ch.unisg.tapastasks.tasks.application.port.in.TaskAssignedEvent;
import ch.unisg.tapastasks.tasks.application.port.in.TaskAssignedEventHandler;
import ch.unisg.tapastasks.tasks.application.port.out.AddTaskPort;
import ch.unisg.tapastasks.tasks.application.port.out.LoadTaskPort;
import ch.unisg.tapastasks.tasks.domain.Task;
import ch.unisg.tapastasks.tasks.domain.TaskList;
import ch.unisg.tapastasks.tasks.domain.TaskNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import javax.transaction.Transactional;
import java.util.Optional;
@RequiredArgsConstructor
@Component
@Transactional
public class TaskAssignedHandler implements TaskAssignedEventHandler {
// TODO Why do we need this event handler when a service exists that does the same?
private final AddTaskPort addTaskToRepositoryPort;
private final LoadTaskPort loadTaskFromRepositoryPort;
@Override
public Task handleTaskAssigned(TaskAssignedEvent taskAssignedEvent) throws TaskNotFoundException {
TaskList taskList = TaskList.getTapasTaskList();
return taskList.changeTaskStatusToAssigned(taskAssignedEvent.getTaskId(),
taskAssignedEvent.getServiceProvider());
// retrieve the task based on ID
Optional<Task> taskFromRepo = Optional.ofNullable(loadTaskFromRepositoryPort.loadTask(taskAssignedEvent.getTaskId()));
// update the status to assigned
Task updatedTask = taskFromRepo.get();
updatedTask.setTaskStatus(new Task.TaskStatus(Task.Status.ASSIGNED));
// save updated task in repo
addTaskToRepositoryPort.addTask(updatedTask);
return updatedTask;
}
}

View File

@ -2,18 +2,44 @@ package ch.unisg.tapastasks.tasks.application.handler;
import ch.unisg.tapastasks.tasks.application.port.in.TaskExecutedEvent;
import ch.unisg.tapastasks.tasks.application.port.in.TaskExecutedEventHandler;
import ch.unisg.tapastasks.tasks.application.port.out.AddTaskPort;
import ch.unisg.tapastasks.tasks.application.port.out.LoadTaskPort;
import ch.unisg.tapastasks.tasks.domain.Task;
import ch.unisg.tapastasks.tasks.domain.TaskList;
import ch.unisg.tapastasks.tasks.domain.TaskNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import javax.transaction.Transactional;
import java.util.Optional;
@RequiredArgsConstructor
@Component
@Transactional
public class TaskExecutedHandler implements TaskExecutedEventHandler {
private final AddTaskPort addTaskToRepositoryPort;
private final LoadTaskPort loadTaskFromRepositoryPort;
@Override
public Task handleTaskExecuted(TaskExecutedEvent taskExecutedEvent) throws TaskNotFoundException {
TaskList taskList = TaskList.getTapasTaskList();
return taskList.changeTaskStatusToExecuted(taskExecutedEvent.getTaskId(),
taskExecutedEvent.getServiceProvider(), taskExecutedEvent.getOutputData());
// retrieve the task based on ID
Optional<Task> taskFromRepo = Optional.ofNullable(loadTaskFromRepositoryPort.loadTask(taskExecutedEvent.getTaskId()));
// update the status to assigned
Task updatedTask = taskFromRepo.get();
updatedTask.setTaskStatus(new Task.TaskStatus(Task.Status.EXECUTED));
if(taskExecutedEvent.getServiceProvider().isPresent()){
updatedTask.setProvider(taskExecutedEvent.getServiceProvider().get());
}
if(taskExecutedEvent.getOutputData().isPresent()){
updatedTask.setOutputData(taskExecutedEvent.getOutputData().get());
}
// save updated task in repo
addTaskToRepositoryPort.addTask(updatedTask);
return updatedTask;
}
}

View File

@ -2,18 +2,40 @@ package ch.unisg.tapastasks.tasks.application.handler;
import ch.unisg.tapastasks.tasks.application.port.in.TaskStartedEvent;
import ch.unisg.tapastasks.tasks.application.port.in.TaskStartedEventHandler;
import ch.unisg.tapastasks.tasks.application.port.out.AddTaskPort;
import ch.unisg.tapastasks.tasks.application.port.out.LoadTaskPort;
import ch.unisg.tapastasks.tasks.domain.Task;
import ch.unisg.tapastasks.tasks.domain.TaskList;
import ch.unisg.tapastasks.tasks.domain.TaskNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import javax.transaction.Transactional;
import java.util.Optional;
@RequiredArgsConstructor
@Component
@Transactional
public class TaskStartedHandler implements TaskStartedEventHandler {
private final AddTaskPort addTaskToRepositoryPort;
private final LoadTaskPort loadTaskFromRepositoryPort;
@Override
public Task handleTaskStarted(TaskStartedEvent taskStartedEvent) throws TaskNotFoundException {
TaskList taskList = TaskList.getTapasTaskList();
return taskList.changeTaskStatusToRunning(taskStartedEvent.getTaskId(),
taskStartedEvent.getServiceProvider());
// retrieve the task based on ID
Optional<Task> taskFromRepo = Optional.ofNullable(loadTaskFromRepositoryPort.loadTask(taskStartedEvent.getTaskId()));
// update the status to assigned
Task updatedTask = taskFromRepo.get();
updatedTask.setTaskStatus(new Task.TaskStatus(Task.Status.RUNNING));
if(taskStartedEvent.getServiceProvider().isPresent()){
updatedTask.setProvider(taskStartedEvent.getServiceProvider().get());
}
// save updated task in repo
addTaskToRepositoryPort.addTask(updatedTask);
return updatedTask;
}
}

View File

@ -14,12 +14,8 @@ public class DeleteTaskCommand extends SelfValidating<DeleteTaskCommand> {
@NotNull
private final TaskId taskId;
@NotNull
private final OriginalTaskUri taskUri;
public DeleteTaskCommand(TaskId taskId, OriginalTaskUri taskUri){
public DeleteTaskCommand(TaskId taskId){
this.taskId=taskId;
this.taskUri = taskUri;
this.validateSelf();
}
}

View File

@ -0,0 +1,7 @@
package ch.unisg.tapastasks.tasks.application.port.out;
import ch.unisg.tapastasks.tasks.domain.Task;
public interface DeleteTaskPort {
void deleteTask(Task.TaskId taskId);
}

View File

@ -1,10 +1,9 @@
package ch.unisg.tapastasks.tasks.application.port.out;
import ch.unisg.tapastasks.tasks.domain.Task;
import ch.unisg.tapastasks.tasks.domain.TaskList;
public interface LoadTaskPort {
Task loadTask(Task.TaskId taskId, TaskList.TaskListName taskListName);
Task loadTask(Task.TaskId taskId);
}

View File

@ -1,11 +0,0 @@
package ch.unisg.tapastasks.tasks.application.port.out;
import ch.unisg.tapastasks.tasks.domain.TaskList;
public interface TaskListLock {
void lockTaskList(TaskList.TaskListName taskListName);
void releaseTaskList(TaskList.TaskListName taskListName);
}

View File

@ -4,11 +4,9 @@ import ch.unisg.tapastasks.tasks.application.port.in.AddNewTaskToTaskListCommand
import ch.unisg.tapastasks.tasks.application.port.in.AddNewTaskToTaskListUseCase;
import ch.unisg.tapastasks.tasks.application.port.out.AddTaskPort;
import ch.unisg.tapastasks.tasks.application.port.out.NewTaskAddedEventPort;
import ch.unisg.tapastasks.tasks.application.port.out.TaskListLock;
import ch.unisg.tapastasks.tasks.domain.Task;
import ch.unisg.tapastasks.tasks.domain.NewTaskAddedEvent;
import ch.unisg.tapastasks.tasks.domain.TaskList;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@ -28,34 +26,32 @@ public class AddNewTaskToTaskListService implements AddNewTaskToTaskListUseCase
@Override
public Task addNewTaskToTaskList(AddNewTaskToTaskListCommand command) {
TaskList taskList = TaskList.getTapasTaskList();
Task newTask;
if (command.getOriginalTaskUri().isPresent() && command.getInputData().isPresent()) {
newTask = taskList.addNewTaskWithNameAndTypeAndOriginalTaskUriAndInputData(command.getTaskName(),
newTask = Task.createTaskWithNameAndTypeAndOriginalTaskUriAndInputData(command.getTaskName(),
command.getTaskType(), command.getOriginalTaskUri().get(), command.getInputData().get());
} else if (command.getOriginalTaskUri().isPresent()) {
newTask = taskList.addNewTaskWithNameAndTypeAndOriginalTaskUri(command.getTaskName(),
newTask = Task.createTaskWithNameAndTypeAndOriginalTaskUri(command.getTaskName(),
command.getTaskType(), command.getOriginalTaskUri().get());
} else if (command.getInputData().isPresent()) {
newTask = taskList.addNewTaskWithNameAndTypeAndInputData(command.getTaskName(),
newTask = Task.createTaskWithNameAndTypeAndInputData(command.getTaskName(),
command.getTaskType(), command.getInputData().get());
} else {
newTask = taskList.addNewTaskWithNameAndType(command.getTaskName(), command.getTaskType());
newTask = Task.createTaskWithNameAndType(command.getTaskName(), command.getTaskType());
}
addTaskToRepositoryPort.addTask(newTask);
//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.
//Domain events are usually rather "fat". In our implementation we simplify at this point. In general, it is
//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.
// TODO What to do with this task list name
if (newTask != null) {
addTaskToRepositoryPort.addTask(newTask);
NewTaskAddedEvent newTaskAdded = new NewTaskAddedEvent(
newTask.getTaskName().getValue(),
taskList.getTaskListName().getValue(),
"tapas-tasks-group1",
newTask.getTaskId().getValue(),
newTask.getTaskType().getValue(),
Objects.isNull(newTask.getInputData()) ? null : newTask.getInputData().getValue()

View File

@ -2,11 +2,12 @@ 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.AddTaskPort;
import ch.unisg.tapastasks.tasks.application.port.out.ExternalTaskExecutedEvent;
import ch.unisg.tapastasks.tasks.application.port.out.ExternalTaskExecutedEventHandler;
import ch.unisg.tapastasks.tasks.application.port.out.LoadTaskPort;
import ch.unisg.tapastasks.tasks.domain.Task;
import ch.unisg.tapastasks.tasks.domain.Task.*;
import ch.unisg.tapastasks.tasks.domain.TaskList;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
@ -18,26 +19,31 @@ import java.util.Optional;
@Transactional
public class CompleteTaskService implements CompleteTaskUseCase {
private final AddTaskPort addTaskToRepositoryPort;
private final LoadTaskPort loadTaskFromRepositoryPort;
private final ExternalTaskExecutedEventHandler externalTaskExecutedEventHandler;
@Override
public Task completeTask(CompleteTaskCommand command){
TaskList taskList = TaskList.getTapasTaskList();
Optional<Task> updatedTask = taskList.retrieveTaskById(command.getTaskId());
// retrieve the task based on ID
Optional<Task> taskFromRepo = Optional.ofNullable(loadTaskFromRepositoryPort.loadTask(command.getTaskId()));
Task newTask = updatedTask.get();
newTask.setOutputData(command.getOutputData());
newTask.setTaskStatus(new TaskStatus(Task.Status.EXECUTED));
Task updatedTask = taskFromRepo.get();
updatedTask.setOutputData(command.getOutputData());
updatedTask.setTaskStatus(new TaskStatus(Task.Status.EXECUTED));
if (newTask.getOriginalTaskUri() != null) {
// save updated task in repo
addTaskToRepositoryPort.addTask(updatedTask);
if (updatedTask.getOriginalTaskUri() != null) {
ExternalTaskExecutedEvent event = new ExternalTaskExecutedEvent(
newTask.getTaskId(),
newTask.getOriginalTaskUri(),
newTask.getOutputData()
updatedTask.getTaskId(),
updatedTask.getOriginalTaskUri(),
updatedTask.getOutputData()
);
externalTaskExecutedEventHandler.handleEvent(event);
}
return newTask;
return updatedTask;
}
}

View File

@ -3,10 +3,12 @@ package ch.unisg.tapastasks.tasks.application.service;
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.out.AddTaskPort;
import ch.unisg.tapastasks.tasks.application.port.out.CanTaskBeDeletedPort;
import ch.unisg.tapastasks.tasks.application.port.out.DeleteTaskPort;
import ch.unisg.tapastasks.tasks.application.port.out.LoadTaskPort;
import ch.unisg.tapastasks.tasks.domain.DeleteTaskEvent;
import ch.unisg.tapastasks.tasks.domain.Task;
import ch.unisg.tapastasks.tasks.domain.TaskList;
import jdk.jshell.spi.ExecutionControl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
@ -19,18 +21,20 @@ import java.util.Optional;
@Transactional
public class DeleteTaskService implements DeleteTaskUseCase {
private final DeleteTaskPort deleteTaskFromRepositoryPort;
private final LoadTaskPort loadTaskFromRepositoryPort;
private final CanTaskBeDeletedPort canTaskBeDeletedPort;
@Override
public Optional<Task> deleteTask(DeleteTaskCommand command){
// retrieve the task based on ID
Optional<Task> taskFromRepo = Optional.ofNullable(loadTaskFromRepositoryPort.loadTask(command.getTaskId()));
TaskList taskList = TaskList.getTapasTaskList();
Optional<Task> updatedTask = taskList.retrieveTaskById(command.getTaskId());
if(updatedTask.isPresent() && updatedTask.get().getTaskStatus().getValue().equals(Task.Status.OPEN)){
if(taskFromRepo.isPresent() && taskFromRepo.get().getTaskStatus().getValue().equals(Task.Status.OPEN)){
// If task exists, and it is open then we try deleting it from the roster
if(canTaskBeDeletedPort.canTaskBeDeletedEvent(new DeleteTaskEvent(command.getTaskId().getValue(), command.getTaskUri().getValue()))){
return taskList.deleteTaskById(command.getTaskId());
if(canTaskBeDeletedPort.canTaskBeDeletedEvent(new DeleteTaskEvent(command.getTaskId().getValue()))){
deleteTaskFromRepositoryPort.deleteTask(taskFromRepo.get().getTaskId());
return taskFromRepo;
}
}

View File

@ -1,18 +0,0 @@
package ch.unisg.tapastasks.tasks.application.service;
import ch.unisg.tapastasks.tasks.application.port.out.TaskListLock;
import ch.unisg.tapastasks.tasks.domain.TaskList;
import org.springframework.stereotype.Component;
@Component
public class NoOpTaskListLock implements TaskListLock {
@Override
public void lockTaskList(TaskList.TaskListName taskListName) {
//do nothing
}
@Override
public void releaseTaskList(TaskList.TaskListName taskListName) {
//do nothing
}
}

View File

@ -4,7 +4,6 @@ import ch.unisg.tapastasks.tasks.application.port.in.RetrieveTaskFromTaskListQue
import ch.unisg.tapastasks.tasks.application.port.in.RetrieveTaskFromTaskListUseCase;
import ch.unisg.tapastasks.tasks.application.port.out.LoadTaskPort;
import ch.unisg.tapastasks.tasks.domain.Task;
import ch.unisg.tapastasks.tasks.domain.TaskList;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@ -22,12 +21,9 @@ public class RetrieveTaskFromTaskListService implements RetrieveTaskFromTaskList
@Override
public Optional<Task> retrieveTaskFromTaskList(RetrieveTaskFromTaskListQuery query) {
TaskList taskList = TaskList.getTapasTaskList();
Optional<Task> task = taskList.retrieveTaskById(query.getTaskId());
Optional<Task> taskFromRepo = Optional.ofNullable(loadTaskFromRepositoryPort.loadTask(query.getTaskId()));
Optional<Task> taskFromRepo = Optional.ofNullable(loadTaskFromRepositoryPort.loadTask(query.getTaskId(), taskList.getTaskListName()));
return task;
return taskFromRepo;
}
}

View File

@ -2,9 +2,10 @@ package ch.unisg.tapastasks.tasks.application.service;
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.out.AddTaskPort;
import ch.unisg.tapastasks.tasks.application.port.out.LoadTaskPort;
import ch.unisg.tapastasks.tasks.domain.Task;
import ch.unisg.tapastasks.tasks.domain.Task.*;
import ch.unisg.tapastasks.tasks.domain.TaskList;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
@ -16,16 +17,21 @@ import java.util.Optional;
@Transactional
public class TaskAssignedService implements TaskAssignedUseCase {
private final AddTaskPort addTaskToRepositoryPort;
private final LoadTaskPort loadTaskFromRepositoryPort;
@Override
public Task assignTask(TaskAssignedCommand command) {
// retrieve the task based on ID
TaskList taskList = TaskList.getTapasTaskList();
Optional<Task> task = taskList.retrieveTaskById(command.getTaskId());
Optional<Task> taskFromRepo = Optional.ofNullable(loadTaskFromRepositoryPort.loadTask(command.getTaskId()));
// update the status to assigned
Task updatedTask = task.get();
Task updatedTask = taskFromRepo.get();
updatedTask.setTaskStatus(new TaskStatus(Status.ASSIGNED));
// save updated task in repo
addTaskToRepositoryPort.addTask(updatedTask);
return updatedTask;
}
}

View File

@ -2,10 +2,8 @@ package ch.unisg.tapastasks.tasks.domain;
public class DeleteTaskEvent {
public String taskId;
public String taskUri;
public DeleteTaskEvent(String taskId, String taskUri){
public DeleteTaskEvent(String taskId){
this.taskId = taskId;
this.taskUri = taskUri;
}
}

View File

@ -91,7 +91,7 @@ public class Task {
this.outputData = outputData;
}
protected static Task createTaskWithNameAndType(TaskName name, TaskType type) {
public static Task createTaskWithNameAndType(TaskName name, TaskType type) {
return new Task(name, type);
}
@ -100,12 +100,12 @@ public class Task {
return new Task(name, type, originalTaskUri);
}
protected static Task createTaskWithNameAndTypeAndInputData(TaskName name, TaskType type,
public static Task createTaskWithNameAndTypeAndInputData(TaskName name, TaskType type,
InputData inputData) {
return new Task(name, type, inputData);
}
protected static Task createTaskWithNameAndTypeAndOriginalTaskUriAndInputData(TaskName name, TaskType type,
public static Task createTaskWithNameAndTypeAndOriginalTaskUriAndInputData(TaskName name, TaskType type,
OriginalTaskUri originalTaskUri, InputData inputData) {
return new Task(name, type, originalTaskUri, inputData);
}

View File

@ -1,152 +0,0 @@
package ch.unisg.tapastasks.tasks.domain;
import lombok.Getter;
import lombok.Value;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
/**This is our aggregate root**/
public class TaskList {
Logger logger = Logger.getLogger(TaskList.class.getName());
@Getter
private final TaskListName taskListName;
@Getter
private final ListOfTasks listOfTasks;
//Note: We do not care about the management of task lists, there is only one within this service
//--> using the Singleton pattern here to make lives easy; we will later load it from a repo
private static final TaskList taskList = new TaskList(new TaskListName("tapas-tasks-group1"));
private TaskList(TaskListName taskListName) {
this.taskListName = taskListName;
this.listOfTasks = new ListOfTasks(new LinkedList<Task>());
}
public static TaskList getTapasTaskList() {
return taskList;
}
//Only the aggregate root is allowed to create new tasks and add them to the task list.
//Note: Here we could add some sophisticated invariants/business rules that the aggregate root checks
public Task addNewTaskWithNameAndType(Task.TaskName name, Task.TaskType type) {
Task newTask = Task.createTaskWithNameAndType(name, type);
this.addNewTaskToList(newTask);
return newTask;
}
public Task addNewTaskWithNameAndTypeAndOriginalTaskUri(Task.TaskName name, Task.TaskType type,
Task.OriginalTaskUri originalTaskUri) {
Task newTask = Task.createTaskWithNameAndTypeAndOriginalTaskUri(name, type, originalTaskUri);
this.addNewTaskToList(newTask);
return newTask;
}
public Task addNewTaskWithNameAndTypeAndInputData(Task.TaskName name, Task.TaskType type,
Task.InputData inputData) {
Task newTask = Task.createTaskWithNameAndTypeAndInputData(name, type, inputData);
this.addNewTaskToList(newTask);
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
//domain event publisher and subscribers (see "Implementing Domain-Driven Design by V. Vernon, pp. 296ff).
listOfTasks.value.add(newTask);
String message = "New task created! Id: " + newTask.getTaskId().getValue() +
" | Name: " + newTask.getTaskName().getValue();
if (newTask.getInputData() != null) {
message += " | Input data: " + newTask.getInputData().getValue();
}
logger.log(Level.INFO, message);
logger.log(Level.INFO, "Number of tasks: {0}", listOfTasks.value.size());
}
public Optional<Task> retrieveTaskById(Task.TaskId id) {
for (Task task : listOfTasks.value) {
if (task.getTaskId().getValue().equalsIgnoreCase(id.getValue())) {
return Optional.of(task);
}
}
return Optional.empty();
}
public Optional<Task> deleteTaskById(Task.TaskId id) {
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<Task.ServiceProvider> serviceProvider)
throws TaskNotFoundException {
return changeTaskStatus(id, new Task.TaskStatus(Task.Status.ASSIGNED), serviceProvider, Optional.empty());
}
public Task changeTaskStatusToRunning(Task.TaskId id, Optional<Task.ServiceProvider> serviceProvider)
throws TaskNotFoundException {
return changeTaskStatus(id, new Task.TaskStatus(Task.Status.RUNNING), serviceProvider, Optional.empty());
}
public Task changeTaskStatusToExecuted(Task.TaskId id, Optional<Task.ServiceProvider> serviceProvider,
Optional<Task.OutputData> outputData) throws TaskNotFoundException {
return changeTaskStatus(id, new Task.TaskStatus(Task.Status.EXECUTED), serviceProvider, outputData);
}
private Task changeTaskStatus(Task.TaskId id, Task.TaskStatus status, Optional<Task.ServiceProvider> serviceProvider,
Optional<Task.OutputData> outputData) {
Optional<Task> 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 {
private String value;
}
@Value
public static class ListOfTasks {
private List<Task> value;
}
}

View File

@ -3,7 +3,6 @@ package ch.unisg.tapastasks;
import ch.unisg.tapastasks.tasks.adapter.in.formats.TaskJsonRepresentation;
import ch.unisg.tapastasks.tasks.application.port.out.AddTaskPort;
import ch.unisg.tapastasks.tasks.domain.Task;
import ch.unisg.tapastasks.tasks.domain.TaskList;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
@ -42,7 +41,7 @@ public class AddNewTaskToTaskListSystemTest {
then(respTaskId).isNotEmpty();
then(respTaskName).isEqualTo(taskName.getValue());
then(respTaskType).isEqualTo(taskType.getValue());
then(TaskList.getTapasTaskList().getListOfTasks().getValue()).hasSize(1);
//then(TaskList.getTapasTaskList().getListOfTasks().getValue()).hasSize(1);
}
@ -51,7 +50,7 @@ public class AddNewTaskToTaskListSystemTest {
Task.TaskType taskType,
Task.OriginalTaskUri originalTaskUri) throws JSONException {
TaskList.getTapasTaskList().getListOfTasks().getValue().clear();
//TaskList.getTapasTaskList().getListOfTasks().getValue().clear();
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", TaskJsonRepresentation.MEDIA_TYPE);

View File

@ -1,7 +1,6 @@
package ch.unisg.tapastasks.tasks.adapter.out.persistence.mongodb;
import ch.unisg.tapastasks.tasks.domain.Task;
import ch.unisg.tapastasks.tasks.domain.TaskList;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo;
@ -45,7 +44,7 @@ public class TaskPersistenceAdapterTest {
);
adapterUnderTest.addTask(testTask);
MongoTaskDocument retrievedDoc = taskRepository.findByTaskId(testTaskId,testTaskListName);
MongoTaskDocument retrievedDoc = taskRepository.findByTaskId(testTaskId);
assertThat(retrievedDoc.taskId).isEqualTo(testTaskId);
assertThat(retrievedDoc.taskName).isEqualTo(testTaskName);

View File

@ -3,10 +3,8 @@ package ch.unisg.tapastasks.tasks.application.service;
import ch.unisg.tapastasks.tasks.application.port.in.AddNewTaskToTaskListCommand;
import ch.unisg.tapastasks.tasks.application.port.out.AddTaskPort;
import ch.unisg.tapastasks.tasks.application.port.out.NewTaskAddedEventPort;
import ch.unisg.tapastasks.tasks.application.port.out.TaskListLock;
import ch.unisg.tapastasks.tasks.domain.NewTaskAddedEvent;
import ch.unisg.tapastasks.tasks.domain.Task;
import ch.unisg.tapastasks.tasks.domain.TaskList;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
@ -19,7 +17,6 @@ import static org.assertj.core.api.Assertions.*;
public class AddNewTaskToTaskListServiceTest {
private final AddTaskPort addTaskPort = Mockito.mock(AddTaskPort.class);
private final TaskListLock taskListLock = Mockito.mock(TaskListLock.class);
private final NewTaskAddedEventPort newTaskAddedEventPort = Mockito.mock(NewTaskAddedEventPort.class);
private final AddNewTaskToTaskListService addNewTaskToTaskListService = new AddNewTaskToTaskListService(
newTaskAddedEventPort, addTaskPort);
@ -46,11 +43,6 @@ public class AddNewTaskToTaskListServiceTest {
}
private TaskList givenAnEmptyTaskList(TaskList taskList) {
taskList.getListOfTasks().getValue().clear();
return taskList;
}
private Task givenATaskWithNameAndTypeAndURI(Task.TaskName taskName, Task.TaskType taskType,
Optional<Task.OriginalTaskUri> originalTaskUri) {
Task task = Mockito.mock(Task.class);

View File

@ -11,7 +11,7 @@ public class TaskListTest {
@Test
void addNewTaskToTaskListSuccess() {
TaskList taskList = TaskList.getTapasTaskList();
/*TaskList taskList = TaskList.getTapasTaskList();
taskList.getListOfTasks().getValue().clear();
Task newTask = taskList.addNewTaskWithNameAndType(new Task.TaskName("My-Test-Task"),
new Task.TaskType("My-Test-Type"));
@ -19,11 +19,12 @@ public class TaskListTest {
assertThat(newTask.getTaskName().getValue()).isEqualTo("My-Test-Task");
assertThat(taskList.getListOfTasks().getValue()).hasSize(1);
assertThat(taskList.getListOfTasks().getValue().get(0)).isEqualTo(newTask);
*/
}
@Test
void retrieveTaskSuccess() {
/*
TaskList taskList = TaskList.getTapasTaskList();
Task newTask = taskList.addNewTaskWithNameAndType(new Task.TaskName("My-Test-Task2"),
new Task.TaskType("My-Test-Type2"));
@ -31,11 +32,12 @@ public class TaskListTest {
Task retrievedTask = taskList.retrieveTaskById(newTask.getTaskId()).get();
assertThat(retrievedTask).isEqualTo(newTask);
*/
}
@Test
void retrieveTaskFailure() {
/*
TaskList taskList = TaskList.getTapasTaskList();
Task newTask = taskList.addNewTaskWithNameAndType(new Task.TaskName("My-Test-Task3"),
new Task.TaskType("My-Test-Type3"));
@ -45,5 +47,7 @@ public class TaskListTest {
Optional<Task> retrievedTask = taskList.retrieveTaskById(fakeId);
assertThat(retrievedTask.isPresent()).isFalse();
*/
}
}