From 4a57e1c536c1112f49e1dc85468b5891bfedacc7 Mon Sep 17 00:00:00 2001 From: Suthagar23 Date: Wed, 20 Jun 2018 18:55:45 +0530 Subject: [PATCH] LABC-3 Added implementation for assigning the patients to the locations on registration Removed personAttribute creation Added fixes for PR comments PR Review fixes Added minor fix Added some fixes according to the PR reviews Minor fix Changed the locationCheck method structure minor fix Changed method name Added fixes for PR reviews Added fixes for PR reviews Changed method to compare Changed Compare method Minor improvements Minor modifications Minor fix --- .../PatientSearchAdviser.java | 77 +++++++++++++++++++ omod/pom.xml | 6 ++ .../field/LocationsFragmentController.java | 16 ++++ omod/src/main/resources/config.xml | 8 +- .../resources/webModuleApplicationContext.xml | 4 +- .../main/webapp/fragments/field/locations.gsp | 16 ++++ pom.xml | 30 +++++++- 7 files changed, 154 insertions(+), 3 deletions(-) create mode 100644 api/src/main/java/org/openmrs/module/locationbasedaccess/PatientSearchAdviser.java create mode 100644 omod/src/main/java/org/openmrs/module/locationbasedaccess/fragment/controller/field/LocationsFragmentController.java create mode 100644 omod/src/main/webapp/fragments/field/locations.gsp diff --git a/api/src/main/java/org/openmrs/module/locationbasedaccess/PatientSearchAdviser.java b/api/src/main/java/org/openmrs/module/locationbasedaccess/PatientSearchAdviser.java new file mode 100644 index 0000000..87819b5 --- /dev/null +++ b/api/src/main/java/org/openmrs/module/locationbasedaccess/PatientSearchAdviser.java @@ -0,0 +1,77 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + *

+ * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ + +package org.openmrs.module.locationbasedaccess; + +import org.aopalliance.aop.Advice; +import org.aopalliance.intercept.MethodInvocation; +import org.aopalliance.intercept.MethodInterceptor; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.openmrs.Patient; +import org.openmrs.PersonAttributeType; +import org.openmrs.api.context.Context; +import org.springframework.aop.Advisor; +import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class PatientSearchAdviser extends StaticMethodMatcherPointcutAdvisor implements Advisor { + + private static final Log log = LogFactory.getLog(PatientSearchAdviser.class); + public static final String LOCATION_ATTRIBUTE_GLOBAL_PROPERTY_NAME = "locationbasedaccess.locationAttributeUuid"; + + @Override + public boolean matches(Method method, Class targetClass) { + if (method.getName().equals("getPatients")) { + return true; + } + return false; + } + + @Override + public Advice getAdvice() { + return new PatientSearchAdvise(); + } + + private class PatientSearchAdvise implements MethodInterceptor { + + public Object invoke(MethodInvocation invocation) throws Throwable { + Integer sessionLocationId = Context.getUserContext().getLocationId(); + String locationAttributeUuid = Context.getAdministrationService().getGlobalProperty(LOCATION_ATTRIBUTE_GLOBAL_PROPERTY_NAME); + List patientList = (List) invocation.proceed(); + if (StringUtils.isNotBlank(locationAttributeUuid)) { + final PersonAttributeType personAttributeType = Context.getPersonService().getPersonAttributeTypeByUuid(locationAttributeUuid); + if (sessionLocationId != null) { + String sessionLocationUuid = Context.getLocationService().getLocation(sessionLocationId).getUuid(); + + for (Iterator iterator = patientList.iterator(); iterator.hasNext(); ) { + Patient patient = iterator.next(); + if (!compare(patient.getAttribute(personAttributeType).getValue(), sessionLocationUuid)) { + iterator.remove(); + } + } + } else { + // If the sessionLocationId is null, then return a empty list + log.debug("Search Patient : Null Session Location in the UserContext"); + return new ArrayList(); + } + } + return patientList; + } + + private Boolean compare(String value1, String value2) { + return (StringUtils.isNotBlank(value1) && StringUtils.isNotBlank(value2)) && value1.equals(value2); + } + } +} diff --git a/omod/pom.xml b/omod/pom.xml index c7768dc..65efaf9 100644 --- a/omod/pom.xml +++ b/omod/pom.xml @@ -19,6 +19,12 @@ ${project.parent.version} compile + + org.openmrs.module + appui-omod + ${appuiVersion} + provided + diff --git a/omod/src/main/java/org/openmrs/module/locationbasedaccess/fragment/controller/field/LocationsFragmentController.java b/omod/src/main/java/org/openmrs/module/locationbasedaccess/fragment/controller/field/LocationsFragmentController.java new file mode 100644 index 0000000..0df4e3e --- /dev/null +++ b/omod/src/main/java/org/openmrs/module/locationbasedaccess/fragment/controller/field/LocationsFragmentController.java @@ -0,0 +1,16 @@ +package org.openmrs.module.locationbasedaccess.fragment.controller.field; + +import org.openmrs.Location; +import org.openmrs.ui.framework.fragment.FragmentModel; +import org.openmrs.api.context.Context; +import javax.servlet.http.HttpSession; +import java.util.List; + +public class LocationsFragmentController { + + public void controller(FragmentModel model, + HttpSession session) { + List activeLocations = Context.getLocationService().getAllLocations(); + model.addAttribute("activeLocations", activeLocations); + } +} diff --git a/omod/src/main/resources/config.xml b/omod/src/main/resources/config.xml index df6c3ad..cdaa591 100644 --- a/omod/src/main/resources/config.xml +++ b/omod/src/main/resources/config.xml @@ -18,7 +18,7 @@ - + org.openmrs.module.uiframework @@ -46,5 +46,11 @@ messages_es.properties + + + org.openmrs.api.PatientService + org.openmrs.module.locationbasedaccess.PatientSearchAdviser + + diff --git a/omod/src/main/resources/webModuleApplicationContext.xml b/omod/src/main/resources/webModuleApplicationContext.xml index 5b53dec..d1bef5b 100644 --- a/omod/src/main/resources/webModuleApplicationContext.xml +++ b/omod/src/main/resources/webModuleApplicationContext.xml @@ -5,6 +5,8 @@ http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> - + + + diff --git a/omod/src/main/webapp/fragments/field/locations.gsp b/omod/src/main/webapp/fragments/field/locations.gsp new file mode 100644 index 0000000..4a9aca4 --- /dev/null +++ b/omod/src/main/webapp/fragments/field/locations.gsp @@ -0,0 +1,16 @@ + +

+
+

+ + +

+
+
+ diff --git a/pom.xml b/pom.xml index fd3d0ba..1382bd9 100644 --- a/pom.xml +++ b/pom.xml @@ -22,6 +22,10 @@ 2.0.5 + 3.9 + 2.4.0 + 2.9 + 1.7 UTF-8 @@ -64,7 +68,31 @@ provided - + + org.openmrs.module + uiframework-api + ${uiframeworkVersion} + provided + + + + org.openmrs.module + uicommons-api + ${uicommonsVersion} + provided + + + org.openmrs.module + appframework-api + ${appframeworkVersion} + provided + + + org.openmrs.module + appui-omod + ${appuiVersion} + provided + org.openmrs.test