|
| 1 | +package org.labkey.nirc_ehr.notification; |
| 2 | + |
| 3 | +import org.jetbrains.annotations.Nullable; |
| 4 | +import org.labkey.api.data.Container; |
| 5 | +import org.labkey.api.data.TableInfo; |
| 6 | +import org.labkey.api.data.TableSelector; |
| 7 | +import org.labkey.api.ehr.notification.AbstractEHRNotification; |
| 8 | +import org.labkey.api.query.QueryService; |
| 9 | +import org.labkey.api.query.UserSchema; |
| 10 | +import org.labkey.api.security.User; |
| 11 | +import org.labkey.api.settings.AppProps; |
| 12 | + |
| 13 | +import java.util.Date; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Set; |
| 16 | + |
| 17 | + |
| 18 | +public class NIRCProcedureOverdueNotification extends AbstractEHRNotification |
| 19 | +{ |
| 20 | + @Override |
| 21 | + public String getName() |
| 22 | + { |
| 23 | + return "NIRC Procedure Overdue Notification"; |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public String getScheduleDescription() |
| 28 | + { |
| 29 | + return "Weekly on Friday at 8:00 AM"; |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public String getCronString() |
| 34 | + { |
| 35 | + return "0 0 8 ? * 6"; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public @Nullable String getMessageBodyHTML(Container c, User u) |
| 40 | + { |
| 41 | + List<ProcedureOverdue> procsOverdueList = getOverDueProcedures(c, u); |
| 42 | + StringBuilder html = new StringBuilder(); |
| 43 | + html.append("<html>"); |
| 44 | + |
| 45 | + if (!procsOverdueList.isEmpty()) |
| 46 | + { |
| 47 | + html.append("<h4>Animals with overdue procedures</h4>"); |
| 48 | + appendTableHtml(c, html, procsOverdueList); |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + html.append("<h4>No overdue procedures</h4>"); |
| 53 | + } |
| 54 | + |
| 55 | + html.append("<br/>"); |
| 56 | + |
| 57 | + String procOverdueReportLink = AppProps.getInstance().getBaseServerUrl() + AppProps.getInstance().getContextPath() + c.getPath() + "/query-executeQuery.view?schemaName=study&query.queryName=prcOverdue"; |
| 58 | + html.append("<a href='").append(procOverdueReportLink).append("'>"); |
| 59 | + html.append("Click here to view Overdue Procedures report with additional actions.</a>"); |
| 60 | + |
| 61 | + html.append("</html>"); |
| 62 | + return html.toString(); |
| 63 | + } |
| 64 | + |
| 65 | + private void appendTableHtml(Container c, StringBuilder html, List<ProcedureOverdue> procedureOverdueList) |
| 66 | + { |
| 67 | + html.append("<table style=\"border-collapse: collapse;\">"); |
| 68 | + html.append("<tr>"); |
| 69 | + html.append(NotificationHelper.getNotificationGridCellHeader(c, ("Id"))); |
| 70 | + html.append(NotificationHelper.getNotificationGridCellHeader(c, ("Species"))); |
| 71 | + html.append(NotificationHelper.getNotificationGridCellHeader(c,"Room")); |
| 72 | + html.append(NotificationHelper.getNotificationGridCellHeader(c,"Cage")); |
| 73 | + html.append(NotificationHelper.getNotificationGridCellHeader(c,"Procedure")); |
| 74 | + html.append(NotificationHelper.getNotificationGridCellHeader(c,"Ordered By")); |
| 75 | + html.append(NotificationHelper.getNotificationGridCellHeader(c,"Window Start")); |
| 76 | + html.append(NotificationHelper.getNotificationGridCellHeader(c,"Window End")); |
| 77 | + html.append(NotificationHelper.getNotificationGridCellHeader(c,"Days Overdue")); |
| 78 | + html.append(NotificationHelper.getNotificationGridCellHeader(c,"Remark")); |
| 79 | + |
| 80 | + html.append("</tr>"); |
| 81 | + |
| 82 | + for (int i = 0; i < procedureOverdueList.size(); i++) |
| 83 | + { |
| 84 | + ProcedureOverdue pod = procedureOverdueList.get(i); |
| 85 | + String bgColor = i % 2 == 0 ? "#f2f2f2" : "#fff"; |
| 86 | + html.append("<tr style=\"background-color:").append(bgColor).append("\">"); |
| 87 | + |
| 88 | + html.append(NotificationHelper.getNotificationGridCell(c, pod.getId(), null, false, true, false)); |
| 89 | + html.append(NotificationHelper.getNotificationGridCell(c, pod.getSpecies())); |
| 90 | + html.append(NotificationHelper.getNotificationGridCell(c, pod.getRoom())); |
| 91 | + html.append(NotificationHelper.getNotificationGridCell(c, pod.getCage())); |
| 92 | + html.append(NotificationHelper.getNotificationGridCell(c, pod.getProcedure())); |
| 93 | + html.append(NotificationHelper.getNotificationGridCell(c, pod.getOrderedBy())); |
| 94 | + html.append(NotificationHelper.getNotificationGridCell(c, NotificationHelper.getFormattedDate(c, pod.getWindowStart()))); |
| 95 | + html.append(NotificationHelper.getNotificationGridCell(c, NotificationHelper.getFormattedDate(c, pod.getWindowEnd()))); |
| 96 | + html.append(NotificationHelper.getNotificationGridCell(c, String.valueOf(pod.getDaysOverdue()))); |
| 97 | + html.append(NotificationHelper.getNotificationGridCell(c, pod.getRemark())); |
| 98 | + html.append("</tr>"); |
| 99 | + } |
| 100 | + |
| 101 | + html.append("</table>"); |
| 102 | + } |
| 103 | + |
| 104 | + private List<ProcedureOverdue> getOverDueProcedures(Container c, User u) throws IllegalStateException |
| 105 | + { |
| 106 | + UserSchema userSchema = QueryService.get().getUserSchema(u, c, "study"); |
| 107 | + TableInfo tableInfo = userSchema.getTable("prcOverdueNotification", null); |
| 108 | + |
| 109 | + if (null == tableInfo) |
| 110 | + { |
| 111 | + throw new IllegalStateException("Expected 'prcOverdueNotification' query for the 'NIRC Procedure Overdue' notification"); |
| 112 | + } |
| 113 | + |
| 114 | + TableSelector tableSelector = new TableSelector(tableInfo, Set.of("Id", "species", "room", "cage", "procedure", "orderedBy", "windowStart", "windowEnd", "daysOverdue", "remark")); |
| 115 | + return tableSelector.getArrayList(ProcedureOverdue.class); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public String getDescription() |
| 120 | + { |
| 121 | + return "Weekly notification sent Friday morning for animals with procedures past the scheduled window"; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public String getEmailSubject(Container c) |
| 126 | + { |
| 127 | + return "Procedure overdue notification"; |
| 128 | + } |
| 129 | + |
| 130 | + public static class ProcedureOverdue |
| 131 | + { |
| 132 | + public String Id; |
| 133 | + public String species; |
| 134 | + public String room; |
| 135 | + public String cage; |
| 136 | + public String cageObjectId; |
| 137 | + public String procedure; |
| 138 | + public String orderedBy; |
| 139 | + public Date windowStart; |
| 140 | + public Date windowEnd; |
| 141 | + public int daysOverdue; |
| 142 | + public String remark; |
| 143 | + |
| 144 | + public String getId() |
| 145 | + { |
| 146 | + return Id; |
| 147 | + } |
| 148 | + |
| 149 | + public void setId(String id) |
| 150 | + { |
| 151 | + Id = id; |
| 152 | + } |
| 153 | + |
| 154 | + public String getSpecies() |
| 155 | + { |
| 156 | + return species; |
| 157 | + } |
| 158 | + |
| 159 | + public void setSpecies(String species) |
| 160 | + { |
| 161 | + this.species = species; |
| 162 | + } |
| 163 | + |
| 164 | + public String getRoom() |
| 165 | + { |
| 166 | + return room; |
| 167 | + } |
| 168 | + |
| 169 | + public void setRoom(String room) |
| 170 | + { |
| 171 | + this.room = room; |
| 172 | + } |
| 173 | + |
| 174 | + public String getCage() |
| 175 | + { |
| 176 | + return cage; |
| 177 | + } |
| 178 | + |
| 179 | + public void setCage(String cage) |
| 180 | + { |
| 181 | + this.cage = cage; |
| 182 | + } |
| 183 | + |
| 184 | + public String getCageObjectId() |
| 185 | + { |
| 186 | + return cageObjectId; |
| 187 | + } |
| 188 | + |
| 189 | + public void setCageObjectId(String cageObjectId) |
| 190 | + { |
| 191 | + this.cageObjectId = cageObjectId; |
| 192 | + } |
| 193 | + |
| 194 | + public String getProcedure() |
| 195 | + { |
| 196 | + return procedure; |
| 197 | + } |
| 198 | + |
| 199 | + public void setProcedure(String procedure) |
| 200 | + { |
| 201 | + this.procedure = procedure; |
| 202 | + } |
| 203 | + |
| 204 | + public String getOrderedBy() |
| 205 | + { |
| 206 | + return orderedBy; |
| 207 | + } |
| 208 | + |
| 209 | + public void setOrderedBy(String orderedBy) |
| 210 | + { |
| 211 | + this.orderedBy = orderedBy; |
| 212 | + } |
| 213 | + |
| 214 | + public Date getWindowStart() |
| 215 | + { |
| 216 | + return windowStart; |
| 217 | + } |
| 218 | + |
| 219 | + public void setWindowStart(Date windowStart) |
| 220 | + { |
| 221 | + this.windowStart = windowStart; |
| 222 | + } |
| 223 | + |
| 224 | + public Date getWindowEnd() |
| 225 | + { |
| 226 | + return windowEnd; |
| 227 | + } |
| 228 | + |
| 229 | + public void setWindowEnd(Date windowEnd) |
| 230 | + { |
| 231 | + this.windowEnd = windowEnd; |
| 232 | + } |
| 233 | + |
| 234 | + public int getDaysOverdue() |
| 235 | + { |
| 236 | + return daysOverdue; |
| 237 | + } |
| 238 | + |
| 239 | + public void setDaysOverdue(int daysOverdue) |
| 240 | + { |
| 241 | + this.daysOverdue = daysOverdue; |
| 242 | + } |
| 243 | + |
| 244 | + public String getRemark() |
| 245 | + { |
| 246 | + return remark; |
| 247 | + } |
| 248 | + |
| 249 | + public void setRemark(String remark) |
| 250 | + { |
| 251 | + this.remark = remark; |
| 252 | + } |
| 253 | + } |
| 254 | +} |
0 commit comments