Search in sources :

Example 1 with ActionHandlerException

use of org.motechproject.mds.exception.action.ActionHandlerException in project motech by motech.

the class ActionHandlerServiceImpl method setPlainInstanceProperty.

private void setPlainInstanceProperty(Object instance, Field field, Object value) throws ActionHandlerException {
    try {
        Class<?> propertyType = PropertyUtil.getPropertyType(instance, field.getName());
        Object parsedValue = TypeHelper.parse(value, propertyType);
        PropertyUtil.setProperty(instance, field.getName(), parsedValue);
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        throw new ActionHandlerException("Cannot set instance property " + field.getName() + " with value " + value, e);
    }
}
Also used : ActionHandlerException(org.motechproject.mds.exception.action.ActionHandlerException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with ActionHandlerException

use of org.motechproject.mds.exception.action.ActionHandlerException in project motech by motech.

the class ActionHandlerServiceImpl method setRelationshipInstanceProperty.

private void setRelationshipInstanceProperty(Object instance, Field field, Object value) throws ActionHandlerException {
    RelationshipHolder relationshipHolder = new RelationshipHolder(field);
    try {
        if (null != value) {
            String relatedClassName = relationshipHolder.getRelatedClass();
            MotechDataService relatedClassDataService = getEntityDataService(relatedClassName);
            if (relationshipHolder.isManyToOne() || relationshipHolder.isOneToOne()) {
                Object relatedInstance = getRelatedInstance(relatedClassDataService, value);
                PropertyUtil.safeSetProperty(instance, field.getName(), relatedInstance);
            } else if (relationshipHolder.isManyToMany() || relationshipHolder.isOneToMany()) {
                List<Object> relatedInstances = getRelatedInstances(relatedClassDataService, value);
                PropertyUtil.safeSetCollectionProperty(instance, field.getName(), relatedInstances);
            }
        } else {
            PropertyUtil.safeSetProperty(instance, field.getName(), null);
        }
    } catch (RuntimeException e) {
        throw new ActionHandlerException("Cannot set instance property " + field.getName() + " with value " + value, e);
    }
}
Also used : ActionHandlerException(org.motechproject.mds.exception.action.ActionHandlerException) RelationshipHolder(org.motechproject.mds.domain.RelationshipHolder) ArrayList(java.util.ArrayList) List(java.util.List) MotechDataService(org.motechproject.mds.service.MotechDataService)

Aggregations

ActionHandlerException (org.motechproject.mds.exception.action.ActionHandlerException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 RelationshipHolder (org.motechproject.mds.domain.RelationshipHolder)1 MotechDataService (org.motechproject.mds.service.MotechDataService)1