@Override @Transactional public <T> Records<T> getRelatedFieldValue( Long entityId, Long instanceId, String fieldName, QueryParams queryParams) { EntityDto entity = getEntity(entityId); validateCredentials(entity); String entityName = entity.getName(); MotechDataService service = getServiceForEntity(entity); Object instance = service.findById(instanceId); if (instance == null) { throw new ObjectNotFoundException(entityName, instanceId); } try { Collection<T> relatedAsColl = TypeHelper.asCollection(PropertyUtil.getProperty(instance, fieldName)); List<T> filtered = InMemoryQueryFilter.filter(relatedAsColl, queryParams); int recordCount = relatedAsColl.size(); int rowCount = (int) Math.ceil(recordCount / (double) queryParams.getPageSize()); return new Records<>(queryParams.getPage(), rowCount, recordCount, filtered); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { throw new ObjectReadException(entityName, e); } }
private void setProperty( Object instance, FieldRecord fieldRecord, MotechDataService service, Long deleteValueFieldId) 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); } validateNonEditableField(fieldRecord, instance, parsedValue); 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); }
public final void setValue(Object value) { if (type != null && List.class.getName().equals(type.getTypeClass())) { if (isMultiSelect()) { this.value = TypeHelper.parse(stringifyEnums(value), List.class); } else { if (value instanceof List) { // for a single object list we return the value(for single select inputs) List list = (List) value; this.value = (CollectionUtils.isNotEmpty(list)) ? stringifyEnums(list.get(0)) : null; } else { List list = (List) TypeHelper.parse(stringifyEnums(value), List.class); this.value = (CollectionUtils.isNotEmpty(list)) ? list.get(0) : list; } } } else { this.value = value; } extendOptionsIfNecessary(); }
private Object parseValue( ComboboxHolder holder, String methodParameterType, FieldRecord fieldRecord, ClassLoader classLoader) { Object parsedValue = fieldRecord.getValue(); String valueAsString = null == parsedValue ? null : TypeHelper.format(parsedValue); if (parsedValue instanceof Map) { if (fieldRecord.getMetadata(MAP_KEY_TYPE) != null && fieldRecord.getMetadata(MAP_VALUE_TYPE) != null) { Map<Object, Object> parsedValueAsMap = (Map<Object, Object>) parsedValue; Map<Object, Object> parsedMap = new LinkedHashMap<>(); for (Iterator<Map.Entry<Object, Object>> it = parsedValueAsMap.entrySet().iterator(); it.hasNext(); ) { Map.Entry<Object, Object> entry = it.next(); parsedMap.put( TypeHelper.parseMapValue( entry.getKey(), fieldRecord.getMetadata(MAP_KEY_TYPE).getValue(), true), TypeHelper.parseMapValue( entry.getValue(), fieldRecord.getMetadata(MAP_VALUE_TYPE).getValue(), false)); it.remove(); } parsedValueAsMap.putAll(parsedMap); } } else if (null != holder && holder.isEnumCollection()) { String genericType = holder.getEnumName(); parsedValue = TypeHelper.parse(valueAsString, holder.getTypeClassName(), genericType, classLoader); } else { parsedValue = TypeHelper.parse(valueAsString, methodParameterType, classLoader); } return parsedValue; }
private void setInstanceFieldMap(Object instance, FieldDto entityField) { String strMap = entityField.getBasic().getDefaultValue().toString(); String keyMetadata; String valueMetadata; if (entityField.getMetadata(MAP_KEY_TYPE) == null || entityField.getMetadata(MAP_VALUE_TYPE) == null) { keyMetadata = String.class.getName(); valueMetadata = String.class.getName(); } else { keyMetadata = entityField.getMetadata(MAP_KEY_TYPE).getValue(); valueMetadata = entityField.getMetadata(MAP_VALUE_TYPE).getValue(); } PropertyUtil.safeSetProperty( instance, entityField.getBasic().getName(), TypeHelper.parseStringToMap(keyMetadata, valueMetadata, strMap)); }
private Object getDisplayValueForCombobox(FieldDto field, Object value) { Object displayValue; if (Constants.Util.FALSE.equalsIgnoreCase( field.getSettingsValueAsString(Constants.Settings.ALLOW_USER_SUPPLIED))) { String mapString = field.getSettingsValueAsString(Constants.Settings.COMBOBOX_VALUES); Map<String, String> comboboxValues = TypeHelper.parseStringToMap(mapString); if (value instanceof Collection) { Collection valuesToDisplay = new ArrayList(); Collection enumList = (Collection) value; for (Object enumValue : enumList) { String valueFromMap = comboboxValues.get(ObjectUtils.toString(enumValue)); valuesToDisplay.add(StringUtils.isNotEmpty(valueFromMap) ? valueFromMap : enumValue); } displayValue = valuesToDisplay; } else { String valueFromMap = comboboxValues.get(ObjectUtils.toString(value)); displayValue = StringUtils.isNotEmpty(valueFromMap) ? valueFromMap : value; } } else { displayValue = value; } return displayValue; }
private Object findRelatedObjectById(Object id, MotechDataService service) { // We need parse id value to the long type return service.findById(TypeHelper.parseNumber(id, Long.class.getName()).longValue()); }