Skip to content

Commit

Permalink
Added implementation for assigning the patients to the locations on r…
Browse files Browse the repository at this point in the history
…egistration

License headers updated

Removed unused constants

Implemented custom  fragement for the location selection

Implemented custom  fragement for the location selection

Added solution to fetch the session location property

Changed Location global property to user context

Removed AOP implementation

Added accessLocation filter for PatientSearch
  • Loading branch information
Suthagar23 committed Jun 16, 2018
1 parent 6dbd5a2 commit e0deb09
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 4 deletions.
Expand Up @@ -10,7 +10,9 @@

package org.openmrs.module.locationbasedaccess;

import org.openmrs.module.locationbasedaccess.common.Helper;
import org.openmrs.PersonAttributeType;
import org.openmrs.api.context.Context;
import org.openmrs.module.locationbasedaccess.common.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.openmrs.module.BaseModuleActivator;
Expand Down Expand Up @@ -46,6 +48,18 @@ public void willStart() {
* @see ModuleActivator#started()
*/
public void started() {
if (Context.getPersonService().getPersonAttributeTypeByName(Constants.ACCESS_LOCATION_PERSON_ATTRIBUTE_TYPE_NAME) == null) {
PersonAttributeType personAttributeType = new PersonAttributeType();
personAttributeType.setUuid("8b5c95ef-103c-41bc-9f24-368b8f77e070");
personAttributeType.setName(Constants.ACCESS_LOCATION_PERSON_ATTRIBUTE_TYPE_NAME);
personAttributeType.setFormat("java.lang.String");
personAttributeType.setDescription("Given Access location for the person");
Context.getPersonService().savePersonAttributeType(personAttributeType);
}

if(Context.getAdministrationService().getGlobalProperty(Constants.SHOW_PATIENTS_WHO_WITHOUT_ACCESS_LOCATION) == null) {
Context.getAdministrationService().setGlobalProperty(Constants.SHOW_PATIENTS_WHO_WITHOUT_ACCESS_LOCATION,"true");
}
log.info("Location Based Access Control Module started");
}

Expand Down
@@ -0,0 +1,111 @@
/**
* 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.
* <p>
* 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.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.Location;
import org.openmrs.Patient;
import org.openmrs.Person;
import org.openmrs.PersonAttribute;
import org.openmrs.api.context.Context;
import org.openmrs.module.locationbasedaccess.common.Constants;
import org.springframework.aop.Advisor;
import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;

/**
* It will used to implement the AOP Methods for PatientServiceImpl class
*/
public class PatientSearchAdvise extends StaticMethodMatcherPointcutAdvisor implements Advisor {

protected static final Log log = LogFactory.getLog(PatientSearchAdvise.class);

@Override
public boolean matches(Method method, Class targetClass) {
// For PatientServiceImpl.savePatient()
if (method.getName().equals("getPatients")) {
return true;
}
// For PatientServiceImpl.getPatient()
else if (method.getName().equals("getPatient")) {
return true;
}
return false;
}

@Override
public Advice getAdvice() {
return new UserControlAdvice();
}

/**
* This inner class will be used to intercept the caught method using AOP
*/
private class UserControlAdvice implements MethodInterceptor {

public Object invoke(MethodInvocation invocation) throws Throwable {
Object currentResult = invocation.proceed();
Integer sessionLocationId = Context.getUserContext().getLocationId();

if(sessionLocationId != null) {
Location sessionLocation = Context.getLocationService().getLocation(sessionLocationId);
// For PatientServiceImpl.getPatient()
if(currentResult instanceof Patient) {
Patient currentPatient = (Patient)currentResult;
currentPatient = checkPatientForAccessLocation(currentPatient, sessionLocation);
return currentPatient;
}
// For PatientServiceImpl.savePatient()
else {
List<Patient> currentPatientResults = (List<Patient>)currentResult;
for (Iterator<Patient> iterator = currentPatientResults.iterator(); iterator.hasNext(); ) {
Patient patient = iterator.next();
patient = checkPatientForAccessLocation(patient, sessionLocation);
if(patient == null) {
iterator.remove();
}
}
return currentPatientResults;
}
}
else {
// If session location doesn't available, return empty patient
log.debug("Search Patient : Null Session Location in the UserContext");
return null;
}
}

private Patient checkPatientForAccessLocation(Patient patient, Location sessionLocation) {
Person person = patient.getPerson();
PersonAttribute personAttribute = person.getAttribute(Constants.ACCESS_LOCATION_PERSON_ATTRIBUTE_TYPE_NAME);
if (personAttribute == null) {
log.debug("Patient AccessLocation attribute missing for Patient - " + person.getGivenName());
String showPatientsWithoutAccessLocation = Context.getAdministrationService().getGlobalProperty(Constants.SHOW_PATIENTS_WHO_WITHOUT_ACCESS_LOCATION);
if( showPatientsWithoutAccessLocation.equals("false")) {
patient = null;
}
}
else {
if(!personAttribute.getValue().equals(sessionLocation.getUuid())) {
patient = null;
}
}
return patient;
}
}
}
@@ -0,0 +1,17 @@
/**
* 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.common;

public class Constants {

public static final String ACCESS_LOCATION_PERSON_ATTRIBUTE_TYPE_NAME = "accessControlLocation";
public static final String SHOW_PATIENTS_WHO_WITHOUT_ACCESS_LOCATION = "showPatientsWithoutAccessLocation";
}
6 changes: 6 additions & 0 deletions omod/pom.xml
Expand Up @@ -19,6 +19,12 @@
<version>${project.parent.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>appui-omod</artifactId>
<version>${appuiVersion}</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
@@ -0,0 +1,39 @@
package org.openmrs.module.locationbasedaccess.fragment.controller.field;

import org.openmrs.Location;
import org.openmrs.api.context.Context;
import org.openmrs.module.appui.UiSessionContext;
import org.openmrs.ui.framework.fragment.FragmentModel;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
* Fields and actions to provide registrationInfo view.
*
* @author Romain Buisson
*/
public class LocationsFragmentController {

public void controller(FragmentModel model,
HttpSession session) {

// Fetch the current logged in attribute from the session
String LOCATION_SESSION_ATTRIBUTE = UiSessionContext.LOCATION_SESSION_ATTRIBUTE;
Integer sessionLocationId = (Integer) session.getAttribute(LOCATION_SESSION_ATTRIBUTE);

List<Location> activeLocations =new ArrayList<Location>();
activeLocations.add(Context.getLocationService().getLocation(sessionLocationId));
for(Location location : Context.getLocationService().getAllLocations(true)) {
if(location.getLocationId() != sessionLocationId) {
activeLocations.add(location);
}
}

model.addAttribute("activeLocations", activeLocations);
}
}




8 changes: 7 additions & 1 deletion omod/src/main/resources/config.xml
Expand Up @@ -18,7 +18,7 @@

<!-- Required Modules -->
<require_modules>

<require_module version="${uicommonsVersion}">org.openmrs.module.uicommons</require_module>
</require_modules>
<!-- / Required Modules -->

Expand Down Expand Up @@ -46,5 +46,11 @@
<file>messages_es.properties</file>
</messages>
<!-- /Internationalization -->

<advice>
<point>org.openmrs.api.PatientService</point>
<class>org.openmrs.module.locationbasedaccess.PatientSearchAdvise</class>
</advice>

</module>

4 changes: 3 additions & 1 deletion omod/src/main/resources/webModuleApplicationContext.xml
Expand Up @@ -5,6 +5,8 @@
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!-- Add here beans related to the web context -->

<bean class="org.openmrs.ui.framework.StandardModuleUiConfiguration">
<property name="moduleId" value="${project.parent.artifactId}"/>
</bean>

</beans>
22 changes: 22 additions & 0 deletions omod/src/main/webapp/fragments/field/locations.gsp
@@ -0,0 +1,22 @@
<%
ui.includeJavascript("uicommons", "angular.js")
ui.includeJavascript("uicommons", "angular-common.js")
ui.includeJavascript("uicommons", "angular-resource.min.js")
ui.includeJavascript("uicommons", "angular-ui/ui-bootstrap-tpls-0.6.0.min.js")
%>

<div>
<div>
<p class="left">
<label>
Please select the default access location
</label>
<select id="${config.formFieldName}" name="${config.formFieldName}" class="rel_type">
<% activeLocations.each { location -> %>
<option value="${location.uuid}">${location.name}</option>
<% } %>
</select>
</p>
</div>
</div>

30 changes: 29 additions & 1 deletion pom.xml
Expand Up @@ -22,6 +22,10 @@

<properties>
<openMRSVersion>2.0.5</openMRSVersion>
<uiframeworkVersion>3.9</uiframeworkVersion>
<uicommonsVersion>2.4.0</uicommonsVersion>
<appframeworkVersion>2.9</appframeworkVersion>
<appuiVersion>1.7</appuiVersion>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Expand Down Expand Up @@ -64,7 +68,31 @@
<scope>provided</scope>
</dependency>


<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>uiframework-api</artifactId>
<version>${uiframeworkVersion}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>uicommons-api</artifactId>
<version>${uicommonsVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>appframework-api</artifactId>
<version>${appframeworkVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>appui-omod</artifactId>
<version>${appuiVersion}</version>
<scope>provided</scope>
</dependency>
<!-- For testing -->
<dependency>
<groupId>org.openmrs.test</groupId>
Expand Down

0 comments on commit e0deb09

Please sign in to comment.