示例#1
0
  public static boolean isFieldCollectionOfModel(Field field) {
    boolean res = true;
    try {

      String type = field.getGenericType().toString();
      String paraType = type.substring(type.indexOf('<') + 1, type.indexOf('>'));
      debug("paraType=" + paraType);
      if (ModelObject.class.isAssignableFrom(Class.forName(paraType))) {
        res = true;
      } else {
        res = false;
      }
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return res;
  }
示例#2
0
  // to be used in DAOImpl.update only
  public static void copySimpleProperties(ModelObject dest, ModelObject orig) {
    try {
      PropertyDescriptor srcDescriptors[] =
          BeanUtilsBean.getInstance().getPropertyUtils().getPropertyDescriptors(dest);
      for (PropertyDescriptor pd : srcDescriptors) {
        String name = pd.getName();
        if ("class".equals(name)) {
          continue;
        }
        if ("id".equals(name)) {
          // TODO hardcoded.  for update, never change Id field
          continue;
        }

        Class type = pd.getPropertyType();
        debug("copySimpleProperties--  name=" + name + ", type=" + type);
        if (Collection.class.isAssignableFrom(type)) {
          Field field = dest.getClass().getDeclaredField(name);
          if (MyPropertyUtil.isFieldCollectionOfModel(field)) {
            // ingore collection of Model
            continue;
          } else {

            PropertyUtils.setProperty(dest, name, PropertyUtils.getProperty(orig, name));
          }
        } else if (ModelObject.class.isAssignableFrom(type)) {
          // we do not support editing of associated object at this point
          // nor pointing to another associated object
          continue;
        } else {
          PropertyUtils.setProperty(dest, name, PropertyUtils.getProperty(orig, name));
        }
      }
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
示例#3
0
  public static void form2To(ModelObject dest, Map<String, Object> orig) {

    debug("form2To-- props:" + orig);

    try {

      HashMap<String, Object> _props = new HashMap<String, Object>();

      PropertyDescriptor srcDescriptors[] =
          BeanUtilsBean.getInstance().getPropertyUtils().getPropertyDescriptors(dest);

      for (PropertyDescriptor pd : srcDescriptors) {
        String fn = pd.getName();
        if ("class".equals(fn)) {
          continue;
        }
        Class type = pd.getPropertyType();

        debug("fn:" + fn + " type:" + type);
        if (!orig.containsKey(fn)) {
          continue;
        }

        Object value = orig.get(fn);
        if (value == null) {
          _props.put(fn, value);
          continue;
        }

        if (ModelObject.class.isAssignableFrom(type)) {
          String bean = type.getSimpleName();
          if (value instanceof String) {
            // the value is ID
            debug("form2To-- type=" + type);
            ModelObject mo = (ModelObject) type.newInstance();
            mo.setModelId((String) value);
            //                    ModelObject mo = getModelObject(bean, (String)value);
            _props.put(fn, mo);
          } else if (value instanceof Map) {
            Map<String, Object> cprops = (Map<String, Object>) value;
            ModelObject mo = (ModelObject) type.newInstance();
            form2To(mo, cprops);
            _props.put(fn, mo);
          } else {
            throw new RuntimeException(
                "Unknown value type: " + value + "," + value.getClass() + " bean:" + bean);
          }
        } else if (Collection.class.isAssignableFrom(type)) {
          Collection col = null;
          if (value != null) {
            if (MyPropertyUtil.isFieldCollectionOfModel(dest.getClass().getDeclaredField(fn))) {
              // NOTE: this means always Add/Update child ModelObject separately
              continue;
            } else {
              //                      String bean = config.getItemType(obj.getModelName(), fn);
              if (value instanceof String) {
                col = (Collection) PropertyUtils.getProperty(dest, fn);
                String val = (String) value;
                if (((String) val).indexOf(",") > 0) {
                  // for properties like Goods.categoryIds which is internally list of String,
                  // and submitted in the format of comma-separated string
                  // TODO escape ","
                  String[] values = ConvertUtil.split(val, ",");
                  col.addAll(Arrays.asList(values));
                } else {
                  col.add(value);
                }

              } else if (value instanceof Collection) {

                Collection c = (Collection) value;
                col = (Collection) PropertyUtils.getProperty(dest, fn);
                debug("size: " + c.size());
                col.addAll(c);
                //                            _props.put(fn, c);
              }
            }

            //            		else {
            ////                      throw new RuntimeException("Unknown value type: " +
            // value+","+value.getClass()+" bean:"+bean);
            //              		 throw new RuntimeException("Unknown value type: " +
            // value+","+value.getClass());
            //                  }
          } else {
            // keep the old value
            col = (Collection) PropertyUtils.getProperty(dest, fn);
          }
          //                _props.put(fn, col);
        } else {
          _props.put(fn, value);
        }
      }

      debug("form2To-- _props:" + _props);

      BeanUtils.populate(dest, _props);

    } catch (Exception e) {
      e.printStackTrace();
    }
  }