From 689c9feef4978d64c2f5125485e69ce8cd9ab22f Mon Sep 17 00:00:00 2001
From: Aditya Bhansali <adityab3@illinois.edu>
Date: Sun, 15 Nov 2020 07:14:23 -0600
Subject: [PATCH] [backend] added health info logic to preregister action

---
 .../action/AddPreRegisteredPatientAction.java | 44 +++++++++++++++----
 1 file changed, 35 insertions(+), 9 deletions(-)

diff --git a/iTrust/src/edu/ncsu/csc/itrust/action/AddPreRegisteredPatientAction.java b/iTrust/src/edu/ncsu/csc/itrust/action/AddPreRegisteredPatientAction.java
index 639ffd0..46d44b6 100644
--- a/iTrust/src/edu/ncsu/csc/itrust/action/AddPreRegisteredPatientAction.java
+++ b/iTrust/src/edu/ncsu/csc/itrust/action/AddPreRegisteredPatientAction.java
@@ -1,15 +1,22 @@
 package edu.ncsu.csc.itrust.action;
 
-
-import edu.ncsu.csc.itrust.RandomPassword;
 import edu.ncsu.csc.itrust.beans.PatientBean;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+import edu.ncsu.csc.itrust.beans.HealthRecord;
+import edu.ncsu.csc.itrust.beans.forms.HealthRecordForm;
 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.dao.mysql.HealthRecordsDAO;
 import edu.ncsu.csc.itrust.enums.Role;
+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.AddPatientValidator;
+import edu.ncsu.csc.itrust.validate.HealthRecordFormValidator;
 
 /**
  * Used for Add Pre-registered Patient page (PreRegisterPatient.jsp). This just adds an empty patient, creates an entered password for
@@ -22,7 +29,8 @@ import edu.ncsu.csc.itrust.validate.AddPatientValidator;
 public class AddPreRegisteredPatientAction {
 	private PatientDAO patientDAO;
 	private AuthDAO authDAO;
-	private long loggedInMID;
+    private long loggedInMID;
+    private HealthRecordsDAO healthDAO;
 
 	/**
 	 * Just the factory and logged in MID
@@ -33,7 +41,8 @@ public class AddPreRegisteredPatientAction {
 	public AddPreRegisteredPatientAction(DAOFactory factory, long loggedInMID) {
 		this.patientDAO = factory.getPatientDAO();
 		this.loggedInMID = loggedInMID;
-		this.authDAO = factory.getAuthDAO();
+        this.authDAO = factory.getAuthDAO();
+        this.healthDAO = factory.getHealthRecordsDAO();
 	}
 	
 	/**
@@ -47,9 +56,9 @@ public class AddPreRegisteredPatientAction {
 	 */
 
 	
-	public long addPatient(PatientBean p) throws FormValidationException, ITrustException {
-		
+	public long addPatient(PatientBean p, HealthRecordForm h) throws FormValidationException, ITrustException {
         new AddPatientValidator().validate(p);
+        new HealthRecordFormValidator().validatePreregistration(h);
         
         // Make sure the email is unique
         if (patientDAO.isEmailInUse(p.getEmail())) {
@@ -62,7 +71,24 @@ public class AddPreRegisteredPatientAction {
 		String pwd = authDAO.addUser(newMID, Role.PREREGISTEREDPATIENT, p.getPassword()); 
 		
 		p.setPassword(pwd);
-		patientDAO.editPatient(p, loggedInMID);
-		return newMID;
-	}
+        patientDAO.editPatient(p, loggedInMID);
+
+        // Convert the HealthRecordForm into a HeathRecord
+        HealthRecord r = formToRecord(h, newMID);
+        healthDAO.add(r);
+
+        return newMID;
+    }
+    
+    // TODO: this is taken directly from EditHealthAction.java (DRY)
+    private HealthRecord formToRecord(HealthRecordForm form, long pid) {
+        HealthRecord record = new HealthRecord();
+        record.setPatientID(pid);
+        if (!"".equals(form.getHeight()))
+            record.setHeight(Double.parseDouble(form.getHeight()));
+        if (!"".equals(form.getWeight()))
+		    record.setWeight(Double.parseDouble(form.getWeight()));
+		record.setSmoker(Integer.parseInt(form.getIsSmoker()));
+		return record;
+    }
 }
-- 
GitLab