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
package edu.ncsu.csc.itrust.action;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import edu.ncsu.csc.itrust.action.base.ViewEntryAction;
import edu.ncsu.csc.itrust.beans.FoodEntryBean;
import edu.ncsu.csc.itrust.beans.PersonnelBean;
import edu.ncsu.csc.itrust.dao.DAOFactory;
import edu.ncsu.csc.itrust.dao.mysql.FoodEntryDAO;
import edu.ncsu.csc.itrust.dao.mysql.PatientDAO;
import edu.ncsu.csc.itrust.dao.mysql.PersonnelDAO;
import edu.ncsu.csc.itrust.exception.DBException;
import edu.ncsu.csc.itrust.exception.FormValidationException;
import edu.ncsu.csc.itrust.exception.ITrustException;
/**
* We need to decide how we want to do this. We can include a check here in
* getting a food diary to ensure that the person viewing it is either a patient
* or an HCP with the specialty of 'nutritionist' before letting them get the
* food diary, or we could break this up into separate classes and separate jsp
* pages. ViewFoodEntryAction.java Version 1 2/21/2015 Copyright notice: none
*/
public class ViewFoodEntryAction implements ViewEntryAction {
private FoodEntryDAO foodEntryDAO;
private PatientDAO patientDAO;
private long loggedInMID;
private PersonnelDAO personnelDAO;
/**
* Uses the factory to construct foodentrydao and patientdao
*
* @param factory
* DAO factory to use
* @param loggedInMID
* who is currently logged in
*/
public ViewFoodEntryAction(DAOFactory factory, long loggedInMID) {
this.patientDAO = factory.getPatientDAO();
this.personnelDAO = factory.getPersonnelDAO();
this.foodEntryDAO = factory.getFoodEntryDAO();
this.loggedInMID = loggedInMID;
}
/**
* Takes in which patient you want to view the food diary for, and then
* returns all of the food dairy entries for that patient. It first checks
* to ensure that the person requesting the food diary is either a patient,
* or is an HCP with a specialty of nutritionist.
*
* @param patientMID
* the id of the patient whose food diary we want
* @return a list of the patient's food diary entries
*/
public List<FoodEntryBean> getDiary(long patientMID)
throws ITrustException {
PersonnelBean personnel;
try {
/*
* This nightmare if-statement just checks for two things. Either:
* 1) The patient is viewing their own diary.
* 2) A nutritionist designated for the provided PID is viewing the diary.
* the Nutritionist must be the designated nutritionist
*/
if ((patientDAO.checkPatientExists(loggedInMID) && loggedInMID == patientMID)
|| (((personnel = personnelDAO.getPersonnel(loggedInMID)) != null) && personnel
.getSpecialty() != null && personnel.getSpecialty().equalsIgnoreCase("Nutritionist") &&
(patientDAO.getDesignatedNutritionist(patientMID) == loggedInMID))) {
return foodEntryDAO.getPatientFoodDiary(patientMID);
} else {
throw new ITrustException("You do not have permission to "
+ "view the Food Diary!");
}
} catch (DBException d) {
throw new ITrustException("Error retrieving Food Diary");
}
}
/**
* Gets the totals of carbs, protein, sugar, calories, sodium, fat, and
* fiber a user has eaten sorted by day.
*
* @param patientMID
* the patient we are looking at
* @return an entry that contains the totals for each day that a user has an
* entry in his food diary
* @throws ITrustException
*/
public List<FoodEntryBean> getDiaryTotals(long patientMID)
throws ITrustException {
PersonnelBean personnel;
try {
/*
* This nightmare if-statement just checks for two things. Either:
* 1) The patient is viewing their own diary.
* 2) A nutritionist designated for the provided PID is viewing the diary.
*/
if ((patientDAO.checkPatientExists(loggedInMID) && loggedInMID == patientMID)
|| (((personnel = personnelDAO.getPersonnel(loggedInMID)) != null) && personnel
.getSpecialty() != null && personnel.getSpecialty().equalsIgnoreCase("Nutritionist") &&
(patientDAO.getDesignatedNutritionist(patientMID) == loggedInMID))) {
return foodEntryDAO.getPatientFoodDiaryTotals(patientMID);
} else {
throw new ITrustException("You do not have permission to "
+ "view the Food Diary!");
}
} catch (DBException d) {
throw new ITrustException("Error retrieving Food Diary");
}
}
/**
* Returns a list of food diary entries between two dates.
*
* @param lowerDate
* the first date
* @param upperDate
* the second date
* @return list of TransactionBeans
* @throws DBException
* @throws FormValidationException
*/
public List<FoodEntryBean> getBoundedDiary(String lowerDate,
String upperDate, long patientMID) throws ITrustException,
FormValidationException {
PersonnelBean personnel;
try {
/*
* This nightmare if-statement just checks for two things. Either:
* 1) The patient is viewing their own diary.
* 2) A nutritionist designated for the provided PID is viewing the diary.
*/
if ((patientDAO.checkPatientExists(loggedInMID) && loggedInMID == patientMID)
|| (((personnel = personnelDAO.getPersonnel(loggedInMID)) != null) && personnel
.getSpecialty() != null && personnel.getSpecialty().equalsIgnoreCase("Nutritionist") &&
(patientDAO.getDesignatedNutritionist(patientMID) == loggedInMID))) {
/*
* Month can have 1 or 2 digits, same with day, and year must
* have 4.
*/
Pattern p = Pattern
.compile("[0-9]{1,2}?/[0-9]{1,2}?/[0-9]{4}?");
Matcher m = p.matcher(lowerDate);
Matcher n = p.matcher(upperDate);
/*
* If it fails to match either of them, throw the form
* validation exception
*/
if (!m.matches() || !n.matches()) {
throw new FormValidationException(
"Enter dates in MM/dd/yyyy");
}
Date lower = new SimpleDateFormat("MM/dd/yyyy")
.parse(lowerDate);
Date upper = new SimpleDateFormat("MM/dd/yyyy")
.parse(upperDate);
if (lower.after(upper)) {
throw new FormValidationException(
"Start date must be before end date!");
}
return foodEntryDAO.getBoundedFoodDiary(lower, upper,
patientMID);
} else {
throw new ITrustException("You do not have permission to "
+ "view the Food Diary!");
}
} catch (DBException e) {
throw new ITrustException("Error retrieving Food Diary");
} catch (ParseException d) {
throw new ITrustException("Error parsing Dates");
}
}
/**
* Gets the totals of carbs, protein, sugar, calories, sodium, fat, and
* fiber a user has eaten in a given date range, sorted by day.
*
* @param lowerDate
* the first date
* @param upperDate
* the second date
* @param patientMID
* the patient we are looking at
* @return an entry that contains the totals for each day in the given range
* for the patient
* @throws ITrustException
*/
public List<FoodEntryBean> getBoundedDiaryTotals(String lowerDate,
String upperDate, long patientMID) throws ITrustException,
FormValidationException {
PersonnelBean personnel;
try {
/*
* This nightmare if-statement just checks for two things. Either:
* 1) The patient is viewing their own diary.
* 2) A nutritionist designated for the provided PID is viewing the diary.
*/
if ((patientDAO.checkPatientExists(loggedInMID) && loggedInMID == patientMID)
|| (((personnel = personnelDAO.getPersonnel(loggedInMID)) != null) && personnel
.getSpecialty() != null && personnel.getSpecialty().equalsIgnoreCase("Nutritionist") &&
(patientDAO.getDesignatedNutritionist(patientMID) == loggedInMID))) {
/*
* Month can have 1 or 2 digits, same with day, and year must
* have 4.
*/
Pattern p = Pattern
.compile("[0-9]{1,2}?/[0-9]{1,2}?/[0-9]{4}?");
Matcher m = p.matcher(lowerDate);
Matcher n = p.matcher(upperDate);
/*
* If it fails to match either of them, throw the form
* validation exception
*/
if (!m.matches() || !n.matches()) {
throw new FormValidationException(
"Enter dates in MM/dd/yyyy");
}
Date lower = new SimpleDateFormat("MM/dd/yyyy")
.parse(lowerDate);
Date upper = new SimpleDateFormat("MM/dd/yyyy")
.parse(upperDate);
if (lower.after(upper)) {
throw new FormValidationException(
"Start date must be before end date!");
}
return foodEntryDAO.getBoundedFoodDiaryTotals(lower, upper,
patientMID);
} else {
throw new ITrustException("You do not have permission to "
+ "view the Food Diary!");
}
} catch (DBException e) {
throw new ITrustException("Error retrieving Food Diary");
} catch (ParseException d) {
throw new ITrustException("Error parsing Dates");
}
}
}