Skip to content

Commit

Permalink
TRUNK-5007: Enable the ExistingOrNewVisitAssignmentHandler to handle …
Browse files Browse the repository at this point in the history
…wild cards for encounter type to visit type.
  • Loading branch information
IamMujuziMoses committed Mar 27, 2024
1 parent 38f2d9b commit f2c409d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,15 @@ private static VisitType loadVisitType(EncounterType encounterType) throws APIEx

VisitService visitService = Context.getVisitService();
String targetEncounterTypeId = encounterType.getId().toString();
String targetEncounterTypeUuid = encounterType.getUuid();

String[] mappings = value.split(",");
for (String mapping : mappings) {
int index = mapping.indexOf(':');
if (index > 0) {
String encounterTypeIdOrUuid = mapping.substring(0, index).trim();
if (targetEncounterTypeId.equals(encounterTypeIdOrUuid)
|| encounterType.getUuid().equals(encounterTypeIdOrUuid)) {
if ("default".equals(encounterTypeIdOrUuid) || targetEncounterTypeId.equals(encounterTypeIdOrUuid)
|| targetEncounterTypeUuid.equals(encounterTypeIdOrUuid)) {
String visitTypeIdOrUuid = mapping.substring(index + 1).trim();
VisitType visitType;
if (StringUtils.isNumeric(visitTypeIdOrUuid)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,75 @@ public void beforeCreateEncounter_shouldResolveEncounterAndVisitTypeUuidsAsGloba
assertEquals(Context.getVisitService().getVisitTypeByUuid(visitTypeUuid), encounter.getVisit()
.getVisitType());
}

/**
* @see org.openmrs.api.handler.ExistingVisitAssignmentHandler#beforeCreateEncounter(Encounter)
*/
@Test
public void beforeCreateEncounter_shouldResolveDefaultWildCardEncounterTypeAndVisitTypeIdMappingGlobalPropertyValues() {
Encounter encounter = Context.getEncounterService().getEncounter(1);
assertNull(encounter.getVisit());

Calendar calendar = Calendar.getInstance();
calendar.setTime(encounter.getEncounterDatetime());
calendar.set(Calendar.YEAR, 1900);

encounter.setEncounterDatetime(calendar.getTime());

GlobalProperty gp = new GlobalProperty(OpenmrsConstants.GP_ENCOUNTER_TYPE_TO_VISIT_TYPE_MAPPING, "default:2");
Context.getAdministrationService().saveGlobalProperty(gp);

new ExistingOrNewVisitAssignmentHandler().beforeCreateEncounter(encounter);

assertNotNull(encounter.getVisit());
assertEquals(Context.getVisitService().getVisitType(2), encounter.getVisit().getVisitType());
}

/**
* @see org.openmrs.api.handler.ExistingVisitAssignmentHandler#beforeCreateEncounter(Encounter)
*/
@Test
public void beforeCreateEncounter_shouldResolveDefaultWildCardEncounterTypeAndVisitTypeUuidMappingGlobalPropertyValues() {
Encounter encounter = Context.getEncounterService().getEncounter(1);
assertNull(encounter.getVisit());

Calendar calendar = Calendar.getInstance();
calendar.setTime(encounter.getEncounterDatetime());
calendar.set(Calendar.YEAR, 1900);

encounter.setEncounterDatetime(calendar.getTime());

GlobalProperty gp = new GlobalProperty(OpenmrsConstants.GP_ENCOUNTER_TYPE_TO_VISIT_TYPE_MAPPING,
"default:c0c579b0-8e59-401d-8a4a-976a0b183519");
Context.getAdministrationService().saveGlobalProperty(gp);

new ExistingOrNewVisitAssignmentHandler().beforeCreateEncounter(encounter);

assertNotNull(encounter.getVisit());
assertEquals(Context.getVisitService().getVisitType(1), encounter.getVisit().getVisitType());
}

/**
* @see org.openmrs.api.handler.ExistingVisitAssignmentHandler#beforeCreateEncounter(Encounter)
*/
@Test
public void beforeCreateEncounter_shouldResolveDefaultWildCardEncounterTypeMappingsOverrideGlobalPropertyValues() {
Encounter encounter = Context.getEncounterService().getEncounter(1);
assertNull(encounter.getVisit());

Calendar calendar = Calendar.getInstance();
calendar.setTime(encounter.getEncounterDatetime());
calendar.set(Calendar.YEAR, 1900);

encounter.setEncounterDatetime(calendar.getTime());

GlobalProperty gp = new GlobalProperty(OpenmrsConstants.GP_ENCOUNTER_TYPE_TO_VISIT_TYPE_MAPPING,
"3:4, 5:2, default:c0c579b0-8e59-401d-8a4a-976a0b183519, 2:2");
Context.getAdministrationService().saveGlobalProperty(gp);

new ExistingOrNewVisitAssignmentHandler().beforeCreateEncounter(encounter);

assertNotNull(encounter.getVisit());
assertEquals(Context.getVisitService().getVisitType(1), encounter.getVisit().getVisitType());
}
}

0 comments on commit f2c409d

Please sign in to comment.