DataChangeEventHandler extends HibernateDaoSupport implements EventHandler { @Override public void handle(final AdmitPatientEvent event) { if (event == null) { return; } final String patientId = event.getPatientId(); ExternalPatient patient = findPatient(patientId); if (patient == null) { final PatientDetails patientDetails = event.getPatientDetails(); if (patientDetails == null) { throw new NullPointerException("Patient [" + patientId + "] does not exist; details not provided for a new patient creation!"); } patient = new ExternalPatientImpl(); patient.setExternalId(patientId); patient.setName(patientDetails.getName()); patient.setAddress(patientDetails.getAddress()); patient.setGender(patientDetails.getGender()); getHibernateTemplate().save(patient); } final ExternalCentralCase centralCase = new ExternalCentralCaseImpl(); centralCase.setEffective(Intervals.infiniteFrom(event.getWhen())); centralCase.setPatient(patient); centralCase.setOutpatient(event.getCentralCaseDetails().isOutpatient()); centralCase.setCuringCareProfessional(findMedicalStaff(event.getCentralCaseDetails().getCuringCareProfessionalId())); centralCase.setSupervisoryCareProfessional(findMedicalStaff(event.getCentralCaseDetails().getSupervisoryCareProfessionalId())); centralCase.setRoomAndBed(event.getCentralCaseDetails().getRoomAndBed()); final Serializable centralCaseId = getHibernateTemplate().save(centralCase); centralCase.setExternalId(String.valueOf(centralCaseId)); final ExternalEpisode episode = new ExternalEpisodeImpl(); episode.setCentralCase(centralCase); final ExternalCareProvider careProvider = findExternalEntity( event.getCareProviderId(), externalCareProvider, ExternalCareProvider.class); if (careProvider == null) { throw new IllegalArgumentException("Care provider [" + event.getCareProviderId() + "] does not exist!"); } episode.setCareProvider(careProvider); episode.setEffective(Intervals.infiniteFrom(event.getWhen())); final Serializable episodeId = getHibernateTemplate().save(episode); episode.setExternalId(String.valueOf(episodeId)); final ExternalEncounter encounter = new ExternalEncounterImpl(); encounter.setType(ExternalEncounterType.ADMISSION); encounter.setWhen(event.getWhen()); encounter.setEpisode(episode); final Serializable encounterId = getHibernateTemplate().save(encounter); encounter.setExternalId(String.valueOf(encounterId)); }  @Override void handle(final AdmitPatientEvent event); @Override void handle(final DischargePatientEvent event);  }
@Test public void testAdmitNullPatient() { eventHandler.handle((AdmitPatientEvent)null); }
@Test(expected = NullPointerException.class) public void testAdmitNewPatientWithoutPatientAndCentralCaseDetails() { eventHandler.handle(new AdmitPatientEvent("1000", new DateTime(), null, "1", null)); }
@Test(expected = NullPointerException.class) public void testAdmitNewPatientWithoutPatientDetails() { final CentralCaseDetails centralCaseDetails = new CentralCaseDetails(false, "1", "1", "R1-B1"); eventHandler.handle(new AdmitPatientEvent("1000", new DateTime(), null, "1", centralCaseDetails)); }
@Test(expected = NullPointerException.class) public void testAdmitNewPatientWithoutCentralCaseDetails() { final PatientDetails patientDetails = new PatientDetails("New Patient", new DateTime(2000, 1, 1, 0, 0), Gender.MALE, "New address"); eventHandler.handle(new AdmitPatientEvent("1000", new DateTime(), patientDetails, "1", null)); }
@Test public void testAdmitNewPatientToKnownCareProvider() { final PatientDetails patientDetails = new PatientDetails("New Patient", new DateTime(2000, 1, 1, 0, 0), Gender.MALE, "New address"); final CentralCaseDetails centralCaseDetails = new CentralCaseDetails(false, "1", "1", "R1-B1"); final DateTime now = new DateTime(); eventHandler.handle(new AdmitPatientEvent("1000", now, patientDetails, "1", centralCaseDetails)); }
@Test(expected = IllegalArgumentException.class) public void testAdmitNewPatientToUnknownCareProvider() { final PatientDetails patientDetails = new PatientDetails("New Patient", new DateTime(2000, 1, 1, 0, 0), Gender.MALE, "New address"); final CentralCaseDetails centralCaseDetails = new CentralCaseDetails(false, "1", "1", "R1-B1"); final DateTime now = new DateTime(); eventHandler.handle(new AdmitPatientEvent("1000", now, patientDetails, "999", centralCaseDetails)); }
@Test(expected = NullPointerException.class) public void testAdmitExistingPatientWithoutCentralCaseDetails() { eventHandler.handle(new AdmitPatientEvent("1", new DateTime(), null, "1", null)); }
@Test(expected = IllegalArgumentException.class) public void testAdmitExistingPatientToUnknownCareProvider() { final CentralCaseDetails centralCaseDetails = new CentralCaseDetails(false, "1", "1", "R1-B1"); final DateTime now = new DateTime(); eventHandler.handle(new AdmitPatientEvent("1", now, null, "999", centralCaseDetails)); }
@Test public void testAdmitExistingPatient() { final CentralCaseDetails centralCaseDetails = new CentralCaseDetails(false, "1", "1", "R1-B1"); final DateTime now = new DateTime(); eventHandler.handle(new AdmitPatientEvent("1", now, null, "1", centralCaseDetails)); }