From 4dfe33acad8d542d43b844f47262bfdafdfa0691 Mon Sep 17 00:00:00 2001
From: xuechen5 <xuechen5@illinois.edu>
Date: Wed, 18 Nov 2020 06:34:44 -0600
Subject: [PATCH] Add function .getAllPreRegisteredPatients() and Modify
 .getAllPatients()

---
 .../ncsu/csc/itrust/dao/mysql/PatientDAO.java | 127 +++---------------
 1 file changed, 19 insertions(+), 108 deletions(-)

diff --git a/iTrust/src/edu/ncsu/csc/itrust/dao/mysql/PatientDAO.java b/iTrust/src/edu/ncsu/csc/itrust/dao/mysql/PatientDAO.java
index 74a5ae0..8fa4935 100644
--- a/iTrust/src/edu/ncsu/csc/itrust/dao/mysql/PatientDAO.java
+++ b/iTrust/src/edu/ncsu/csc/itrust/dao/mysql/PatientDAO.java
@@ -60,97 +60,6 @@ public class PatientDAO {
 		this.procedureLoader = new ProcedureBeanLoader(true);
 	}
 
-	/**
-	 * Returns if the patient is registered
-	 *
-	 * @param mid The MID of the patient in question.
-	 * @return A boolean representing if the patient is registered
-	 * @throws ITrustException
-	 * @throws DBException
-	 */
-	public boolean getRegistered(long mid) throws ITrustException, DBException {
-		Connection conn = null;
-		PreparedStatement ps = null;
-		try {
-			conn = factory.getConnection();
-			ps = conn.prepareStatement("SELECT DateOfDeactivation FROM patients WHERE MID=?");
-			ps.setLong(1, mid);
-			ResultSet rs;
-			rs = ps.executeQuery();
-			if (rs.next()) {
-				if (rs.getDate("DateOfDeactivation") == null) {
-					return true;
-				} else {
-					return false;
-				}
-			} else {
-				rs.close();
-				ps.close();
-				throw new ITrustException("User does not exist");
-			}
-		} catch (SQLException e) {
-			throw new DBException(e);
-		} finally {
-			DBUtil.closeConnection(conn, ps);
-		}
-	}
-
-	/**
-	 * Sets that a patient is registered
-	 *
-	 * @param mid The MID of the patient in question.
-	 * @return A boolean representing if the transaction completed successfully
-	 * @throws ITrustException
-	 * @throws DBException
-	 */
-	public boolean activatePreregisteredPatient(long mid) {
-		Connection conn = null;
-        PreparedStatement ps = null;
-        try {
-            conn = factory.getConnection();
-            ps = conn.prepareStatement("UPDATE patients SET DateOfDeactivation = NULL WHERE mid = ?");
-            ps.setLong(1, mid);
-            ps.executeUpdate();
-            ps.close();
-            return true;
-
-        } catch (SQLException e) {
-
-            throw new DBException(e);
-        } finally {
-            DBUtil.closeConnection(conn, ps);
-        }
-	}
-
-	/**
-	 * Sets that a patient is not registered
-	 *
-	 * @param mid The MID of the patient in question.
-	 * @return A boolean representing if the transaction completed successfully
-	 * @throws ITrustException
-	 * @throws DBException
-	 */
-	public boolean deactivatePreregisteredPatient(long mid) {
-		Connection conn = null;
-        PreparedStatement ps = null;
-        try {
-            conn = factory.getConnection();
-            ps = conn.prepareStatement("UPDATE patients SET DateofDeactivation = ? WHERE mid = ?");
-            Date date = new Date(Calendar.getInstance().getTime().getTime());
-            ps.setDate(1, date);
-            ps.setLong(2, mid);
-            ps.executeUpdate();
-            ps.close();
-            return true;
-
-        } catch (SQLException e) {
-
-            throw new DBException(e);
-        } finally {
-            DBUtil.closeConnection(conn, ps);
-        }
-	}
-
 	/**
 	 * Returns the name for the given MID
 	 * 
@@ -202,7 +111,7 @@ public class PatientDAO {
 			conn = factory.getConnection();
 			ps = conn.prepareStatement("SELECT role FROM users WHERE MID=? AND Role=?");
 			ps.setLong(1, mid);
-			ps.setString(2, role);
+			ps.setString(2, "patient");     // role);               /////////////////////////     ??????????????  
 			ResultSet rs;
 			rs = ps.executeQuery();
 			if (rs.next()) {
@@ -234,13 +143,14 @@ public class PatientDAO {
 		PreparedStatement ps = null;
 		try {
 			conn = factory.getConnection();
-			ps = conn.prepareStatement("INSERT INTO patients(DateOfDeactivation) VALUES(?)");
-			Date date = new Date(Calendar.getInstance().getTime().getTime());
-            ps.setDate(1, date);
+			ps = conn.prepareStatement("INSERT INTO patients(MID) VALUES(NULL)");
 			ps.executeUpdate();
+			
 			long a = DBUtil.getLastInsert(conn);
 			ps.close();
+			
 			return a;
+			
 		} catch (SQLException e) {
 			
 			throw new DBException(e);
@@ -315,8 +225,9 @@ public class PatientDAO {
 		} finally {
 			DBUtil.closeConnection(conn, ps);
 		}
-    }
-    
+	}
+	
+	/*added for UC91 */
     public boolean isEmailInUse(String email) throws DBException {
         Connection conn = null;
 		PreparedStatement ps = null;
@@ -1012,19 +923,19 @@ public class PatientDAO {
 			DBUtil.closeConnection(conn, ps);
 		}
 	}
