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
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.SleepEntryBean;
import edu.ncsu.csc.itrust.dao.DAOFactory;
import edu.ncsu.csc.itrust.dao.mysql.SleepEntryDAO;
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.SleepEntryValidator;
/**
* AddSleepEntryAction.java
* Version 1
* 4/6/2015
* Copyright notice: none
* Responsible for adding a new sleep entry to a patient's Sleep Diary.
*/
public class AddSleepEntryAction implements AddEntryAction {
private SleepEntryDAO sleepDAO;
private SleepEntryValidator sleepValidator;
private long loggedInMID;
/**
* Uses the factory to instantiate the SleepEntryDAO
*
* @param factory
* which DAOFactory to use
* @param loggedInMID
* who is currently logged in
*/
public AddSleepEntryAction(DAOFactory factory, long loggedInMID) {
sleepDAO = new SleepEntryDAO(factory);
sleepValidator = new SleepEntryValidator();
this.loggedInMID = loggedInMID;
}
/**
* Adds a new sleep entry to the sleep diary of the specified patient.
* Currently it checks to ensure that the patient whose sleep dairy we are
* adding to is the logged in patient, and that the patient exists.
* Since only patients can add to only their own sleep diaries,
* we do not need the MID of the patient whose sleep diary we are adding to,
* we just use the MID of the logged in user.
*
* @param entry
* the SleepEntryBean to add to the sleep Diary
* @return Either a string saying the sleep entry was added successfully, or
* a string saying there was an error
* @throws FormValidationException
*/
public String addEntry(EntryBean entry)
throws FormValidationException {
try {
SleepEntryBean sleepEntry = (SleepEntryBean) entry;
sleepValidator.validate(sleepEntry);
sleepEntry.setPatientID(loggedInMID);
sleepDAO.addSleepEntry(sleepEntry);
return "Success: Entry for "
+ sleepEntry.getStrDate().toString()
+ " was added successfully!";
} catch (DBException e) {
return e.getMessage();
} catch (ITrustException d) {
return d.getMessage();
}
}
}