From 74529fb410b0058bc3872124fe04a8174ec1d3e7 Mon Sep 17 00:00:00 2001 From: Giuseppe Baittiner Date: Wed, 29 Nov 2023 12:40:05 +0100 Subject: [PATCH] feat: introduce clear cf for issuer if required --- .../gtw/statusmanager/config/Constants.java | 1 + .../mongo/impl/TransactionEventsRepo.java | 31 ++++++++++++------- .../statusmanager/service/impl/ConfigSRV.java | 25 +++++++++++++++ 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/config/Constants.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/config/Constants.java index 793ef18..53bf78e 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/config/Constants.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/config/Constants.java @@ -31,6 +31,7 @@ public static final class Fields { public static final String TRACE_ID = "traceId"; public static final String WORKFLOW_INSTANCE_ID = "workflow_instance_id"; public static final String EVENT_DATE = "eventDate"; + public static final String EVENT_ISSUER = "issuer"; public static final String EXPIRING_DATE = "expiring_date"; private Fields() {} diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/repository/mongo/impl/TransactionEventsRepo.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/repository/mongo/impl/TransactionEventsRepo.java index d63c7d6..fef1079 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/repository/mongo/impl/TransactionEventsRepo.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/repository/mongo/impl/TransactionEventsRepo.java @@ -12,7 +12,6 @@ package it.finanze.sanita.fse2.ms.gtw.statusmanager.repository.mongo.impl; import com.mongodb.MongoException; -import it.finanze.sanita.fse2.ms.gtw.statusmanager.config.Constants; import it.finanze.sanita.fse2.ms.gtw.statusmanager.exceptions.BusinessException; import it.finanze.sanita.fse2.ms.gtw.statusmanager.exceptions.OperationException; import it.finanze.sanita.fse2.ms.gtw.statusmanager.repository.entity.FhirEvent; @@ -34,7 +33,8 @@ import java.util.TimeZone; import java.util.stream.Collectors; -import static it.finanze.sanita.fse2.ms.gtw.statusmanager.config.Constants.Logs.ERR_REP_FHIR_EVENTS; +import static it.finanze.sanita.fse2.ms.gtw.statusmanager.config.Constants.Fields.*; +import static it.finanze.sanita.fse2.ms.gtw.statusmanager.config.Constants.Logs.ERR_REP_FHIR_EVENTS; @Slf4j @Repository @@ -54,21 +54,24 @@ public void saveEvent(String workflowInstanceId, String json) { Document doc = Document.parse(json); SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTERN); simpleDateFormat.setTimeZone(TimeZone.getDefault()); - Date eventDate = simpleDateFormat.parse(doc.getString(Constants.Fields.EVENT_DATE)); - doc.put(Constants.Fields.EVENT_DATE, eventDate); - doc.put(Constants.Fields.WORKFLOW_INSTANCE_ID, workflowInstanceId); - String eventType = doc.getString(Constants.Fields.EVENT_TYPE); - String eventStatus = doc.getString(Constants.Fields.EVENT_STATUS); + Date eventDate = simpleDateFormat.parse(doc.getString(EVENT_DATE)); + doc.put(EVENT_DATE, eventDate); + doc.put(WORKFLOW_INSTANCE_ID, workflowInstanceId); + String eventType = doc.getString(EVENT_TYPE); + String eventStatus = doc.getString(EVENT_STATUS); Query query = new Query(); if(!"UNKNOWN_WORKFLOW_ID".equals(workflowInstanceId)) { - query.addCriteria(Criteria.where(Constants.Fields.WORKFLOW_INSTANCE_ID).is(workflowInstanceId). - and(Constants.Fields.EVENT_TYPE).is(eventType).and(Constants.Fields.EVENT_STATUS).is(eventStatus)); + query.addCriteria(Criteria.where(WORKFLOW_INSTANCE_ID).is(workflowInstanceId). + and(EVENT_TYPE).is(eventType).and(EVENT_STATUS).is(eventStatus)); } else { - query.addCriteria(Criteria.where(Constants.Fields.TRACE_ID).is(doc.getString(Constants.Fields.TRACE_ID)). - and(Constants.Fields.EVENT_TYPE).is(eventType).and(Constants.Fields.EVENT_STATUS).is(eventStatus)); + query.addCriteria(Criteria.where(TRACE_ID).is(doc.getString(TRACE_ID)). + and(EVENT_TYPE).is(eventType).and(EVENT_STATUS).is(eventStatus)); } Date expiringDate = DateUtility.addDay(new Date(), configSRV.getExpirationDate()); - doc.put(Constants.Fields.EXPIRING_DATE, expiringDate); + doc.put(EXPIRING_DATE, expiringDate); + if(doc.containsKey(EVENT_ISSUER) && !configSRV.isCfOnIssuerAllowed()) { + doc.replace(EVENT_ISSUER, clearIssuer(doc.getString(EVENT_ISSUER))); + } mongo.upsert(query, Update.fromDocument(doc, "_id"), FhirEvent.class); } catch(Exception ex){ log.error("Error while save event : " , ex); @@ -96,4 +99,8 @@ public int saveEventsFhir(List wif, Date timestamp, Date expiration) thr return insertions; } + private String clearIssuer(String issuer) { + return issuer.contains("#") ? issuer.split("#")[0] : issuer; + } + } diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/service/impl/ConfigSRV.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/service/impl/ConfigSRV.java index cbb7f0c..e26c2a9 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/service/impl/ConfigSRV.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/service/impl/ConfigSRV.java @@ -13,6 +13,7 @@ import it.finanze.sanita.fse2.ms.gtw.statusmanager.client.IConfigClient; import it.finanze.sanita.fse2.ms.gtw.statusmanager.service.IConfigSRV; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.tuple.Pair; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.event.ApplicationStartedEvent; @@ -26,6 +27,7 @@ import static it.finanze.sanita.fse2.ms.gtw.statusmanager.client.routes.base.ClientRoutes.Config.PROPS_NAME_EXP_DAYS; import static it.finanze.sanita.fse2.ms.gtw.statusmanager.client.routes.base.ClientRoutes.Config.PROPS_NAME_ISSUER_CF; +@Slf4j @Service public class ConfigSRV implements IConfigSRV { @@ -44,6 +46,7 @@ public ConfigSRV() { void initialize() { refreshExpirationDate(); refreshIsCfOnIssuerAllowed(); + runningConfiguration(); } private void refreshExpirationDate() { @@ -65,6 +68,7 @@ public Integer getExpirationDate() { if (new Date().getTime() - pair.getKey() >= DELTA_MS) { synchronized(PROPS_NAME_EXP_DAYS) { refreshExpirationDate(); + verifyExpirationDate(pair); } } return (Integer) props.get(PROPS_NAME_EXP_DAYS).getValue(); @@ -79,8 +83,29 @@ public Boolean isCfOnIssuerAllowed() { if (new Date().getTime() - pair.getKey() >= DELTA_MS) { synchronized(PROPS_NAME_ISSUER_CF) { refreshIsCfOnIssuerAllowed(); + verifyIsCfOnIssuerAllowed(pair); } } return (Boolean) props.get(PROPS_NAME_ISSUER_CF).getValue(); } + + private void runningConfiguration() { + props.forEach((id, pair) -> log.info("[GTW-CONFIG] key: {} | value: {}", id, pair.getValue())); + } + + private void verifyExpirationDate(Pair pair) { + int previous = (int) pair.getValue(); + int current = (int) props.get(PROPS_NAME_EXP_DAYS).getValue(); + if(previous != current) { + log.info("Switching {} from {} to {}", PROPS_NAME_EXP_DAYS, previous, current); + } + } + + private void verifyIsCfOnIssuerAllowed(Pair pair) { + Boolean previous = (Boolean) pair.getValue(); + boolean current = (boolean) props.get(PROPS_NAME_ISSUER_CF).getValue(); + if(previous != current) { + log.info("Switching {} from {} to {}", PROPS_NAME_ISSUER_CF, previous, current); + } + } }