use of org.openmrs.api.APIException in project openmrs-core by openmrs.
the class HL7ServiceImpl method loadHL7InArchiveData.
/**
* @see org.openmrs.hl7.HL7Service#loadHL7InArchiveData(HL7InArchive)
*/
@Override
public void loadHL7InArchiveData(HL7InArchive archive) throws APIException {
// quit early if there is no archive to work with
if (archive == null) {
return;
}
// quit early if the message is not migrated or already loaded
if (!OpenmrsUtil.nullSafeEquals(archive.getMessageState(), HL7Constants.HL7_STATUS_MIGRATED) || archive.isLoaded()) {
return;
}
try {
archive.setHL7Data(OpenmrsUtil.getFileAsString(new File(new URI(archive.getHL7Data()))));
archive.setLoaded(true);
} catch (URISyntaxException e) {
throw new APIException("Hl7Service.malformed.archive.location", new Object[] { archive.getHL7Data() }, e);
} catch (IOException e) {
throw new APIException("Hl7Service.unable.convert.archive", new Object[] { archive.getHL7Data() }, e);
}
}
use of org.openmrs.api.APIException in project openmrs-core by openmrs.
the class PersonServiceImpl method retirePersonAttributeType.
/**
* @see org.openmrs.api.PersonService#retirePersonAttributeType(PersonAttributeType, String)
*/
@Override
public PersonAttributeType retirePersonAttributeType(PersonAttributeType type, String retiredReason) throws APIException {
checkIfPersonAttributeTypesAreLocked();
if (retiredReason == null || retiredReason.length() < 1) {
throw new APIException("Person.retiring.reason.required", (Object[]) null);
}
type.setRetired(true);
type.setRetiredBy(Context.getAuthenticatedUser());
type.setRetireReason(retiredReason);
type.setDateRetired(new Date());
return dao.savePersonAttributeType(type);
}
use of org.openmrs.api.APIException in project openmrs-core by openmrs.
the class UserServiceImpl method checkPrivileges.
/**
* Convenience method to check if the authenticated user has all privileges they are giving out
*
* @param new user that has privileges
*/
private void checkPrivileges(User user) {
Collection<Role> roles = user.getAllRoles();
List<String> requiredPrivs = new ArrayList<>();
for (Role r : roles) {
if (r.getRole().equals(RoleConstants.SUPERUSER) && !Context.hasPrivilege(PrivilegeConstants.ASSIGN_SYSTEM_DEVELOPER_ROLE)) {
throw new APIException("User.you.must.have.role", new Object[] { RoleConstants.SUPERUSER });
}
if (r.getPrivileges() != null) {
for (Privilege p : r.getPrivileges()) {
if (!Context.hasPrivilege(p.getPrivilege())) {
requiredPrivs.add(p.getPrivilege());
}
}
}
}
if (requiredPrivs.size() == 1) {
throw new APIException("User.you.must.have.privilege", new Object[] { requiredPrivs.get(0) });
} else if (requiredPrivs.size() > 1) {
StringBuilder txt = new StringBuilder("You must have the following privileges in order to assign them: ");
for (String s : requiredPrivs) {
txt.append(s).append(", ");
}
throw new APIException(txt.substring(0, txt.length() - 2));
}
}
use of org.openmrs.api.APIException in project openmrs-core by openmrs.
the class CustomDatatypeUtil method getValueReferences.
/**
* Uses the appropriate datatypes to convert all values in the input map to their valueReference equivalents.
* This is a convenience method for calling XyzService.getXyz(..., attributeValues, ...).
*
* @param datatypeValues
* @return a map similar to the input parameter, but with typed values converted to their reference equivalents
*/
public static <T extends AttributeType<?>, U> Map<T, String> getValueReferences(Map<T, U> datatypeValues) {
Map<T, String> serializedAttributeValues = null;
if (datatypeValues != null) {
serializedAttributeValues = new HashMap<>();
for (Map.Entry<T, U> e : datatypeValues.entrySet()) {
T vat = e.getKey();
CustomDatatype<U> customDatatype = (CustomDatatype<U>) getDatatype(vat);
String valueReference;
try {
valueReference = customDatatype.getReferenceStringForValue(e.getValue());
} catch (UnsupportedOperationException ex) {
throw new APIException("CustomDatatype.error.cannot.search", new Object[] { customDatatype.getClass() });
}
serializedAttributeValues.put(vat, valueReference);
}
}
return serializedAttributeValues;
}
use of org.openmrs.api.APIException in project openmrs-core by openmrs.
the class DaemonTest method executeScheduledTask_shouldNotBeCalledFromOtherMethodsOtherThanTimerSchedulerTask.
/**
* @see Daemon#executeScheduledTask(Task)
*/
@Test
public void executeScheduledTask_shouldNotBeCalledFromOtherMethodsOtherThanTimerSchedulerTask() throws Throwable {
try {
Daemon.executeScheduledTask(new HelloWorldTask());
Assert.fail("Should not be here, an exception should have been thrown in the line above");
} catch (APIException e) {
Assert.assertTrue(e.getMessage().startsWith(Context.getMessageSourceService().getMessage("Scheduler.timer.task.only", new Object[] { this.getClass().getName() }, null)));
}
}
Aggregations