Permalink
Browse files

PTM-83: Save and update incremental patient matching report to the da…

…tabase
  • Loading branch information...
Lahiru-J committed Jul 23, 2017
1 parent a327c3e commit 7704534457797845ecf6896072a3c441494d8299
@@ -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;
@@ -75,6 +76,12 @@
public static String status = NO_PROCESS;
public static int i;
/**
* Constant name is given for a report in the incremental patient matching process
*/
private static final String REPORT_NAME_PREFIX = "incremental-report-";
private static final String PENDING = "PENDING";
/**
*
* Get the list of steps (statuses) that the analysis has to go through
@@ -122,7 +129,7 @@
String driver = c.getProperty("connection.driver_class");
log.info("URL: " + url);
Connection databaseConnection = null;
Connection databaseConnection;
try {
Class.forName(driver);
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
@@ -149,7 +156,8 @@
public static Map<String, Object> InitScratchTable(Map<String,Object> objects){
Connection databaseConnection=(Connection) objects.get("databaseConnection");
RecMatchConfig recMatchConfig=(RecMatchConfig) objects.get("recMatchConfig");
MatchingConfig matchingConfig = ((List<MatchingConfig>)objects.get("matchingConfigLists")).get(0);
List<MatchingConfig> matchingConfigList = (List<MatchingConfig>)objects.get("matchingConfigLists");
MatchingConfig matchingConfig = matchingConfigList.get(0);
String driver=(String) objects.get("driver");
String url = (String) objects.get("url");
String user = (String) objects.get("user");
@@ -166,7 +174,15 @@
databaseConnection, recMatchConfig.getLinkDataSource1(),
driver, url, user, passwd);
recordStore.clearRecords();
OpenMRSReader reader = new OpenMRSReader(globalIncludeColumns);
// If only a single strategy has been selected
Report report = null;
if(matchingConfigList.size() == 1){
String reportName = REPORT_NAME_PREFIX + matchingConfig.getName();
report = Context.getService(PatientMatchingReportMetadataService.class).getReportByName(reportName);
}
OpenMRSReader reader = new OpenMRSReader(globalIncludeColumns, report != null ? report.getCreatedOn() : null);
reader.init();
while (reader.hasNextRecord()) {
recordStore.storeRecord(reader.nextRecord());
}
@@ -309,12 +325,14 @@
throw new RuntimeException(e);
}
String reportName = "dedup-report-"+configString+dateString;
configString = configString.substring(0, configString.length() - 1);
String reportName = REPORT_NAME_PREFIX + configString;
DedupMatchResultList handler = (DedupMatchResultList)objects.get("handler");
handler.flattenPairIdList();
List<Set<Long>> matchingPairs = handler.getFlattenedPairIds();
persistReportToDB(reportName, matchingPairs, globalIncludeColumns);
persistIncrementalReportToDB(reportName, matchingPairs, globalIncludeColumns,
(List<MatchingConfig>)objects.get("matchingConfigLists"));
return objects;
}
//New Method9 End 10
@@ -355,20 +373,20 @@
* @param rptName The report name of the new report
* @param matchingPairs A list of the matching pair sets
*/
public static void persistReportToDB(String rptName, List<Set<Long>> matchingPairs, Set<String> includeColumns) throws FileNotFoundException {
public static void persistReportToDB(String rptName, List<Set<Long>> matchingPairs, Set<String> includeColumns,
List<MatchingConfig> matchingConfigList) throws FileNotFoundException {
Report report = new Report();
report.setCreatedBy(Context.getAuthenticatedUser());
report.setReportName(rptName);
report.setCreatedOn(new Date());
String selectedStrategies = MatchingRunData.getInstance().getFileStrat();
String[] selectedStrategyNamesArray = selectedStrategies.split(",");
Set<PatientMatchingConfiguration> usedConfigurations = new TreeSet<PatientMatchingConfiguration>();
PatientMatchingReportMetadataService reportMetadataService = Context.getService(PatientMatchingReportMetadataService.class);
for(String strategyName : selectedStrategyNamesArray){
PatientMatchingConfiguration configuration = reportMetadataService.findPatientMatchingConfigurationByName(strategyName);
usedConfigurations.add(configuration);
}
for(MatchingConfig matchingConfig : matchingConfigList){
PatientMatchingConfiguration configuration =
reportMetadataService.findPatientMatchingConfigurationByName(matchingConfig.getName());
usedConfigurations.add(configuration);
}
report.setUsedConfigurationList(usedConfigurations);
PatientService patientService = Context.getPatientService();
@@ -378,7 +396,7 @@ public static void persistReportToDB(String rptName, List<Set<Long>> matchingPai
for(Long patientId: matchSet){
MatchingRecord matchingRecord = new MatchingRecord();
matchingRecord.setGroupId(j);
matchingRecord.setState("PENDING"); //TODO move to a constant
matchingRecord.setState(PENDING);
matchingRecord.setPatient(patientService.getPatient(patientId.intValue()));
matchingRecord.setReport(report);
@@ -399,19 +417,101 @@ public static void persistReportToDB(String rptName, List<Set<Long>> matchingPai
Set<ReportGenerationStep> reportGenerationSteps = new TreeSet<ReportGenerationStep>();
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);
reportGenerationSteps.add(step);
}
report.setReportGenerationSteps(reportGenerationSteps);
if(proTimeList != null){
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);
reportGenerationSteps.add(step);
}
report.setReportGenerationSteps(reportGenerationSteps);
}
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,
List<MatchingConfig> matchingConfigList) throws FileNotFoundException {
PatientMatchingReportMetadataService reportMetadataService = Context.getService(PatientMatchingReportMetadataService.class);
Report report = reportMetadataService.getReportByName(rptName);
// Incremental process will be carried out considering the existing report
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>();
// Get the group ids and patient ids to a Hash Map
if(!matchingRecordSet.isEmpty()){
for (MatchingRecord record: matchingRecordSet){
patientIdGroupIdMap.put(record.getPatient().getPatientId(),record.getGroupId());
}
}
// Available maximum group id for the next placement
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);
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);
List<Long> proTimeList = MatchingRunData.getInstance().getProTimeList();
if(proTimeList != null){
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,matchingConfigList);
}
}
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);
}
}
@@ -9,6 +9,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -19,6 +20,7 @@
import org.hibernate.SessionFactory;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.type.StandardBasicTypes;
import org.openmrs.Patient;
import org.openmrs.PatientIdentifierType;
@@ -62,26 +64,29 @@
private Collection<String> projections;
/**
*
*/
private Date reportCreatedOn;
public OpenMRSReader() {
this(null);
this(null,null);
}
public OpenMRSReader(Collection<String> projections) {
public OpenMRSReader(Collection<String> projections, Date reportCreatedOn) {
pageNumber = 0;
resultMode = MODE_PATIENT;
expand_patient = true;
if (projections != null && !projections.contains("org.openmrs.Patient.patientId")) {
projections.add("org.openmrs.Patient.patientId");
}
this.projections = projections;
log.info("Getting all patient records ...");
updatePatientList();
log.info("Finish intialization ...");
this.projections = null;
this.reportCreatedOn = reportCreatedOn;
}
public void init(){
log.info("Getting all patient records ...");
updatePatientList();
log.info("Finished initialization ...");
}
public void setExpandPatient(boolean expand){
@@ -93,6 +98,12 @@ private Criteria createCriteria(){
criteria = createHibernateSession().createCriteria(Patient.class)
.setMaxResults(PAGING_SIZE)
.setFirstResult(pageNumber * PAGING_SIZE);
// Add restriction to fetch the patients based on the date report created on
if(reportCreatedOn != null)
criteria.add(Restrictions.or(Restrictions.gt("dateCreated",reportCreatedOn),
Restrictions.gt("dateChanged",reportCreatedOn)));
if (projections != null) {
resultMode = MODE_PROJECTION;
ProjectionList projectionList = Projections.projectionList();
@@ -122,7 +133,7 @@ private Criteria createCriteria(){
}
return criteria;
}
private Session createHibernateSession() {
HibernateConnection connection = new HibernateConnection();
SessionFactory sessionFactory = connection.getSessionFactory();
@@ -0,0 +1,21 @@
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.openmrs.module.patientmatching.ConfigurationEntry" table="patientmatching_configurationEntries">
<id name="entryId" column="entry_id"
unsaved-value="0">
<generator class="identity" />
</id>
<discriminator column="entry_id" insert="false" />
<many-to-one name="patientMatchingConfiguration" class="org.openmrs.module.patientmatching.PatientMatchingConfiguration" not-null="true">
<column name="configuration_id" />
</many-to-one>
<property name="fieldViewName" column="field_view_name" length="255" unique="true" />
<property name="fieldName" column="field_name" not-null="true" />
<property name="inclusion" column="inclusion" not-null="false" />
<property name="blockOrder" column="block_order" not-null="false" />
<property name="flag" column="flag" not-null="false" />
</class>
</hibernate-mapping>
@@ -0,0 +1,18 @@
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.openmrs.module.patientmatching.MatchingRecord" table="patientmatching_matchingset">
<id name="setId" column="set_id" unsaved-value="0">
<generator class="identity" />
</id>
<property name="groupId" type="java.lang.Integer" column="group_id" not-null="true" />
<many-to-one name="report" class="org.openmrs.module.patientmatching.Report" not-null="true">
<column name="report_id" />
</many-to-one>
<many-to-one name="patient" column="patient_id" class="org.openmrs.Patient"/>
<property name="state" type="java.lang.String" column="state" not-null="true"/>
<set name="matchingRecordAttributeSet" lazy="true" inverse="true" cascade="all-delete-orphan" sort="natural">
<key column="matching_record_id" not-null="true"/>
<one-to-many class="org.openmrs.module.patientmatching.MatchingRecordAttribute"/>
</set>
</class>
</hibernate-mapping>
@@ -0,0 +1,14 @@
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping auto-import="false">
<class name="org.openmrs.module.patientmatching.MatchingRecordAttribute" table="patientmatching_matching_attribute">
<id name="recordAttributeId" column="record_attribute_id"
unsaved-value="0">
<generator class="identity" />
</id>
<many-to-one name="matchingRecord" class="org.openmrs.module.patientmatching.MatchingRecord" not-null="true">
<column name="matching_record_id" />
</many-to-one>
<property name="fieldName" column="field_name" length="128" />
<property name="fieldValue" column="field_value" not-null="true" length="128"/>
</class>
</hibernate-mapping>
@@ -0,0 +1,23 @@
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.openmrs.module.patientmatching">
<class name="PatientMatchingConfiguration" table="patientmatching_configuration">
<id name="configurationId" column="configuration_id" unsaved-value="0">
<generator class="identity" />
</id>
<property name="configurationName" column="configuration_name" length="255" unique="true" />
<property name="randomSampleSize" column="random_sample_size" not-null="true" />
<property name="usingRandomSample" column="using_random_sample" not-null="true"/>
<property name="estimatedPairs" column="estimated_pairs" not-null="true"/>
<property name="estimatedTime" column="estimated_time" not-null="true"/>
<property name="totalRecords" column="total_records" not-null="true"/>
<!-- bi-directional one-to-many association -->
<set name="configurationEntries" lazy="true" inverse="true" cascade="all-delete-orphan" sort="natural">
<key column="configuration_id" not-null="true" />
<one-to-many class="org.openmrs.module.patientmatching.ConfigurationEntry" />
</set>
</class>
</hibernate-mapping>
Oops, something went wrong.

0 comments on commit 7704534

Please sign in to comment.