use of org.openmrs.api.APIException in project openmrs-module-pihcore by PIH.
the class AssignDossierNumber method afterPatientCreated.
@Override
public synchronized void afterPatientCreated(Patient patient, Map<String, String[]> map) {
PatientIdentifierType dossierIdentifierType = MetadataUtils.existing(PatientIdentifierType.class, PihHaitiPatientIdentifierTypes.DOSSIER_NUMBER.uuid());
Location medicalRecordLocation = getMedicalRecordLocation();
String dossierId = "";
dossierId = identifierSourceService.generateIdentifier(dossierIdentifierType, medicalRecordLocation, "generating a new dossier number");
// double check to make sure this identifier is not in use--since manual entry is allowed, it could be
while (dossierIdentifierInUse(dossierId, dossierIdentifierType, medicalRecordLocation)) {
log.error("Attempted to generate duplicate dossier identifier " + dossierId);
dossierId = identifierSourceService.generateIdentifier(dossierIdentifierType, medicalRecordLocation, "generating a new dossier number");
}
if (StringUtils.isBlank(dossierId)) {
throw new APIException("Unable to generate dossier number for patient " + patient);
}
PatientIdentifier dossierIdentifier = new PatientIdentifier(dossierId, dossierIdentifierType, medicalRecordLocation);
patient.addIdentifier(dossierIdentifier);
patientService.savePatientIdentifier(dossierIdentifier);
}
use of org.openmrs.api.APIException in project openmrs-module-pihcore by PIH.
the class ActiveVisitsAjaxController method toJson.
public static String toJson(Object obj) {
ObjectMapper mapper = new ObjectMapper();
StringWriter sw = new StringWriter();
try {
mapper.writeValue(sw, obj);
} catch (Exception e) {
throw new APIException("Error converting to JSON", e);
}
return sw.toString();
}
use of org.openmrs.api.APIException 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.api.APIException in project openmrs-core by openmrs.
the class ImageHandler method saveObs.
/**
* @see org.openmrs.obs.ComplexObsHandler#saveObs(org.openmrs.Obs)
*/
@Override
public Obs saveObs(Obs obs) throws APIException {
// Get the buffered image from the ComplexData.
BufferedImage img = null;
Object data = obs.getComplexData().getData();
if (data instanceof BufferedImage) {
img = (BufferedImage) obs.getComplexData().getData();
} else if (data instanceof InputStream) {
try {
img = ImageIO.read((InputStream) data);
if (img == null) {
throw new IllegalArgumentException("Invalid image file");
}
} catch (IOException e) {
throw new APIException("Obs.error.unable.convert.complex.data", new Object[] { "input stream" }, e);
}
}
if (img == null) {
throw new APIException("Obs.error.cannot.save.complex", new Object[] { obs.getObsId() });
}
File outfile = null;
try {
outfile = getOutputFileToWrite(obs);
String extension = getExtension(obs.getComplexData().getTitle());
// TODO: Check this extension against the registered extensions for validity
// Write the file to the file system.
ImageIO.write(img, extension, outfile);
// Set the Title and URI for the valueComplex
obs.setValueComplex(extension + " image |" + outfile.getName());
// Remove the ComlexData from the Obs
obs.setComplexData(null);
} catch (IOException ioe) {
if (outfile != null && outfile.length() == 0) {
// OpenJDK 7 & 8 may leave a 0-byte file when ImageIO.write(..) fails.
outfile.delete();
}
throw new APIException("Obs.error.trying.write.complex", null, ioe);
}
return obs;
}
use of org.openmrs.api.APIException in project openmrs-core by openmrs.
the class BinaryStreamHandler method saveObs.
/**
* @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;
}
Aggregations