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);
}
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);
}
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);
}
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;
}
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);
}
}
Aggregations