Permalink
Browse files

PTM-82: Functionality to load patients considering date created and c…

…hanged
  • Loading branch information...
Lahiru-J committed Jul 14, 2017
1 parent a327c3e commit b3ffd77b394cf021b3b4552bda726100e39b6190
@@ -75,6 +75,11 @@
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-";
/**
*
* Get the list of steps (statuses) that the analysis has to go through
@@ -149,7 +154,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 +172,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,7 +323,7 @@
throw new RuntimeException(e);
}
String reportName = "dedup-report-"+configString+dateString;
String reportName = REPORT_NAME_PREFIX + configString+dateString;
DedupMatchResultList handler = (DedupMatchResultList)objects.get("handler");
handler.flattenPairIdList();
@@ -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>
@@ -0,0 +1,33 @@
<!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.Report" table="patientmatching_report">
<id name="reportId" column="report_id"
unsaved-value="0">
<generator class="identity" />
</id>
<property name="reportName" column="report_name" length="255" />
<property name="createdOn" type="java.util.Date" column="created_on" not-null="true" length="19"/>
<many-to-one name="createdBy" class="org.openmrs.User">
<column name="created_by" />
</many-to-one>
<set name="matchingRecordSet" lazy="true" inverse="true" cascade="all-delete-orphan" sort="natural">
<key column="report_id" not-null="true"/>
<one-to-many class="org.openmrs.module.patientmatching.MatchingRecord"/>
</set>
<set name="reportGenerationSteps" lazy="true" inverse="true" cascade="all-delete-orphan" sort="natural">
<key column="report_id" not-null="true"/>
<one-to-many class="org.openmrs.module.patientmatching.ReportGenerationStep"/>
</set>
<set name="usedConfigurationList" table="patientmatching_report_configuration" cascade="none">
<key column="report_id"/>
<many-to-many column="configuration_id"
class="org.openmrs.module.patientmatching.PatientMatchingConfiguration"/>
</set>
</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.ReportGenerationStep" table=" patientmatching_report_generation_step">
<id name="stepId" type="int" column="step_id"
unsaved-value="0">
<generator class="identity" />
</id>
<many-to-one name="report" class="org.openmrs.module.patientmatching.Report" not-null="true">
<column name="report_id" />
</many-to-one>
<property name="processName" type="java.lang.String" column="process_name" length="50" />
<property name="sequenceNo" type="java.lang.Integer" column="sequence_number" not-null="true" />
<property name="timeTaken" type="java.lang.Integer" column="time_taken" not-null="false" />
</class>
</hibernate-mapping>
@@ -0,0 +1,68 @@
package org.regenstrief.linkage.io;
import org.hibernate.Criteria;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openmrs.test.BaseModuleContextSensitiveTest;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
public class OpenMRSReaderTest extends BaseModuleContextSensitiveTest {
@Before
public void setup() throws Exception {
executeDataSet("PatientMatchingTestDataSet.xml");
}
@Test
public void createCriteria_shouldNotReturnNullAndEmpty() throws ParseException, ClassNotFoundException, IllegalAccessException,
InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
// Creating a collection of projections
Collection<String> projections = Arrays.asList("org.openmrs.Patient.birthdate",
"org.openmrs.Patient.gender","org.openmrs.Patient.patientId","org.openmrs.PersonAddress.country",
"org.openmrs.PersonAddress.countyDistrict","org.openmrs.PersonAddress.postalCode",
"org.openmrs.PersonAddress.stateProvince","org.openmrs.PersonName.familyName");
// Initialize date variable for report created date
Date dateReportCreatedOn;
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
dateReportCreatedOn = dateFormat.parse("18-01-2006");
// Reflection class for OpenMRSReader as the method going to be tested is a private method
Class reflectionClass = OpenMRSReader.class;
Object objReader = reflectionClass.newInstance();
// Field for collection of projections
Field fieldProjections = reflectionClass.getDeclaredField("projections");
fieldProjections.setAccessible(true);
fieldProjections.set(objReader,projections);
// Field for date report created on
Field fieldDateReportCreatedOn = reflectionClass.getDeclaredField("reportCreatedOn");
fieldDateReportCreatedOn.setAccessible(true);
fieldDateReportCreatedOn.set(objReader,dateReportCreatedOn);
// Get the method createCriteria()
Method methodCreateCriteria = reflectionClass.getDeclaredMethod("createCriteria",null);
methodCreateCriteria.setAccessible(true);
Object objCriteria = methodCreateCriteria.invoke(objReader);
// criteria neither be null nor empty
Criteria criteria = ((Criteria)objCriteria);
Assert.assertNotNull(criteria);
Assert.assertNotEquals(0,criteria.list().size());
}
}
Oops, something went wrong.

0 comments on commit b3ffd77

Please sign in to comment.