Search in sources :

Example 1 with APIException

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);
}
Also used : APIException(org.openmrs.api.APIException) PatientIdentifierType(org.openmrs.PatientIdentifierType) PatientIdentifier(org.openmrs.PatientIdentifier) Location(org.openmrs.Location)

Example 2 with APIException

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();
}
Also used : APIException(org.openmrs.api.APIException) StringWriter(java.io.StringWriter) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) APIException(org.openmrs.api.APIException) EvaluationException(org.openmrs.module.reporting.evaluation.EvaluationException) IOException(java.io.IOException)

Example 3 with APIException

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;
}
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 4 with APIException

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;
}
Also used : APIException(org.openmrs.api.APIException) FileImageInputStream(javax.imageio.stream.FileImageInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

Example 5 with APIException

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;
}
Also used : APIException(org.openmrs.api.APIException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) APIException(org.openmrs.api.APIException)

Aggregations

APIException (org.openmrs.api.APIException)84 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)10 Date (java.util.Date)10 File (java.io.File)9 Obs (org.openmrs.Obs)7 List (java.util.List)6 Map (java.util.Map)6 FileInputStream (java.io.FileInputStream)5 FileOutputStream (java.io.FileOutputStream)5 Concept (org.openmrs.Concept)5 OpenmrsObject (org.openmrs.OpenmrsObject)5 User (org.openmrs.User)5 InputStream (java.io.InputStream)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 Order (org.openmrs.Order)4 FileNotFoundException (java.io.FileNotFoundException)3 OutputStreamWriter (java.io.OutputStreamWriter)3 MessageDigest (java.security.MessageDigest)3 HashMap (java.util.HashMap)3