Skip to content

Commit

Permalink
Merge pull request #98 from mozzy11/master
Browse files Browse the repository at this point in the history
XDS sender should persist last successful request date
  • Loading branch information
mozzy11 authored Nov 20, 2024
2 parents 8407846 + 384d6e7 commit 765c356
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -496,6 +496,8 @@ public final class XdsSenderConstants {

public static final String ECID_IDENTIFIER_TYPE_NAME = "ECID";

public static final String PULL_NOTIFICATIONS_TASK_LAST_SUCCESS_RUN = "xdssender.PullNotificationsTask.lastSuccessfulRun";

private XdsSenderConstants() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
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;
import javax.xml.namespace.QName;
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;
Expand All @@ -25,6 +32,7 @@
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;
Expand Down Expand Up @@ -58,11 +66,15 @@ 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;


@Override
public List<Message> getNewMessages() {
lastRequestDate = Context.getAdministrationService().getGlobalProperty(XdsSenderConstants.PULL_NOTIFICATIONS_TASK_LAST_SUCCESS_RUN , "");
LocationTag loginLocationTag = Context.getLocationService().getLocationTagByName(LOCATION_TAG_NAME);
List<Location> locations = Context.getLocationService().getLocationsByTag(loginLocationTag);
List<Message> returnMessages = new ArrayList<>();
Expand Down Expand Up @@ -149,6 +161,12 @@ 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) && isValidISODate(lastRequestDate) ? String.format("since=\"%s\"", lastRequestDate)
: "";
String maximumNumberElement = StringUtils.isBlank(lastRequestDate)
? "<ns2:MaximumNumber>100</ns2:MaximumNumber>\r\n"
: "";

String getMessagesPayload = String.format("<SOAP-ENV:Envelope\r\n "
+ "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n"
+ " <SOAP-ENV:Header/>\r\n"
Expand All @@ -159,12 +177,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"
+ " <ns2:MaximumNumber>100</ns2:MaximumNumber>\r\n"
+ " facility=\"%s\" %s>\r\n"
+ " %s"
+ " </ns2:GetMessages>\r\n"
+ " </SOAP-ENV:Body>\r\n"
+ "</SOAP-ENV:Envelope>",
facilitySiteCode);
facilitySiteCode ,sinceAttribute ,maximumNumberElement);
log.debug(getMessagesPayload);

RequestBody body = RequestBody.create(getMessagesPayload, mediaType);
Expand All @@ -185,6 +203,15 @@ private GetMessagesResponse getResponseHttpClient(GetMessages requestPayload) th
return null;
}
}

private boolean isValidISODate(String dateString) {
try {
DateTimeFormatter.ISO_INSTANT.parse(dateString);
return true;
} catch (DateTimeParseException e) {
return false;
}
}

private void addAuthorizationHeader() {
TransportContext context = TransportContextHolder.getTransportContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import ca.uhn.hl7v2.model.Message;
import org.openmrs.api.context.Context;
import org.openmrs.hl7.HL7Service;
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.scheduler.tasks.AbstractTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Instant;
import java.time.format.DateTimeFormatter;

public class PullNotificationsTask extends AbstractTask {

private static final Logger LOGGER = LoggerFactory.getLogger(PullNotificationsTask.class);
Expand All @@ -23,14 +27,21 @@ public class PullNotificationsTask extends AbstractTask {
public void execute() {
LOGGER.info("Executing " + TASK_NAME);
HL7Service hl7Service = Context.getHL7Service();
boolean success = true;
for (Message msg : getNotificationsPullPointClient().getNewMessages()) {
try {
hl7Service.processHL7Message(msg);
}
catch (Exception e) {
success = false;
LOGGER.error(e.getMessage(), e);
}
}

if(success){
Context.getAdministrationService().setGlobalProperty(XdsSenderConstants.PULL_NOTIFICATIONS_TASK_LAST_SUCCESS_RUN ,DateTimeFormatter.ISO_INSTANT.format(Instant.now()));
}

}

private NotificationsPullPointClient getNotificationsPullPointClient() {
Expand Down
4 changes: 4 additions & 0 deletions omod/src/main/resources/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@
<description>CR Root</description>
<defaultValue>http://openclientregistry.org/fhir/sourceid</defaultValue>
</globalProperty>
<globalProperty>
<property>xdssender.PullNotificationsTask.lastSuccessfulRun</property>
<description>Time Stamp of Last Succesful Run for XDS Sender Pull Notifications Task </description>
</globalProperty>

<!-- AOP
<advice>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</modules>

<properties>
<revision>2.4.0-SNAPSHOT</revision>
<revision>2.5.0-SNAPSHOT</revision>
<openmrsPlatformVersion>2.0.5</openmrsPlatformVersion>
<everestVersion>1.1.0</everestVersion>
<dcm4chee-xds2Version>2.0.6</dcm4chee-xds2Version>
Expand Down

0 comments on commit 765c356

Please sign in to comment.