From ee08c5faf419feacdb046804212f802822348c9b Mon Sep 17 00:00:00 2001
From: ahmada4 <ahmada4@illinois.edu>
Date: Thu, 29 Oct 2020 19:44:42 -0500
Subject: [PATCH] added CauseOfDeathTrendsReportDAO.java file

---
 .../mysql/CauseOfDeathTrendsReportDAO.java    | 159 ++++++++++++++++++
 1 file changed, 159 insertions(+)
 create mode 100644 iTrust/src/edu/ncsu/csc/itrust/dao/mysql/CauseOfDeathTrendsReportDAO.java

diff --git a/iTrust/src/edu/ncsu/csc/itrust/dao/mysql/CauseOfDeathTrendsReportDAO.java b/iTrust/src/edu/ncsu/csc/itrust/dao/mysql/CauseOfDeathTrendsReportDAO.java
new file mode 100644
index 0000000..ea90aa1
--- /dev/null
+++ b/iTrust/src/edu/ncsu/csc/itrust/dao/mysql/CauseOfDeathTrendsReportDAO.java
@@ -0,0 +1,159 @@
+package edu.ncsu.csc.itrust.dao.mysql;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Date;
+import java.util.Date;
+import java.util.List;
+import java.util.ArrayList;
+import edu.ncsu.csc.itrust.DBUtil;
+import edu.ncsu.csc.itrust.dao.DAOFactory;
+import edu.ncsu.csc.itrust.exception.DBException;
+
+public class CauseOfDeathTrendsReportDAO {
+	
+	private DAOFactory factory;
+
+	/**
+	 * The typical constructor.
+	 * @param factory The {@link DAOFactory} associated with this DAO, which is used for obtaining SQL connections, etc.
+	 */
+	public CauseOfDeathTrendsReportDAO(DAOFactory factory) {
+		this.factory = factory;
+    }
+
+	/**
+	* Returns a list of the top two causes of deaths for a particular HCP
+	* 
+	* @param hcpMID The MID of the HCP to look up.
+	* @param gender Gender of the patients - All, Female, Male
+	* @param startDate Start date of search.
+	* @param endDate End date of search.
+	* @return A java.util.List of TopTwoDeathsForHCP.
+	* @throws DBException
+	*/
+    public List<String> getTopTwoDeathsForHCP(long hcpMID, String gender, Date startDate, Date endDate) throws DBException {
+		Connection conn = null;
+		PreparedStatement stmt = null;
+		List<String> results = new ArrayList<String>();
+
+		try {
+			conn = factory.getConnection();
+			if(gender.equalsIgnoreCase("All")){
+				stmt = conn.prepareStatement("SELECT DISTINCT CauseOfDeath, COUNT(CauseOfDeath) FROM patients" 
+				+ "WHERE HCPID = ? AND DateOfDeath IS NOT NULL AND YEAR(?) >= YEAR(DateOfDeath) AND YEAR(?) <= YEAR(DateOfDeath)"
+				+ "GROUP BY CauseOfDeath ORDER BY COUNT(CauseOfDeath) DESC");
+				stmt.setDate(1, startDate);
+				stmt.setDate(2, endDate);
+			}
+			else{
+				stmt = conn.prepareStatement("SELECT DISTINCT CauseOfDeath, COUNT(CauseOfDeath) FROM patients" 
+				+ "WHERE HCPID = ? AND DateOfDeath IS NOT NULL AND Gender = ? AND YEAR(?) >= YEAR(DateOfDeath) AND YEAR(?) <= YEAR(DateOfDeath)" 
+				+ "GROUP BY CauseOfDeath ORDER BY COUNT(CauseOfDeath) DESC");
+				stmt.setString(1, gender);
+				stmt.setDate(2, startDate);
+				stmt.setDate(3, endDate);
+			}
+			ResultSet rs = stmt.executeQuery();
+			int count = 0;
+			while(rs.next() && count < 2) {
+				String result = rs.getString("CauseOfDeath");
+				if(!result.isEmpty())
+				{
+					String name = this.getCodeName(result);
+					results.add("Name: " + name + ", Code: " + result + ", Number of Deaths: " + rs.getString("COUNT(CauseOfDeath)"));
+					count++;
+				}
+			}
+			rs.close();
+		} catch (SQLException e) {
+			throw new DBException(e);
+		} finally {
+			DBUtil.closeConnection(conn, stmt);
+		}
+		return results;
+	}
+	
+	/**
+	* Returns a list of the top two causes of deaths for a all HCPs
+	* 
+	* @param gender Gender of the patients - All, Female, Male
+	* @param startDate Start date of search.
+	* @param endDate End date of search.
+	* @return A java.util.List of TopTwoDeathsForAll.
+	* @throws DBException
+	*/
+    public List<String> getTopTwoDeathsForAll(String gender, Date startDate, Date endDate) throws DBException {
+		Connection conn = null;
+		PreparedStatement stmt = null;
+		List<String> results = new ArrayList<String>();
+
+		try {
+			conn = factory.getConnection();
+			if(gender.equalsIgnoreCase("All")){
+				stmt = conn.prepareStatement("SELECT DISTINCT CauseOfDeath, COUNT(CauseOfDeath) FROM patients" 
+				+ "WHERE YEAR(?) >= YEAR(DateOfDeath) AND YEAR(?) <= YEAR(DateOfDeath)" 
+				+ "GROUP BY CauseOfDeath ORDER BY COUNT(CauseOfDeath) DESC");
+				stmt.setDate(1, startDate);
+				stmt.setDate(2, endDate);
+			}
+			else{
+				stmt = conn.prepareStatement("SELECT DISTINCT CauseOfDeath, COUNT(CauseOfDeath) FROM patients" 
+				+ "WHERE Gender = ? AND YEAR(?) >= YEAR(DateOfDeath) AND YEAR(?) <= YEAR(DateOfDeath)" 
+				+ "GROUP BY CauseOfDeath ORDER BY COUNT(CauseOfDeath) DESC");
+				stmt.setString(1, gender);
+				stmt.setDate(2, startDate);
+				stmt.setDate(3, endDate);
+			}
+			ResultSet rs = stmt.executeQuery();
+			int count = 0;
+			while(rs.next() && count < 2) {
+				String result = rs.getString("CauseOfDeath");
+				if(!result.isEmpty())
+				{
+					String name = this.getCodeName(result);
+					results.add("Name: " + name + ", Code: " + result + ", Number of Deaths: " + rs.getString("COUNT(CauseOfDeath)"));
+					count++;
+				}
+			}
+			rs.close();
+		} catch (SQLException e) {
+			throw new DBException(e);
+		} finally {
+			DBUtil.closeConnection(conn, stmt);
+		}
+		return results;
+    }
+
+	/**
+	* Returns the code name for a particular ICD-9CM code.
+	* 
+	* @param code ICD-9CM code.
+	* @return the name of the ICD-9CM code.
+	* @throws DBException
+	*/
+    public String getCodeName(String code) throws DBException {
+		Connection conn = null;
+		PreparedStatement stmt = null;
+
+		try {
+			conn = factory.getConnection();
+			stmt = conn.prepareStatement("SELECT Description FROM icdcodes WHERE Code = ?");
+			stmt.setString(1, code);
+			ResultSet rs = stmt.executeQuery();
+			if(rs.next()) {
+				String result = rs.getString("Description");
+				rs.close();
+				return result;
+			}
+			rs.close();
+			return null;
+		} catch (SQLException e) {
+			throw new DBException(e);
+		} finally {
+			DBUtil.closeConnection(conn, stmt);
+		}
+	}
+}
-- 
GitLab