/** * Returns a {@link File} for the given obs complex data to be written to. The output file * location is determined off of the {@link OpenmrsConstants#GLOBAL_PROPERTY_COMPLEX_OBS_DIR} * and the file name is determined off the current obs.getComplexData().getTitle(). * * @param obs the Obs with a non-null complex data on it * @return File that the complex data should be written to */ public File getOutputFileToWrite(Obs obs) throws IOException { String title = obs.getComplexData().getTitle(); String titleWithoutExtension = FilenameUtils.removeExtension(title); String extension = "." + StringUtils.defaultIfEmpty(FilenameUtils.getExtension(title), "dat"); String uuid = obs.getUuid(); String filename; if (StringUtils.isNotBlank(titleWithoutExtension)) { filename = titleWithoutExtension + "_" + uuid + extension; } else { filename = uuid + extension; } File dir = OpenmrsUtil.getDirectoryInApplicationDataDirectory( Context.getAdministrationService().getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_COMPLEX_OBS_DIR)); File outputfile = new File(dir, filename); return outputfile; }
@Test public void getOutputFileToWrite_shouldCorrectlyNameNullTitledFile() throws IOException, ParseException { ComplexData complexDataWithNullTitle = new ComplexData(null, null); Obs obsWithNullTitle = new Obs(); obsWithNullTitle.setComplexData(complexDataWithNullTitle); File nullTitleFile = handler.getOutputFileToWrite(obsWithNullTitle); nullTitleFile.createNewFile(); String[] nameWithNullTitle = nullTitleFile.getName().split("\\."); String uuidPartWithNullTitle = nameWithNullTitle[0]; String extensionPartWithNullTitle = nameWithNullTitle[1]; assertEquals(extensionPartWithNullTitle, "dat"); assertEquals(uuidPartWithNullTitle, obsWithNullTitle.getUuid()); }
@Test public void getOutputFileToWrite_shouldCorrectlyNameTitledFileWithoutExtension() throws IOException, ParseException { ComplexData complexDataWithoutExtension = new ComplexData(FilenameUtils.removeExtension(FILENAME), null); Obs obsWithoutExtension = new Obs(); obsWithoutExtension.setComplexData(complexDataWithoutExtension); File extensionlessFile = handler.getOutputFileToWrite(obsWithoutExtension); extensionlessFile.createNewFile(); String[] nameWithoutExtension = extensionlessFile.getName().split("_|\\."); String titlePartExtensionless = nameWithoutExtension[0]; String uuidPartExtensionless = nameWithoutExtension[1]; String extensionPartExtensionless = nameWithoutExtension[2]; assertEquals(titlePartExtensionless, FilenameUtils.removeExtension(FILENAME)); assertEquals(extensionPartExtensionless, "dat"); assertEquals(uuidPartExtensionless, obsWithoutExtension.getUuid()); }
private void mergeObservationsNotContainedInEncounters(Patient preferred, Patient notPreferred, PersonMergeLogData mergedData) { // move all obs that weren't contained in encounters // TODO: this should be a copy, not a move ObsService obsService = Context.getObsService(); for (Obs obs : obsService.getObservationsByPerson(notPreferred)) { if (obs.getEncounter() == null && !obs.getVoided()) { obs.setPerson(preferred); Obs persisted = obsService.saveObs(obs, "Merged from patient #" + notPreferred.getPatientId()); mergedData.addMovedIndependentObservation(persisted.getUuid()); } } }
@Test public void getOutputFileToWrite_shouldCorrectlyNameTitledFileWithExtension() throws IOException, ParseException { ComplexData complexDataWithTitle = new ComplexData(FILENAME, null); Obs obsWithTitle = new Obs(); obsWithTitle.setComplexData(complexDataWithTitle); File titledFile = handler.getOutputFileToWrite(obsWithTitle); titledFile.createNewFile(); String[] nameWithTitle = titledFile.getName().split("_|\\."); String titlePart = nameWithTitle[0]; String uuidPartWithTitle = nameWithTitle[1]; String extensionPart = nameWithTitle[2]; assertEquals(titlePart, FilenameUtils.removeExtension(FILENAME)); assertEquals(extensionPart, "txt"); assertEquals(uuidPartWithTitle, obsWithTitle.getUuid()); }
String filename = "nameOfFile_" + obsToSave.getUuid() + ".txt"; File complexObsDir = OpenmrsUtil.getDirectoryInApplicationDataDirectory(as .getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_COMPLEX_OBS_DIR));
ob.setLocation(cp.getEncounter().getLocation()); ob.setPerson(cp.getEncounter().getPatient()); if (ob.getUuid() == null) { ob.setUuid(UUID.randomUUID().toString());
@Test public void newInstance_shouldCopyMostFields() throws Exception { Obs obs = new Obs(); obs.setStatus(Obs.Status.PRELIMINARY); obs.setInterpretation(Obs.Interpretation.LOW); obs.setConcept(new Concept()); obs.setValueNumeric(1.2); Obs copy = Obs.newInstance(obs); // these fields are not copied assertThat(copy.getObsId(), nullValue()); assertThat(copy.getUuid(), not(obs.getUuid())); // other fields are copied assertThat(copy.getConcept(), is(obs.getConcept())); assertThat(copy.getValueNumeric(), is(obs.getValueNumeric())); assertThat(copy.getStatus(), is(obs.getStatus())); assertThat(copy.getInterpretation(), is(obs.getInterpretation())); // TODO test that the rest of the fields are set }
Assert.assertNotNull(complexObs.getValueComplex()); Assert.assertNotNull(complexObs.getComplexData()); Assert.assertEquals(complexObs, os.getObsByUuid(complexObs.getUuid()));
/** * @see PatientService#mergePatients(Patient,Patient) */ @Test public void mergePatients_shouldAuditMovedIndependentObservations() throws Exception { //retrieve patients Patient preferred = patientService.getPatient(999); Patient notPreferred = patientService.getPatient(7); voidOrders(Collections.singleton(notPreferred)); //get an observation for notPreferred and make it independent from any encounter Obs obs = Context.getObsService().getObs(7); obs.setEncounter(null); obs.setComment("this observation is for testing the merge"); Context.getObsService().saveObs(obs, "Reason cannot be blank"); //merge the two patients and retrieve the audit object PersonMergeLog audit = mergeAndRetrieveAudit(preferred, notPreferred); String uuid = null; List<Obs> observations = Context.getObsService().getObservationsByPerson(preferred); for (Obs o : observations) { if (obs.getComment().equals(o.getComment())) { uuid = o.getUuid(); } } Assert.assertTrue("moving of independent observation was not audited", isValueInList(uuid, audit.getPersonMergeLogData().getMovedIndependentObservations())); }
byte[] bytes = DatatypeConverter.parseBase64Binary(value.toString()); ComplexData complexData = new ComplexData(obs.getUuid() + ".raw", new ByteArrayInputStream(bytes)); obs.setComplexData(complexData); } else if (obs.getConcept().getDatatype().isCoded()) {