Search in sources :

Example 1 with ComplexData

use of org.openmrs.obs.ComplexData in project openmrs-core by openmrs.

the class BinaryDataHandler method getObs.

/**
 * Currently supports the following views: org.openmrs.obs.ComplexObsHandler#RAW_VIEW
 *
 * @see org.openmrs.obs.ComplexObsHandler#getObs(org.openmrs.Obs, java.lang.String)
 */
@Override
public Obs getObs(Obs obs, String view) {
    File file = getComplexDataFile(obs);
    log.debug("value complex: " + obs.getValueComplex());
    log.debug("file path: " + file.getAbsolutePath());
    ComplexData complexData = null;
    // Raw view (i.e. the file as is)
    if (ComplexObsHandler.RAW_VIEW.equals(view)) {
        // to handle problem with downloading/saving files with blank spaces or commas in their names
        // also need to remove the "file" text appended to the end of the file name
        String[] names = obs.getValueComplex().split("\\|");
        String originalFilename = names[0];
        originalFilename = originalFilename.replaceAll(",", "").replaceAll(" ", "").replaceAll("file$", "");
        try {
            complexData = new ComplexData(originalFilename, OpenmrsUtil.getFileAsBytes(file));
        } catch (IOException e) {
            log.error("Trying to read file: " + file.getAbsolutePath(), e);
        }
    } else {
        // NOTE: if adding support for another view, don't forget to update supportedViews list above
        return null;
    }
    Assert.notNull(complexData, "Complex data must not be null");
    // Get the Mime Type and set it
    String mimeType = OpenmrsUtil.getFileMimeType(file);
    complexData.setMimeType(mimeType);
    obs.setComplexData(complexData);
    return obs;
}
Also used : ComplexData(org.openmrs.obs.ComplexData) IOException(java.io.IOException) File(java.io.File)

Example 2 with ComplexData

use of org.openmrs.obs.ComplexData in project openmrs-core by openmrs.

the class BinaryDataHandler method saveObs.

/**
 * TODO should this support a StringReader too?
 *
 * @see org.openmrs.obs.ComplexObsHandler#saveObs(org.openmrs.Obs)
 */
@Override
public Obs saveObs(Obs obs) throws APIException {
    // Get the buffered file  from the ComplexData.
    ComplexData complexData = obs.getComplexData();
    if (complexData == null) {
        log.error("Cannot save complex data where obsId=" + obs.getObsId() + " because its ComplexData is null.");
        return obs;
    }
    FileOutputStream fout = null;
    try {
        File outfile = getOutputFileToWrite(obs);
        fout = new FileOutputStream(outfile);
        Object data = obs.getComplexData().getData();
        if (data instanceof byte[]) {
            fout.write((byte[]) data);
        } else if (InputStream.class.isAssignableFrom(data.getClass())) {
            try {
                OpenmrsUtil.copyFile((InputStream) data, fout);
            } catch (IOException e) {
                throw new APIException("Obs.error.unable.convert.complex.data", new Object[] { "input stream" }, e);
            }
        }
        // Set the Title and URI for the valueComplex
        obs.setValueComplex(outfile.getName() + " file |" + outfile.getName());
        // Remove the ComplexData from the Obs
        obs.setComplexData(null);
    } catch (IOException ioe) {
        throw new APIException("Obs.error.trying.write.complex", null, ioe);
    } finally {
        try {
            fout.close();
        } catch (Exception e) {
        // pass
        }
    }
    return obs;
}
Also used : APIException(org.openmrs.api.APIException) ComplexData(org.openmrs.obs.ComplexData) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) APIException(org.openmrs.api.APIException) IOException(java.io.IOException)

Example 3 with ComplexData

use of org.openmrs.obs.ComplexData in project openmrs-core by openmrs.

the class AbstractHandler method getObs.

/**
 * @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;
}
Also used : ComplexData(org.openmrs.obs.ComplexData) IOException(java.io.IOException) File(java.io.File)

Example 4 with ComplexData

use of org.openmrs.obs.ComplexData in project openmrs-core by openmrs.

the class TextHandler method getObs.

/**
 * @see org.openmrs.obs.ComplexObsHandler#getObs(org.openmrs.Obs, java.lang.String)
 */
