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