-
+	
 	/**
-	 * Lists every preregistered patient in the database.
+	 * Lists every patient in the database.
 	 * 
-	 * @return A java.util.List of PatientBeans representing the preregistered patients.
+	 * @return A java.util.List of PatientBeans representing the patients.
 	 * @throws DBException
 	 */
-	public List<PatientBean> getAllPreregisteredPatients() throws DBException {
+	public List<PatientBean> getAllPatients() throws DBException {
 		Connection conn = null;
 		PreparedStatement ps = null;
 		try {
 			conn = factory.getConnection();
-			ps = conn.prepareStatement("SELECT * FROM patients WHERE DateOfDeactivation IS NOT NULL");
+			ps = conn.prepareStatement("SELECT * FROM patients, users where patients.MID = users.MID and users.Role='patient' ");
 			ResultSet rs = ps.executeQuery();
 			List<PatientBean> loadlist = patientLoader.loadList(rs);
 			rs.close();
@@ -1038,18 +949,19 @@ public class PatientDAO {
 		}
 	}
 	
+	
 	/**
-	 * Lists every patient in the database.
+	 * Lists every Pre-Registered patient in the database.
 	 * 
-	 * @return A java.util.List of PatientBeans representing the patients.
+	 * @return A java.util.List of PatientBeans representing the Pre-Registered patients.
 	 * @throws DBException
 	 */
-	public List<PatientBean> getAllPatients() throws DBException {
+	public List<PatientBean> getAllPreRegisteredPatients() throws DBException {
 		Connection conn = null;
 		PreparedStatement ps = null;
 		try {
 			conn = factory.getConnection();
-			ps = conn.prepareStatement("SELECT * FROM patients ");
+			ps = conn.prepareStatement("SELECT * FROM patients, users where patients.MID = users.MID and users.Role='PreRegisteredPatient' ");
 			ResultSet rs = ps.executeQuery();
 			List<PatientBean> loadlist = patientLoader.loadList(rs);
 			rs.close();
@@ -1063,6 +975,7 @@ public class PatientDAO {
 		}
 	}
 	
+	
 	/**
 	 * Return a list of patients with a special-diagnosis-history who
 	 * have the logged in HCP as a DHCP and whose medications are going to
@@ -1342,6 +1255,4 @@ public class PatientDAO {
 			DBUtil.closeConnection(conn, ps);
 		}
 	}
-
-	
 }
-- 
GitLab