HL7QueryController extends BaseHL7QueryController { @RequestMapping(method = RequestMethod.GET) @ResponseBody public Object getEncounters(@RequestParam(value = "patientId", required = false) String patientId, @RequestParam(value = "idTypeUuid", required = false) String idTypeUuid, @RequestParam(value = "encounterUuid", required = false) String encounterUuid, @RequestParam(value = "startDate", required = false) Date startDate, @RequestParam(value = "endDate", required = false) Date endDate, HttpServletRequest request, HttpServletResponse response) { List<Encounter> encounters = new ArrayList<Encounter>(); EncounterService encounterService = Context.getEncounterService(); HL7QueryService hL7QueryService = Context.getService(HL7QueryService.class); HL7Template template = null; Patient patient = null; if (StringUtils.isBlank(patientId) && StringUtils.isBlank(encounterUuid)) return ExceptionUtil.generateMessage(request, response, ErrorDetailsEnum.MISSING_IDENTIFIERS, null); String templateNameGP = Context.getAdministrationService().getGlobalProperty( HL7QueryConstants.HL7QUERY_GP_ORUR01_TEMPLATE); template = hL7QueryService.getHL7TemplateByName(templateNameGP); if (template == null) return ExceptionUtil.generateMessage(request, response, ErrorDetailsEnum.MISSING_TEMPLATE, templateNameGP); if (encounterUuid != null) { Encounter encounter = encounterService.getEncounterByUuid(encounterUuid); if (encounter == null) return ExceptionUtil.generateMessage(request, response, ErrorDetailsEnum.MISSING_UUID, encounterUuid); patient = encounter.getPatient(); encounters.add(encounterService.getEncounterByUuid(encounterUuid)); } else { PatientService patientService = Context.getPatientService(); PatientIdentifierType identifierType = patientService.getPatientIdentifierTypeByUuid(idTypeUuid); if (identifierType == null) return ExceptionUtil.generateMessage(request, response, ErrorDetailsEnum.MISSING_IDENTIFIER_TYPE, idTypeUuid); List<PatientIdentifierType> idTypes = new ArrayList<PatientIdentifierType>(); idTypes.add(identifierType); List<Patient> patients = Context.getPatientService().getPatients(null, patientId, idTypes, true); if (patients.size() == 0) return ExceptionUtil.generateMessage(request, response, ErrorDetailsEnum.MISSING_PATIENT, identifierType.getName() + " " + patientId); else if (patients.size() > 1) return ExceptionUtil.generateMessage(request, response, ErrorDetailsEnum.MULTIPLE_PATIENTS, identifierType.getName() + " " + patientId); patient = patients.get(0); encounters.addAll(encounterService.getEncounters(patient, null, startDate, endDate, null, null, null, false)); } Map<String, Object> bindings = new HashMap<String, Object>(); bindings.put("patient", patient); bindings.put("encounters", encounters); String hl7Output = null; try{ hl7Output = hL7QueryService.evaluateTemplate(template, bindings); }catch(Exception e){ return ExceptionUtil.generateMessage(request, response, ErrorDetailsEnum.INTERNAL_SERVER_ERROR, e); } String acceptHeader = request.getHeader("Accept"); if (acceptHeader == null || !acceptHeader.contains("text/xml")) { try { hl7Output = hL7QueryService.renderPipeDelimitedHl7(hl7Output); } catch (Exception e) { log.error("Internal error while processing the hl7 message", e); } } return hl7Output; }  @RequestMapping(method = RequestMethod.GET) @ResponseBody Object getEncounters(@RequestParam(value = "patientId", required = false) String patientId,
	                            @RequestParam(value = "idTypeUuid", required = false) String idTypeUuid,
	                            @RequestParam(value = "encounterUuid", required = false) String encounterUuid,
	                            @RequestParam(value = "startDate", required = false) Date startDate,
	                            @RequestParam(value = "endDate", required = false) Date endDate, 
	                            HttpServletRequest request, HttpServletResponse response);  }