@Override
public Obs getObs(Obs obs, String view) {
    File file = getComplexDataFile(obs);
    log.debug("value complex: " + obs.getValueComplex());
    log.debug("file path: " + file.getAbsolutePath());
    ComplexData complexData = null;
    if (ComplexObsHandler.TEXT_VIEW.equals(view) || ComplexObsHandler.RAW_VIEW.equals(view)) {
        // to handle problem with downloading/saving files with blank spaces or commas in their names
        // also need to remove the "file" text appended to the end of the file name
        String[] names = obs.getValueComplex().split("\\|");
        String originalFilename = names[0];
        originalFilename = originalFilename.replaceAll(",", "").replaceAll(" ", "").replaceAll("file$", "");
        try {
            complexData = ComplexObsHandler.RAW_VIEW.equals(view) ? new ComplexData(originalFilename, OpenmrsUtil.getFileAsBytes(file)) : new ComplexData(originalFilename, OpenmrsUtil.getFileAsString(file));
        } catch (IOException e) {
            log.error("Trying to read file: " + file.getAbsolutePath(), e);
        }
    } else if (ComplexObsHandler.URI_VIEW.equals(view)) {
        complexData = new ComplexData(file.getName(), file.getPath());
    } else {
        // NOTE: if adding support for another view, don't forget to update supportedViews list above
        return null;
    }
    Assert.notNull(complexData, "Complex data must not be null");
    // Get the Mime Type and set it
    String mimeType = OpenmrsUtil.getFileMimeType(file);
    mimeType = !(mimeType.equals("application/octet-stream")) ? mimeType : "text/plain";
    complexData.setMimeType(mimeType);
    obs.setComplexData(complexData);
    return obs;
}
Also used : ComplexData(org.openmrs.obs.ComplexData) IOException(java.io.IOException) File(java.io.File)

Example 5 with ComplexData

use of org.openmrs.obs.ComplexData in project openmrs-core by openmrs.

the class TextHandler method saveObs.

/**
 * @see org.openmrs.obs.ComplexObsHandler#saveObs(org.openmrs.Obs)
 */
@Override
public Obs saveObs(Obs obs) throws APIException {
    ComplexData complexData = obs.getComplexData();
    if (complexData == null) {
        log.error("Cannot save complex data where obsId=" + obs.getObsId() + " because its ComplexData is null.");
        return obs;
    }
    BufferedWriter fout = null;
    try {
        File outfile = getOutputFileToWrite(obs);
        fout = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outfile), StandardCharsets.UTF_8));
        Reader tempRd;
        Object data = obs.getComplexData().getData();
        if (data instanceof char[]) {
            fout.write((char[]) data);
        } else if (Reader.class.isAssignableFrom(data.getClass())) {
            try {
                tempRd = new BufferedReader((Reader) data);
                while (true) {
                    int character = tempRd.read();
                    if (character == -1) {
                        break;
                    }
                    fout.write(character);
                }
                tempRd.close();
            } catch (IOException e) {
                throw new APIException("Obs.error.unable.convert.complex.data", new Object[] { "Reader" }, e);
            }
        } else if (InputStream.class.isAssignableFrom(data.getClass())) {
            try {
                IOUtils.copy((InputStream) data, fout);
            } catch (IOException e) {
                throw new APIException("Obs.error.unable.convert.complex.data", new Object[] { "input stream" }, e);
            }
        }
        // Set the Title and URI for the valueComplex
        obs.setValueComplex(outfile.getName() + " file |" + outfile.getName());
        // Remove the ComplexData from the Obs
        obs.setComplexData(null);
    } catch (IOException ioe) {
        throw new APIException("Obs.error.trying.write.complex", null, ioe);
    } finally {
        try {
            fout.close();
        } catch (Exception e) {
        // pass
        }
    }
    return obs;
}
Also used : APIException(org.openmrs.api.APIException) ComplexData(org.openmrs.obs.ComplexData) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) File(java.io.File) APIException(org.openmrs.api.APIException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

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