From ae9e3acbd2da6f26ea8a8aa734f8e5c3c07bd386 Mon Sep 17 00:00:00 2001 From: "julius.lautz" Date: Sun, 21 Nov 2021 19:40:32 +0100 Subject: [PATCH] added database to roster --- roster/pom.xml | 6 ++- .../ch/unisg/roster/RosterApplication.java | 15 ++++++ .../mongodb/MongoRosterDocument.java | 21 ++++++++ .../out/persistence/mongodb/RosterMapper.java | 23 +++++++++ .../mongodb/RosterPersistenceAdapter.java | 50 +++++++++++++++++++ .../persistence/mongodb/RosterRepository.java | 9 ++++ .../port/in/AddRosterItemPort.java | 9 ++++ .../application/port/in/DeleteRosterItem.java | 8 +++ .../port/in/LoadRosterItemPort.java | 12 +++++ .../service/ApplyForTaskService.java | 4 ++ .../service/TaskCompletedService.java | 3 ++ .../ch/unisg/roster/roster/domain/Roster.java | 7 +++ .../roster/roster/domain/RosterItem.java | 4 ++ 13 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 roster/src/main/java/ch/unisg/roster/roster/adapter/out/persistence/mongodb/MongoRosterDocument.java create mode 100644 roster/src/main/java/ch/unisg/roster/roster/adapter/out/persistence/mongodb/RosterMapper.java create mode 100644 roster/src/main/java/ch/unisg/roster/roster/adapter/out/persistence/mongodb/RosterPersistenceAdapter.java create mode 100644 roster/src/main/java/ch/unisg/roster/roster/adapter/out/persistence/mongodb/RosterRepository.java create mode 100644 roster/src/main/java/ch/unisg/roster/roster/application/port/in/AddRosterItemPort.java create mode 100644 roster/src/main/java/ch/unisg/roster/roster/application/port/in/DeleteRosterItem.java create mode 100644 roster/src/main/java/ch/unisg/roster/roster/application/port/in/LoadRosterItemPort.java diff --git a/roster/pom.xml b/roster/pom.xml index 791e0d0..8596ef7 100644 --- a/roster/pom.xml +++ b/roster/pom.xml @@ -73,9 +73,13 @@ common 0.0.1-SNAPSHOT + + org.springframework.data + spring-data-mongodb + - + diff --git a/roster/src/main/java/ch/unisg/roster/RosterApplication.java b/roster/src/main/java/ch/unisg/roster/RosterApplication.java index 1b12ca3..58cd429 100644 --- a/roster/src/main/java/ch/unisg/roster/RosterApplication.java +++ b/roster/src/main/java/ch/unisg/roster/RosterApplication.java @@ -1,8 +1,13 @@ package ch.unisg.roster; +import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; +import ch.unisg.roster.roster.adapter.out.persistence.mongodb.RosterRepository; +import ch.unisg.roster.roster.application.port.in.LoadRosterItemPort; +import ch.unisg.roster.roster.domain.Roster; +import ch.unisg.roster.roster.domain.RosterItem; import org.eclipse.paho.client.mqttv3.MqttException; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -11,19 +16,24 @@ import org.springframework.core.env.ConfigurableEnvironment; import ch.unisg.roster.roster.adapter.common.clients.TapasMqttClient; import ch.unisg.roster.roster.adapter.in.messaging.mqtt.ExecutorEventMqttListener; import ch.unisg.roster.roster.adapter.in.messaging.mqtt.ExecutorEventsMqttDispatcher; +import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; @SpringBootApplication +@EnableMongoRepositories(basePackageClasses = RosterRepository.class) public class RosterApplication { static Logger logger = Logger.getLogger(RosterApplication.class.getName()); private static ConfigurableEnvironment ENVIRONMENT; + private static final LoadRosterItemPort loadRosterItemPort; + public static void main(String[] args) { SpringApplication rosterApp = new SpringApplication(RosterApplication.class); ENVIRONMENT = rosterApp.run(args).getEnvironment(); bootstrapMarketplaceWithMqtt(); + initialiseRoster(); } /** @@ -42,4 +52,9 @@ public class RosterApplication { } } + private static void initialiseRoster(){ + List rosterItemList = loadRosterItemPort.loadAllRosterItems(); + Roster.getInstance().initialiseRoster(rosterItemList); + } + } diff --git a/roster/src/main/java/ch/unisg/roster/roster/adapter/out/persistence/mongodb/MongoRosterDocument.java b/roster/src/main/java/ch/unisg/roster/roster/adapter/out/persistence/mongodb/MongoRosterDocument.java new file mode 100644 index 0000000..f0d133b --- /dev/null +++ b/roster/src/main/java/ch/unisg/roster/roster/adapter/out/persistence/mongodb/MongoRosterDocument.java @@ -0,0 +1,21 @@ +package ch.unisg.roster.roster.adapter.out.persistence.mongodb; + +import lombok.Data; +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +@Data +@Document(collection="roster") +public class MongoRosterDocument { + + @Id + public String taskId; + public String taskType; + public String executorURI; + + public MongoRosterDocument(String taskId, String taskType, String executorURI){ + this.taskId = taskId; + this.taskType = taskType; + this.executorURI = executorURI; + } +} diff --git a/roster/src/main/java/ch/unisg/roster/roster/adapter/out/persistence/mongodb/RosterMapper.java b/roster/src/main/java/ch/unisg/roster/roster/adapter/out/persistence/mongodb/RosterMapper.java new file mode 100644 index 0000000..940088f --- /dev/null +++ b/roster/src/main/java/ch/unisg/roster/roster/adapter/out/persistence/mongodb/RosterMapper.java @@ -0,0 +1,23 @@ +package ch.unisg.roster.roster.adapter.out.persistence.mongodb; + + +import ch.unisg.common.valueobject.ExecutorURI; +import ch.unisg.roster.roster.domain.RosterItem; +import org.springframework.stereotype.Component; + +@Component +public class RosterMapper { + + RosterItem maptoDomainEntity(MongoRosterDocument rosterItem) { + return new RosterItem(rosterItem.taskId, rosterItem.taskType, + new ExecutorURI(rosterItem.executorURI)); + } + + MongoRosterDocument mapToMongoDocument(RosterItem rosterItem){ + return new MongoRosterDocument( + rosterItem.getTaskID(), + rosterItem.getTaskType(), + rosterItem.getExecutorURI().getValue().toString()); + } +} + diff --git a/roster/src/main/java/ch/unisg/roster/roster/adapter/out/persistence/mongodb/RosterPersistenceAdapter.java b/roster/src/main/java/ch/unisg/roster/roster/adapter/out/persistence/mongodb/RosterPersistenceAdapter.java new file mode 100644 index 0000000..9239ff0 --- /dev/null +++ b/roster/src/main/java/ch/unisg/roster/roster/adapter/out/persistence/mongodb/RosterPersistenceAdapter.java @@ -0,0 +1,50 @@ +package ch.unisg.roster.roster.adapter.out.persistence.mongodb; + + +import ch.unisg.roster.roster.application.port.in.AddRosterItemPort; +import ch.unisg.roster.roster.application.port.in.DeleteRosterItem; +import ch.unisg.roster.roster.application.port.in.LoadRosterItemPort; +import ch.unisg.roster.roster.domain.RosterItem; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; + +@Component +@RequiredArgsConstructor +public class RosterPersistenceAdapter implements AddRosterItemPort, LoadRosterItemPort, DeleteRosterItem { + + @Autowired + private final RosterRepository rosterRepository; + private final RosterMapper rosterMapper; + + @Override + public void addRosterItem(RosterItem rosterItem){ + MongoRosterDocument mongoRosterDocument = rosterMapper.mapToMongoDocument(rosterItem); + rosterRepository.save(mongoRosterDocument); + } + + @Override + public RosterItem loadRosterItem(String taskId){ + MongoRosterDocument mongoRosterDocument = rosterRepository.findByTaskId(taskId); + RosterItem rosterItem = rosterMapper.maptoDomainEntity(mongoRosterDocument); + return rosterItem; + } + + @Override + public List loadAllRosterItems(){ + List rosterList = rosterRepository.findAll(); + List rosterItemList = new ArrayList<>(); + for(MongoRosterDocument rosterDocument : rosterList){ + rosterItemList.add(rosterMapper.maptoDomainEntity(rosterDocument)); + } + return rosterItemList; + } + + @Override + public void deleteRosterItem(String taskId){ + rosterRepository.deleteById(taskId); + } +} diff --git a/roster/src/main/java/ch/unisg/roster/roster/adapter/out/persistence/mongodb/RosterRepository.java b/roster/src/main/java/ch/unisg/roster/roster/adapter/out/persistence/mongodb/RosterRepository.java new file mode 100644 index 0000000..eff2b56 --- /dev/null +++ b/roster/src/main/java/ch/unisg/roster/roster/adapter/out/persistence/mongodb/RosterRepository.java @@ -0,0 +1,9 @@ +package ch.unisg.roster.roster.adapter.out.persistence.mongodb; + +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface RosterRepository extends MongoRepository{ + public MongoRosterDocument findByTaskId(String taskId); +} diff --git a/roster/src/main/java/ch/unisg/roster/roster/application/port/in/AddRosterItemPort.java b/roster/src/main/java/ch/unisg/roster/roster/application/port/in/AddRosterItemPort.java new file mode 100644 index 0000000..8b1a62e --- /dev/null +++ b/roster/src/main/java/ch/unisg/roster/roster/application/port/in/AddRosterItemPort.java @@ -0,0 +1,9 @@ +package ch.unisg.roster.roster.application.port.in; + +import ch.unisg.roster.roster.domain.RosterItem; + +public interface AddRosterItemPort { + + void addRosterItem(RosterItem rosterItem); + +} diff --git a/roster/src/main/java/ch/unisg/roster/roster/application/port/in/DeleteRosterItem.java b/roster/src/main/java/ch/unisg/roster/roster/application/port/in/DeleteRosterItem.java new file mode 100644 index 0000000..d014e18 --- /dev/null +++ b/roster/src/main/java/ch/unisg/roster/roster/application/port/in/DeleteRosterItem.java @@ -0,0 +1,8 @@ +package ch.unisg.roster.roster.application.port.in; + +import ch.unisg.roster.roster.domain.RosterItem; + +public interface DeleteRosterItem { + + void deleteRosterItem(String taskId); +} diff --git a/roster/src/main/java/ch/unisg/roster/roster/application/port/in/LoadRosterItemPort.java b/roster/src/main/java/ch/unisg/roster/roster/application/port/in/LoadRosterItemPort.java new file mode 100644 index 0000000..69ccc27 --- /dev/null +++ b/roster/src/main/java/ch/unisg/roster/roster/application/port/in/LoadRosterItemPort.java @@ -0,0 +1,12 @@ +package ch.unisg.roster.roster.application.port.in; + +import ch.unisg.roster.roster.domain.RosterItem; + +import java.util.List; + +public interface LoadRosterItemPort { + + RosterItem loadRosterItem(String taskId); + + List loadAllRosterItems(); +} diff --git a/roster/src/main/java/ch/unisg/roster/roster/application/service/ApplyForTaskService.java b/roster/src/main/java/ch/unisg/roster/roster/application/service/ApplyForTaskService.java index 26b75aa..897f234 100644 --- a/roster/src/main/java/ch/unisg/roster/roster/application/service/ApplyForTaskService.java +++ b/roster/src/main/java/ch/unisg/roster/roster/application/service/ApplyForTaskService.java @@ -2,6 +2,8 @@ package ch.unisg.roster.roster.application.service; import javax.transaction.Transactional; +import ch.unisg.roster.roster.application.port.in.AddRosterItemPort; +import ch.unisg.roster.roster.domain.RosterItem; import org.springframework.stereotype.Component; import ch.unisg.roster.roster.application.port.in.ApplyForTaskCommand; @@ -18,6 +20,7 @@ import lombok.RequiredArgsConstructor; public class ApplyForTaskService implements ApplyForTaskUseCase { private final TaskAssignedEventPort taskAssignedEventPort; + private final AddRosterItemPort addRosterItemPort; /** * Checks if a task is available and assignes it to the executor. If task got assigned a task @@ -31,6 +34,7 @@ public class ApplyForTaskService implements ApplyForTaskUseCase { if (task != null) { taskAssignedEventPort.publishTaskAssignedEvent(new TaskAssignedEvent(task.getTaskID())); + addRosterItemPort.addRosterItem(new RosterItem(task.getTaskID(), task.getTaskType().getValue(), command.getExecutorURI())); } return task; diff --git a/roster/src/main/java/ch/unisg/roster/roster/application/service/TaskCompletedService.java b/roster/src/main/java/ch/unisg/roster/roster/application/service/TaskCompletedService.java index 69b65d1..041ccac 100644 --- a/roster/src/main/java/ch/unisg/roster/roster/application/service/TaskCompletedService.java +++ b/roster/src/main/java/ch/unisg/roster/roster/application/service/TaskCompletedService.java @@ -2,6 +2,7 @@ package ch.unisg.roster.roster.application.service; import javax.transaction.Transactional; +import ch.unisg.roster.roster.application.port.in.DeleteRosterItem; import org.springframework.stereotype.Component; import ch.unisg.roster.roster.application.port.in.TaskCompletedCommand; @@ -17,6 +18,7 @@ import lombok.RequiredArgsConstructor; public class TaskCompletedService implements TaskCompletedUseCase { private final TaskCompletedEventPort taskCompletedEventPort; + private final DeleteRosterItem deleteRosterItem; /** * Completes the task in the roster and publishes a task completed event. @@ -26,6 +28,7 @@ public class TaskCompletedService implements TaskCompletedUseCase { public void taskCompleted(TaskCompletedCommand command) { Roster.getInstance().taskCompleted(command.getTaskID()); + deleteRosterItem.deleteRosterItem(command.getTaskID()); taskCompletedEventPort.publishTaskCompleted(new TaskCompletedEvent(command.getTaskID(), command.getTaskStatus(), command.getTaskResult())); diff --git a/roster/src/main/java/ch/unisg/roster/roster/domain/Roster.java b/roster/src/main/java/ch/unisg/roster/roster/domain/Roster.java index cc9a0a6..a6b7f19 100644 --- a/roster/src/main/java/ch/unisg/roster/roster/domain/Roster.java +++ b/roster/src/main/java/ch/unisg/roster/roster/domain/Roster.java @@ -3,6 +3,7 @@ package ch.unisg.roster.roster.domain; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -83,4 +84,10 @@ public class Roster { return queues.get(taskType.getValue()).removeIf(task -> task.getTaskID().equalsIgnoreCase(taskID)); } + public void initialiseRoster(List rosterItemList){ + for(RosterItem rosterItem : rosterItemList){ + rosterMap.put(rosterItem.getTaskID(), rosterItem); + } + } + } diff --git a/roster/src/main/java/ch/unisg/roster/roster/domain/RosterItem.java b/roster/src/main/java/ch/unisg/roster/roster/domain/RosterItem.java index cc39c6c..dbe5a87 100644 --- a/roster/src/main/java/ch/unisg/roster/roster/domain/RosterItem.java +++ b/roster/src/main/java/ch/unisg/roster/roster/domain/RosterItem.java @@ -20,4 +20,8 @@ public class RosterItem { this.executorURI = executorURI; } + public static RosterItem withIDTypeExecutorURI(String taskId, String taskType, + ExecutorURI executorURI){ + return new RosterItem(taskId, taskType, executorURI); + } }