Search in sources :

Example 6 with APIException

use of org.openmrs.api.APIException 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)

Example 7 with APIException

use of org.openmrs.api.APIException in project openmrs-core by openmrs.

the class AttributeIntegrationTest method shouldTestAddingAnAttributeToSomethingAndSavingIt.

@Test
public void shouldTestAddingAnAttributeToSomethingAndSavingIt() throws InvalidCustomValueException, ParseException {
    Visit visit = service.getVisit(1);
    VisitAttributeType auditDate = service.getVisitAttributeType(1);
    VisitAttribute legalDate = new VisitAttribute();
    legalDate.setAttributeType(auditDate);
    // try using a subclass of java.util.Date, to make sure the handler can take subclasses.
    legalDate.setValue(new java.sql.Date(new SimpleDateFormat("yyyy-MM-dd").parse("2011-04-15").getTime()));
    visit.addAttribute(legalDate);
    service.saveVisit(visit);
    // saving the visit should have caused the date to be validated and saved
    Assert.assertNotNull(legalDate.getValueReference());
    Assert.assertEquals("2011-04-15", legalDate.getValueReference());
    VisitAttribute badDate = new VisitAttribute();
    badDate.setAttributeType(auditDate);
    // no value
    visit.addAttribute(badDate);
    try {
        service.saveVisit(visit);
        Assert.fail("Should have failed because of bad date attribute");
    } catch (APIException ex) {
    // expected this
    }
}
Also used : APIException(org.openmrs.api.APIException) Visit(org.openmrs.Visit) VisitAttributeType(org.openmrs.VisitAttributeType) VisitAttribute(org.openmrs.VisitAttribute) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 8 with APIException

use of org.openmrs.api.APIException in project openmrs-core by openmrs.

the class HibernateOrderDAO method getNextOrderNumberSeedSequenceValue.

/**
 * @see org.openmrs.api.db.OrderDAO#getNextOrderNumberSeedSequenceValue()
 */
@Override
public Long getNextOrderNumberSeedSequenceValue() {
    GlobalProperty globalProperty = (GlobalProperty) sessionFactory.getCurrentSession().get(GlobalProperty.class, OpenmrsConstants.GP_NEXT_ORDER_NUMBER_SEED, LockOptions.UPGRADE);
    if (globalProperty == null) {
        throw new APIException("GlobalProperty.missing", new Object[] { OpenmrsConstants.GP_NEXT_ORDER_NUMBER_SEED });
    }
    String gpTextValue = globalProperty.getPropertyValue();
    if (StringUtils.isBlank(gpTextValue)) {
        throw new APIException("GlobalProperty.invalid.value", new Object[] { OpenmrsConstants.GP_NEXT_ORDER_NUMBER_SEED });
    }
    Long gpNumericValue;
    try {
        gpNumericValue = Long.parseLong(gpTextValue);
    } catch (NumberFormatException ex) {
        throw new APIException("GlobalProperty.invalid.value", new Object[] { OpenmrsConstants.GP_NEXT_ORDER_NUMBER_SEED });
    }
    globalProperty.setPropertyValue(String.valueOf(gpNumericValue + 1));
    sessionFactory.getCurrentSession().save(globalProperty);
    return gpNumericValue;
}
Also used : APIException(org.openmrs.api.APIException) GlobalProperty(org.openmrs.GlobalProperty)

Example 9 with APIException

use of org.openmrs.api.APIException in project openmrs-core by openmrs.

the class HibernateConceptDAO method getConceptReferenceTermByCode.

/**
 * @see org.openmrs.api.db.ConceptDAO#getConceptReferenceTermByCode(java.lang.String,
 *      org.openmrs.ConceptSource)
 */
@SuppressWarnings("rawtypes")
@Override
public ConceptReferenceTerm getConceptReferenceTermByCode(String code, ConceptSource conceptSource) throws DAOException {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(ConceptReferenceTerm.class);
    criteria.add(Restrictions.eq("code", code));
    criteria.add(Restrictions.eq("conceptSource", conceptSource));
    List terms = criteria.list();
    if (terms.isEmpty()) {
        return null;
    } else if (terms.size() > 1) {
        throw new APIException("ConceptReferenceTerm.foundMultipleTermsWithCodeInSource", new Object[] { code, conceptSource.getName() });
    }
    return (ConceptReferenceTerm) terms.get(0);
}
Also used : APIException(org.openmrs.api.APIException) List(java.util.List) ArrayList(java.util.ArrayList) OpenmrsObject(org.openmrs.OpenmrsObject) Criteria(org.hibernate.Criteria) ConceptReferenceTerm(org.openmrs.ConceptReferenceTerm)

Example 10 with APIException

use of org.openmrs.api.APIException in project openmrs-core by openmrs.

the class Context method shutdown.

/**
 * Stops the OpenMRS System Should be called after all activity has ended and application is
 * closing
 */
public static void shutdown() {
    log.debug("Shutting down the scheduler");
    try {
        // Needs to be shutdown before Hibernate
        SchedulerUtil.shutdown();
    } catch (Exception e) {
        log.warn("Error while shutting down scheduler service", e);
    }
    log.debug("Shutting down the modules");
    try {
        ModuleUtil.shutdown();
    } catch (Exception e) {
        log.warn("Error while shutting down module system", e);
    }
    log.debug("Shutting down the context");
    try {
        ContextDAO dao = null;
        try {
            dao = getContextDAO();
        } catch (APIException e) {
        // pass
        }
        if (dao != null) {
            dao.shutdown();
        }
    } catch (Exception e) {
        log.warn("Error while shutting down context dao", e);
    }
}
Also used : ContextDAO(org.openmrs.api.db.ContextDAO) APIException(org.openmrs.api.APIException) DatabaseUpdateException(org.openmrs.util.DatabaseUpdateException) InputRequiredException(org.openmrs.util.InputRequiredException) APIException(org.openmrs.api.APIException) ModuleMustStartException(org.openmrs.module.ModuleMustStartException) MessageException(org.openmrs.notification.MessageException)

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