Skip to content
Snippets Groups Projects
Commit 478c1cb6 authored by sbobo3's avatar sbobo3
Browse files

Merge remote-tracking branch 'origin/UC20' into UC20

parents 18dfd3cd 02591b31
No related branches found
No related tags found
2 merge requests!7Uc20,!2UC20 Data Access Object Completed
Showing
with 261 additions and 37 deletions
INSERT INTO patients (MID, Gender, DateOfDeath, CauseOfDeath)
VALUES (81,'Male','2019-07-01',84.50);
INSERT INTO officevisits(id,HCPID,PatientID)
VALUES (201,9000000000,81);
\ No newline at end of file
INSERT INTO patients (MID, Gender, DateOfDeath, CauseOfDeath)
VALUES (90,'Male','2019-01-01',487.00);
INSERT INTO officevisits(id,HCPID,PatientID)
VALUES (210,9000000001,90);
\ No newline at end of file
INSERT INTO patients (MID, Gender, DateOfDeath, CauseOfDeath)
VALUES (82,'Male','2018-07-01',11.40);
INSERT INTO officevisits(id,HCPID,PatientID)
VALUES (202,9000000000,82);
\ No newline at end of file
INSERT INTO patients (MID, Gender, DateOfDeath, CauseOfDeath)
VALUES (83,'Female','2018-07-01',11.40);
INSERT INTO officevisits(id,HCPID,PatientID)
VALUES (203,9000000000,83);
\ No newline at end of file
INSERT INTO patients (MID, Gender, DateOfDeath, CauseOfDeath)
VALUES (84,'Male','2018-12-01',11.40);
INSERT INTO officevisits(id,HCPID,PatientID)
VALUES (204,9000000000,84);
\ No newline at end of file
INSERT INTO patients (MID, Gender, DateOfDeath, CauseOfDeath)
VALUES (85,'Male','2020-01-01',84.50);
INSERT INTO officevisits(id,HCPID,PatientID)
VALUES (205,9000000000,85);
\ No newline at end of file
INSERT INTO patients (MID, Gender, DateOfDeath, CauseOfDeath)
VALUES (86,'Male','2018-12-01',487.00);
INSERT INTO officevisits(id,HCPID,PatientID)
VALUES (206,9000000000,86);
\ No newline at end of file
INSERT INTO patients (MID, Gender, DateOfDeath, CauseOfDeath)
VALUES (87,'Female','2019-12-01',487.00);
INSERT INTO officevisits(id,HCPID,PatientID)
VALUES (207,9000000001,87);
\ No newline at end of file
INSERT INTO patients (MID, Gender, DateOfDeath, CauseOfDeath)
VALUES (88,'Female','2020-07-01',487.00);
INSERT INTO officevisits(id,HCPID,PatientID)
VALUES (208,9000000001,88);
\ No newline at end of file
INSERT INTO patients (MID, Gender, DateOfDeath, CauseOfDeath)
VALUES (89,'Male','2020-01-01',487.00);
INSERT INTO officevisits(id,HCPID,PatientID)
VALUES (209,9000000001,89);
\ No newline at end of file
......@@ -5,7 +5,6 @@ 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;
......@@ -43,10 +42,7 @@ public class CauseOfDeathTrendsReportDAO {
ResultSet rs = stmt.executeQuery();
while(rs.next()) {
Long patient = rs.getLong("PatientID");
if(!patient.isEmpty())
{
patients.add(patient);
}
patients.add(patient);
}
rs.close();
} catch (SQLException e) {
......@@ -67,22 +63,29 @@ public class CauseOfDeathTrendsReportDAO {
* @return A java.util.List of TopTwoDeathsForHCP.
* @throws DBException
*/
public List<String> getTopTwoDeathsForHCP(long hcpMID, String gender, Date startDate, Date endDate) 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>();
String query = null;
try {
conn = factory.getConnection();
if(gender.equalsIgnoreCase("All")){
String query = "SELECT DISTINCT CauseOfDeath, COUNT(CauseOfDeath) FROM patients WHERE MID IN (";
query = "SELECT DISTINCT CauseOfDeath, COUNT(CauseOfDeath) FROM patients WHERE MID IN (";
for(int i = 0; i < patients.size(); i++)
{
query += patients.get(i) + ",";
if (i+1 == patients.size()){
query += patients.get(i);
}
else{
query += patients.get(i) + ",";
}
}
query += ") AND DateOfDeath IS NOT NULL AND YEAR(?) >= YEAR(DateOfDeath) AND YEAR(?) <= YEAR(DateOfDeath)"
query += ") AND DateOfDeath IS NOT NULL AND YEAR(DateOfDeath) >= YEAR(?) AND YEAR(DateOfDeath) <= YEAR(?)"
+ "GROUP BY CauseOfDeath ORDER BY COUNT(CauseOfDeath) DESC";
stmt = conn.prepareStatement(query);
stmt.setDate(1, startDate);
......@@ -90,12 +93,17 @@ public class CauseOfDeathTrendsReportDAO {
}
else{
String query = "SELECT DISTINCT CauseOfDeath, COUNT(CauseOfDeath) FROM patients WHERE MID IN (";
query = "SELECT DISTINCT CauseOfDeath, COUNT(CauseOfDeath) FROM patients WHERE MID IN (";
for(int i = 0; i < patients.size(); i++)
{
query += patients.get(i) + ",";
if (i+1 == patients.size()){
query += patients.get(i);
}
else{
query += patients.get(i) + ",";
}
}
query += ") AND DateOfDeath IS NOT NULL AND Gender = ? AND YEAR(?) >= YEAR(DateOfDeath) AND YEAR(?) <= YEAR(DateOfDeath)"
query += ") AND DateOfDeath IS NOT NULL AND Gender = ? AND YEAR(DateOfDeath) >= YEAR(?) AND YEAR(DateOfDeath) <= YEAR(?)"
+ "GROUP BY CauseOfDeath ORDER BY COUNT(CauseOfDeath) DESC";
stmt = conn.prepareStatement(query);
stmt.setString(1, gender);
......@@ -106,21 +114,19 @@ public class CauseOfDeathTrendsReportDAO {
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++;
}
String deaths = rs.getString("COUNT(CauseOfDeath)");
String name = this.getCodeName(cause);
causes.add("Name: " + name + ", Code: " + cause + ", Deaths: " + deaths);
count++;
}
rs.close();
stmt.close();
} catch (SQLException e) {
throw new DBException(e);
} finally {
DBUtil.closeConnection(conn, stmt);
return causes;
}
return causes;
}
/**
......@@ -136,20 +142,25 @@ public class CauseOfDeathTrendsReportDAO {
Connection conn = null;
PreparedStatement stmt = null;
List<String> causes = new ArrayList<String>();
String query = null;
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");
query = "SELECT DISTINCT CauseOfDeath, COUNT(CauseOfDeath) FROM patients"
+ " WHERE DateOfDeath IS NOT NULL AND YEAR(DateOfDeath) >= YEAR(?) AND YEAR(DateOfDeath) <= YEAR(?)"
+ "GROUP BY CauseOfDeath ORDER BY COUNT(CauseOfDeath) DESC";
stmt = conn.prepareStatement(query);
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");
query = "SELECT DISTINCT CauseOfDeath, COUNT(CauseOfDeath) FROM patients"
+ " WHERE DateOfDeath IS NOT NULL AND Gender = ? AND YEAR(DateOfDeath) >= YEAR(?) AND YEAR(DateOfDeath) <= YEAR(?)"
+ "GROUP BY CauseOfDeath ORDER BY COUNT(CauseOfDeath) DESC";
stmt = conn.prepareStatement(query);
stmt.setString(1, gender);
stmt.setDate(2, startDate);
stmt.setDate(3, endDate);
......@@ -158,22 +169,20 @@ public class CauseOfDeathTrendsReportDAO {
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++;
}
String deaths = rs.getString("COUNT(CauseOfDeath)");
String name = this.getCodeName(cause);
causes.add("Name: " + name + ", Code: " + cause + ", Deaths: " + deaths);
count++;
}
rs.close();
stmt.close();
} catch (SQLException e) {
throw new DBException(e);
} finally {
DBUtil.closeConnection(conn, stmt);
return causes;
}
return causes;
}
}
/**
* Returns the code name for a particular ICD-9CM code.
......@@ -192,8 +201,16 @@ public class CauseOfDeathTrendsReportDAO {
stmt = conn.prepareStatement("SELECT Description FROM icdcodes WHERE Code = ?");
stmt.setString(1, code);
ResultSet rs = stmt.executeQuery();
result = rs.getString("Description");
rs.close();
if(rs.next()){
result = rs.getString("Description");
rs.close();
stmt.close();
}
else{
rs.close();
stmt.close();
return null;
}
} catch (SQLException e) {
throw new DBException(e);
} finally {
......
package edu.ncsu.csc.itrust.unit.dao.CauseOfDeathTrendsReportDAO;
import java.util.List;
import java.util.ArrayList;
import java.sql.Date;
import junit.framework.TestCase;
import edu.ncsu.csc.itrust.dao.mysql.CauseOfDeathTrendsReportDAO;
import edu.ncsu.csc.itrust.unit.datagenerators.TestDataGenerator;
import edu.ncsu.csc.itrust.unit.testutils.TestDAOFactory;
import edu.ncsu.csc.itrust.unit.testutils.EvilDAOFactory;
import edu.ncsu.csc.itrust.exception.DBException;
public class CauseOfDeathTrendsReportTest extends TestCase {
private TestDataGenerator gen = new TestDataGenerator();
private CauseOfDeathTrendsReportDAO cod;
private CauseOfDeathTrendsReportDAO evilDAO = new CauseOfDeathTrendsReportDAO(EvilDAOFactory.getEvilInstance());
@Override
protected void setUp() throws Exception {
gen = new TestDataGenerator();
gen.clearAllTables();
gen.deadPatient1();
gen.deadPatient2();
gen.deadPatient3();
gen.deadPatient4();
gen.deadPatient5();
gen.deadPatient6();
gen.deadPatient7();
gen.deadPatient8();
gen.deadPatient9();
gen.deadPatient10();
gen.icd9cmCodes();
cod = new CauseOfDeathTrendsReportDAO(TestDAOFactory.getTestInstance());
}
public void testPatientsForHCPValid() throws Exception {
List<Long> deaths = cod.getPatientsForHCP(Long.parseLong("9000000000"));
assertEquals(6, deaths.size());
assertEquals(Long.valueOf(81), deaths.get(0));
assertEquals(Long.valueOf(82), deaths.get(1));
assertEquals(Long.valueOf(83), deaths.get(2));
assertEquals(Long.valueOf(84), deaths.get(3));
assertEquals(Long.valueOf(85), deaths.get(4));
assertEquals(Long.valueOf(86), deaths.get(5));
}
public void testPatientsForHCPException() throws Exception {
try {
evilDAO.getPatientsForHCP(Long.parseLong("0000000000"));
fail("DBException should have been thrown");
} catch (DBException e) {
assertEquals(EvilDAOFactory.MESSAGE, e.getSQLException().getMessage());
}
}
public void testTopTwoDeathsForHCPValid() throws Exception {
List<String> deaths = cod.getTopTwoDeathsForHCP(Long.parseLong("9000000000"), "All", Date.valueOf("2018-01-01"), Date.valueOf("2020-12-31"));
assertEquals(2, deaths.size());
assertEquals("Name: Tuberculosis of the lung, Code: 11.40, Deaths: 3", deaths.get(0));
assertEquals("Name: Malaria, Code: 84.50, Deaths: 2", deaths.get(1));
}
public void testTopTwoDeathsForHCPMaleValid() throws Exception {
List<String> deaths = cod.getTopTwoDeathsForHCP(Long.parseLong("9000000000"), "Male", Date.valueOf("2018-01-01"), Date.valueOf("2020-12-31"));
assertEquals(2, deaths.size());
assertEquals("Name: Malaria, Code: 84.50, Deaths: 2", deaths.get(0));
assertEquals("Name: Tuberculosis of the lung, Code: 11.40, Deaths: 2", deaths.get(1));
}
public void testTopTwoDeathsForHCPException() throws Exception {
try {
evilDAO.getTopTwoDeathsForHCP(Long.parseLong("0000000000"), "All", Date.valueOf("2018-01-01"), Date.valueOf("2020-12-31"));
fail("DBException should have been thrown");
} catch (DBException e) {
assertEquals(EvilDAOFactory.MESSAGE, e.getSQLException().getMessage());
}
}
public void testTopTwoDeathsForAllValid() throws Exception {
List<String> deaths = cod.getTopTwoDeathsForAll("All", Date.valueOf("2018-01-01"), Date.valueOf("2020-12-31"));
assertEquals(2, deaths.size());
assertEquals("Name: Influenza, Code: 487.00, Deaths: 5", deaths.get(0));
assertEquals("Name: Tuberculosis of the lung, Code: 11.40, Deaths: 3", deaths.get(1));
}
public void testTopTwoDeathsForAllFemaleValid() throws Exception {
List<String> deaths = cod.getTopTwoDeathsForAll("Female", Date.valueOf("2018-01-01"), Date.valueOf("2020-12-31"));
assertEquals(2, deaths.size());
assertEquals("Name: Influenza, Code: 487.00, Deaths: 2", deaths.get(0));
assertEquals("Name: Tuberculosis of the lung, Code: 11.40, Deaths: 1", deaths.get(1));
}
public void testCodeNameExists() throws Exception {
String deaths = cod.getCodeName("84.50");
assertEquals("Malaria", deaths);
}
public void testCodeNameDoesNotExist() throws Exception {
String deaths = cod.getCodeName("8450");
assertEquals(null, deaths);
}
}
\ No newline at end of file
......@@ -121,6 +121,46 @@ public class TestDataGenerator {
+ "/deadRecurringPatients.sql");
}
public void deadPatient1() throws FileNotFoundException, SQLException, IOException {
new DBBuilder(factory).executeSQLFile(DIR + "/deadPatient1.sql");
}
public void deadPatient2() throws FileNotFoundException, SQLException, IOException {
new DBBuilder(factory).executeSQLFile(DIR + "/deadPatient2.sql");
}
public void deadPatient3() throws FileNotFoundException, SQLException, IOException {
new DBBuilder(factory).executeSQLFile(DIR + "/deadPatient3.sql");
}
public void deadPatient4() throws FileNotFoundException, SQLException, IOException {
new DBBuilder(factory).executeSQLFile(DIR + "/deadPatient4.sql");
}
public void deadPatient5() throws FileNotFoundException, SQLException, IOException {
new DBBuilder(factory).executeSQLFile(DIR + "/deadPatient5.sql");
}
public void deadPatient6() throws FileNotFoundException, SQLException, IOException {
new DBBuilder(factory).executeSQLFile(DIR + "/deadPatient6.sql");
}
public void deadPatient7() throws FileNotFoundException, SQLException, IOException {
new DBBuilder(factory).executeSQLFile(DIR + "/deadPatient7.sql");
}
public void deadPatient8() throws FileNotFoundException, SQLException, IOException {
new DBBuilder(factory).executeSQLFile(DIR + "/deadPatient8.sql");
}
public void deadPatient9() throws FileNotFoundException, SQLException, IOException {
new DBBuilder(factory).executeSQLFile(DIR + "/deadPatient9.sql");
}
public void deadPatient10() throws FileNotFoundException, SQLException, IOException {
new DBBuilder(factory).executeSQLFile(DIR + "/deadPatient10.sql");
}
public void diagnosedPatient_OldAndNewVisit() throws SQLException,
FileNotFoundException, IOException {
new DBBuilder(factory).executeSQLFile(DIR
......
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