/** * @see org.openmrs.obs.ComplexObsHandler#purgeComplexData(org.openmrs.Obs) */ public boolean purgeComplexData(Obs obs) { File file = getComplexDataFile(obs); if (!file.exists()) { return true; } else if (file.delete()) { obs.setComplexData(null); return true; } log.warn( "Could not delete complex data object for obsId=" + obs.getObsId() + " located at " + file.getAbsolutePath()); return false; }
/** * @see org.openmrs.obs.ComplexObsHandler#getObs(Obs, String) */ public Obs getObs(Obs obs, String view) { File file = BinaryDataHandler.getComplexDataFile(obs); log.debug("value complex: " + obs.getValueComplex()); log.debug("file path: " + file.getAbsolutePath()); ComplexData complexData = null; try { complexData = new ComplexData(file.getName(), OpenmrsUtil.getFileAsBytes(file)); } catch (IOException e) { log.error("Trying to read file: " + file.getAbsolutePath(), e); } String mimeType = OpenmrsUtil.getFileMimeType(file); complexData.setMimeType(mimeType); obs.setComplexData(complexData); return obs; }
obs.setComplexData(complexData);
complexData.setMimeType(mimeType); obs.setComplexData(complexData);
complexData.setMimeType(mimeType); obs.setComplexData(complexData);
complexData.setMimeType(mimeType); obs.setComplexData(complexData);
/** * @see org.openmrs.obs.ComplexObsHandler#saveObs(org.openmrs.Obs) */ @Override public Obs saveObs(Obs obs) throws APIException { try { // Write the File to the File System String fileName = obs.getComplexData().getTitle(); File outfile = getOutputFileToWrite(obs); OutputStream out = new FileOutputStream(outfile, false); FileInputStream mediaStream = (FileInputStream) obs.getComplexData().getData(); OpenmrsUtil.copyFile(mediaStream, out); // Store the filename in the Obs obs.setComplexData(null); obs.setValueComplex(fileName + "|" + outfile.getName()); // close the stream out.close(); } catch (IOException ioe) { throw new APIException("Obs.error.trying.write.complex", null, ioe); } return obs; }
/** * @see ComplexObsHandler#saveObs(Obs) */ @Override public Obs saveObs(Obs obs) throws APIException { try { // Write the File to the File System String fileName = obs.getComplexData().getTitle(); InputStream in = (InputStream) obs.getComplexData().getData(); File outfile = getOutputFileToWrite(obs); OutputStream out = new FileOutputStream(outfile, false); OpenmrsUtil.copyFile(in, out); // Store the filename in the Obs obs.setComplexData(null); obs.setValueComplex(fileName + "|" + outfile.getName()); // close the stream out.close(); } catch (Exception e) { throw new APIException("Obs.error.writing.binary.data.complex", null, e); } return obs; }
obs.setComplexData(complexData);
@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()); }
@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_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()); }
obsToSave.setComplexData(complexData); os.saveObs(obsToSave, null); ComplexData updatedComplexData = new ComplexData("nameOfUpdatedFile.txt", updatedInput); obsToSave.setComplexData(updatedComplexData); try { os.saveObs(obsToSave, changeMessage);
obsToSave.setComplexData(complexData);
obsToSave.setComplexData(complexData);
obs1.setComplexData(complexData1); obs2.setComplexData(complexData2);
obs1.setComplexData(complexData); obs2.setComplexData(complexData);
obs1.setComplexData(complexData); obs2.setComplexData(complexData);
obs1.setComplexData(complexData); obs2.setComplexData(complexData);
obs1.setComplexData(complexData); obs2.setComplexData(complexData);