From fdc65ffcde4d737cfe0297ca4da69488dd46b41d Mon Sep 17 00:00:00 2001 From: Darius Jazayeri Date: Wed, 1 Oct 2014 12:47:50 -0400 Subject: [PATCH] AM-168 , UHM-1578 - Redo confidential appointment work (since original implementation behaved wrong) --- .../AppointmentSchedulingConstants.java | 7 +++++ .../PatientToAppointmentDataEvaluator.java | 24 +++++++++++++++ .../PersonToAppointmentDataEvaluator.java | 24 +++++++++++++++ ...PatientToAppointmentDataEvaluatorTest.java | 30 ++++++++++++------- .../PersonToAppointmentDataEvaluatorTest.java | 19 ++++++++++++ omod/src/main/resources/config.xml | 4 +++ 6 files changed, 97 insertions(+), 11 deletions(-) create mode 100644 api/src/main/java/org/openmrs/module/appointmentscheduling/AppointmentSchedulingConstants.java diff --git a/api/src/main/java/org/openmrs/module/appointmentscheduling/AppointmentSchedulingConstants.java b/api/src/main/java/org/openmrs/module/appointmentscheduling/AppointmentSchedulingConstants.java new file mode 100644 index 00000000..b3b9c870 --- /dev/null +++ b/api/src/main/java/org/openmrs/module/appointmentscheduling/AppointmentSchedulingConstants.java @@ -0,0 +1,7 @@ +package org.openmrs.module.appointmentscheduling; + +public class AppointmentSchedulingConstants { + + public static final String PRIVILEGE_VIEW_CONFIDENTIAL_APPOINTMENT_DETAILS = "Task: appointmentschedulingui.viewConfidential"; + +} diff --git a/api/src/main/java/org/openmrs/module/appointmentscheduling/reporting/data/evaluator/PatientToAppointmentDataEvaluator.java b/api/src/main/java/org/openmrs/module/appointmentscheduling/reporting/data/evaluator/PatientToAppointmentDataEvaluator.java index 33a2785c..261360d3 100644 --- a/api/src/main/java/org/openmrs/module/appointmentscheduling/reporting/data/evaluator/PatientToAppointmentDataEvaluator.java +++ b/api/src/main/java/org/openmrs/module/appointmentscheduling/reporting/data/evaluator/PatientToAppointmentDataEvaluator.java @@ -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; @@ -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; @@ -42,6 +44,28 @@ public EvaluatedAppointmentData evaluate(AppointmentDataDefinition definition, E Map 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 appointmentIds = AppointmentDataUtil.getAppointmentIdsForContext(context, true); + confidentialQuery.whereIn("a.appointmentId", appointmentIds); + } + + // remove confidential ones + Map confidentialMap = evaluationService.evaluateToMap(confidentialQuery, Integer.class, Boolean.class, context); + for (Iterator> iterator = convertedIds.entrySet().iterator(); iterator.hasNext(); ) { + Map.Entry 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(); diff --git a/api/src/main/java/org/openmrs/module/appointmentscheduling/reporting/data/evaluator/PersonToAppointmentDataEvaluator.java b/api/src/main/java/org/openmrs/module/appointmentscheduling/reporting/data/evaluator/PersonToAppointmentDataEvaluator.java index ef011bad..e0d34aba 100644 --- a/api/src/main/java/org/openmrs/module/appointmentscheduling/reporting/data/evaluator/PersonToAppointmentDataEvaluator.java +++ b/api/src/main/java/org/openmrs/module/appointmentscheduling/reporting/data/evaluator/PersonToAppointmentDataEvaluator.java @@ -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; @@ -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; @@ -45,6 +47,28 @@ public EvaluatedAppointmentData evaluate(AppointmentDataDefinition definition, E Map 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 appointmentIds = AppointmentDataUtil.getAppointmentIdsForContext(context, true); + confidentialQuery.whereIn("a.appointmentId", appointmentIds); + } + + // remove confidential ones + Map confidentialMap = evaluationService.evaluateToMap(confidentialQuery, Integer.class, Boolean.class, context); + for (Iterator> iterator = convertedIds.entrySet().iterator(); iterator.hasNext(); ) { + Map.Entry 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(); diff --git a/api/src/test/java/org/openmrs/module/appointmentscheduling/reporting/data/evaluator/PatientToAppointmentDataEvaluatorTest.java b/api/src/test/java/org/openmrs/module/appointmentscheduling/reporting/data/evaluator/PatientToAppointmentDataEvaluatorTest.java index 475fe0a7..a74ccd2e 100644 --- a/api/src/test/java/org/openmrs/module/appointmentscheduling/reporting/data/evaluator/PatientToAppointmentDataEvaluatorTest.java +++ b/api/src/test/java/org/openmrs/module/appointmentscheduling/reporting/data/evaluator/PatientToAppointmentDataEvaluatorTest.java @@ -2,7 +2,6 @@ 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; @@ -10,9 +9,11 @@ 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; @@ -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 diff --git a/api/src/test/java/org/openmrs/module/appointmentscheduling/reporting/data/evaluator/PersonToAppointmentDataEvaluatorTest.java b/api/src/test/java/org/openmrs/module/appointmentscheduling/reporting/data/evaluator/PersonToAppointmentDataEvaluatorTest.java index 4a393ce8..b9b12261 100644 --- a/api/src/test/java/org/openmrs/module/appointmentscheduling/reporting/data/evaluator/PersonToAppointmentDataEvaluatorTest.java +++ b/api/src/test/java/org/openmrs/module/appointmentscheduling/reporting/data/evaluator/PersonToAppointmentDataEvaluatorTest.java @@ -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 { @@ -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()); diff --git a/omod/src/main/resources/config.xml b/omod/src/main/resources/config.xml index a5823883..3344c125 100644 --- a/omod/src/main/resources/config.xml +++ b/omod/src/main/resources/config.xml @@ -214,6 +214,10 @@ Request Appointments Ability to request new appointments + + + Task: appointmentschedulingui.viewConfidential + Ability to see details of confidential appointments (you also need View Appointments privileges)