From ec49906bacb30d76970fd97f4f2295198433e374 Mon Sep 17 00:00:00 2001
From: Lakshmi Kolli <69940873+kollil@users.noreply.github.com>
Date: Mon, 11 Nov 2024 10:08:03 -0800
Subject: [PATCH] Pathology assignments alert (#1173)
* Created a new alert to report any animal assignments where projected release condition is terminal in the last 35 days.
* Updated the center project display.
* Updated the display fields on the report
Updated the cron job time
---
.../study/pathAssignmentData.query.xml | 9 +
.../queries/study/pathAssignmentData.sql | 21 +++
.../org/labkey/onprc_ehr/ONPRC_EHRModule.java | 3 +
.../PathAssignmentNotification.java | 160 ++++++++++++++++++
4 files changed, 193 insertions(+)
create mode 100644 onprc_ehr/resources/queries/study/pathAssignmentData.query.xml
create mode 100644 onprc_ehr/resources/queries/study/pathAssignmentData.sql
create mode 100644 onprc_ehr/src/org/labkey/onprc_ehr/notification/PathAssignmentNotification.java
diff --git a/onprc_ehr/resources/queries/study/pathAssignmentData.query.xml b/onprc_ehr/resources/queries/study/pathAssignmentData.query.xml
new file mode 100644
index 000000000..ad055e69c
--- /dev/null
+++ b/onprc_ehr/resources/queries/study/pathAssignmentData.query.xml
@@ -0,0 +1,9 @@
+
+
+
+
+ Assignments for Pathology
+
+
+
+
diff --git a/onprc_ehr/resources/queries/study/pathAssignmentData.sql b/onprc_ehr/resources/queries/study/pathAssignmentData.sql
new file mode 100644
index 000000000..66f5c17c8
--- /dev/null
+++ b/onprc_ehr/resources/queries/study/pathAssignmentData.sql
@@ -0,0 +1,21 @@
+SELECT Id,
+-- Id.demographics.gender as Sex,
+ CASE
+ WHEN Id.demographics.gender = 'F' THEN 'Female'
+ WHEN Id.demographics.gender = 'M' THEN 'Male'
+ ELSE 'Unknown'
+ END AS Sex,
+ Id.Age.ageinyearsrounded as AgeInYearsRounded,
+ project.name as project,
+ project.protocol.investigatorId.lastname as Investigator,
+ project.title as Title,
+ date,
+ enddate,
+ projectedRelease,
+ (Select meaning from ehr_lookups.animal_condition where code = assignment.projectedReleaseCondition) as projectedReleaseCondition,
+ (Select meaning from ehr_lookups.animal_condition where code = assignment.assignCondition) as assignCondition,
+ (Select meaning from ehr_lookups.animal_condition where code = assignment.releaseCondition) as releaseCondition,
+FROM assignment
+Where projectedReleaseCondition = 206 --'Terminal'
+And date <= curdate() and date >= timestampadd(SQL_TSI_DAY, -35, curdate())
+
diff --git a/onprc_ehr/src/org/labkey/onprc_ehr/ONPRC_EHRModule.java b/onprc_ehr/src/org/labkey/onprc_ehr/ONPRC_EHRModule.java
index ff06b7cd2..df0f81a3f 100644
--- a/onprc_ehr/src/org/labkey/onprc_ehr/ONPRC_EHRModule.java
+++ b/onprc_ehr/src/org/labkey/onprc_ehr/ONPRC_EHRModule.java
@@ -225,6 +225,9 @@ protected void doStartupAfterSpringConfig(ModuleContext moduleContext)
//Added Aug 9th, 2024 Kollil
ns.registerNotification(new AdminNotifications(this));
+ //Added Nov 5th, 2024 Kollil
+ ns.registerNotification(new PathAssignmentNotification(this));
+
//Added 8-7-2018 R.Blasa
ns.registerNotification(new BirthHousingMismatchNotification(this));
diff --git a/onprc_ehr/src/org/labkey/onprc_ehr/notification/PathAssignmentNotification.java b/onprc_ehr/src/org/labkey/onprc_ehr/notification/PathAssignmentNotification.java
new file mode 100644
index 000000000..82df2695a
--- /dev/null
+++ b/onprc_ehr/src/org/labkey/onprc_ehr/notification/PathAssignmentNotification.java
@@ -0,0 +1,160 @@
+package org.labkey.onprc_ehr.notification;
+
+import org.apache.commons.lang3.time.DateUtils;
+import org.labkey.api.data.ColumnInfo;
+import org.labkey.api.data.CompareType;
+import org.labkey.api.data.Container;
+import org.labkey.api.data.ContainerFilter;
+import org.labkey.api.data.Results;
+import org.labkey.api.data.ResultsImpl;
+import org.labkey.api.data.Selector;
+import org.labkey.api.data.SimpleFilter;
+import org.labkey.api.data.Sort;
+import org.labkey.api.data.TableInfo;
+import org.labkey.api.data.TableSelector;
+import org.labkey.api.module.Module;
+import org.labkey.api.query.FieldKey;
+import org.labkey.api.query.QueryService;
+import org.labkey.api.security.User;
+import org.labkey.api.util.PageFlowUtil;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+
+/**
+ * Created by kollil on 10/24/2019.
+ */
+
+public class PathAssignmentNotification extends ColonyAlertsNotification
+{
+ public PathAssignmentNotification(Module owner)
+ {
+ super(owner);
+ }
+
+ @Override
+ public String getName()
+ {
+ return "Pathology Assignemnt Notification";
+ }
+
+ @Override
+ public String getEmailSubject(Container c)
+ {
+ return "Animal assignment alerts for Pathology: " + getDateTimeFormat(c).format(new Date());
+ }
+
+ @Override
+ public String getCronString()
+ {
+ return "0 0 9 ? * MON";
+
+ }
+
+ @Override
+ public String getScheduleDescription()
+ {
+ return "every Monday at 9Am";
+ }
+
+ @Override
+ public String getDescription()
+ {
+ return "The report is designed to send animal assignments data to pathology every Monday!";
+ }
+
+ @Override
+ public String getMessageBodyHTML(Container c, User u)
+ {
+ StringBuilder msg = new StringBuilder();
+
+ pathAssignmentAlert(c, u, msg);
+
+ return msg.toString();
+ }
+
+ /**
+ * Kollil, 11/05/2024 : Ticket # 11319, listing assignments made with release condition as 'terminal' with the anticipated release date. It could look something like this:
+ * a. Query: Assignment date = within the last 35 days, with 'projected release condition' = Terminal
+ * b. Report: Table with columns as attached (I created a custom view as an example, but you can include whatever info is relevant)
+ **/
+ private void pathAssignmentAlert(Container c, User u, final StringBuilder msg)
+ {
+ if (QueryService.get().getUserSchema(u, c, "study") == null)
+ {
+ msg.append("Warning: The study schema has not been enabled in this folder, so the alert cannot run!
");
+ return;
+ }
+ //assignments query
+ TableInfo ti = QueryService.get().getUserSchema(u, c, "study").getTable("pathAssignmentData", ContainerFilter.Type.AllFolders.create(c, u));
+ TableSelector ts = new TableSelector(ti, null, null);
+ long count = ts.getRowCount();
+
+ //Get num of rows
+ if (count > 0)
+ {
+ msg.append("" + count + " assignments found with projected release condition = Terminal in the last 35 days:");
+ msg.append("Click here to view the assignments in PRIME
\n");
+ msg.append("
");
+ }
+ else
+ {
+ msg.append(" There are no assignments found with projected release condition = Terminal in the last 35 days ");
+ msg.append("
");
+ }
+
+ //Display the daily report in the email
+ if (count > 0)
+ {
+ Set columns = new HashSet<>();
+ columns.add(FieldKey.fromString("Id"));
+ columns.add(FieldKey.fromString("Sex"));
+ columns.add(FieldKey.fromString("AgeInYearsRounded"));
+ columns.add(FieldKey.fromString("project"));
+ columns.add(FieldKey.fromString("Investigator"));
+ columns.add(FieldKey.fromString("Title"));
+ columns.add(FieldKey.fromString("date"));
+ columns.add(FieldKey.fromString("enddate"));
+ columns.add(FieldKey.fromString("projectedRelease"));
+ columns.add(FieldKey.fromString("projectedReleaseCondition"));
+ columns.add(FieldKey.fromString("assignCondition"));
+ columns.add(FieldKey.fromString("releaseCondition"));
+
+ final Map colMap = QueryService.get().getColumns(ti, columns);
+ TableSelector ts2 = new TableSelector(ti, colMap.values(), null, null);
+
+ msg.append("
Assignments:
\n");
+ msg.append("");
+ msg.append("");
+ msg.append("Id | Sex | Age (Years, Rounded) | Center Project | Investigator | Title | Assign Date | Release Date | Projected Release Date | Projected Release Condition | Condition At Assignment | Condition At Release |
");
+
+ ts2.forEach(object -> {
+ Results rs = new ResultsImpl(object, colMap);
+ String url = getParticipantURL(c, rs.getString("Id"));
+
+ msg.append("");
+ msg.append(" " + PageFlowUtil.filter(rs.getString("Id")) + " | \n");
+ msg.append("" + PageFlowUtil.filter(rs.getString("Sex")) + " | ");
+ msg.append("" + PageFlowUtil.filter(rs.getString("AgeInYearsRounded")) + " | ");
+ msg.append("" + PageFlowUtil.filter(rs.getString("project")) + " | ");
+ msg.append("" + PageFlowUtil.filter(rs.getString("Investigator")) + " | ");
+ msg.append("" + PageFlowUtil.filter(rs.getString("Title")) + " | ");
+ msg.append("" + PageFlowUtil.filter(rs.getString("date")) + " | ");
+ msg.append("" + PageFlowUtil.filter(rs.getString("enddate")) + " | ");
+ msg.append("" + PageFlowUtil.filter(rs.getString("projectedRelease")) + " | ");
+ msg.append("" + PageFlowUtil.filter(rs.getString("projectedReleaseCondition")) + " | ");
+ msg.append("" + PageFlowUtil.filter(rs.getString("assigncondition")) + " | ");
+ msg.append("" + PageFlowUtil.filter(rs.getString("releasecondition")) + " | ");
+ msg.append("
");
+ });
+ msg.append("
");
+ }
+ }
+}