コード例 #1
0
  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()))) {
      if (type.equals(TypeDto.ONE_TO_MANY_RELATIONSHIP)
          || type.equals(TypeDto.MANY_TO_MANY_RELATIONSHIP)) {
        Class<?> genericType =
            (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
        serviceForRelatedClass =
            DataServiceHelper.getDataService(bundleContext, genericType.getName());
      } else if (type.equals(TypeDto.MANY_TO_ONE_RELATIONSHIP)
          || type.equals(TypeDto.ONE_TO_ONE_RELATIONSHIP)) {
        serviceForRelatedClass =
            DataServiceHelper.getDataService(bundleContext, parameterType.getName());
      }

      value = buildRelatedInstances(serviceForRelatedClass, parameterType, fieldRecord.getValue());
    }

    Method method = MethodUtils.getAccessibleMethod(instance.getClass(), methodName, parameterType);
    invokeMethod(method, instance, value, methodName, fieldName);
  }
コード例 #2
0
  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);
  }
コード例 #3
0
 private Map<Long, String> buildDisplayValuesMap(Collection values)
     throws InvocationTargetException, IllegalAccessException {
   Map<Long, String> displayValues = new HashMap<>();
   for (Object obj : values) {
     Method method = MethodUtils.getAccessibleMethod(obj.getClass(), "getId", (Class[]) null);
     Long key = (Long) method.invoke(obj);
     String uiRepresentation = UIRepresentationUtil.uiRepresentationString(obj);
     if (uiRepresentation != null) {
       displayValues.put(
           key,
           uiRepresentation.length() > UI_REPRESENTATION_MAX_LENGTH
               ? uiRepresentation.substring(0, UI_REPRESENTATION_MAX_LENGTH + 1) + ELLIPSIS
               : uiRepresentation);
     } else {
       String toStringResult = obj.toString();
       displayValues.put(
           key,
           toStringResult.length() > TO_STRING_MAX_LENGTH
               ? toStringResult.substring(0, TO_STRING_MAX_LENGTH + 1) + ELLIPSIS
               : toStringResult);
     }
   }
   return displayValues;
 }