Merge pull request #84 from SCS-ASSE-FS21-Group1/Persistence-mongodb

Persistence mongodb
This commit is contained in:
Marcel 2021-11-21 21:18:14 +01:00 committed by GitHub
commit d70ea75fb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 150 additions and 6 deletions

View File

@ -68,6 +68,15 @@
<artifactId>org.eclipse.paho.client.mqttv3</artifactId> <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.5</version> <version>1.2.5</version>
<scope>compile</scope> <scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>3.2.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency> </dependency>
</dependencies> </dependencies>

View File

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

View File

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

View File

@ -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<MongoExecutorDocument, String> {
public MongoExecutorDocument findByExecutorUri(String executorUri, String executorTaskType);
public List<MongoExecutorDocument> findByExecutorTaskType(String executorTaskType);
}

View File

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

View File

@ -0,0 +1,9 @@
package ch.unisg.executorpool.application.port.out;
import ch.unisg.executorpool.domain.ExecutorClass;
public interface AddExecutorPort {
void addExecutor(ExecutorClass executorClass);
}

View File

@ -1,7 +1,6 @@
package ch.unisg.executorpool.application.port.out; package ch.unisg.executorpool.application.port.out;
import ch.unisg.executorpool.domain.ExecutorAddedEvent; import ch.unisg.executorpool.domain.ExecutorAddedEvent;
import org.eclipse.paho.client.mqttv3.MqttException;
public interface ExecutorAddedEventPort { public interface ExecutorAddedEventPort {
void publishExecutorAddedEvent(ExecutorAddedEvent event); void publishExecutorAddedEvent(ExecutorAddedEvent event);

View File

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

View File

@ -0,0 +1,9 @@
package ch.unisg.executorpool.application.port.out;
import ch.unisg.executorpool.domain.ExecutorClass;
public interface RemoveExecutorPort {
void removeExecutor(ExecutorClass executorClass);
}

View File

@ -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.AddNewExecutorToExecutorPoolUseCase;
import ch.unisg.executorpool.application.port.in.AddNewExecutorToExecutorPoolCommand; 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.application.port.out.ExecutorAddedEventPort;
import ch.unisg.executorpool.domain.ExecutorAddedEvent; import ch.unisg.executorpool.domain.ExecutorAddedEvent;
import ch.unisg.executorpool.domain.ExecutorClass; import ch.unisg.executorpool.domain.ExecutorClass;
import ch.unisg.executorpool.domain.ExecutorPool; import ch.unisg.executorpool.domain.ExecutorPool;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.transaction.Transactional; import javax.transaction.Transactional;
@ -16,9 +16,11 @@ import javax.transaction.Transactional;
public class AddNewExecutorToExecutorPoolService implements AddNewExecutorToExecutorPoolUseCase { public class AddNewExecutorToExecutorPoolService implements AddNewExecutorToExecutorPoolUseCase {
private final ExecutorAddedEventPort executorAddedEventPort; private final ExecutorAddedEventPort executorAddedEventPort;
private final AddExecutorPort addExecutorToRepositoryPort;
public AddNewExecutorToExecutorPoolService(ExecutorAddedEventPort executorAddedEventPort){ public AddNewExecutorToExecutorPoolService(ExecutorAddedEventPort executorAddedEventPort, AddExecutorPort addExecutorToRepositoryPort){
this.executorAddedEventPort = executorAddedEventPort; this.executorAddedEventPort = executorAddedEventPort;
this.addExecutorToRepositoryPort = addExecutorToRepositoryPort;
} }
@Override @Override
@ -29,6 +31,8 @@ public class AddNewExecutorToExecutorPoolService implements AddNewExecutorToExec
var executorAddedEvent = new ExecutorAddedEvent(newExecutor); var executorAddedEvent = new ExecutorAddedEvent(newExecutor);
executorAddedEventPort.publishExecutorAddedEvent(executorAddedEvent); executorAddedEventPort.publishExecutorAddedEvent(executorAddedEvent);
addExecutorToRepositoryPort.addExecutor(newExecutor);
return newExecutor; return newExecutor;
} }
} }

View File

@ -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.RemoveExecutorFromExecutorPoolCommand;
import ch.unisg.executorpool.application.port.in.RemoveExecutorFromExecutorPoolUseCase; import ch.unisg.executorpool.application.port.in.RemoveExecutorFromExecutorPoolUseCase;
import ch.unisg.executorpool.application.port.out.ExecutorRemovedEventPort; 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.ExecutorClass;
import ch.unisg.executorpool.domain.ExecutorPool; import ch.unisg.executorpool.domain.ExecutorPool;
import ch.unisg.executorpool.domain.ExecutorRemovedEvent; import ch.unisg.executorpool.domain.ExecutorRemovedEvent;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.transaction.Transactional; import javax.transaction.Transactional;
@ -17,9 +17,11 @@ import java.util.Optional;
public class RemoveExecutorFromExecutorPoolService implements RemoveExecutorFromExecutorPoolUseCase { public class RemoveExecutorFromExecutorPoolService implements RemoveExecutorFromExecutorPoolUseCase {
private final ExecutorRemovedEventPort executorRemovedEventPort; private final ExecutorRemovedEventPort executorRemovedEventPort;
private final RemoveExecutorPort removeExecutorFromRepositoryPort;
public RemoveExecutorFromExecutorPoolService(ExecutorRemovedEventPort executorRemovedEventPort){ public RemoveExecutorFromExecutorPoolService(ExecutorRemovedEventPort executorRemovedEventPort, RemoveExecutorPort removeExecutorFromRepositoryPort){
this.executorRemovedEventPort = executorRemovedEventPort; this.executorRemovedEventPort = executorRemovedEventPort;
this.removeExecutorFromRepositoryPort = removeExecutorFromRepositoryPort;
} }
@Override @Override
@ -32,6 +34,8 @@ public class RemoveExecutorFromExecutorPoolService implements RemoveExecutorFrom
executorRemovedEventPort.publishExecutorRemovedEvent(executorRemovedEvent); executorRemovedEventPort.publishExecutorRemovedEvent(executorRemovedEvent);
} }
removeExecutorFromRepositoryPort.removeExecutor(removedExecutor);
return removedExecutor; return removedExecutor;
} }
} }

View File

@ -1,6 +1,5 @@
package ch.unisg.executorpool.domain; package ch.unisg.executorpool.domain;
import ch.unisg.executorpool.domain.ExecutorClass;
import lombok.Getter; import lombok.Getter;
public class ExecutorRemovedEvent { public class ExecutorRemovedEvent {