Skip to content

Commit

Permalink
Merge pull request I-TECH#164 from ojwanganto/moh510
Browse files Browse the repository at this point in the history
Moh510
  • Loading branch information
Ojwang' Antony authored Feb 5, 2019
2 parents 7d93157 + 6705804 commit a93df3f
Show file tree
Hide file tree
Showing 12 changed files with 375 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
import org.openmrs.Patient;
import org.openmrs.Person;
import org.openmrs.Relationship;
import org.openmrs.RelationshipType;
import org.openmrs.api.PersonService;
import org.openmrs.api.context.Context;
import org.openmrs.calculation.patient.PatientCalculationContext;
import org.openmrs.calculation.result.CalculationResultMap;
import org.openmrs.calculation.result.SimpleResult;
import org.openmrs.module.kenyacore.calculation.AbstractPatientCalculation;

import java.util.Collection;
import java.util.List;
import java.util.Map;

public class ParentCalculation extends AbstractPatientCalculation {
Expand All @@ -36,40 +39,46 @@ public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Obj
PatientCalculationContext context) {

CalculationResultMap ret = new CalculationResultMap();
PersonService personService = Context.getPersonService();
RelationshipType parentChildType = personService.getRelationshipTypeByUuid("8d91a210-c2cc-11de-8d13-0010c6dffd0f");
String parentGender = null;

for (Integer ptId : cohort) {
Person parentObj = null;

Patient patient = Context.getPatientService().getPatient(ptId);
parentSearch: for (Relationship relationship : Context.getPersonService().getRelationshipsByPerson(patient)) {
logger.info("Relationship found. Enumerating ...");
logger.info("Relationship type. " + relationship.getRelationshipType().getbIsToA());

if (relationship.getRelationshipType().getbIsToA().toLowerCase().equals("parent")) {
if (this.parentToSearch.equals("Father")) {

if (relationship.getPersonB().getGender().equals("M")) {
parentObj = relationship.getPersonB();
break parentSearch;
if (this.parentToSearch.equals("Father")) {
parentGender = "F";
} else if (this.parentToSearch.equals("Mother")) {
parentGender = "M";
}

if (parentGender != null) {

for (Integer ptId : cohort) {
Patient patient = Context.getPatientService().getPatient(ptId);
List<Relationship> parentChildRel = personService.getRelationships(null, patient, parentChildType);

// check if it is mothers
Person parent = null;
// a_is_to_b = 'Parent' and b_is_to_a = 'Child'
for (Relationship relationship : parentChildRel) {

if (patient.equals(relationship.getPersonB())) {
if (relationship.getPersonA().getGender().equals(parentGender)) {
parent = relationship.getPersonA();
break;
}

} else if (this.parentToSearch.equals("Mother")) {

if (relationship.getPersonB().getGender().equals("F")) {
parentObj = relationship.getPersonB();
break parentSearch;
} else if (patient.equals(relationship.getPersonA())) {
if (relationship.getPersonB().getGender().equals(parentGender)) {
parent = relationship.getPersonB();
break;
}

}

}
}

if (parentObj == null) {
ret.put(ptId, new SimpleResult(parentObj, this, context));
}else{
ret.put(ptId, new SimpleResult(parentObj.getPersonName().toString(), this, context));


if (parent == null) {
ret.put(ptId, new SimpleResult(parent, this, context));
} else {
ret.put(ptId, new SimpleResult(parent.getPersonName().toString(), this, context));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
package org.openmrs.module.kenyaemr.calculation.library.mchcs;

import org.apache.commons.lang.StringUtils;
import org.openmrs.Patient;
import org.openmrs.PersonAddress;
import org.openmrs.api.context.Context;
Expand All @@ -17,13 +18,15 @@
import org.openmrs.calculation.result.SimpleResult;
import org.openmrs.module.kenyacore.calculation.AbstractPatientCalculation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class PersonAddressCalculation extends AbstractPatientCalculation {

private String address;


public PersonAddressCalculation(String address) {
this.address = address.toLowerCase();
Expand All @@ -42,57 +45,22 @@ public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Obj
String personAddressString = null;

Patient patient = Context.getPatientService().getPatient(ptId);

if (this.address == null) {

PersonAddress personAddress = patient.getPersonAddress();

if (personAddress != null) {

String landmark = personAddress.getAddress2();
String estate = personAddress.getCityVillage();

personAddressString = "";
if (landmark != null && !landmark.equals("")) {
personAddressString += landmark;
}

if (estate != null && !estate.equals("")) {
personAddressString += " | ";
personAddressString += estate;
}
}
PersonAddress personAddress = patient.getPersonAddress();
List<String> addresses = new ArrayList<String>();

// get village
if (personAddress.getCityVillage() != null) {
addresses.add(patient.getPersonAddress().getCityVillage());
}

// get landmark
if (personAddress.getAddress2() != null) {
addresses.add(patient.getPersonAddress().getAddress2());
}
else {

if (this.address.equals("province")) {
personAddressString = patient.getPersonAddress().getStateProvince();
}

if (this.address.equals("district")) {
personAddressString = patient.getPersonAddress().getCountyDistrict();
}

if (this.address.equals("division")) {
personAddressString = patient.getPersonAddress().getAddress4();
}

if (this.address.equals("location")) {
personAddressString = patient.getPersonAddress().getAddress6();
}

if (this.address.equals("sublocation")) {
personAddressString = patient.getPersonAddress().getAddress5();
}

if (this.address.equals("landmark")) {
personAddressString = patient.getPersonAddress().getAddress2();
}

if (this.address.equals("village")) {
personAddressString = patient.getPersonAddress().getCityVillage();
}


if (addresses.size() > 0) {
personAddressString = StringUtils.join(addresses, "|");

}

ret.put(ptId, new SimpleResult(personAddressString, this, context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.openmrs.Patient;
import org.openmrs.Person;
import org.openmrs.User;
import org.openmrs.api.AdministrationService;
import org.openmrs.api.ConceptService;
import org.openmrs.api.EncounterService;
import org.openmrs.api.FormService;
Expand Down Expand Up @@ -115,6 +116,9 @@ public void perform(PrintWriter out) {

}
out.println("Completed migration for drug regimen history");
out.println("Voiding encounters with null regimen....");
voidAllEncountersWithNullRegimen();
out.println("Successfully completed all drug regimen migration operations");

}

Expand Down Expand Up @@ -352,4 +356,23 @@ protected Set<Integer> getPatientsWithOrders() {
Cohort patientsHavingDrugOrder = Context.getPatientSetService().getPatientsHavingDrugOrder(null, null, null, null, null, null, null, null);
return patientsHavingDrugOrder.getMemberIds();
}

protected void voidAllEncountersWithNullRegimen(){

String encountersWithNullRegimenQry = "update encounter e inner join \n" +
"(\n" +
"select \n" +
"e.encounter_id,\n" +
"max(if(o.concept_id=1193,o.value_coded,null)) as regimen \n" +
"from encounter e \n" +
"inner join obs o on e.encounter_id = o.encounter_id and o.voided=0 and o.concept_id in(1193,1252,5622,1191,1255,1268) \n" +
"inner join ( select encounter_type, uuid,name from form where uuid in('da687480-e197-11e8-9f32-f2801f1b9fd1') ) f on f.encounter_type=e.encounter_type \n" +
"group by e.encounter_id having regimen is null\n" +
") t on e.encounter_id=t.encounter_id\n" +
"set e.voided=1;";

AdministrationService as = Context.getAdministrationService();
List<List<Object>> rowsAffected = as.executeSQL(encountersWithNullRegimenQry, false);
System.out.println("Rows affected: " + rowsAffected.size());
}
}
Loading

0 comments on commit a93df3f

Please sign in to comment.