Skip to content
Snippets Groups Projects
Commit 23c01b14 authored by ahmada4's avatar ahmada4
Browse files

updated getTopTwoDeathsForHCP method to resolve sql query

parent ee08c5fa
No related branches found
No related tags found
2 merge requests!7Uc20,!2UC20 Data Access Object Completed
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 patients for a particular HCP
*
* @param hcpMID The MID of the HCP to look up.
* @return A java.util.List of PatientsForHCP.
* @throws DBException
*/
public List<Long> getPatientsForHCP(long hcpMID) throws DBException {
Connection conn = null;
PreparedStatement stmt = null;
List<Long> patients = new ArrayList<Long>();
try {
conn = factory.getConnection();
stmt = conn.prepareStatement("SELECT DISTINCT PatientID FROM officevisits WHERE HCPID = ?");
stmt.setLong(1, hcpMID);
ResultSet rs = stmt.executeQuery();
while(rs.next()) {
Long patient = rs.getLong("PatientID");
if(!patient.isEmpty())
{
patients.add(patient);
}
}
rs.close();
} catch (SQLException e) {
throw new DBException(e);
} finally {
DBUtil.closeConnection(conn, stmt);
}
return patients;
}
/**
* 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<Long> patients = this.getPatientsForHCP(hcpMID);
List<String> causes = new ArrayList<String>();
try {
conn = factory.getConnection();
if(gender.equalsIgnoreCase("All")){
String query = "SELECT DISTINCT CauseOfDeath, COUNT(CauseOfDeath) FROM patients WHERE MID IN (";
for(int i = 0; i < patients.size(); i++)
{
query += patients.get(i) + ",";
}
query += ") AND DateOfDeath IS NOT NULL AND YEAR(?) >= YEAR(DateOfDeath) AND YEAR(?) <= YEAR(DateOfDeath)"
+ "GROUP BY CauseOfDeath ORDER BY COUNT(CauseOfDeath) DESC";
stmt = conn.prepareStatement(query);
stmt.setDate(1, startDate);
stmt.setDate(2, endDate);
}
else{
String query = "SELECT DISTINCT CauseOfDeath, COUNT(CauseOfDeath) FROM patients WHERE MID IN (";
for(int i = 0; i < patients.size(); i++)
{
query += patients.get(i) + ",";
}
query += ") AND DateOfDeath IS NOT NULL AND Gender = ? AND YEAR(?) >= YEAR(DateOfDeath) AND YEAR(?) <= YEAR(DateOfDeath)"
+ "GROUP BY CauseOfDeath ORDER BY COUNT(CauseOfDeath) DESC";
stmt = conn.prepareStatement(query);
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 cause = rs.getString("CauseOfDeath");
int deaths = rs.getString("COUNT(CauseOfDeath)");
if(!cause.isEmpty())
{
String name = this.getCodeName(cause);
causes.add("Name: " + name + ", Code: " + cause + ", Deaths: " + deaths);
count++;
}
}
rs.close();
} catch (SQLException e) {
throw new DBException(e);
} finally {
DBUtil.closeConnection(conn, stmt);
}
return causes;
}
/**
* 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> causes = 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 cause = rs.getString("CauseOfDeath");
int deaths = rs.getString("COUNT(CauseOfDeath)");
if(!cause.isEmpty())
{
String name = this.getCodeName(cause);
causes.add("Name: " + name + ", Code: " + cause + ", Deaths: " + deaths);
count++;
}
}
rs.close();
} catch (SQLException e) {
throw new DBException(e);
} finally {
DBUtil.closeConnection(conn, stmt);
}
return causes;
}
/**
* 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;
String result = null;
try {
conn = factory.getConnection();
stmt = conn.prepareStatement("SELECT Description FROM icdcodes WHERE Code = ?");
stmt.setString(1, code);
ResultSet rs = stmt.executeQuery();
result = rs.getString("Description");
rs.close();
} catch (SQLException e) {
throw new DBException(e);
} finally {
DBUtil.closeConnection(conn, stmt);
}
return result;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment