Search in sources :

Example 6 with OpenmrsObject

use of org.openmrs.OpenmrsObject in project openmrs-core by openmrs.

the class OpenmrsObjectSaveHandler method handle.

/**
 * This sets the uuid property on the given OpenmrsObject if it is non-null.
 *
 * @see org.openmrs.api.handler.RequiredDataHandler#handle(org.openmrs.OpenmrsObject,
 *      org.openmrs.User, java.util.Date, java.lang.String)
 * @should set empty string properties to null
 * @should not set empty string properties to null for AllowEmptyStrings annotation
 * @should not trim empty strings for AllowLeadingOrTrailingWhitespace annotation
 * @should trim strings without AllowLeadingOrTrailingWhitespace annotation
 * @should trim empty strings for AllowEmptyStrings annotation
 */
@Override
public void handle(OpenmrsObject openmrsObject, User creator, Date dateCreated, String reason) {
    if (openmrsObject.getUuid() == null) {
        openmrsObject.setUuid(UUID.randomUUID().toString());
    }
    // Set all empty string properties, that do not have the AllowEmptyStrings annotation, to null.
    // And also trim leading and trailing white space for properties that do not have the
    // AllowLeadingOrTrailingWhitespace annotation.
    PropertyDescriptor[] properties = PropertyUtils.getPropertyDescriptors(openmrsObject);
    for (PropertyDescriptor property : properties) {
        if (property.getPropertyType() == null) {
            continue;
        }
        // don't have a setter (e.g. Patient.familyName)
        if (property.getWriteMethod() == null || property.getReadMethod() == null) {
            continue;
        }
        // Ignore properties that have a deprecated getter or setter
        if (property.getWriteMethod().getAnnotation(Deprecated.class) != null || property.getReadMethod().getAnnotation(Deprecated.class) != null) {
            continue;
        }
        // TODO We shouldn't be doing this for all immutable types and fields
        if (openmrsObject instanceof Obs || !property.getPropertyType().equals(String.class)) {
            continue;
        }
        try {
            Object value = PropertyUtils.getProperty(openmrsObject, property.getName());
            if (value == null) {
                continue;
            }
            Object valueBeforeTrim = value;
            if (property.getWriteMethod().getAnnotation(AllowLeadingOrTrailingWhitespace.class) == null) {
                value = ((String) value).trim();
                // If we have actually trimmed any space, set the trimmed value.
                if (!valueBeforeTrim.equals(value)) {
                    PropertyUtils.setProperty(openmrsObject, property.getName(), value);
                }
            }
            // Check if user is interested in setting empty strings to null
            if (property.getWriteMethod().getAnnotation(AllowEmptyStrings.class) != null) {
                continue;
            }
            if ("".equals(value) && !(openmrsObject instanceof Voidable && ((Voidable) openmrsObject).getVoided())) {
                // Set to null only if object is not already voided
                PropertyUtils.setProperty(openmrsObject, property.getName(), null);
            }
        } catch (UnsupportedOperationException ex) {
            // there is no need to log this. These should be (mostly) silently skipped over
            if (log.isInfoEnabled()) {
                log.info("The property " + property.getName() + " is no longer supported and should be ignored.", ex);
            }
        } catch (InvocationTargetException ex) {
            if (log.isWarnEnabled()) {
                log.warn("Failed to access property " + property.getName() + "; accessor threw exception.", ex);
            }
        } catch (Exception ex) {
            throw new APIException("failed.change.property.value", new Object[] { property.getName() }, ex);
        }
    }
}
Also used : Obs(org.openmrs.Obs) APIException(org.openmrs.api.APIException) PropertyDescriptor(java.beans.PropertyDescriptor) AllowLeadingOrTrailingWhitespace(org.openmrs.annotation.AllowLeadingOrTrailingWhitespace) OpenmrsObject(org.openmrs.OpenmrsObject) Voidable(org.openmrs.Voidable) InvocationTargetException(java.lang.reflect.InvocationTargetException) APIException(org.openmrs.api.APIException) InvocationTargetException(java.lang.reflect.InvocationTargetException) AllowEmptyStrings(org.openmrs.annotation.AllowEmptyStrings)

Example 7 with OpenmrsObject

use of org.openmrs.OpenmrsObject in project openmrs-core by openmrs.

the class RequiredDataAdviceTest method getChildCollection_shouldGetValueOfGivenChildCollectionOnGivenField.

/**
 * @see RequiredDataAdvice#getChildCollection(OpenmrsObject, Field)
 */
@Test
public void getChildCollection_shouldGetValueOfGivenChildCollectionOnGivenField() throws Exception {
    MiniOpenmrsObject oo = new MiniOpenmrsObject();
    List<Location> locs = new ArrayList<>();
    Location location = new Location(1);
    locs.add(location);
    oo.setLocations(locs);
    Collection<OpenmrsObject> fetchedLocations = RequiredDataAdvice.getChildCollection(oo, MiniOpenmrsObject.class.getDeclaredField("locations"));
    Assert.assertTrue(fetchedLocations.contains(location));
}
Also used : ArrayList(java.util.ArrayList) OpenmrsObject(org.openmrs.OpenmrsObject) BaseOpenmrsObject(org.openmrs.BaseOpenmrsObject) Location(org.openmrs.Location) BaseContextMockTest(org.openmrs.test.BaseContextMockTest) Test(org.junit.Test)

Aggregations

OpenmrsObject (org.openmrs.OpenmrsObject)7 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Collection (java.util.Collection)2 Test (org.junit.Test)2 Location (org.openmrs.Location)2 Voidable (org.openmrs.Voidable)2 APIException (org.openmrs.api.APIException)2 Reflect (org.openmrs.util.Reflect)2 PropertyDescriptor (java.beans.PropertyDescriptor)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 BaseOpenmrsObject (org.openmrs.BaseOpenmrsObject)1 Obs (org.openmrs.Obs)1 Retireable (org.openmrs.Retireable)1 User (org.openmrs.User)1 AllowDirectAccess (org.openmrs.annotation.AllowDirectAccess)1 AllowEmptyStrings (org.openmrs.annotation.AllowEmptyStrings)1 AllowLeadingOrTrailingWhitespace (org.openmrs.annotation.AllowLeadingOrTrailingWhitespace)1