From 86cd0c24b4d81e0d503d03888e6e9b4c03a76b33 Mon Sep 17 00:00:00 2001 From: IamMujuziMoses Date: Wed, 27 Mar 2024 20:52:28 +0300 Subject: [PATCH] TRUNK-5007: Enable the ExistingOrNewVisitAssignmentHandler to handle wild cards for encounter type to visit type. --- .../ExistingOrNewVisitAssignmentHandler.java | 5 +- ...istingOrNewVisitAssignmentHandlerTest.java | 73 +++++++++++++++++++ 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/org/openmrs/api/handler/ExistingOrNewVisitAssignmentHandler.java b/api/src/main/java/org/openmrs/api/handler/ExistingOrNewVisitAssignmentHandler.java index f31db1a13d5c..87a92ef9337d 100644 --- a/api/src/main/java/org/openmrs/api/handler/ExistingOrNewVisitAssignmentHandler.java +++ b/api/src/main/java/org/openmrs/api/handler/ExistingOrNewVisitAssignmentHandler.java @@ -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)) { diff --git a/api/src/test/java/org/openmrs/api/handler/ExistingOrNewVisitAssignmentHandlerTest.java b/api/src/test/java/org/openmrs/api/handler/ExistingOrNewVisitAssignmentHandlerTest.java index 85c185c1d119..172e40b3dd82 100644 --- a/api/src/test/java/org/openmrs/api/handler/ExistingOrNewVisitAssignmentHandlerTest.java +++ b/api/src/test/java/org/openmrs/api/handler/ExistingOrNewVisitAssignmentHandlerTest.java @@ -155,4 +155,77 @@ public void beforeCreateEncounter_shouldResolveEncounterAndVisitTypeUuidsAsGloba assertEquals(Context.getVisitService().getVisitTypeByUuid(visitTypeUuid), encounter.getVisit() .getVisitType()); } + + /** + * @see org.openmrs.api.handler.ExistingOrNewVisitAssignmentHandler#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.ExistingOrNewVisitAssignmentHandler#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().getVisitTypeByUuid("c0c579b0-8e59-401d-8a4a-976a0b183519"), + encounter.getVisit().getVisitType()); + } + + /** + * @see org.openmrs.api.handler.ExistingOrNewVisitAssignmentHandler#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().getVisitTypeByUuid("c0c579b0-8e59-401d-8a4a-976a0b183519"), + encounter.getVisit().getVisitType()); + } }