@Test @Verifies(value = "should return the expected hl7 output as xml if the xml header exists", method = "getEncounters(String,String,String,Date,Date,HttpServletRequest)") public void getEncounters_shouldReturnTheExpectedHl7OutputAsXmlIfTheXmlHeaderExists() throws Exception { String expectedOutput = IOUtils.toString(getClass().getClassLoader().getResourceAsStream( "expectedORUR01XmlOutput.xml")); expectedOutput = StringUtils.deleteWhitespace(expectedOutput); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.addHeader("Accept", "text/xml"); String hl7Output = new HL7QueryController().getEncounters(null, null, ENCOUNTER_1_UUID, null, null, request, response) .toString(); hl7Output = StringUtils.deleteWhitespace(hl7Output); hl7Output = StringUtils.replace(hl7Output, StringUtils.substringBetween(hl7Output, "<TS.1>", "</TS.1>"), ""); hl7Output = StringUtils.replace(hl7Output, StringUtils.substringBetween(hl7Output, "<MSH.10>", "</MSH.10>"), ""); Assert.assertEquals(expectedOutput, hl7Output); }
@Test @Verifies(value = "should return the expected hl7 in the format that matches the accept header value", method = "getEncounters(String,String,String,Date,Date,HttpServletRequest)") public void getEncounters_shouldReturnTheExpectedHl7InTheFormatThatMatchesTheAcceptHeaderValue() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); String hl7Output = new HL7QueryController().getEncounters(null, null, ENCOUNTER_1_UUID, null, null, request, response) .toString(); hl7Output = StringUtils.deleteWhitespace(hl7Output); Assert.assertTrue(hl7Output.contains("||ORU^R01^ORU_R01|")); Assert.assertTrue(hl7Output.contains("|D^C|2.5^RWA|||||||||CLSM_V0.83PID|1PV1||0|")); Assert.assertFalse(hl7Output.contains("<ORU_R01 xmlns=\"urn:hl7-org:v2xml\">")); Assert.assertFalse(hl7Output.contains("</ORU_R01>")); }
@Test @Verifies(value = "should return the patient encounters given the patient identifier and id type", method = "getEncounters(String,String,String,Date,Date,HttpServletRequest)") public void getEncounters_shouldReturnThePatientEncountersGivenThePatientIdentifierAndIdType() throws Exception { String expectedOutput = IOUtils.toString(getClass().getClassLoader().getResourceAsStream( "expectedOutput_encountersByPatient.xml")); expectedOutput = StringUtils.deleteWhitespace(expectedOutput); final String identifier = "6TS-4"; final String identifierTypeUuid = "1a339fe9-38bc-4ab3-b180-320988c0b968"; final int expectedEncounterCount = 3; Patient patient = Context.getPatientService().getPatient(7); Assert.assertEquals(identifier, patient.getPatientIdentifier().getIdentifier()); Assert.assertEquals(identifierTypeUuid, patient.getPatientIdentifier().getIdentifierType().getUuid()); Assert.assertEquals(expectedEncounterCount, Context.getEncounterService().getEncountersByPatient(patient).size()); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.addHeader("Accept", "text/xml"); String hl7Output = new HL7QueryController().getEncounters(identifier, identifierTypeUuid, null, null, null, request, response) .toString(); hl7Output = StringUtils.deleteWhitespace(hl7Output); hl7Output = StringUtils.replace(hl7Output, StringUtils.substringBetween(hl7Output, "<TS.1>", "</TS.1>"), ""); hl7Output = StringUtils.replace(hl7Output, StringUtils.substringBetween(hl7Output, "<MSH.10>", "</MSH.10>"), ""); Assert.assertEquals(expectedOutput, hl7Output); }
@Test @Verifies(value = "should return the patient encounters matching specified start and end encounter dates", method = "getEncounters(String,String,String,Date,Date,HttpServletRequest)") public void getEncounters_shouldReturnThePatientEncountersMatchingSpecifiedStartAndEndEncounterDates() throws Exception { String expectedOutput = IOUtils.toString(getClass().getClassLoader().getResourceAsStream( "expectedOutput_encountersByStartAndEndDate.xml")); expectedOutput = StringUtils.deleteWhitespace(expectedOutput); final String identifier = "6TS-4"; final String identifierTypeUuid = "1a339fe9-38bc-4ab3-b180-320988c0b968"; Patient patient = Context.getPatientService().getPatient(7); Assert.assertEquals(identifier, patient.getPatientIdentifier().getIdentifier()); Assert.assertEquals(identifierTypeUuid, patient.getPatientIdentifier().getIdentifierType().getUuid()); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.addHeader("Accept", "text/xml"); Encounter expectedEncounter = Context.getEncounterService().getEncounter(4); String hl7Output = new HL7QueryController().getEncounters(identifier, identifierTypeUuid, null, expectedEncounter.getEncounterDatetime(), expectedEncounter.getEncounterDatetime(), request, response).toString(); hl7Output = StringUtils.deleteWhitespace(hl7Output); hl7Output = StringUtils.replace(hl7Output, StringUtils.substringBetween(hl7Output, "<TS.1>", "</TS.1>"), ""); hl7Output = StringUtils.replace(hl7Output, StringUtils.substringBetween(hl7Output, "<MSH.10>", "</MSH.10>"), ""); Assert.assertEquals(expectedOutput, hl7Output); }
@Test @Verifies(value = "should return appopriately formed error message if patient id and encounter uuid are null", method = "getEncounters(String,String,String,Date,Date,HttpServletRequest)") public void getEncounters_shouldReturnAppopriatelyFormedErrorMessageIfPatientIdAndEncounterUuidAreNull() throws Exception { String expectedOutput = IOUtils.toString(getClass().getClassLoader().getResourceAsStream( "missingIdentifiersXmlError.xml")); expectedOutput = StringUtils.deleteWhitespace(expectedOutput); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.addHeader("Accept", "text/xml"); String hl7Output = new HL7QueryController().getEncounters(null, null, null, null, null, request, response) .toString(); hl7Output = StringUtils.deleteWhitespace(hl7Output); Assert.assertEquals(hl7Output, expectedOutput); String expectedPipeOutput = IOUtils.toString(getClass().getClassLoader().getResourceAsStream( "missingIdentifiersJsonError.json")); expectedPipeOutput = StringUtils.deleteWhitespace(expectedPipeOutput); MockHttpServletRequest pipeRequest = new MockHttpServletRequest(); MockHttpServletResponse pipeResponse = new MockHttpServletResponse(); String hl7PipeOutput = new HL7QueryController().getEncounters(null, null, null, null, null, pipeRequest, pipeResponse) .toString(); hl7Output = StringUtils.deleteWhitespace(hl7PipeOutput); Assert.assertEquals(hl7PipeOutput, expectedPipeOutput); }
@Test @Verifies(value = "should return appopriately formed error message if encounter uuid is not found", method = "getEncounters(String,String,String,Date,Date,HttpServletRequest)") public void getEncounters_shouldReturnAppopriatelyFormedErrorMessageIfEncounterUuidIsNotFound() throws Exception { String expectedOutput = IOUtils.toString(getClass().getClassLoader().getResourceAsStream( "missingEncounterUuidXmlError.xml")); expectedOutput = StringUtils.deleteWhitespace(expectedOutput); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.addHeader("Accept", "text/xml"); String hl7Output = new HL7QueryController().getEncounters(null, null, "0", null, null, request, response) .toString(); hl7Output = StringUtils.deleteWhitespace(hl7Output); Assert.assertEquals(hl7Output, expectedOutput); String expectedPipeOutput = IOUtils.toString(getClass().getClassLoader().getResourceAsStream( "missingEncounterUuidJsonError.json")); MockHttpServletRequest pipeRequest = new MockHttpServletRequest(); MockHttpServletResponse pipeResponse = new MockHttpServletResponse(); String hl7PipeOutput = new HL7QueryController().getEncounters(null, null, "0", null, null, pipeRequest, pipeResponse) .toString(); Assert.assertEquals(hl7PipeOutput, expectedPipeOutput); }