Search in sources :

Example 11 with ComplexData

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;
}
Also used : ComplexData(org.openmrs.obs.ComplexData) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 12 with ComplexData

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();
    }
}
Also used : Concept(org.openmrs.Concept) Obs(org.openmrs.Obs) CharArrayReader(java.io.CharArrayReader) Reader(java.io.Reader) Date(java.util.Date) CharArrayReader(java.io.CharArrayReader) ComplexData(org.openmrs.obs.ComplexData) File(java.io.File) Person(org.openmrs.Person) Location(org.openmrs.Location) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Example 13 with ComplexData

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
        }
    }
}
Also used : Concept(org.openmrs.Concept) Obs(org.openmrs.Obs) CharArrayReader(java.io.CharArrayReader) Reader(java.io.Reader) Date(java.util.Date) ParseException(java.text.ParseException) ExpectedException(org.junit.rules.ExpectedException) IOException(java.io.IOException) CharArrayReader(java.io.CharArrayReader) ComplexData(org.openmrs.obs.ComplexData) File(java.io.File) Person(org.openmrs.Person) Location(org.openmrs.Location) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Example 14 with ComplexData

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;
}
Also used : ComplexData(org.openmrs.obs.ComplexData) Calendar(java.util.Calendar) Date(java.util.Date)

Aggregations

ComplexData (org.openmrs.obs.ComplexData)14 File (java.io.File)11 IOException (java.io.IOException)7 Date (java.util.Date)5 Concept (org.openmrs.Concept)5 Reader (java.io.Reader)4 Obs (org.openmrs.Obs)4 APIException (org.openmrs.api.APIException)4 CharArrayReader (java.io.CharArrayReader)3 Test (org.junit.Test)3 Location (org.openmrs.Location)3 Person (org.openmrs.Person)3 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)3 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 FileOutputStream (java.io.FileOutputStream)2 HL7Exception (ca.uhn.hl7v2.HL7Exception)1 Type (ca.uhn.hl7v2.model.Type)1 Varies (ca.uhn.hl7v2.model.Varies)1 CE (ca.uhn.hl7v2.model.v25.datatype.CE)1