From fda50d2a8e94fdcd92fb072166570ccb30d46a7c Mon Sep 17 00:00:00 2001 From: mozzy11 Date: Thu, 7 Nov 2024 14:36:37 +0300 Subject: [PATCH 1/8] XDS sender should perisist last successful request date --- .../module/xdssender/XdsSenderConstants.java | 4 +- .../xdssender/api/domain/dao/CcdDao.java | 6 +++ .../api/domain/dao/impl/CcdDaoImpl.java | 14 ++++++ .../xdssender/api/model/RequestDate.java | 43 +++++++++++++++++++ .../NotificationsPullPointClientImpl.java | 33 ++++++++++++-- .../api/scheduler/PullNotificationsTask.java | 19 ++++++++ .../xdssender/api/service/CcdService.java | 21 +++++++++ .../api/service/impl/CcdServiceImpl.java | 12 ++++++ api/src/main/resources/liquibase.xml | 17 ++++++++ pom.xml | 2 +- 10 files changed, 166 insertions(+), 5 deletions(-) create mode 100644 api/src/main/java/org/openmrs/module/xdssender/api/model/RequestDate.java diff --git a/api/src/main/java/org/openmrs/module/xdssender/XdsSenderConstants.java b/api/src/main/java/org/openmrs/module/xdssender/XdsSenderConstants.java index e7209c0..3ea110a 100644 --- a/api/src/main/java/org/openmrs/module/xdssender/XdsSenderConstants.java +++ b/api/src/main/java/org/openmrs/module/xdssender/XdsSenderConstants.java @@ -484,7 +484,7 @@ public final class XdsSenderConstants { public static final String SCT_TEMPLATE_HISTORY_OF_BLOOD_TRANSFUSIONS = "1.3.6.1.4.1.19376.1.5.3.1.1.9.12"; - public static final String LOCATION_SITECODE_ATTRIBUTE_UUID = "6242bf19-207e-4076-9d28-9290525b8ed9"; + public static final String LOCATION_SITECODE_ATTRIBUTE_UUID = "0e52924e-4ebb-40ba-9b83-b198b532653b"; public static final String SYSTEM_IDENTIFIER_TYPE_NAME = "SYSTEM"; @@ -496,6 +496,8 @@ public final class XdsSenderConstants { public static final String ECID_IDENTIFIER_TYPE_NAME = "ECID"; + public static final String SOAP_REQUEST_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"; + private XdsSenderConstants() { } } diff --git a/api/src/main/java/org/openmrs/module/xdssender/api/domain/dao/CcdDao.java b/api/src/main/java/org/openmrs/module/xdssender/api/domain/dao/CcdDao.java index 104c010..d61534a 100644 --- a/api/src/main/java/org/openmrs/module/xdssender/api/domain/dao/CcdDao.java +++ b/api/src/main/java/org/openmrs/module/xdssender/api/domain/dao/CcdDao.java @@ -1,11 +1,17 @@ package org.openmrs.module.xdssender.api.domain.dao; import org.openmrs.Patient; +import org.openmrs.api.APIException; import org.openmrs.module.xdssender.api.domain.Ccd; +import org.openmrs.module.xdssender.api.model.RequestDate; public interface CcdDao { Ccd saveOrUpdate(Ccd ccd); Ccd find(Patient patient); + + RequestDate saveOrUpdateRequestDate(RequestDate requestDate) throws APIException ; + + RequestDate getLastRequestDate() throws APIException; } diff --git a/api/src/main/java/org/openmrs/module/xdssender/api/domain/dao/impl/CcdDaoImpl.java b/api/src/main/java/org/openmrs/module/xdssender/api/domain/dao/impl/CcdDaoImpl.java index 4a4651c..84ff504 100644 --- a/api/src/main/java/org/openmrs/module/xdssender/api/domain/dao/impl/CcdDaoImpl.java +++ b/api/src/main/java/org/openmrs/module/xdssender/api/domain/dao/impl/CcdDaoImpl.java @@ -2,9 +2,11 @@ import org.hibernate.Query; import org.openmrs.Patient; +import org.openmrs.api.APIException; import org.openmrs.api.db.hibernate.DbSessionFactory; import org.openmrs.module.xdssender.api.domain.Ccd; import org.openmrs.module.xdssender.api.domain.dao.CcdDao; +import org.openmrs.module.xdssender.api.model.RequestDate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; @@ -34,4 +36,16 @@ public Ccd find(Patient patient) { query.setParameter("patient", patient); return (Ccd) query.uniqueResult(); } + + @Override + public RequestDate saveOrUpdateRequestDate(RequestDate requestDate) throws APIException { + sessionFactory.getCurrentSession().saveOrUpdate(requestDate); + return requestDate; + } + + @Override + public RequestDate getLastRequestDate() throws APIException { + String hql = "FROM RequestDate rd WHERE rd.date = (SELECT MAX(t.date) FROM RequestDate t)"; + return (RequestDate)sessionFactory.getCurrentSession().createQuery(hql).uniqueResult(); + } } diff --git a/api/src/main/java/org/openmrs/module/xdssender/api/model/RequestDate.java b/api/src/main/java/org/openmrs/module/xdssender/api/model/RequestDate.java new file mode 100644 index 0000000..df7ba33 --- /dev/null +++ b/api/src/main/java/org/openmrs/module/xdssender/api/model/RequestDate.java @@ -0,0 +1,43 @@ +package org.openmrs.module.xdssender.api.model; + +import java.util.Date; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.openmrs.BaseOpenmrsObject; + +@Entity +@Table(name = "request_date") +public class RequestDate extends BaseOpenmrsObject{ + + @Id + @GeneratedValue + @Column(name = "id") + private Integer id; + + @Column(name = "date") + private Date requestDate; + + @Override + public Integer getId() { + return id; + } + + @Override + public void setId(Integer id) { + this.id = id; + } + + public Date getRequestDate() { + return requestDate; + } + + public void setRequestDate(Date requestDate) { + this.requestDate = requestDate; + } + +} diff --git a/api/src/main/java/org/openmrs/module/xdssender/api/notificationspullpoint/impl/NotificationsPullPointClientImpl.java b/api/src/main/java/org/openmrs/module/xdssender/api/notificationspullpoint/impl/NotificationsPullPointClientImpl.java index 4bfbb67..c72f13e 100644 --- a/api/src/main/java/org/openmrs/module/xdssender/api/notificationspullpoint/impl/NotificationsPullPointClientImpl.java +++ b/api/src/main/java/org/openmrs/module/xdssender/api/notificationspullpoint/impl/NotificationsPullPointClientImpl.java @@ -4,8 +4,12 @@ import java.math.BigInteger; import java.net.HttpURLConnection; import java.nio.charset.StandardCharsets; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; import java.util.List; +import java.util.TimeZone; import javax.xml.bind.JAXBContext; import javax.xml.bind.Unmarshaller; @@ -21,10 +25,12 @@ import org.openmrs.module.labintegration.api.hl7.messages.util.OruR01Util; import org.openmrs.module.xdssender.XdsSenderConfig; import org.openmrs.module.xdssender.XdsSenderConstants; +import org.openmrs.module.xdssender.api.model.RequestDate; import org.openmrs.module.xdssender.api.notificationspullpoint.NotificationsPullPointClient; import org.openmrs.module.xdssender.notificationspullpoint.GetMessages; import org.openmrs.module.xdssender.notificationspullpoint.GetMessagesResponse; import org.openmrs.module.xdssender.notificationspullpoint.NotificationMessageHolderType; +import org.openmrs.module.xdssender.api.service.CcdService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -58,11 +64,32 @@ public class NotificationsPullPointClientImpl extends WebServiceGatewaySupport i public static final String FACILITY_QNAME = "facility"; private static final BigInteger MAX_MESSAGES_PER_REQUEST = BigInteger.valueOf(100); + private String lastRequestDate = ""; + @Autowired private XdsSenderConfig config; + @Autowired + private CcdService ccdService; + @Override public List getNewMessages() { + SimpleDateFormat dateFormatter = new SimpleDateFormat(XdsSenderConstants.SOAP_REQUEST_DATE_TIME_FORMAT); + TimeZone timeZone = TimeZone.getDefault(); + dateFormatter.setTimeZone(timeZone); + Date newDate = new Date(); + + Calendar calendar = Calendar.getInstance(); + calendar.setTime(newDate); + calendar.add(Calendar.DAY_OF_YEAR, -5); + Date fiveDaysAgo = calendar.getTime(); + + RequestDate lastRequest = ccdService.getLastRequestDate(); + lastRequestDate = dateFormatter.format(fiveDaysAgo); + if (lastRequest != null) { + lastRequestDate = dateFormatter.format(lastRequest.getRequestDate()); + } + LocationTag loginLocationTag = Context.getLocationService().getLocationTagByName(LOCATION_TAG_NAME); List locations = Context.getLocationService().getLocationsByTag(loginLocationTag); List returnMessages = new ArrayList<>(); @@ -159,12 +186,12 @@ private GetMessagesResponse getResponseHttpClient(GetMessages requestPayload) th + " xmlns:ns4=\"http://docs.oasis-open.org/wsrf/bf-2\"\r\n" + " xmlns:ns5=\"http://docs.oasis-open.org/wsn/t-1\"\r\n" + " xmlns:ns6=\"http://docs.oasis-open.org/wsn/br-2\"\r\n" - + " facility=\"%s\">\r\n" - + " 100\r\n" + + " facility=\"%s\"\r\n" + + " since=\"%s\">\r\n" + " \r\n" + " \r\n" + "", - facilitySiteCode); + facilitySiteCode ,lastRequestDate); log.debug(getMessagesPayload); RequestBody body = RequestBody.create(getMessagesPayload, mediaType); diff --git a/api/src/main/java/org/openmrs/module/xdssender/api/scheduler/PullNotificationsTask.java b/api/src/main/java/org/openmrs/module/xdssender/api/scheduler/PullNotificationsTask.java index a4111be..f86285a 100644 --- a/api/src/main/java/org/openmrs/module/xdssender/api/scheduler/PullNotificationsTask.java +++ b/api/src/main/java/org/openmrs/module/xdssender/api/scheduler/PullNotificationsTask.java @@ -3,11 +3,15 @@ import ca.uhn.hl7v2.model.Message; import org.openmrs.api.context.Context; import org.openmrs.hl7.HL7Service; +import org.openmrs.module.xdssender.api.model.RequestDate; import org.openmrs.module.xdssender.api.notificationspullpoint.NotificationsPullPointClient; import org.openmrs.module.xdssender.api.notificationspullpoint.impl.NotificationsPullPointClientImpl; +import org.openmrs.module.xdssender.api.service.CcdService; +import org.openmrs.module.xdssender.api.service.impl.CcdServiceImpl; import org.openmrs.scheduler.tasks.AbstractTask; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Date; public class PullNotificationsTask extends AbstractTask { @@ -23,18 +27,33 @@ public class PullNotificationsTask extends AbstractTask { public void execute() { LOGGER.info("Executing " + TASK_NAME); HL7Service hl7Service = Context.getHL7Service(); + Date newDate = new Date(); + Boolean success = true; for (Message msg : getNotificationsPullPointClient().getNewMessages()) { try { hl7Service.processHL7Message(msg); } catch (Exception e) { + success = false; LOGGER.error(e.getMessage(), e); } } + + if(success){ + RequestDate req = new RequestDate(); + req.setRequestDate(newDate); + getCcdService().saveOrUpdateRequestDate(req); + } + } private NotificationsPullPointClient getNotificationsPullPointClient() { return Context.getRegisteredComponent("xdssender.NotificationsPullPointClientImpl", NotificationsPullPointClientImpl.class); } + + private CcdService getCcdService() { + return Context.getRegisteredComponent("xdsSender.CcdService", + CcdServiceImpl.class); + } } diff --git a/api/src/main/java/org/openmrs/module/xdssender/api/service/CcdService.java b/api/src/main/java/org/openmrs/module/xdssender/api/service/CcdService.java index 81f908c..e39861d 100644 --- a/api/src/main/java/org/openmrs/module/xdssender/api/service/CcdService.java +++ b/api/src/main/java/org/openmrs/module/xdssender/api/service/CcdService.java @@ -2,7 +2,9 @@ import org.dcm4chee.xds2.common.exception.XDSException; import org.openmrs.Patient; +import org.openmrs.api.APIException; import org.openmrs.module.xdssender.api.domain.Ccd; +import org.openmrs.module.xdssender.api.model.RequestDate; import org.springframework.transaction.annotation.Transactional; import java.io.OutputStream; @@ -23,4 +25,23 @@ public interface CcdService { @Transactional(readOnly = true) void downloadCcdAsPDF(OutputStream stream, Patient patient); + + /** + * Saves an TaskRequest + * + * @param requestDate + * @return RequestDate + * @throws APIException + */ + @Transactional + RequestDate saveOrUpdateRequestDate(RequestDate requestDate) throws APIException; + + + /** + * Returns the Last Request Date + + * @throws APIException + */ + @Transactional(readOnly = true) + RequestDate getLastRequestDate() throws APIException; } diff --git a/api/src/main/java/org/openmrs/module/xdssender/api/service/impl/CcdServiceImpl.java b/api/src/main/java/org/openmrs/module/xdssender/api/service/impl/CcdServiceImpl.java index 80cb407..9a9358c 100644 --- a/api/src/main/java/org/openmrs/module/xdssender/api/service/impl/CcdServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/xdssender/api/service/impl/CcdServiceImpl.java @@ -6,9 +6,11 @@ import org.hl7.fhir.r4.model.Bundle; import org.hl7.fhir.r4.model.HumanName; import org.openmrs.Patient; +import org.openmrs.api.APIException; import org.openmrs.module.xdssender.XdsSenderConfig; import org.openmrs.module.xdssender.api.domain.Ccd; import org.openmrs.module.xdssender.api.domain.dao.CcdDao; +import org.openmrs.module.xdssender.api.model.RequestDate; import org.openmrs.module.xdssender.api.service.CcdService; import org.openmrs.module.xdssender.api.service.XdsImportService; import org.openmrs.module.xdssender.api.xds.XdsUtil; @@ -133,4 +135,14 @@ public Ccd downloadAndSaveCcd(Patient patient) throws XDSException { public void downloadCcdAsPDF(OutputStream stream, Patient patient) { LOGGER.info("CCD PDF is being downloaded."); } + + @Override + public RequestDate saveOrUpdateRequestDate(RequestDate requestDate) throws APIException { + return ccdDao.saveOrUpdateRequestDate(requestDate); + } + + @Override + public RequestDate getLastRequestDate() throws APIException { + return ccdDao.getLastRequestDate(); + } } diff --git a/api/src/main/resources/liquibase.xml b/api/src/main/resources/liquibase.xml index 97662c1..2839fb5 100644 --- a/api/src/main/resources/liquibase.xml +++ b/api/src/main/resources/liquibase.xml @@ -48,5 +48,22 @@ baseColumnNames="patient_id" referencedTableName="patient" referencedColumnNames="patient_id" /> + + + + + + Creating the request_date table + + + + + + + + + + + diff --git a/pom.xml b/pom.xml index df51025..3593799 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ - 2.4.0-SNAPSHOT + 2.4.1-SNAPSHOT 2.0.5 1.1.0 2.0.6 From 5ca65c687b121df728c030d7284d57148745001e Mon Sep 17 00:00:00 2001 From: mozzy11 Date: Thu, 7 Nov 2024 15:52:58 +0300 Subject: [PATCH 2/8] format --- .../xdssender/api/domain/dao/impl/CcdDaoImpl.java | 14 +++++++------- .../impl/NotificationsPullPointClientImpl.java | 6 +++--- .../api/scheduler/PullNotificationsTask.java | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/api/src/main/java/org/openmrs/module/xdssender/api/domain/dao/impl/CcdDaoImpl.java b/api/src/main/java/org/openmrs/module/xdssender/api/domain/dao/impl/CcdDaoImpl.java index 84ff504..91cbdbc 100644 --- a/api/src/main/java/org/openmrs/module/xdssender/api/domain/dao/impl/CcdDaoImpl.java +++ b/api/src/main/java/org/openmrs/module/xdssender/api/domain/dao/impl/CcdDaoImpl.java @@ -39,13 +39,13 @@ public Ccd find(Patient patient) { @Override public RequestDate saveOrUpdateRequestDate(RequestDate requestDate) throws APIException { - sessionFactory.getCurrentSession().saveOrUpdate(requestDate); - return requestDate; - } + sessionFactory.getCurrentSession().saveOrUpdate(requestDate); + return requestDate; + } @Override - public RequestDate getLastRequestDate() throws APIException { - String hql = "FROM RequestDate rd WHERE rd.date = (SELECT MAX(t.date) FROM RequestDate t)"; - return (RequestDate)sessionFactory.getCurrentSession().createQuery(hql).uniqueResult(); - } + public RequestDate getLastRequestDate() throws APIException { + String hql = "FROM RequestDate rd WHERE rd.date = (SELECT MAX(t.date) FROM RequestDate t)"; + return (RequestDate) sessionFactory.getCurrentSession().createQuery(hql).uniqueResult(); + } } diff --git a/api/src/main/java/org/openmrs/module/xdssender/api/notificationspullpoint/impl/NotificationsPullPointClientImpl.java b/api/src/main/java/org/openmrs/module/xdssender/api/notificationspullpoint/impl/NotificationsPullPointClientImpl.java index c72f13e..0c1aee4 100644 --- a/api/src/main/java/org/openmrs/module/xdssender/api/notificationspullpoint/impl/NotificationsPullPointClientImpl.java +++ b/api/src/main/java/org/openmrs/module/xdssender/api/notificationspullpoint/impl/NotificationsPullPointClientImpl.java @@ -75,15 +75,15 @@ public class NotificationsPullPointClientImpl extends WebServiceGatewaySupport i @Override public List getNewMessages() { SimpleDateFormat dateFormatter = new SimpleDateFormat(XdsSenderConstants.SOAP_REQUEST_DATE_TIME_FORMAT); - TimeZone timeZone = TimeZone.getDefault(); - dateFormatter.setTimeZone(timeZone); + TimeZone timeZone = TimeZone.getDefault(); + dateFormatter.setTimeZone(timeZone); Date newDate = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(newDate); calendar.add(Calendar.DAY_OF_YEAR, -5); Date fiveDaysAgo = calendar.getTime(); - + RequestDate lastRequest = ccdService.getLastRequestDate(); lastRequestDate = dateFormatter.format(fiveDaysAgo); if (lastRequest != null) { diff --git a/api/src/main/java/org/openmrs/module/xdssender/api/scheduler/PullNotificationsTask.java b/api/src/main/java/org/openmrs/module/xdssender/api/scheduler/PullNotificationsTask.java index f86285a..60f35a1 100644 --- a/api/src/main/java/org/openmrs/module/xdssender/api/scheduler/PullNotificationsTask.java +++ b/api/src/main/java/org/openmrs/module/xdssender/api/scheduler/PullNotificationsTask.java @@ -28,7 +28,7 @@ public void execute() { LOGGER.info("Executing " + TASK_NAME); HL7Service hl7Service = Context.getHL7Service(); Date newDate = new Date(); - Boolean success = true; + Boolean success = true; for (Message msg : getNotificationsPullPointClient().getNewMessages()) { try { hl7Service.processHL7Message(msg); From e46bb3996440ef1dfaf5e1d3ad0ab90d23dff8b5 Mon Sep 17 00:00:00 2001 From: mozzy11 Date: Thu, 7 Nov 2024 15:55:24 +0300 Subject: [PATCH 3/8] format liquibase --- api/src/main/resources/liquibase.xml | 30 +++++++++++++++------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/api/src/main/resources/liquibase.xml b/api/src/main/resources/liquibase.xml index 2839fb5..246808a 100644 --- a/api/src/main/resources/liquibase.xml +++ b/api/src/main/resources/liquibase.xml @@ -48,22 +48,24 @@ baseColumnNames="patient_id" referencedTableName="patient" referencedColumnNames="patient_id" /> - - - - - - Creating the request_date table - - - - - + + + + + + + + Creating the request_date table + + + + + - - - + + + From 212af48bbf8bd19b32a3193820b3c93ea5e74c90 Mon Sep 17 00:00:00 2001 From: mozzy11 Date: Thu, 7 Nov 2024 19:28:46 +0300 Subject: [PATCH 4/8] Update module to persist request date as a GP instead of a new Object --- .../module/xdssender/XdsSenderConstants.java | 2 +- .../xdssender/api/domain/dao/CcdDao.java | 8 +--- .../api/domain/dao/impl/CcdDaoImpl.java | 16 +------ .../xdssender/api/model/RequestDate.java | 43 ------------------- .../NotificationsPullPointClientImpl.java | 30 ++++--------- .../api/scheduler/PullNotificationsTask.java | 20 +++------ .../xdssender/api/service/CcdService.java | 23 +--------- .../api/service/impl/CcdServiceImpl.java | 14 +----- api/src/main/resources/liquibase.xml | 20 --------- omod/src/main/resources/config.xml | 4 ++ 10 files changed, 23 insertions(+), 157 deletions(-) delete mode 100644 api/src/main/java/org/openmrs/module/xdssender/api/model/RequestDate.java diff --git a/api/src/main/java/org/openmrs/module/xdssender/XdsSenderConstants.java b/api/src/main/java/org/openmrs/module/xdssender/XdsSenderConstants.java index 3ea110a..7ce9655 100644 --- a/api/src/main/java/org/openmrs/module/xdssender/XdsSenderConstants.java +++ b/api/src/main/java/org/openmrs/module/xdssender/XdsSenderConstants.java @@ -496,7 +496,7 @@ public final class XdsSenderConstants { public static final String ECID_IDENTIFIER_TYPE_NAME = "ECID"; - public static final String SOAP_REQUEST_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"; + public static final String PULL_NOTIFICATIONS_TASK_LAST_SUCCESS_RUN = "xdssender.PullNotificationsTask.lastSuccessfulRun"; private XdsSenderConstants() { } diff --git a/api/src/main/java/org/openmrs/module/xdssender/api/domain/dao/CcdDao.java b/api/src/main/java/org/openmrs/module/xdssender/api/domain/dao/CcdDao.java index d61534a..018518e 100644 --- a/api/src/main/java/org/openmrs/module/xdssender/api/domain/dao/CcdDao.java +++ b/api/src/main/java/org/openmrs/module/xdssender/api/domain/dao/CcdDao.java @@ -1,17 +1,11 @@ package org.openmrs.module.xdssender.api.domain.dao; import org.openmrs.Patient; -import org.openmrs.api.APIException; import org.openmrs.module.xdssender.api.domain.Ccd; -import org.openmrs.module.xdssender.api.model.RequestDate; public interface CcdDao { Ccd saveOrUpdate(Ccd ccd); Ccd find(Patient patient); - - RequestDate saveOrUpdateRequestDate(RequestDate requestDate) throws APIException ; - - RequestDate getLastRequestDate() throws APIException; -} +} \ No newline at end of file diff --git a/api/src/main/java/org/openmrs/module/xdssender/api/domain/dao/impl/CcdDaoImpl.java b/api/src/main/java/org/openmrs/module/xdssender/api/domain/dao/impl/CcdDaoImpl.java index 91cbdbc..fce7e0b 100644 --- a/api/src/main/java/org/openmrs/module/xdssender/api/domain/dao/impl/CcdDaoImpl.java +++ b/api/src/main/java/org/openmrs/module/xdssender/api/domain/dao/impl/CcdDaoImpl.java @@ -2,11 +2,9 @@ import org.hibernate.Query; import org.openmrs.Patient; -import org.openmrs.api.APIException; import org.openmrs.api.db.hibernate.DbSessionFactory; import org.openmrs.module.xdssender.api.domain.Ccd; import org.openmrs.module.xdssender.api.domain.dao.CcdDao; -import org.openmrs.module.xdssender.api.model.RequestDate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; @@ -36,16 +34,4 @@ public Ccd find(Patient patient) { query.setParameter("patient", patient); return (Ccd) query.uniqueResult(); } - - @Override - public RequestDate saveOrUpdateRequestDate(RequestDate requestDate) throws APIException { - sessionFactory.getCurrentSession().saveOrUpdate(requestDate); - return requestDate; - } - - @Override - public RequestDate getLastRequestDate() throws APIException { - String hql = "FROM RequestDate rd WHERE rd.date = (SELECT MAX(t.date) FROM RequestDate t)"; - return (RequestDate) sessionFactory.getCurrentSession().createQuery(hql).uniqueResult(); - } -} +} \ No newline at end of file diff --git a/api/src/main/java/org/openmrs/module/xdssender/api/model/RequestDate.java b/api/src/main/java/org/openmrs/module/xdssender/api/model/RequestDate.java deleted file mode 100644 index df7ba33..0000000 --- a/api/src/main/java/org/openmrs/module/xdssender/api/model/RequestDate.java +++ /dev/null @@ -1,43 +0,0 @@ -package org.openmrs.module.xdssender.api.model; - -import java.util.Date; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.Table; - -import org.openmrs.BaseOpenmrsObject; - -@Entity -@Table(name = "request_date") -public class RequestDate extends BaseOpenmrsObject{ - - @Id - @GeneratedValue - @Column(name = "id") - private Integer id; - - @Column(name = "date") - private Date requestDate; - - @Override - public Integer getId() { - return id; - } - - @Override - public void setId(Integer id) { - this.id = id; - } - - public Date getRequestDate() { - return requestDate; - } - - public void setRequestDate(Date requestDate) { - this.requestDate = requestDate; - } - -} diff --git a/api/src/main/java/org/openmrs/module/xdssender/api/notificationspullpoint/impl/NotificationsPullPointClientImpl.java b/api/src/main/java/org/openmrs/module/xdssender/api/notificationspullpoint/impl/NotificationsPullPointClientImpl.java index 0c1aee4..7ebeb67 100644 --- a/api/src/main/java/org/openmrs/module/xdssender/api/notificationspullpoint/impl/NotificationsPullPointClientImpl.java +++ b/api/src/main/java/org/openmrs/module/xdssender/api/notificationspullpoint/impl/NotificationsPullPointClientImpl.java @@ -17,6 +17,7 @@ import javax.xml.transform.TransformerException; import org.apache.commons.codec.binary.Base64; +import org.apache.commons.lang3.StringUtils; import org.openmrs.Location; import org.openmrs.LocationAttribute; import org.openmrs.LocationTag; @@ -25,7 +26,6 @@ import org.openmrs.module.labintegration.api.hl7.messages.util.OruR01Util; import org.openmrs.module.xdssender.XdsSenderConfig; import org.openmrs.module.xdssender.XdsSenderConstants; -import org.openmrs.module.xdssender.api.model.RequestDate; import org.openmrs.module.xdssender.api.notificationspullpoint.NotificationsPullPointClient; import org.openmrs.module.xdssender.notificationspullpoint.GetMessages; import org.openmrs.module.xdssender.notificationspullpoint.GetMessagesResponse; @@ -69,27 +69,10 @@ public class NotificationsPullPointClientImpl extends WebServiceGatewaySupport i @Autowired private XdsSenderConfig config; - @Autowired - private CcdService ccdService; @Override public List getNewMessages() { - SimpleDateFormat dateFormatter = new SimpleDateFormat(XdsSenderConstants.SOAP_REQUEST_DATE_TIME_FORMAT); - TimeZone timeZone = TimeZone.getDefault(); - dateFormatter.setTimeZone(timeZone); - Date newDate = new Date(); - - Calendar calendar = Calendar.getInstance(); - calendar.setTime(newDate); - calendar.add(Calendar.DAY_OF_YEAR, -5); - Date fiveDaysAgo = calendar.getTime(); - - RequestDate lastRequest = ccdService.getLastRequestDate(); - lastRequestDate = dateFormatter.format(fiveDaysAgo); - if (lastRequest != null) { - lastRequestDate = dateFormatter.format(lastRequest.getRequestDate()); - } - + lastRequestDate = Context.getAdministrationService().getGlobalProperty(XdsSenderConstants.PULL_NOTIFICATIONS_TASK_LAST_SUCCESS_RUN , ""); LocationTag loginLocationTag = Context.getLocationService().getLocationTagByName(LOCATION_TAG_NAME); List locations = Context.getLocationService().getLocationsByTag(loginLocationTag); List returnMessages = new ArrayList<>(); @@ -176,6 +159,9 @@ private GetMessagesResponse getResponseHttpClient(GetMessages requestPayload) th OkHttpClient client = new OkHttpClient().newBuilder().build(); MediaType mediaType = MediaType.parse("text/xml; charset=utf-8"); String facilitySiteCode = requestPayload.getOtherAttributes().get(new QName(FACILITY_QNAME)); + String sinceAttribute = StringUtils.isNotBlank(lastRequestDate) ? String.format("since=\"%s\"", lastRequestDate) : ""; + String maximumNumberElement = StringUtils.isBlank(lastRequestDate) ? "100\r\n" : ""; + String getMessagesPayload = String.format("\r\n" + " \r\n" @@ -186,12 +172,12 @@ private GetMessagesResponse getResponseHttpClient(GetMessages requestPayload) th + " xmlns:ns4=\"http://docs.oasis-open.org/wsrf/bf-2\"\r\n" + " xmlns:ns5=\"http://docs.oasis-open.org/wsn/t-1\"\r\n" + " xmlns:ns6=\"http://docs.oasis-open.org/wsn/br-2\"\r\n" - + " facility=\"%s\"\r\n" - + " since=\"%s\">\r\n" + + " facility=\"%s\" %s>\r\n" + + " %s" + " \r\n" + " \r\n" + "", - facilitySiteCode ,lastRequestDate); + facilitySiteCode ,sinceAttribute ,maximumNumberElement); log.debug(getMessagesPayload); RequestBody body = RequestBody.create(getMessagesPayload, mediaType); diff --git a/api/src/main/java/org/openmrs/module/xdssender/api/scheduler/PullNotificationsTask.java b/api/src/main/java/org/openmrs/module/xdssender/api/scheduler/PullNotificationsTask.java index 60f35a1..b317135 100644 --- a/api/src/main/java/org/openmrs/module/xdssender/api/scheduler/PullNotificationsTask.java +++ b/api/src/main/java/org/openmrs/module/xdssender/api/scheduler/PullNotificationsTask.java @@ -3,15 +3,15 @@ import ca.uhn.hl7v2.model.Message; import org.openmrs.api.context.Context; import org.openmrs.hl7.HL7Service; -import org.openmrs.module.xdssender.api.model.RequestDate; +import org.openmrs.module.xdssender.XdsSenderConstants; import org.openmrs.module.xdssender.api.notificationspullpoint.NotificationsPullPointClient; import org.openmrs.module.xdssender.api.notificationspullpoint.impl.NotificationsPullPointClientImpl; -import org.openmrs.module.xdssender.api.service.CcdService; -import org.openmrs.module.xdssender.api.service.impl.CcdServiceImpl; import org.openmrs.scheduler.tasks.AbstractTask; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Date; + +import java.time.Instant; +import java.time.format.DateTimeFormatter; public class PullNotificationsTask extends AbstractTask { @@ -27,8 +27,7 @@ public class PullNotificationsTask extends AbstractTask { public void execute() { LOGGER.info("Executing " + TASK_NAME); HL7Service hl7Service = Context.getHL7Service(); - Date newDate = new Date(); - Boolean success = true; + boolean success = true; for (Message msg : getNotificationsPullPointClient().getNewMessages()) { try { hl7Service.processHL7Message(msg); @@ -40,9 +39,7 @@ public void execute() { } if(success){ - RequestDate req = new RequestDate(); - req.setRequestDate(newDate); - getCcdService().saveOrUpdateRequestDate(req); + Context.getAdministrationService().setGlobalProperty(XdsSenderConstants.PULL_NOTIFICATIONS_TASK_LAST_SUCCESS_RUN ,DateTimeFormatter.ISO_INSTANT.format(Instant.now())); } } @@ -51,9 +48,4 @@ private NotificationsPullPointClient getNotificationsPullPointClient() { return Context.getRegisteredComponent("xdssender.NotificationsPullPointClientImpl", NotificationsPullPointClientImpl.class); } - - private CcdService getCcdService() { - return Context.getRegisteredComponent("xdsSender.CcdService", - CcdServiceImpl.class); - } } diff --git a/api/src/main/java/org/openmrs/module/xdssender/api/service/CcdService.java b/api/src/main/java/org/openmrs/module/xdssender/api/service/CcdService.java index e39861d..4305d57 100644 --- a/api/src/main/java/org/openmrs/module/xdssender/api/service/CcdService.java +++ b/api/src/main/java/org/openmrs/module/xdssender/api/service/CcdService.java @@ -2,9 +2,7 @@ import org.dcm4chee.xds2.common.exception.XDSException; import org.openmrs.Patient; -import org.openmrs.api.APIException; import org.openmrs.module.xdssender.api.domain.Ccd; -import org.openmrs.module.xdssender.api.model.RequestDate; import org.springframework.transaction.annotation.Transactional; import java.io.OutputStream; @@ -25,23 +23,4 @@ public interface CcdService { @Transactional(readOnly = true) void downloadCcdAsPDF(OutputStream stream, Patient patient); - - /** - * Saves an TaskRequest - * - * @param requestDate - * @return RequestDate - * @throws APIException - */ - @Transactional - RequestDate saveOrUpdateRequestDate(RequestDate requestDate) throws APIException; - - - /** - * Returns the Last Request Date - - * @throws APIException - */ - @Transactional(readOnly = true) - RequestDate getLastRequestDate() throws APIException; -} +} \ No newline at end of file diff --git a/api/src/main/java/org/openmrs/module/xdssender/api/service/impl/CcdServiceImpl.java b/api/src/main/java/org/openmrs/module/xdssender/api/service/impl/CcdServiceImpl.java index 9a9358c..75b3c69 100644 --- a/api/src/main/java/org/openmrs/module/xdssender/api/service/impl/CcdServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/xdssender/api/service/impl/CcdServiceImpl.java @@ -6,11 +6,9 @@ import org.hl7.fhir.r4.model.Bundle; import org.hl7.fhir.r4.model.HumanName; import org.openmrs.Patient; -import org.openmrs.api.APIException; import org.openmrs.module.xdssender.XdsSenderConfig; import org.openmrs.module.xdssender.api.domain.Ccd; import org.openmrs.module.xdssender.api.domain.dao.CcdDao; -import org.openmrs.module.xdssender.api.model.RequestDate; import org.openmrs.module.xdssender.api.service.CcdService; import org.openmrs.module.xdssender.api.service.XdsImportService; import org.openmrs.module.xdssender.api.xds.XdsUtil; @@ -135,14 +133,4 @@ public Ccd downloadAndSaveCcd(Patient patient) throws XDSException { public void downloadCcdAsPDF(OutputStream stream, Patient patient) { LOGGER.info("CCD PDF is being downloaded."); } - - @Override - public RequestDate saveOrUpdateRequestDate(RequestDate requestDate) throws APIException { - return ccdDao.saveOrUpdateRequestDate(requestDate); - } - - @Override - public RequestDate getLastRequestDate() throws APIException { - return ccdDao.getLastRequestDate(); - } -} +} \ No newline at end of file diff --git a/api/src/main/resources/liquibase.xml b/api/src/main/resources/liquibase.xml index 246808a..ba20581 100644 --- a/api/src/main/resources/liquibase.xml +++ b/api/src/main/resources/liquibase.xml @@ -48,24 +48,4 @@ baseColumnNames="patient_id" referencedTableName="patient" referencedColumnNames="patient_id" /> - - - - - - - - Creating the request_date table - - - - - - - - - - - - diff --git a/omod/src/main/resources/config.xml b/omod/src/main/resources/config.xml index b96677d..1899a50 100644 --- a/omod/src/main/resources/config.xml +++ b/omod/src/main/resources/config.xml @@ -170,6 +170,10 @@ CR Root http://openclientregistry.org/fhir/sourceid + + xdssender.PullNotificationsTask.lastSuccessfulRun + Time Stamp of Last Succesful Run for XDS Sender Pull Notifications Task +