コード例 #1
0
  public static String getPropertyNameForAssociationEntity(
      Class<?> entityClass, MetaClass metaClass, String key) {
    String[] readablePropertyNames = metaClass.getGetterNames();
    for (String propertyName : readablePropertyNames) {
      Method getterMethod = findGetterMethod(propertyName, entityClass);
      if (getterMethod == null) {
        continue;
      }

      Association association = AnnotationUtils.findAnnotation(getterMethod, Association.class);
      if (association != null) {
        String joinColumn = association.joinColumn();
        if (key.equals(joinColumn)) {
          return propertyName;
        }
      }
    }

    return null;
  }
コード例 #2
0
  public static <T> List<ColumnModel> getColumnModelListForUpdate(
      Configuration configuration,
      Class<?> entityClass,
      T entity,
      boolean isMapUnderscoreToCamelCase,
      Map<String, Object> parameterMap) {
    MetaClass metaClass = MetaClass.forClass(entityClass, configuration.getReflectorFactory());
    String[] readablePropertyNames = metaClass.getGetterNames();

    List<ColumnModel> columnModelList = new ArrayList<ColumnModel>();
    ColumnModel columnModel = null;
    for (String propertyName : readablePropertyNames) {
      Method getterMethod = findGetterMethod(propertyName, entityClass);
      if (getterMethod == null) {
        continue;
      }

      if (AnnotationUtils.findAnnotation(getterMethod, Transient.class) != null
          || AnnotationUtils.findAnnotation(getterMethod, CreatedDate.class) != null) {
        continue;
      }

      columnModel = new ColumnModel();
      try {
        Invoker method = metaClass.getGetInvoker(propertyName);
        try {
          Object value = method.invoke(entity, null);
          String columnName = getColumnName(propertyName, isMapUnderscoreToCamelCase);

          // Handle PK
          if (AnnotationUtils.findAnnotation(getterMethod, Id.class) != null) {
            Assert.notNull(value, propertyName + " is required");
            parameterMap.put(columnName, value);
            continue;
          }

          // Handle the property that annotated by ${#link ModifiedDate}
          if (AnnotationUtils.findAnnotation(getterMethod, ModifiedDate.class) != null) {
            if (methodReturnTypeCheck(getterMethod, Date.class)) {
              value = new Date();
            }
          }

          if (value != null
              || AnnotationUtils.findAnnotation(getterMethod, NullableForUpdate.class) != null) {
            // Handle the property that annotated by {#link Enumerated}
            Enumerated enumrated = AnnotationUtils.findAnnotation(getterMethod, Enumerated.class);
            if (enumrated != null) {
              if (enumrated.value() == EnumType.ORDINAL) {
                Class<?> returnType = getterMethod.getReturnType();
                value =
                    Enum.valueOf(returnType.asSubclass(Enum.class), String.valueOf(value))
                        .ordinal();
              }
            }

            // Handle the property that annotated by {#link Association}
            if (AnnotationUtils.findAnnotation(getterMethod, Association.class) != null) {
              Association association =
                  AnnotationUtils.findAnnotation(getterMethod, Association.class);
              String joinColumn = association.joinColumn();
              if (joinColumn == null || "".equals(joinColumn.trim())) {
                columnName = columnName + "_id";
              } else {
                columnName = association.joinColumn();
              }

              Class<?> returnType = getterMethod.getReturnType();
              MetaClass associationMetaClass =
                  MetaClass.forClass(returnType, configuration.getReflectorFactory());
              Invoker m =
                  associationMetaClass.getGetInvoker(
                      "id"); // TODO, now just support the primary key that named "id"
              value = m.invoke(value, null);
            }

            columnModel.setName(columnName);
            columnModel.setValue(value);
            columnModelList.add(columnModel);
          }

        } catch (Throwable t) {
          throw ExceptionUtil.unwrapThrowable(t);
        }
      } catch (Throwable t) {
        throw new ReflectionException(
            "Could not get the value of property '"
                + propertyName
                + "' of '"
                + entityClass
                + "' Cause: "
                + t.toString(),
            t);
      }
    }

    return columnModelList;
  }