From 2aeaf86e59ea79c01b977b140cc66b73b8480f26 Mon Sep 17 00:00:00 2001 From: rahimiankeanu Date: Sun, 21 Nov 2021 18:07:07 +0100 Subject: [PATCH] Persistence layer in executor pool (RemoveExecutorFrom... under Service folder still has an error) --- executor-pool/pom.xml | 9 +++++ .../persistence/mongodb/ExecutorMapper.java | 31 ++++++++++++++ .../mongodb/ExecutorPersistenceAdapter.java | 40 +++++++++++++++++++ .../mongodb/ExecutorRepository.java | 15 +++++++ .../mongodb/MongoExecutorDocument.java | 16 ++++++++ .../application/port/out/AddExecutorPort.java | 9 +++++ .../port/out/ExecutorAddedEventPort.java | 1 - .../port/out/LoadExecutorPort.java | 9 +++++ .../port/out/RemoveExecutorPort.java | 9 +++++ .../AddNewExecutorToExecutorPoolService.java | 8 +++- ...RemoveExecutorFromExecutorPoolService.java | 8 +++- .../domain/ExecutorRemovedEvent.java | 1 - 12 files changed, 150 insertions(+), 6 deletions(-) create mode 100644 executor-pool/src/main/java/ch/unisg/executorpool/adapter/out/persistence/mongodb/ExecutorMapper.java create mode 100644 executor-pool/src/main/java/ch/unisg/executorpool/adapter/out/persistence/mongodb/ExecutorPersistenceAdapter.java create mode 100644 executor-pool/src/main/java/ch/unisg/executorpool/adapter/out/persistence/mongodb/ExecutorRepository.java create mode 100644 executor-pool/src/main/java/ch/unisg/executorpool/adapter/out/persistence/mongodb/MongoExecutorDocument.java create mode 100644 executor-pool/src/main/java/ch/unisg/executorpool/application/port/out/AddExecutorPort.java create mode 100644 executor-pool/src/main/java/ch/unisg/executorpool/application/port/out/LoadExecutorPort.java create mode 100644 executor-pool/src/main/java/ch/unisg/executorpool/application/port/out/RemoveExecutorPort.java diff --git a/executor-pool/pom.xml b/executor-pool/pom.xml index 512235d..e8e3774 100644 --- a/executor-pool/pom.xml +++ b/executor-pool/pom.xml @@ -68,6 +68,15 @@ org.eclipse.paho.client.mqttv3 1.2.5 compile + + + org.springframework.data + spring-data-mongodb + 3.2.6 + + + org.springframework.boot + spring-boot-starter-data-mongodb diff --git a/executor-pool/src/main/java/ch/unisg/executorpool/adapter/out/persistence/mongodb/ExecutorMapper.java b/executor-pool/src/main/java/ch/unisg/executorpool/adapter/out/persistence/mongodb/ExecutorMapper.java new file mode 100644 index 0000000..8948522 --- /dev/null +++ b/executor-pool/src/main/java/ch/unisg/executorpool/adapter/out/persistence/mongodb/ExecutorMapper.java @@ -0,0 +1,31 @@ +package ch.unisg.executorpool.adapter.out.persistence.mongodb; + +import java.net.URI; +import java.net.URISyntaxException; + +import org.springframework.stereotype.Component; + +import ch.unisg.executorpool.domain.ExecutorClass; + +@Component +public class ExecutorMapper { + + ExecutorClass mapToDomainEntity(MongoExecutorDocument executorClass) { + try { + return new ExecutorClass( + new ExecutorClass.ExecutorUri(new URI(executorClass.executorUri)), + new ExecutorClass.ExecutorTaskType(executorClass.executorTaskType) + ); + } catch (URISyntaxException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return null; + } + } + + MongoExecutorDocument mapToMongoDocument(ExecutorClass executorClass) { + return new MongoExecutorDocument(executorClass.getExecutorUri().getValue().toString(), + executorClass.getExecutorTaskType().getValue() + ); + } +} diff --git a/executor-pool/src/main/java/ch/unisg/executorpool/adapter/out/persistence/mongodb/ExecutorPersistenceAdapter.java b/executor-pool/src/main/java/ch/unisg/executorpool/adapter/out/persistence/mongodb/ExecutorPersistenceAdapter.java new file mode 100644 index 0000000..a165096 --- /dev/null +++ b/executor-pool/src/main/java/ch/unisg/executorpool/adapter/out/persistence/mongodb/ExecutorPersistenceAdapter.java @@ -0,0 +1,40 @@ +package ch.unisg.executorpool.adapter.out.persistence.mongodb; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import ch.unisg.executorpool.application.port.out.AddExecutorPort; +import ch.unisg.executorpool.application.port.out.LoadExecutorPort; +import ch.unisg.executorpool.application.port.out.RemoveExecutorPort; +import ch.unisg.executorpool.domain.ExecutorClass; +import lombok.RequiredArgsConstructor; + +@Component +@RequiredArgsConstructor +public class ExecutorPersistenceAdapter implements AddExecutorPort, RemoveExecutorPort, LoadExecutorPort { + + @Autowired + private final ExecutorRepository executorRepository; + + private final ExecutorMapper executorMapper; + + @Override + public void addExecutor(ExecutorClass executorClass) { + MongoExecutorDocument mongoExecutorDocument = executorMapper.mapToMongoDocument(executorClass); + executorRepository.save(mongoExecutorDocument); + } + + @Override + public void removeExecutor(ExecutorClass executorClass) { + MongoExecutorDocument mongoExecutorDocument = executorMapper.mapToMongoDocument(executorClass); + executorRepository.delete(mongoExecutorDocument); + } + + @Override + public ExecutorClass loadExecutor(ExecutorClass.ExecutorUri executorUri, ExecutorClass.ExecutorTaskType executorTaskType) { + MongoExecutorDocument mongoExecutorDocument = executorRepository.findByExecutorUri(executorUri.getValue().toString(), executorTaskType.getValue()); + ExecutorClass executorClass = executorMapper.mapToDomainEntity(mongoExecutorDocument); + return executorClass; + } + +} diff --git a/executor-pool/src/main/java/ch/unisg/executorpool/adapter/out/persistence/mongodb/ExecutorRepository.java b/executor-pool/src/main/java/ch/unisg/executorpool/adapter/out/persistence/mongodb/ExecutorRepository.java new file mode 100644 index 0000000..36c3fd0 --- /dev/null +++ b/executor-pool/src/main/java/ch/unisg/executorpool/adapter/out/persistence/mongodb/ExecutorRepository.java @@ -0,0 +1,15 @@ +package ch.unisg.executorpool.adapter.out.persistence.mongodb; + +import java.util.List; + +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface ExecutorRepository extends MongoRepository { + + public MongoExecutorDocument findByExecutorUri(String executorUri, String executorTaskType); + + public List findByExecutorTaskType(String executorTaskType); + +} diff --git a/executor-pool/src/main/java/ch/unisg/executorpool/adapter/out/persistence/mongodb/MongoExecutorDocument.java b/executor-pool/src/main/java/ch/unisg/executorpool/adapter/out/persistence/mongodb/MongoExecutorDocument.java new file mode 100644 index 0000000..3c94ae7 --- /dev/null +++ b/executor-pool/src/main/java/ch/unisg/executorpool/adapter/out/persistence/mongodb/MongoExecutorDocument.java @@ -0,0 +1,16 @@ +package ch.unisg.executorpool.adapter.out.persistence.mongodb; + +import lombok.Data; + +@Data +public class MongoExecutorDocument { + + public String executorUri; + public String executorTaskType; + + public MongoExecutorDocument(String executorUri, String executorTaskType) { + + this.executorUri = executorUri; + this.executorTaskType = executorTaskType; + } +} diff --git a/executor-pool/src/main/java/ch/unisg/executorpool/application/port/out/AddExecutorPort.java b/executor-pool/src/main/java/ch/unisg/executorpool/application/port/out/AddExecutorPort.java new file mode 100644 index 0000000..8e280e9 --- /dev/null +++ b/executor-pool/src/main/java/ch/unisg/executorpool/application/port/out/AddExecutorPort.java @@ -0,0 +1,9 @@ +package ch.unisg.executorpool.application.port.out; + +import ch.unisg.executorpool.domain.ExecutorClass; + +public interface AddExecutorPort { + + void addExecutor(ExecutorClass executorClass); + +} diff --git a/executor-pool/src/main/java/ch/unisg/executorpool/application/port/out/ExecutorAddedEventPort.java b/executor-pool/src/main/java/ch/unisg/executorpool/application/port/out/ExecutorAddedEventPort.java index ad75c75..a94cf16 100644 --- a/executor-pool/src/main/java/ch/unisg/executorpool/application/port/out/ExecutorAddedEventPort.java +++ b/executor-pool/src/main/java/ch/unisg/executorpool/application/port/out/ExecutorAddedEventPort.java @@ -1,7 +1,6 @@ package ch.unisg.executorpool.application.port.out; import ch.unisg.executorpool.domain.ExecutorAddedEvent; -import org.eclipse.paho.client.mqttv3.MqttException; public interface ExecutorAddedEventPort { void publishExecutorAddedEvent(ExecutorAddedEvent event); diff --git a/executor-pool/src/main/java/ch/unisg/executorpool/application/port/out/LoadExecutorPort.java b/executor-pool/src/main/java/ch/unisg/executorpool/application/port/out/LoadExecutorPort.java new file mode 100644 index 0000000..d7cb18a --- /dev/null +++ b/executor-pool/src/main/java/ch/unisg/executorpool/application/port/out/LoadExecutorPort.java @@ -0,0 +1,9 @@ +package ch.unisg.executorpool.application.port.out; + +import ch.unisg.executorpool.domain.ExecutorClass; + +public interface LoadExecutorPort { + + ExecutorClass loadExecutor(ExecutorClass.ExecutorUri executorUri, ExecutorClass.ExecutorTaskType executorTaskType); + +} diff --git a/executor-pool/src/main/java/ch/unisg/executorpool/application/port/out/RemoveExecutorPort.java b/executor-pool/src/main/java/ch/unisg/executorpool/application/port/out/RemoveExecutorPort.java new file mode 100644 index 0000000..5813891 --- /dev/null +++ b/executor-pool/src/main/java/ch/unisg/executorpool/application/port/out/RemoveExecutorPort.java @@ -0,0 +1,9 @@ +package ch.unisg.executorpool.application.port.out; + +import ch.unisg.executorpool.domain.ExecutorClass; + +public interface RemoveExecutorPort { + + void removeExecutor(ExecutorClass executorClass); + +} diff --git a/executor-pool/src/main/java/ch/unisg/executorpool/application/service/AddNewExecutorToExecutorPoolService.java b/executor-pool/src/main/java/ch/unisg/executorpool/application/service/AddNewExecutorToExecutorPoolService.java index 393024a..5ce3ee4 100644 --- a/executor-pool/src/main/java/ch/unisg/executorpool/application/service/AddNewExecutorToExecutorPoolService.java +++ b/executor-pool/src/main/java/ch/unisg/executorpool/application/service/AddNewExecutorToExecutorPoolService.java @@ -2,11 +2,11 @@ package ch.unisg.executorpool.application.service; import ch.unisg.executorpool.application.port.in.AddNewExecutorToExecutorPoolUseCase; import ch.unisg.executorpool.application.port.in.AddNewExecutorToExecutorPoolCommand; +import ch.unisg.executorpool.application.port.out.AddExecutorPort; import ch.unisg.executorpool.application.port.out.ExecutorAddedEventPort; import ch.unisg.executorpool.domain.ExecutorAddedEvent; import ch.unisg.executorpool.domain.ExecutorClass; import ch.unisg.executorpool.domain.ExecutorPool; -import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; import javax.transaction.Transactional; @@ -16,9 +16,11 @@ import javax.transaction.Transactional; public class AddNewExecutorToExecutorPoolService implements AddNewExecutorToExecutorPoolUseCase { private final ExecutorAddedEventPort executorAddedEventPort; + private final AddExecutorPort addExecutorToRepositoryPort; - public AddNewExecutorToExecutorPoolService(ExecutorAddedEventPort executorAddedEventPort){ + public AddNewExecutorToExecutorPoolService(ExecutorAddedEventPort executorAddedEventPort, AddExecutorPort addExecutorToRepositoryPort){ this.executorAddedEventPort = executorAddedEventPort; + this.addExecutorToRepositoryPort = addExecutorToRepositoryPort; } @Override @@ -29,6 +31,8 @@ public class AddNewExecutorToExecutorPoolService implements AddNewExecutorToExec var executorAddedEvent = new ExecutorAddedEvent(newExecutor); executorAddedEventPort.publishExecutorAddedEvent(executorAddedEvent); + addExecutorToRepositoryPort.addExecutor(newExecutor); + return newExecutor; } } diff --git a/executor-pool/src/main/java/ch/unisg/executorpool/application/service/RemoveExecutorFromExecutorPoolService.java b/executor-pool/src/main/java/ch/unisg/executorpool/application/service/RemoveExecutorFromExecutorPoolService.java index 4d2457d..0fc724c 100644 --- a/executor-pool/src/main/java/ch/unisg/executorpool/application/service/RemoveExecutorFromExecutorPoolService.java +++ b/executor-pool/src/main/java/ch/unisg/executorpool/application/service/RemoveExecutorFromExecutorPoolService.java @@ -3,10 +3,10 @@ package ch.unisg.executorpool.application.service; import ch.unisg.executorpool.application.port.in.RemoveExecutorFromExecutorPoolCommand; import ch.unisg.executorpool.application.port.in.RemoveExecutorFromExecutorPoolUseCase; import ch.unisg.executorpool.application.port.out.ExecutorRemovedEventPort; +import ch.unisg.executorpool.application.port.out.RemoveExecutorPort; import ch.unisg.executorpool.domain.ExecutorClass; import ch.unisg.executorpool.domain.ExecutorPool; import ch.unisg.executorpool.domain.ExecutorRemovedEvent; -import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; import javax.transaction.Transactional; @@ -17,9 +17,11 @@ import java.util.Optional; public class RemoveExecutorFromExecutorPoolService implements RemoveExecutorFromExecutorPoolUseCase { private final ExecutorRemovedEventPort executorRemovedEventPort; + private final RemoveExecutorPort removeExecutorFromRepositoryPort; - public RemoveExecutorFromExecutorPoolService(ExecutorRemovedEventPort executorRemovedEventPort){ + public RemoveExecutorFromExecutorPoolService(ExecutorRemovedEventPort executorRemovedEventPort, RemoveExecutorPort removeExecutorFromRepositoryPort){ this.executorRemovedEventPort = executorRemovedEventPort; + this.removeExecutorFromRepositoryPort = removeExecutorFromRepositoryPort; } @Override @@ -32,6 +34,8 @@ public class RemoveExecutorFromExecutorPoolService implements RemoveExecutorFrom executorRemovedEventPort.publishExecutorRemovedEvent(executorRemovedEvent); } + removeExecutorFromRepositoryPort.removeExecutor(removedExecutor); + return removedExecutor; } } diff --git a/executor-pool/src/main/java/ch/unisg/executorpool/domain/ExecutorRemovedEvent.java b/executor-pool/src/main/java/ch/unisg/executorpool/domain/ExecutorRemovedEvent.java index a038928..417e8a7 100644 --- a/executor-pool/src/main/java/ch/unisg/executorpool/domain/ExecutorRemovedEvent.java +++ b/executor-pool/src/main/java/ch/unisg/executorpool/domain/ExecutorRemovedEvent.java @@ -1,6 +1,5 @@ package ch.unisg.executorpool.domain; -import ch.unisg.executorpool.domain.ExecutorClass; import lombok.Getter; public class ExecutorRemovedEvent {