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
package edu.ncsu.csc.itrust.action;
import edu.ncsu.csc.itrust.action.base.AddEntryAction;
import edu.ncsu.csc.itrust.beans.EntryBean;
import edu.ncsu.csc.itrust.beans.FoodEntryBean;
import edu.ncsu.csc.itrust.dao.DAOFactory;
import edu.ncsu.csc.itrust.dao.mysql.FoodEntryDAO;
import edu.ncsu.csc.itrust.exception.DBException;
import edu.ncsu.csc.itrust.exception.FormValidationException;
import edu.ncsu.csc.itrust.exception.ITrustException;
import edu.ncsu.csc.itrust.validate.FoodEntryValidator;
/**
* AddFoodEntryAction.java Version 1 2/21/2015 Copyright notice: none
* Responsible for adding a new food entry to a patient's Food Diary.
*/
public class AddFoodEntryAction implements AddEntryAction {
private FoodEntryDAO foodDAO;
private FoodEntryValidator foodValidator;
private long loggedInMID;
/**
* Uses the factory to instantiate the foodentrydao
*
* @param factory
* which DAOFactory to use
* @param loggedInMID
* who is currently logged in
*/
public AddFoodEntryAction(DAOFactory factory, long loggedInMID) {
foodDAO = new FoodEntryDAO(factory);
foodValidator = new FoodEntryValidator();
this.loggedInMID = loggedInMID;
}
/**
* Adds a new food entry to the food diary of the specified patient.
* Currently it checks to ensure that the patient whose food dairy we are
* adding to is the logged in patient, and that the patient exists, but I
* don't know if we need to have those checks. Since only patients can add
* to only their own food diaries, we do not need the MID of the patient
* whose food diary we are adding to, we just use the MID of the logged in
* user.
*
* @param entry
* the FoodEntryBean to add to the Food Diary
* @return Either a string saying the food entry was added successfully, or
* a string saying there was an error
* @throws FormValidationException
*/
public String addEntry(EntryBean entry)
throws FormValidationException {
try {
FoodEntryBean foodEntry = (FoodEntryBean) entry;
foodValidator.validate(foodEntry);
foodEntry.setPatientID(loggedInMID);
foodDAO.addFoodEntry(foodEntry);
return "Success: " + foodEntry.getFood() + " for "
+ foodEntry.getDateEatenStr().toString()
+ " was added successfully!";
} catch (DBException e) {
return e.getMessage();
} catch (ITrustException d) {
return d.getMessage();
}
}
}