diff options
author | Christof Rabensteiner <christof.rabensteiner@iaik.tugraz.at> | 2019-04-17 15:34:48 +0200 |
---|---|---|
committer | Christof Rabensteiner <christof.rabensteiner@iaik.tugraz.at> | 2019-04-17 15:34:48 +0200 |
commit | 7a003c9287be57fe90173e4dd728ad995ed7036e (patch) | |
tree | 0f1f9f82a5fee3acac64610181d3a1d89cf2b240 /src/main/java/at/gv/egiz/moazs/repository | |
parent | 8e362d5feaf5535bb59bacc1be677d8f10d08f86 (diff) | |
download | moa-zs-7a003c9287be57fe90173e4dd728ad995ed7036e.tar.gz moa-zs-7a003c9287be57fe90173e4dd728ad995ed7036e.tar.bz2 moa-zs-7a003c9287be57fe90173e4dd728ad995ed7036e.zip |
Refactor: Move Repositories to Repository Package
Diffstat (limited to 'src/main/java/at/gv/egiz/moazs/repository')
3 files changed, 117 insertions, 0 deletions
diff --git a/src/main/java/at/gv/egiz/moazs/repository/DeliveryRepository.java b/src/main/java/at/gv/egiz/moazs/repository/DeliveryRepository.java new file mode 100644 index 0000000..d5766b6 --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/repository/DeliveryRepository.java @@ -0,0 +1,15 @@ +package at.gv.egiz.moazs.repository; + +import at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs_.DeliveryRequestType; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public interface DeliveryRepository { + + void add(DeliveryRequestType request); + + Optional<DeliveryRequestType> getDeliveryRequest(String appDeliveryID); + +} diff --git a/src/main/java/at/gv/egiz/moazs/repository/InMemoryDeliveryRepository.java b/src/main/java/at/gv/egiz/moazs/repository/InMemoryDeliveryRepository.java new file mode 100644 index 0000000..e5486d7 --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/repository/InMemoryDeliveryRepository.java @@ -0,0 +1,40 @@ +package at.gv.egiz.moazs.repository; + +import at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs_.DeliveryRequestType; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Repository; + +import java.util.Optional; +import java.util.concurrent.TimeUnit; + +@Repository +@Profile("!cluster") +public class InMemoryDeliveryRepository implements DeliveryRepository { + + private static final Logger logger = LoggerFactory.getLogger(InMemoryDeliveryRepository.class); + + private static final Cache<String, DeliveryRequestType> rep = CacheBuilder.newBuilder() + .expireAfterWrite(30, TimeUnit.MINUTES) + .build(); + + public InMemoryDeliveryRepository() { + logger.info("InMemoryDeliveryRepository()"); + } + + @Override + public void add(DeliveryRequestType request) { + String key = request.getMetaData().getAppDeliveryID(); + logger.info("Save DeliveryRequest with AppDeliveryId = {} to Cache.", key); + rep.put(key, request); + } + + @Override + public Optional<DeliveryRequestType> getDeliveryRequest(String appDeliveryID) { + DeliveryRequestType deliveryRequestType = rep.getIfPresent(appDeliveryID); + return Optional.ofNullable(deliveryRequestType); + } +} diff --git a/src/main/java/at/gv/egiz/moazs/repository/RedisDeliveryRepository.java b/src/main/java/at/gv/egiz/moazs/repository/RedisDeliveryRepository.java new file mode 100644 index 0000000..4f39f31 --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/repository/RedisDeliveryRepository.java @@ -0,0 +1,62 @@ +package at.gv.egiz.moazs.repository; + +import at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs_.DeliveryRequestType; +import at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs_.ObjectFactory; +import at.gv.egiz.moazs.Marshaller; +import org.apache.tools.ant.filters.StringInputStream; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Profile; +import org.springframework.data.redis.core.HashOperations; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Repository; + +import javax.xml.bind.JAXBElement; +import java.util.Optional; + +@Repository +@Profile("cluster") +public class RedisDeliveryRepository implements DeliveryRepository { + + private static final Logger logger = LoggerFactory.getLogger(RedisDeliveryRepository.class); + + private static final String KEY = "MZS_DELIVERY_REQUEST"; + + private final RedisTemplate<String, Object> redisTemplate; + private final Marshaller marshaller; + private final HashOperations<String, String, String> hashOperations; + private final ObjectFactory factory = new ObjectFactory(); + + @Autowired + public RedisDeliveryRepository(RedisTemplate<String, Object> redisTemplate, Marshaller marshaller){ + logger.info("RedisDeliveryRepository()"); + + this.redisTemplate = redisTemplate; + this.hashOperations = redisTemplate.opsForHash(); + this.marshaller = marshaller; + } + + @Override + public void add(DeliveryRequestType request) { + var hashKey = request.getMetaData().getAppDeliveryID(); + var serializedRequest = marshaller.marshallXml(factory.createDeliveryRequest(request)); + + logger.info("Save DeliveryRequest with AppDeliveryId = {} to redis.", hashKey); + hashOperations.put(KEY, hashKey, serializedRequest); + } + + @Override + public Optional<DeliveryRequestType> getDeliveryRequest(String appDeliveryID) { + var serializedRequest = (String) hashOperations.get(KEY, appDeliveryID); + + if (serializedRequest == null) { + return Optional.empty(); + } + + var stream = new StringInputStream(serializedRequest); + var request = (JAXBElement<DeliveryRequestType>) marshaller.unmarshallXml(stream); + return Optional.of(request.getValue()); + } + +} |