Search in sources :

Example 31 with APIException

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);
    }
}
Also used : APIException(org.openmrs.api.APIException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) File(java.io.File) URI(java.net.URI)

Example 32 with APIException

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);
}
Also used : APIException(org.openmrs.api.APIException) Date(java.util.Date)

Example 33 with APIException

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));
    }
}
Also used : Role(org.openmrs.Role) APIException(org.openmrs.api.APIException) ArrayList(java.util.ArrayList) Privilege(org.openmrs.Privilege)

Example 34 with APIException

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;
}
Also used : APIException(org.openmrs.api.APIException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 35 with APIException

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)));
    }
}
Also used : HelloWorldTask(org.openmrs.scheduler.tasks.HelloWorldTask) APIException(org.openmrs.api.APIException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

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