From 734f5abb9ceefb6ee5fd4037ff7ae2fbb42d13c4 Mon Sep 17 00:00:00 2001
From: mjw11 <mjw11@illinois.edu>
Date: Mon, 16 Nov 2020 22:14:41 -0600
Subject: [PATCH] Add getter for if a patient is registered

---
 .../ncsu/csc/itrust/dao/mysql/PatientDAO.java | 38 +++++++++++++++++++
 1 file changed, 38 insertions(+)

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 416a2aa..597a64d 100644
--- a/iTrust/src/edu/ncsu/csc/itrust/dao/mysql/PatientDAO.java
+++ b/iTrust/src/edu/ncsu/csc/itrust/dao/mysql/PatientDAO.java
@@ -60,6 +60,44 @@ 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 registered FROM patients WHERE MID=?");
+			ps.setLong(1, mid);
+			ResultSet rs;
+			rs = ps.executeQuery();
+			if (rs.next()) {
+				Integer b = rs.getObject("registered") != null ? rs.getInt("registered") : null;
+				if (b == 1) {
+					return true;
+				} else if (b == 0) {
+					return false;
+				} else {
+					throw new ITrustException("Number is not a boolean");
+				}
+			} else {
+				rs.close();
+				ps.close();
+				throw new ITrustException("User does not exist");
+			}
+		} catch (SQLException e) {
+			throw new DBException(e);
+		} finally {
+			DBUtil.closeConnection(conn, ps);
+		}
+	}
+
 	/**
 	 * Returns the name for the given MID
 	 * 
-- 
GitLab