use of org.openmrs.obs.ComplexData in project openmrs-core by openmrs.
the class MediaHandler method getObs.
/**
* Currently supports all views and puts the media file data into the ComplexData object
*
* @see org.openmrs.obs.ComplexObsHandler#getObs(org.openmrs.Obs, java.lang.String)
*/
@Override
public Obs getObs(Obs obs, String view) {
File file = getComplexDataFile(obs);
// Raw media
if (ComplexObsHandler.RAW_VIEW.equals(view)) {
try {
String[] names = obs.getValueComplex().split("\\|");
String originalFilename = names[0];
originalFilename = originalFilename.replace(",", "").replace(" ", "");
FileInputStream mediaStream = new FileInputStream(file);
ComplexData complexData = new ComplexData(originalFilename, mediaStream);
// Get the Mime Type and set it
String mimeType = OpenmrsUtil.getFileMimeType(file);
complexData.setMimeType(mimeType);
complexData.setLength(file.length());
obs.setComplexData(complexData);
} catch (FileNotFoundException e) {
log.error("Trying to create media file stream from " + file.getAbsolutePath(), e);
}
} else // No other view supported
// NOTE: if adding support for another view, don't forget to update supportedViews list above
{
return null;
}
return obs;
}
use of org.openmrs.obs.ComplexData in project openmrs-core by openmrs.
the class ObsServiceTest method saveObs_shouldDeleteThePreviousFileWhenAComplexObservationIsUpdatedWithANewComplexValue.
/**
* @see ObsService#saveObs(Obs,String)
*/
@Test
public void saveObs_shouldDeleteThePreviousFileWhenAComplexObservationIsUpdatedWithANewComplexValue() {
String changeMessage = "Testing TRUNK-4538";
executeDataSet(COMPLEX_OBS_XML);
ObsService os = Context.getObsService();
ConceptService cs = Context.getConceptService();
AdministrationService as = Context.getAdministrationService();
// make sure the file isn't there to begin with
File complexObsDir = OpenmrsUtil.getDirectoryInApplicationDataDirectory(as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_COMPLEX_OBS_DIR));
final File createdFile = new File(complexObsDir, "nameOfFile.txt");
if (createdFile.exists())
createdFile.delete();
// the complex data to put onto an obs that will be saved
Reader input = new CharArrayReader("This is a string to save to a file".toCharArray());
ComplexData complexData = new ComplexData("nameOfFile.txt", input);
// must fetch the concept instead of just new Concept(8473) because the attributes on concept are checked
// this is a concept mapped to the text handler
Concept questionConcept = cs.getConcept(8474);
Obs obsToSave = new Obs(new Person(1), questionConcept, new Date(), new Location(1));
obsToSave.setComplexData(complexData);
os.saveObs(obsToSave, null);
File updatedFile = new File(complexObsDir, "nameOfUpdatedFile.txt");
if (updatedFile.exists())
updatedFile.delete();
// the complex data to put onto an obs that will be updated
Reader updatedInput = new CharArrayReader("This is a string to save to a file which uploaded to update an obs".toCharArray());
ComplexData updatedComplexData = new ComplexData("nameOfUpdatedFile.txt", updatedInput);
obsToSave.setComplexData(updatedComplexData);
try {
os.saveObs(obsToSave, changeMessage);
Assert.assertFalse(createdFile.exists());
} finally {
// we always have to delete this inside the same unit test because it is outside the
// database and hence can't be "rolled back" like everything else
updatedFile.delete();
}
}
use of org.openmrs.obs.ComplexData in project openmrs-core by openmrs.
the class ObsServiceTest method saveObs_shouldNotOverwriteFileWhenUpdatingAComplexObs.
/**
* @throws IOException
* @see ObsService#saveObs(Obs,String)
*/
@Test
public void saveObs_shouldNotOverwriteFileWhenUpdatingAComplexObs() throws IOException {
executeDataSet(COMPLEX_OBS_XML);
ObsService os = Context.getObsService();
ConceptService cs = Context.getConceptService();
AdministrationService as = Context.getAdministrationService();
// Create the file that was supposedly put there by another obs
File complexObsDir = OpenmrsUtil.getDirectoryInApplicationDataDirectory(as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_COMPLEX_OBS_DIR));
File previouslyCreatedFile = new File(complexObsDir, "nameOfFile.txt");
FileUtils.writeByteArrayToFile(previouslyCreatedFile, "a string to save to a file".getBytes());
// the file we'll be creating...defining it here so we can delete it in a finally block
File newComplexFile = null;
try {
long oldFileSize = previouslyCreatedFile.length();
// now add a new file to this obs and update it
// ...then make sure the original file is still there
// the complex data to put onto an obs that will be saved
Reader input2 = new CharArrayReader("diff string to save to a file with the same name".toCharArray());
ComplexData complexData = new ComplexData("nameOfFile.txt", input2);
// must fetch the concept instead of just new Concept(8473) because the attributes on concept are checked
// this is a concept mapped to the text handler
Concept questionConcept = cs.getConcept(8474);
Obs obsToSave = new Obs(new Person(1), questionConcept, new Date(), new Location(1));
obsToSave.setComplexData(complexData);
os.saveObs(obsToSave, null);
// make sure the old file still appears now after the save
Assert.assertEquals(oldFileSize, previouslyCreatedFile.length());
String valueComplex = obsToSave.getValueComplex();
String filename = valueComplex.substring(valueComplex.indexOf("|") + 1).trim();
newComplexFile = new File(complexObsDir, filename);
// make sure the file appears now after the save
Assert.assertTrue(newComplexFile.length() > oldFileSize);
} finally {
// clean up the files we created
newComplexFile.delete();
try {
previouslyCreatedFile.delete();
} catch (Exception e) {
// pass
}
}
}
use of org.openmrs.obs.ComplexData in project openmrs-core by openmrs.
the class ObsTest method generateValue.
private Object generateValue(Field field, boolean setAlternateValue) throws Exception {
Object fieldValue;
if (field.getType().equals(Boolean.class)) {
fieldValue = setAlternateValue;
} else if (field.getType().equals(Integer.class)) {
fieldValue = setAlternateValue ? 10 : 17;
} else if (field.getType().equals(Double.class)) {
fieldValue = setAlternateValue ? 5.0 : 7.0;
} else if (field.getType().equals(Date.class)) {
fieldValue = new Date();
if (setAlternateValue) {
Calendar c = Calendar.getInstance();
c.add(Calendar.MINUTE, 2);
fieldValue = c.getTime();
}
} else if (field.getType().equals(String.class)) {
fieldValue = setAlternateValue ? "old" : "new";
} else if (field.getType().equals(Person.class)) {
// setPerson updates the personId, so we want the personIds to match for the tests to be valid
fieldValue = new Person(setAlternateValue ? 10 : 17);
} else if (field.getType().equals(ComplexData.class)) {
fieldValue = new ComplexData(setAlternateValue ? "some complex data" : "Some other value", new Object());
} else if (field.getType().equals(Obs.Interpretation.class)) {
fieldValue = setAlternateValue ? Obs.Interpretation.ABNORMAL : Obs.Interpretation.CRITICALLY_ABNORMAL;
} else if (field.getType().equals(Obs.Status.class)) {
fieldValue = setAlternateValue ? Obs.Status.AMENDED : Obs.Status.PRELIMINARY;
} else {
fieldValue = field.getType().newInstance();
}
assertNotNull("Failed to generate a value for field: Obs." + field.getName());
return fieldValue;
}
Aggregations