コード例 #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
  @Override
  public FieldRecord getInstanceValueAsRelatedField(Long entityId, Long fieldId, Long instanceId) {
    validateCredentialsForReading(getEntity(entityId));
    try {
      FieldRecord fieldRecord;
      FieldDto field = entityService.getEntityFieldById(entityId, fieldId);
      MotechDataService service =
          DataServiceHelper.getDataService(
              bundleContext, field.getMetadata(RELATED_CLASS).getValue());

      Object instance = service.findById(instanceId);
      if (instance == null) {
        throw new ObjectNotFoundException(service.getClassType().getName(), instanceId);
      }
      fieldRecord = new FieldRecord(field);
      fieldRecord.setValue(
          parseValueForDisplay(instance, field.getMetadata(Constants.MetadataKeys.RELATED_FIELD)));
      fieldRecord.setDisplayValue(instance.toString());
      return fieldRecord;
    } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
      throw new ObjectReadException(entityId, e);
    }
  }
コード例 #3
0
 private MotechDataService getServiceForEntity(EntityDto entity) {
   String className = entity.getClassName();
   return DataServiceHelper.getDataService(bundleContext, className);
 }