Newer
Older
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package edu.ncsu.csc.itrust.action;
import java.util.ArrayList;
import java.util.List;
import edu.ncsu.csc.itrust.beans.FlagsBean;
import edu.ncsu.csc.itrust.beans.HealthRecord;
import edu.ncsu.csc.itrust.beans.ObstetricsRecordBean;
import edu.ncsu.csc.itrust.beans.PatientBean;
import edu.ncsu.csc.itrust.dao.DAOFactory;
import edu.ncsu.csc.itrust.dao.mysql.AllergyDAO;
import edu.ncsu.csc.itrust.dao.mysql.FlagsDAO;
import edu.ncsu.csc.itrust.dao.mysql.HealthRecordsDAO;
import edu.ncsu.csc.itrust.dao.mysql.ObstetricsRecordDAO;
import edu.ncsu.csc.itrust.dao.mysql.PatientDAO;
import edu.ncsu.csc.itrust.dao.mysql.PreExistingConditionsDAO;
import edu.ncsu.csc.itrust.enums.DeliveryType;
import edu.ncsu.csc.itrust.enums.FlagValue;
import edu.ncsu.csc.itrust.enums.PregnancyStatus;
import edu.ncsu.csc.itrust.enums.TransactionType;
import edu.ncsu.csc.itrust.exception.FormValidationException;
import edu.ncsu.csc.itrust.exception.ITrustException;
import edu.ncsu.csc.itrust.validate.ObstetricsRecordValidator;
/**
* Used for add obstetrics record page (addObstetricsRecord.jsp).
*
* Very similar to {@link AddOfficeVisitAction}
*
*
*/
public class AddObstetricsAction {
private ObstetricsRecordDAO obstetricsDAO;
private FlagsDAO flagsDAO;
private AllergyDAO allergyDAO;
private PreExistingConditionsDAO conditionsDAO;
private PatientDAO patientDAO;
private long loggedInMID;
private EventLoggingAction loggingAction;
private HealthRecordsDAO hDao;
private DAOFactory prodDAO;
/**
* Just the factory and logged in MID
*
* @param factory
* @param loggedInMID
*/
public AddObstetricsAction(DAOFactory factory, long loggedInMID) {
this.obstetricsDAO = factory.getObstetricsRecordDAO();
this.flagsDAO = factory.getFlagsDAO();
this.allergyDAO = factory.getAllergyDAO();
this.conditionsDAO = factory.getPreExistingConditionsDAO();
this.patientDAO = factory.getPatientDAO();
this.loggedInMID = loggedInMID;
this.loggingAction = new EventLoggingAction(factory);
this.hDao = new HealthRecordsDAO(factory);
this.prodDAO = factory;
}
/**
* Add a new obstetrics record
*
* @param p ObstetricsRecordBean containing the info for the record to be created
* @param flags FlagsBean ArrayList containing all flags manually set on the form that submitted this
* @throws FormValidationException if the patient is not successfully validated
* @throws ITrustException
*/
public void addObstetricsRecord(ObstetricsRecordBean p, ArrayList<FlagsBean> flags)
throws FormValidationException, ITrustException {
if (p != null) {
new ObstetricsRecordValidator().validate(p);
List<ObstetricsRecordBean> initialBeans = null;
//if not an office visit
if (!p.getPregnancyStatus().equals(PregnancyStatus.Office)) {
ObstetricsRecordBean bean = obstetricsDAO.getObstetricsRecordsByMIDLargestpregId(p.getMid());
if (bean == null || bean.getPregnancyStatus() == null) {
p.setPregId(0);
}
else {
p.setPregId(bean.getPregId() + 1);
}
}
//else, it is an office visit
else {
initialBeans = obstetricsDAO.getObstetricsRecordsByMIDPregStatus(
p.getMid(), PregnancyStatus.Initial.toString());
if (initialBeans == null || initialBeans.isEmpty()) {
throw new ITrustException("The patient chosen is not a current obstetrics patient");
}
else {
p.setPregId(initialBeans.get(0).getPregId());
}
}
//add the obstetrics record to the database
obstetricsDAO.addObstetricsRecord(p);
boolean isTwins = false;
//set the manual flags first to load from later and pull if twins is set
for (FlagsBean flag : flags) {
flag.setPregId(p.getPregId());
flagsDAO.setFlag(flag);
if (flag.getFlagValue() == FlagValue.Twins)
isTwins = flag.isFlagged();
}
//Office Visit flags
if (p.getPregnancyStatus().equals(PregnancyStatus.Office)) {
//Weight flag processing
boolean setWeightFlag = false;
List<HealthRecord> records = hDao.getAllRecordsBeforeOVDate(p.getMid(), p.getLmp());
//can only do BMI calculations if a normal office visit (with height/weight) exists before LMP
if (records.isEmpty()) {
records = hDao.getAllHealthRecords(p.getMid());
}
if (!records.isEmpty()) {
double height = records.get(0).getHeight();
double origWeight = records.get(0).getWeight();
double newWeight = p.getWeight();
//calculate the original BMI
double origBMI = HealthRecord.calculateBMI(height, origWeight);
double weightChange = newWeight - origWeight;
//find the expected weight offsets in BMI/weight tables
for (int i = 0; i < FlagsBean.WEIGHTGAINBMIBOUNDS.length; i++) {
//This is the correct upper bound
if (FlagsBean.WEIGHTGAINBMIBOUNDS[i] > origBMI) {
if (isTwins) {
//If not in expected twins range
if (FlagsBean.WeightGainTwinsBMIMin[i] > weightChange ||
FlagsBean.WeightGainTwinsBMIMax[i] < weightChange) {
setWeightFlag = true;
}
} else {
//If not in expected range
if (FlagsBean.WEIGHTGAINBMIMIN[i] > weightChange ||
FlagsBean.WeightGainBMIMax[i] < weightChange) {
setWeightFlag = true;
}
}
break; //exit after computed
}
}
}
//Set weight flag if needed
FlagsBean wFlag = new FlagsBean();
wFlag.setMid(p.getMid());
wFlag.setPregId(p.getPregId());
wFlag.setFlagValue(FlagValue.WeightChange);
wFlag = flagsDAO.getFlag(wFlag);
if (!wFlag.isFlagged()) {
wFlag.setFlagged(setWeightFlag);
flagsDAO.setFlag(wFlag);
}
//Set FHR flag if needed
FlagsBean fhrFlag = new FlagsBean();
fhrFlag.setMid(p.getMid());
fhrFlag.setPregId(p.getPregId());
fhrFlag.setFlagValue(FlagValue.AbnormalFHR);
fhrFlag = flagsDAO.getFlag(fhrFlag);
if (!fhrFlag.isFlagged()) {
if(p.getFhr() > FlagsBean.FHR_MAX_TRIGGER || ((p.getFhr() > 0) && (p.getFhr() < FlagsBean.FHR_MIN_TRIGGER))) {
fhrFlag.setFlagged(true);
flagsDAO.setFlag(fhrFlag);
}
}
//Set BP flag if needed
FlagsBean bpFlag = new FlagsBean();
bpFlag.setMid(p.getMid());
bpFlag.setPregId(p.getPregId());
bpFlag.setFlagValue(FlagValue.HighBloodPressure);
bpFlag = flagsDAO.getFlag(bpFlag);
if (!bpFlag.isFlagged()) {
if(p.getBloodPressureS() > FlagsBean.SYS_BP_MAX_TRIGGER || p.getBloodPressureD() > FlagsBean.DIA_BP_MAX_TRIGGER) {
bpFlag.setFlagged(true);
flagsDAO.setFlag(bpFlag);
}
}
}
//UC67: Allergies Flag
FlagsBean allergiesFlag = new FlagsBean();
allergiesFlag.setFlagValue(FlagValue.MaternalAllergies);
allergiesFlag.setMid(p.getMid());
allergiesFlag.setPregId(p.getPregId());
allergiesFlag.setFlagged(!allergyDAO.getAllergies(p.getMid()).isEmpty()); //if not empty, have allergies
flagsDAO.setFlag(allergiesFlag);
//Advanced maternal age flag
PatientBean chosenPatient = patientDAO.getPatient(p.getMid());
if (chosenPatient.getAge() > 35) {
FlagsBean advancedMaternalAge = new FlagsBean();
advancedMaternalAge.setMid(p.getMid());
advancedMaternalAge.setPregId(p.getPregId());
advancedMaternalAge.setFlagValue(FlagValue.AdvancedMaternalAge);
advancedMaternalAge.setFlagged(true);
flagsDAO.setFlag(advancedMaternalAge);
}
//Rh- flag
if (chosenPatient.getBloodType().toString().contains("-")) {
FlagsBean rhNeg = new FlagsBean();
rhNeg.setMid(p.getMid());
rhNeg.setPregId(p.getPregId());
rhNeg.setFlagValue(FlagValue.rhNeg);
rhNeg.setFlagged(true);
flagsDAO.setFlag(rhNeg);
}
//Genetic miscarriage flag
ViewObstetricsAction viewObstetrics = new ViewObstetricsAction(prodDAO, loggedInMID);
List<ObstetricsRecordBean> priorPregnancies = viewObstetrics.getViewableObstetricsRecordsByMIDType(p.getMid(), PregnancyStatus.Complete);
int priorMiscarriageCount = 0;
for (ObstetricsRecordBean prior : priorPregnancies) {
if(prior.getDeliveryType().equals(DeliveryType.Miscarriage)) {
priorMiscarriageCount++;
}
}
if (priorMiscarriageCount > 1) {
FlagsBean miscarriage = new FlagsBean();
miscarriage.setMid(p.getMid());
miscarriage.setPregId(p.getPregId());
miscarriage.setFlagValue(FlagValue.GeneticMiscarriage);
miscarriage.setFlagged(true);
flagsDAO.setFlag(miscarriage);
}
//Pre-existing conditions flag
List<String> conditions = conditionsDAO.getConditionsByMID(p.getMid());
if (!conditions.isEmpty()) {
FlagsBean conds = new FlagsBean();
conds.setMid(p.getMid());
conds.setPregId(p.getPregId());
conds.setFlagValue(FlagValue.PreExistingConditions);
conds.setFlagged(true);
flagsDAO.setFlag(conds);
}
if (p.getPregnancyStatus().equals(PregnancyStatus.Initial)) {
loggingAction.logEvent(TransactionType.parse(6300), loggedInMID,
p.getMid(), "Initial Obstetrics Record " + p.getOid() + " added");
}
else if (p.getPregnancyStatus().equals(PregnancyStatus.Office)) {
loggingAction.logEvent(TransactionType.parse(6400), loggedInMID,
p.getMid(), "Obstetrics Office Visit " + p.getOid() + " added");
}
}
else {
throw new ITrustException("Cannot add a null Obstetrics Record.");
}
}
}