Permalink
Browse files

PTM-83: Save incremental patient matching report to database

  • Loading branch information...
Lahiru-J committed Jun 25, 2017
1 parent dd51dae commit 33a7e5cf8b3e3f506de2dcf6b2d27a4dea4e688c
@@ -14,6 +14,7 @@
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
import java.util.HashMap;

import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
@@ -323,12 +324,13 @@
throw new RuntimeException(e);
}

String reportName = REPORT_NAME + configString+dateString;
configString = configString.substring(0, configString.length() - 1);
String reportName = REPORT_NAME + configString;
DedupMatchResultList handler = (DedupMatchResultList)objects.get("handler");

handler.flattenPairIdList();
List<Set<Long>> matchingPairs = handler.getFlattenedPairIds();
persistReportToDB(reportName, matchingPairs, globalIncludeColumns);
persistIncrementalReportToDB(reportName, matchingPairs, globalIncludeColumns);
return objects;
}
//New Method9 End 10
@@ -426,6 +428,82 @@ public static void persistReportToDB(String rptName, List<Set<Long>> matchingPai
reportMetadataService.savePatientMatchingReport(report);
}

/**
* Method to persist the report to the database in the incremental patient matching process
* @param rptName The report name of the new report
* @param matchingPairs A list of the matching pair sets
*/
public static void persistIncrementalReportToDB(String rptName, List<Set<Long>> matchingPairs, Set<String> includeColumns) throws FileNotFoundException {
PatientMatchingReportMetadataService reportMetadataService = Context.getService(PatientMatchingReportMetadataService.class);
Report report = reportMetadataService.getReportByName(rptName);

if(report != null){
report.setCreatedBy(Context.getAuthenticatedUser());
report.setCreatedOn(new Date());
PatientService patientService = Context.getPatientService();
Set<MatchingRecord> matchingRecordSet = report.getMatchingRecordSet();
HashMap<Integer,Integer> patientIdGroupIdMap = new HashMap<Integer, Integer>();

if(!matchingRecordSet.isEmpty()){
for (MatchingRecord record: matchingRecordSet){
patientIdGroupIdMap.put(record.getPatient().getPatientId(),record.getGroupId());
}
}

int availableGroupId = Collections.max(patientIdGroupIdMap.values()) + 1;
for (int j = 0; j < matchingPairs.size(); j++) {
Set<Long> matchSet = matchingPairs.get(j);
int groupId = -1;

// Finding the group id
for (Long patientId: matchSet){
if(patientIdGroupIdMap.containsKey(patientId.intValue())){
groupId = patientIdGroupIdMap.get(patientId.intValue());
break;
}
}
for(Long patientId: matchSet){
MatchingRecord matchingRecord = new MatchingRecord();

matchingRecord.setGroupId(groupId == -1 ? availableGroupId : groupId);
matchingRecord.setState("PENDING"); //TODO move to a constant
matchingRecord.setPatient(patientService.getPatient(patientId.intValue()));
matchingRecord.setReport(report);

Set<MatchingRecordAttribute> matchingRecordAttributeSet = new TreeSet<MatchingRecordAttribute>();
Record record = RecordSerializer.deserialize(String.valueOf(patientId));
for(String includedColumn:includeColumns){
MatchingRecordAttribute matchingRecordAttribute = new MatchingRecordAttribute();
matchingRecordAttribute.setFieldName(includedColumn);
matchingRecordAttribute.setFieldValue(record.getDemographic(includedColumn));
matchingRecordAttribute.setMatchingRecord(matchingRecord);
matchingRecordAttributeSet.add(matchingRecordAttribute);
}
matchingRecord.setMatchingRecordAttributeSet(matchingRecordAttributeSet);
report.addMatchingRecord(matchingRecord);
}
if(groupId == -1) availableGroupId++;
}

report.setMatchingRecordSet(matchingRecordSet);
report.clearReportGenerationSteps();
List<Long> proTimeList = MatchingRunData.getInstance().getProTimeList();
int noOfSteps = Math.min(proTimeList.size(),REPORT_GEN_STAGES.length);
for (int j = 0; j < noOfSteps; j++) {
ReportGenerationStep step = new ReportGenerationStep();
step.setProcessName(REPORT_GEN_STAGES[j]);
step.setTimeTaken(proTimeList.get(j).intValue());
step.setReport(report);
step.setSequenceNo(j);
report.addReportGenerationStep(step);
}
reportMetadataService.savePatientMatchingReport(report);

}else{
persistReportToDB(rptName,matchingPairs,includeColumns);
}
}

public static Set<String> getAllFieldsUsed(Report report){
Set<String> fieldsUsed = new TreeSet<String>();
for(PatientMatchingConfiguration configuration: report.getUsedConfigurationList()){
@@ -73,4 +73,12 @@ public void setUsedConfigurationList(Set<PatientMatchingConfiguration> usedConfi
public void setReportGenerationSteps(Set<ReportGenerationStep> reportGenerationSteps){
this.reportGenerationSteps = reportGenerationSteps;
}

public void addMatchingRecord(MatchingRecord matchingRecord){this.matchingRecordSet.add(matchingRecord);}

public void clearReportGenerationSteps(){this.reportGenerationSteps.clear();}

public void addReportGenerationStep(ReportGenerationStep reportGenerationStep){
this.reportGenerationSteps.add(reportGenerationStep);
}
}

0 comments on commit 33a7e5c

Please sign in to comment.