Search in sources :

Example 11 with TypeDto

use of org.motechproject.mds.dto.TypeDto 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)

Example 12 with TypeDto

use of org.motechproject.mds.dto.TypeDto in project motech by motech.

the class InstanceServiceImpl method setRelationProperty.

private void setRelationProperty(Object instance, FieldRecord fieldRecord) throws NoSuchMethodException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException, CannotCompileException {
    String fieldName = fieldRecord.getName();
    String methodName = MemberUtil.getSetterName(fieldName);
    Class<?> clazz = instance.getClass().getClassLoader().loadClass(instance.getClass().getName());
    Field field = FieldUtils.getField(clazz, fieldName, true);
    Class<?> parameterType = field.getType();
    Object value = null;
    MotechDataService serviceForRelatedClass = null;
    TypeDto type = getType(fieldRecord);
    if (StringUtils.isNotEmpty(ObjectUtils.toString(fieldRecord.getValue()))) {
        Class<?> argumentType = null;
        if (type.equals(TypeDto.ONE_TO_MANY_RELATIONSHIP) || type.equals(TypeDto.MANY_TO_MANY_RELATIONSHIP)) {
            argumentType = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
        } else if (type.equals(TypeDto.MANY_TO_ONE_RELATIONSHIP) || type.equals(TypeDto.ONE_TO_ONE_RELATIONSHIP)) {
            argumentType = parameterType;
        }
        serviceForRelatedClass = DataServiceHelper.getDataService(bundleContext, argumentType.getName());
        Object related = PropertyUtil.safeGetProperty(instance, fieldName);
        value = buildRelatedInstances(serviceForRelatedClass, parameterType, argumentType, fieldRecord.getValue(), related);
    }
    Method method = MethodUtils.getAccessibleMethod(instance.getClass(), methodName, parameterType);
    invokeMethod(method, instance, value, methodName, fieldName);
}
Also used : Field(java.lang.reflect.Field) TypeDto(org.motechproject.mds.dto.TypeDto) Method(java.lang.reflect.Method) MotechDataService(org.motechproject.mds.service.MotechDataService)

Example 13 with TypeDto

use of org.motechproject.mds.dto.TypeDto in project motech by motech.

the class AvailableController method getTypes.

@RequestMapping(value = "/available/types", method = RequestMethod.GET)
@ResponseBody
public SelectResult<TypeDto> getTypes(SelectData data) {
    List<TypeDto> list = typeService.getAllTypes();
    // The Long and Date types are available for DDEs exclusively
    list.remove(typeService.findType(Long.class));
    list.remove(typeService.findType(Date.class));
    list.remove(typeService.findType(Relationship.class));
    // The DateTime and LocalDate types from Joda are available for DDEs exclusively
    list.remove(typeService.findType(DateTime.class));
    list.remove(typeService.findType(org.joda.time.LocalDate.class));
    // TextArea type is available only from UI
    list.add(textAreaUIType());
    CollectionUtils.filter(list, new TypeMatcher(data.getTerm(), messageSource));
    Collections.sort(list, new TypeDisplayNameComparator(messageSource));
    return new SelectResult<>(data, list);
}
Also used : SelectResult(org.motechproject.mds.web.SelectResult) Relationship(org.motechproject.mds.domain.Relationship) TypeMatcher(org.motechproject.mds.web.matcher.TypeMatcher) TypeDisplayNameComparator(org.motechproject.mds.web.comparator.TypeDisplayNameComparator) TypeDto(org.motechproject.mds.dto.TypeDto) Date(java.util.Date) DateTime(org.joda.time.DateTime) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 14 with TypeDto

use of org.motechproject.mds.dto.TypeDto in project motech by motech.

the class FieldTestHelper method fieldDto.

public static FieldDto fieldDto(Long id, String name, String className, String displayName, Object defValue) {
    FieldDto fieldDto = new FieldDto();
    fieldDto.setType(new TypeDto(className, "", "", className));
    fieldDto.setBasic(new FieldBasicDto(displayName, name));
    fieldDto.getBasic().setDefaultValue(defValue);
    fieldDto.setId(id);
    return fieldDto;
}
Also used : FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) TypeDto(org.motechproject.mds.dto.TypeDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 15 with TypeDto

use of org.motechproject.mds.dto.TypeDto in project motech by motech.

the class TypeMatcherTest method setUp.

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    for (TypeDto type : TYPES) {
        String displayName = type.getDisplayName();
        String name = displayName.substring(displayName.lastIndexOf('.') + 1);
        when(messageSource.getMessage(displayName, null, null)).thenReturn(name);
    }
}
Also used : TypeDto(org.motechproject.mds.dto.TypeDto) Before(org.junit.Before)

Aggregations

TypeDto (org.motechproject.mds.dto.TypeDto)30 FieldDto (org.motechproject.mds.dto.FieldDto)17 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)9 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)9 EntityDto (org.motechproject.mds.dto.EntityDto)7 MetadataDto (org.motechproject.mds.dto.MetadataDto)7 ComboboxHolder (org.motechproject.mds.domain.ComboboxHolder)6 CtClass (javassist.CtClass)3 FieldValidationDto (org.motechproject.mds.dto.FieldValidationDto)3 LookupDto (org.motechproject.mds.dto.LookupDto)3 IOException (java.io.IOException)2 AccessibleObject (java.lang.reflect.AccessibleObject)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 CannotCompileException (javassist.CannotCompileException)2 Before (org.junit.Before)2 Test (org.junit.Test)2 ClassData (org.motechproject.mds.domain.ClassData)2 Field (org.motechproject.mds.domain.Field)2