diff --git a/iTrust/src/edu/ncsu/csc/itrust/action/AddPreRegisteredPatientAction.java b/iTrust/src/edu/ncsu/csc/itrust/action/AddPreRegisteredPatientAction.java index 7a979786fd96cae07b333c7a2235c89b0f213c1b..54dcbd3b88b409046b2f9823292d73f03bd87e52 100644 --- a/iTrust/src/edu/ncsu/csc/itrust/action/AddPreRegisteredPatientAction.java +++ b/iTrust/src/edu/ncsu/csc/itrust/action/AddPreRegisteredPatientAction.java @@ -6,6 +6,10 @@ import edu.ncsu.csc.itrust.beans.PatientBean; import edu.ncsu.csc.itrust.dao.DAOFactory; import edu.ncsu.csc.itrust.dao.mysql.PatientDAO; import edu.ncsu.csc.itrust.dao.mysql.AuthDAO; +import edu.ncsu.csc.itrust.enums.Role; +import edu.ncsu.csc.itrust.exception.FormValidationException; +import edu.ncsu.csc.itrust.exception.ITrustException; +import edu.ncsu.csc.itrust.validate.AddPatientValidator; /** * Used for Add Pre-registered Patient page (PreRegisterPatient.jsp). This just adds an empty patient, creates an entered password for @@ -27,23 +31,33 @@ public class AddPreRegisteredPatientAction { * @param loggedInMID */ public AddPreRegisteredPatientAction(DAOFactory factory, long loggedInMID) { - this.patientDAO = factory.getPatientDAO(); - this.loggedInMID = loggedInMID; + this.patientDAO = factory.getPatientDAO(); + this.loggedInMID = loggedInMID; this.authDAO = factory.getAuthDAO(); } /** - * Creates a new patient, returns the new MID. + * Creates a new patient, returns the new MID. Adds a new user to the table with a + * preRegisteredPatient role * * @param p patient to be created * @return the new MID of the patient + * @throws FormValidationException if the patient is not successfully validated + * @throws ITrustException */ - public long addPatient(PatientBean p) { - - // Add patient to DB - long newMID = 0L; - + + public long addPatient(PatientBean p) throws FormValidationException, ITrustException { + + new AddPatientValidator().validate(p); + + long newMID = patientDAO.addEmptyPatient(); // the new added row id in the database + p.setMID(newMID); + + String pwd = authDAO.addUser(newMID, Role.PREREGISTEREDPATIENT, p.getPassword()); + + p.setPassword(pwd); + patientDAO.editPatient(p, loggedInMID); return newMID; } }