/
PatientFlagTask.java
133 lines (109 loc) · 3.66 KB
/
PatientFlagTask.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* The contents of this file are subject to the OpenMRS Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs.module.patientflags.task;
import java.util.HashMap;
import java.util.List;
import org.openmrs.Patient;
import org.openmrs.api.context.Context;
import org.openmrs.api.context.Daemon;
import org.openmrs.module.DaemonToken;
import org.openmrs.module.patientflags.Flag;
import org.openmrs.module.patientflags.PatientFlag;
import org.openmrs.module.patientflags.api.FlagService;
public class PatientFlagTask implements Runnable {
private static DaemonToken daemonToken;
private Patient patient;
private Flag flag;
@Override
public void run() {
FlagService flagService = Context.getService(FlagService.class);
if (patient != null) {
generatePatientFlags(patient, flagService);
}
else if (flag != null) {
generatePatientFlags(flag, flagService);
}
else {
evaluateAllFlags();
}
}
public static Runnable evaluateAllFlags() {
return Daemon.runInDaemonThread(new AllFlagsEvaluator(), daemonToken);
}
public static void setDaemonToken(DaemonToken token) {
daemonToken = token;
}
public void generatePatientFlags(Patient patient) {
this.patient = patient;
if (daemonToken != null) {
Daemon.runInDaemonThread(this, daemonToken);
}
}
public void generatePatientFlags(Flag flag) {
this.flag = flag;
if (daemonToken != null) {
Daemon.runInDaemonThread(this, daemonToken);
}
}
private static void generatePatientFlags(Flag flag, FlagService service) {
service.deletePatientFlagsForFlag(flag);
if (!flag.getEnabled() || flag.isRetired()) {
return;
}
HashMap<Object, Object> context = new HashMap<Object, Object>();
org.openmrs.Cohort cohort = service.getFlaggedPatients(flag, context);
if (cohort == null) {
return;
}
java.util.Set<Integer> members = cohort.getMemberIds();
for (Integer patientId : members) {
List<String> flgs = (List<String>)context.get(patientId);
if (flgs != null) {
for (String flg : flgs) {
service.savePatientFlag(new PatientFlag(new Patient(patientId), flag, flg));
}
}
else {
service.savePatientFlag(new PatientFlag(new Patient(patientId), flag, flag.evalMessage(patientId)));
}
}
}
private void generatePatientFlags(Patient patient, FlagService service) {
service.deletePatientFlagsForPatient(patient);
HashMap<Object, Object> context = new HashMap<Object, Object>();
List<Flag> flags = service.generateFlagsForPatient(patient, context);
for (Flag flag : flags) {
List<String> flgs = (List<String>)context.get(patient.getPatientId());
if (flgs != null) {
for (String flg : flgs) {
service.savePatientFlag(new PatientFlag(patient, flag, flg));
}
}
else {
service.savePatientFlag(new PatientFlag(patient, flag, flag.evalMessage(patient.getPatientId())));
}
}
}
//The only reason why we have this class is to be able to run in
//a daemon thread in order to get daemon access to the database
private static class AllFlagsEvaluator implements Runnable {
@Override
public void run() {
FlagService flagService = Context.getService(FlagService.class);
for (Flag flag : flagService.getAllFlags()) {
generatePatientFlags(flag, flagService);
}
}
}
}