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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations