Search in sources :

Example 1 with ComboboxHolder

use of org.motechproject.mds.web.domain.ComboboxHolder in project motech by motech.

the class InstanceServiceImpl method setProperty.

private void setProperty(Object instance, FieldRecord fieldRecord, MotechDataService service, Long deleteValueFieldId, boolean retainId) throws NoSuchMethodException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
    String fieldName = fieldRecord.getName();
    TypeDto type = getType(fieldRecord);
    String methodName = "set" + StringUtils.capitalize(fieldName);
    ComboboxHolder holder = type.isCombobox() ? new ComboboxHolder(instance, fieldRecord) : null;
    String methodParameterType = getMethodParameterType(type, holder);
    ClassLoader classLoader = instance.getClass().getClassLoader();
    Class<?> parameterType;
    Object parsedValue;
    if (Byte[].class.getName().equals(methodParameterType) || byte[].class.getName().equals(methodParameterType)) {
        parameterType = getCorrectByteArrayType(methodParameterType);
        parsedValue = parseBlobValue(fieldRecord, service, fieldName, deleteValueFieldId, instance);
    } else {
        parameterType = classLoader.loadClass(methodParameterType);
        parsedValue = parseValue(holder, methodParameterType, fieldRecord, classLoader);
    }
    MetadataDto versionMetadata = fieldRecord.getMetadata(Constants.MetadataKeys.VERSION_FIELD);
    validateNonEditableField(fieldRecord, instance, parsedValue, versionMetadata);
    Method method = MethodUtils.getAccessibleMethod(instance.getClass(), methodName, parameterType);
    if (method == null && TypeHelper.hasPrimitive(parameterType)) {
        method = MethodUtils.getAccessibleMethod(instance.getClass(), methodName, TypeHelper.getPrimitive(parameterType));
        // if the setter is for a primitive, but we have a null, we leave the default
        if (method != null && parsedValue == null) {
            return;
        }
    }
    invokeMethod(method, instance, parsedValue, methodName, fieldName);
    setTransactionVersion(instance, fieldRecord, retainId, versionMetadata);
}
Also used : ComboboxHolder(org.motechproject.mds.web.domain.ComboboxHolder) MDSClassLoader(org.motechproject.mds.util.MDSClassLoader) TypeDto(org.motechproject.mds.dto.TypeDto) Method(java.lang.reflect.Method) MetadataDto(org.motechproject.mds.dto.MetadataDto)

Aggregations

Method (java.lang.reflect.Method)1 MetadataDto (org.motechproject.mds.dto.MetadataDto)1 TypeDto (org.motechproject.mds.dto.TypeDto)1 MDSClassLoader (org.motechproject.mds.util.MDSClassLoader)1 ComboboxHolder (org.motechproject.mds.web.domain.ComboboxHolder)1