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

added CauseOfDeathTrendsReportDAO.java file

parent aae21b9d
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 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);
}
}
}
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