Esempio n. 1
0
 /** {@inheritDoc} */
 public Object populate(String entityName, Map<String, Object> params) {
   Type type = Model.getType(entityName);
   if (null == type) {
     throw new RuntimeException(entityName + " was not configured!");
   } else {
     return populate(type.newInstance(), type.getName(), params);
   }
 }
Esempio n. 2
0
  /**
   * {@inheritDoc} 初始化对象指定路径的属性。<br>
   * 例如给定属性a.b.c,方法会依次检查a a.b a.b.c是否已经初始化
   */
  public ObjectAndType initProperty(final Object target, String entityName, final String attr) {
    Object propObj = target;
    Object property = null;

    int index = 0;
    String[] attrs = Strings.split(attr, ".");
    Type type = Model.getType(entityName);
    while (index < attrs.length) {
      try {
        property = PropertyUtils.getProperty(propObj, attrs[index]);
        Type propertyType = type.getPropertyType(attrs[index]);
        // 初始化
        if (null == propertyType) {
          logger.error("Cannot find property type [{}] of {}", attrs[index], propObj.getClass());
          throw new RuntimeException(
              "Cannot find property type " + attrs[index] + " of " + propObj.getClass().getName());
        }
        if (null == property) {
          property = propertyType.newInstance();
          try {
            PropertyUtils.setProperty(propObj, attrs[index], property);
          } catch (NoSuchMethodException e) {
            // Try fix jdk error for couldn't find correct setter when object's Set required type is
            // diffent with Get's return type declared in interface.
            Method setter = Reflections.getSetter(propObj.getClass(), attrs[index]);
            if (null != setter) setter.invoke(propObj, property);
            else throw e;
          }
        }
        index++;
        propObj = property;
        type = propertyType;
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }
    return new ObjectAndType(property, type);
  }
Esempio n. 3
0
 /**
  * 将params中的属性([attr(string)->value(object)],放入到实体类中。
  *
  * <p>如果引用到了别的实体,那么<br>
  * 如果params中的id为null,则将该实体的置为null.<br>
  * 否则新生成一个实体,将其id设为params中指定的值。 空字符串按照null处理
  */
 public Object populate(Object entity, String entityName, Map<String, Object> params) {
   Type type = Model.getType(entityName);
   for (final Map.Entry<String, Object> paramEntry : params.entrySet()) {
     String attr = paramEntry.getKey();
     Object value = paramEntry.getValue();
     if (value instanceof String) {
       if (Strings.isEmpty((String) value)) {
         value = null;
       } else if (TRIM_STR) {
         value = ((String) value).trim();
       }
     }
     // 主键
     if (null != type
         && type.isEntityType()
         && attr.equals(((EntityType) type).getIdPropertyName())) {
       if (ValidEntityKeyPredicate.INSTANCE.evaluate(value)) {
         setValue(attr, value, entity);
       } else {
         try {
           PropertyUtils.setProperty(entity, attr, null);
         } catch (Exception e) {
           throw new RuntimeException(e.getMessage());
         }
       }
       continue;
     }
     // 普通属性
     if (-1 == attr.indexOf('.')) {
       setValue(attr, value, entity);
     } else {
       String parentAttr = Strings.substring(attr, 0, attr.lastIndexOf('.'));
       try {
         ObjectAndType ot = initProperty(entity, entityName, parentAttr);
         if (null == ot) {
           logger.error("error attr:[" + attr + "] value:[" + value + "]");
           continue;
         }
         // 属性也是实体类对象
         if (ot.getType().isEntityType()) {
           String foreignKey = ((EntityType) ot.getType()).getIdPropertyName();
           if (attr.endsWith("." + foreignKey)) {
             if (null == value) {
               setValue(parentAttr, null, entity);
             } else {
               Object foreignValue = PropertyUtils.getProperty(entity, attr);
               // 如果外键已经有值
               if (null != foreignValue) {
                 if (!foreignValue.toString().equals(value.toString())) {
                   setValue(parentAttr, null, entity);
                   initProperty(entity, entityName, parentAttr);
                   setValue(attr, value, entity);
                 }
               } else {
                 setValue(attr, value, entity);
               }
             }
           } else {
             setValue(attr, value, entity);
           }
         } else {
           setValue(attr, value, entity);
         }
       } catch (Exception e) {
         logger.error("error attr:[" + attr + "] value:[" + value + "]", e);
       }
     }
     if (logger.isDebugEnabled()) logger.debug("populate attr:[{}] value:[{}]", attr, value);
   }
   return entity;
 }