Skip to content

Commit

Permalink
AM-168 , UHM-1578 - Redo confidential appointment work (since origina…
Browse files Browse the repository at this point in the history
…l implementation behaved wrong)
  • Loading branch information
djazayeri committed Oct 1, 2014
1 parent becff70 commit fdc65ff
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.openmrs.module.appointmentscheduling;

public class AppointmentSchedulingConstants {

public static final String PRIVILEGE_VIEW_CONFIDENTIAL_APPOINTMENT_DETAILS = "Task: appointmentschedulingui.viewConfidential";

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.openmrs.annotation.Handler;
import org.openmrs.api.context.Context;
import org.openmrs.module.appointmentscheduling.Appointment;
import org.openmrs.module.appointmentscheduling.AppointmentSchedulingConstants;
import org.openmrs.module.appointmentscheduling.reporting.data.AppointmentDataUtil;
import org.openmrs.module.appointmentscheduling.reporting.data.EvaluatedAppointmentData;
import org.openmrs.module.appointmentscheduling.reporting.data.definition.AppointmentDataDefinition;
Expand All @@ -16,6 +17,7 @@
import org.openmrs.module.reporting.evaluation.service.EvaluationService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

Expand All @@ -42,6 +44,28 @@ public EvaluatedAppointmentData evaluate(AppointmentDataDefinition definition, E

Map<Integer, Integer> convertedIds = evaluationService.evaluateToMap(q, Integer.class, Integer.class, context);

if (!Context.hasPrivilege(AppointmentSchedulingConstants.PRIVILEGE_VIEW_CONFIDENTIAL_APPOINTMENT_DETAILS)) {
// build a map of appointment ids to whether it is confidential
HqlQueryBuilder confidentialQuery = new HqlQueryBuilder();
confidentialQuery.select("a.appointmentId", "case a.appointmentType.confidential when 0 then false else true end");
confidentialQuery.from(Appointment.class, "a");

if (context != null) {
Set<Integer> appointmentIds = AppointmentDataUtil.getAppointmentIdsForContext(context, true);
confidentialQuery.whereIn("a.appointmentId", appointmentIds);
}

// remove confidential ones
Map<Integer, Boolean> confidentialMap = evaluationService.evaluateToMap(confidentialQuery, Integer.class, Boolean.class, context);
for (Iterator<Map.Entry<Integer, Integer>> iterator = convertedIds.entrySet().iterator(); iterator.hasNext(); ) {
Map.Entry<Integer, Integer> entry = iterator.next();
Integer appointmentId = entry.getKey();
if (confidentialMap.get(appointmentId)) {
iterator.remove();
}
}
}

if (!convertedIds.keySet().isEmpty()) {
// create a new patient evaluation context using the retrieved ids
EvaluationContext patientEvaluationContext = new EvaluationContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.openmrs.annotation.Handler;
import org.openmrs.api.context.Context;
import org.openmrs.module.appointmentscheduling.Appointment;
import org.openmrs.module.appointmentscheduling.AppointmentSchedulingConstants;
import org.openmrs.module.appointmentscheduling.reporting.data.AppointmentDataUtil;
import org.openmrs.module.appointmentscheduling.reporting.data.EvaluatedAppointmentData;
import org.openmrs.module.appointmentscheduling.reporting.data.definition.AppointmentDataDefinition;
Expand All @@ -19,6 +20,7 @@
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

Expand All @@ -45,6 +47,28 @@ public EvaluatedAppointmentData evaluate(AppointmentDataDefinition definition, E

Map<Integer, Integer> convertedIds = evaluationService.evaluateToMap(q, Integer.class, Integer.class, context);

if (!Context.hasPrivilege(AppointmentSchedulingConstants.PRIVILEGE_VIEW_CONFIDENTIAL_APPOINTMENT_DETAILS)) {
// build a map of appointment ids to whether it is confidential
HqlQueryBuilder confidentialQuery = new HqlQueryBuilder();
confidentialQuery.select("a.appointmentId", "case a.appointmentType.confidential when 0 then false else true end");
confidentialQuery.from(Appointment.class, "a");

if (context != null) {
Set<Integer> appointmentIds = AppointmentDataUtil.getAppointmentIdsForContext(context, true);
confidentialQuery.whereIn("a.appointmentId", appointmentIds);
}

// remove confidential ones
Map<Integer, Boolean> confidentialMap = evaluationService.evaluateToMap(confidentialQuery, Integer.class, Boolean.class, context);
for (Iterator<Map.Entry<Integer, Integer>> iterator = convertedIds.entrySet().iterator(); iterator.hasNext(); ) {
Map.Entry<Integer, Integer> entry = iterator.next();
Integer appointmentId = entry.getKey();
if (confidentialMap.get(appointmentId)) {
iterator.remove();
}
}
}

if (!convertedIds.keySet().isEmpty()) {
// create a new Person evaluation context using the retrieved ids
PersonEvaluationContext personEvaluationContext = new PersonEvaluationContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

import org.junit.Before;
import org.junit.Test;
import org.openmrs.PatientIdentifier;
import org.openmrs.PatientIdentifierType;
import org.openmrs.api.context.Context;
import org.openmrs.module.appointmentscheduling.reporting.context.AppointmentEvaluationContext;
import org.openmrs.module.appointmentscheduling.reporting.data.EvaluatedAppointmentData;
import org.openmrs.module.appointmentscheduling.reporting.data.definition.PatientToAppointmentDataDefinition;
import org.openmrs.module.appointmentscheduling.reporting.data.service.AppointmentDataService;
import org.openmrs.module.appointmentscheduling.reporting.query.AppointmentIdSet;
import org.openmrs.module.reporting.data.patient.definition.PatientIdDataDefinition;
import org.openmrs.module.reporting.data.patient.definition.PatientIdentifierDataDefinition;
import org.openmrs.test.BaseModuleContextSensitiveTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
Expand All @@ -29,22 +30,29 @@ public void setup() throws Exception {

@Test
public void evaluate_shouldReturnPatientDataForEachAppointmentInThePassedContext() throws Exception {

PatientIdentifierType pit = Context.getPatientService().getPatientIdentifierType(2);
PatientIdentifierDataDefinition pidd = new PatientIdentifierDataDefinition();
pidd.setIncludeFirstNonNullOnly(true);
pidd.addType(pit);

PatientToAppointmentDataDefinition d = new PatientToAppointmentDataDefinition(pidd);
PatientToAppointmentDataDefinition d = new PatientToAppointmentDataDefinition(new PatientIdDataDefinition());
AppointmentEvaluationContext context = new AppointmentEvaluationContext();
context.setBaseAppointments(new AppointmentIdSet(1, 2, 9)); // in our demo set include two appointments for same patient
EvaluatedAppointmentData ad = appointmentDataService.evaluate(d, context);

assertThat(ad.getData().size(), is(3));
assertThat((PatientIdentifier) ad.getData().get(1), is(Context.getPatientService().getPatient(1).getPatientIdentifier(pit)));
assertThat((PatientIdentifier) ad.getData().get(2), is(Context.getPatientService().getPatient(2).getPatientIdentifier(pit)));
assertThat((PatientIdentifier) ad.getData().get(9), is(Context.getPatientService().getPatient(2).getPatientIdentifier(pit)));
assertThat((Integer) ad.getData().get(1), is(1));
assertThat((Integer) ad.getData().get(2), is(2));
assertThat((Integer) ad.getData().get(9), is(2));
}

@Test
@DirtiesContext
public void evaluate_shouldReturnPatientDataForNonConfidentialAppointments() throws Exception {
Context.becomeUser("butch");

PatientToAppointmentDataDefinition d = new PatientToAppointmentDataDefinition(new PatientIdDataDefinition());
AppointmentEvaluationContext context = new AppointmentEvaluationContext();
context.setBaseAppointments(new AppointmentIdSet(1, 4)); // appointment 1 is confidential
EvaluatedAppointmentData ad = appointmentDataService.evaluate(d, context);

assertThat(ad.getData().size(), is(1));
assertThat((Integer) ad.getData().get(4), is(1));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.openmrs.module.reporting.data.person.definition.BirthdateDataDefinition;
import org.openmrs.test.BaseModuleContextSensitiveTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;

public class PersonToAppointmentDataEvaluatorTest extends BaseModuleContextSensitiveTest {

Expand All @@ -39,6 +40,24 @@ public void evaluate_shouldReturnPersonDataForEachAppointmentInThePassedContext(

}

@Test
@DirtiesContext
public void evaluate_shouldReturnPersonDataForNonConfidentialAppointments() throws Exception {
Context.becomeUser("butch");

PersonToAppointmentDataDefinition d = new PersonToAppointmentDataDefinition(new BirthdateDataDefinition());

AppointmentEvaluationContext context = new AppointmentEvaluationContext();
context.setBaseAppointments(new AppointmentIdSet(2, 4));
EvaluatedAppointmentData ed = Context.getService(AppointmentDataService.class).evaluate(d, context);

Assert.assertEquals(1, ed.getData().size());
BirthdateConverter c = new BirthdateConverter("yyyy-MM-dd");
Assert.assertEquals("1948-01-01", c.convert(ed.getData().get(4)));
// Assert.assertEquals("1975-04-08", c.convert(ed.getData().get(2)));

}

@Test
public void evaluate_shouldReturnEmptySetIfInputSetEmpty() throws Exception {
PersonToAppointmentDataDefinition d = new PersonToAppointmentDataDefinition(new BirthdateDataDefinition());
Expand Down
4 changes: 4 additions & 0 deletions omod/src/main/resources/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@
<privilege>
<name>Request Appointments</name>
<description>Ability to request new appointments</description>
</privilege>
<privilege>
<name>Task: appointmentschedulingui.viewConfidential</name>
<description>Ability to see details of confidential appointments (you also need View Appointments privileges)</description>
</privilege>
<!-- /Privileges -->

Expand Down

0 comments on commit fdc65ff

Please sign in to comment.