예제 #1
0
  /**
   * @param methodName getter method
   * @param clazz value object class
   * @return attribute name related to the specified getter method
   */
  private String getAttributeName(String methodName, Class classType) {
    String attributeName = null;
    if (methodName.startsWith("is"))
      attributeName =
          methodName.substring(2, 3).toLowerCase()
              + (methodName.length() > 3 ? methodName.substring(3) : "");
    else
      attributeName =
          methodName.substring(3, 4).toLowerCase()
              + (methodName.length() > 4 ? methodName.substring(4) : "");

    // an attribute name "Xxxx" becomes "xxxx" and this is not correct!
    try {
      Class c = classType;
      boolean attributeFound = false;
      while (!c.equals(Object.class)) {
        try {
          c.getDeclaredField(attributeName);
          attributeFound = true;
          break;
        } catch (Throwable ex2) {
          c = c.getSuperclass();
        }
      }
      if (!attributeFound) {
        // now trying to find an attribute having the first character in upper case (e.g. "Xxxx")
        String name = attributeName.substring(0, 1).toUpperCase() + attributeName.substring(1);
        c = classType;
        while (!c.equals(Object.class)) {
          try {
            c.getDeclaredField(name);
            attributeFound = true;
            break;
          } catch (Throwable ex2) {
            c = c.getSuperclass();
          }
        }
        if (attributeFound) attributeName = name;
      }
    } catch (Throwable ex1) {
    }

    return attributeName;
  }
예제 #2
0
  /**
   * @param obj ValueObject where updating the value for the specified attribute (identified by
   *     colunm index)
   * @param attributeName attribute name
   * @param value new Object to set onto ValueObject
   */
  public final void setField(ValueObject obj, String attributeName, Object value) {
    try {
      Method[] getter = ((Method[]) voGetterMethods.get(attributeName));
      Method[] setter = ((Method[]) voSetterMethods.get(attributeName));
      if (getter == null)
        Logger.error(
            this.getClass().getName(),
            "setField",
            "No getter method for attribute name '" + attributeName + "'.",
            null);
      if (setter == null)
        Logger.error(
            this.getClass().getName(),
            "setField",
            "No setter method for attribute name '" + attributeName + "'.",
            null);

      if (value != null
          && (value instanceof Number || !value.equals("") && value instanceof String)) {
        if (!getter[getter.length - 1].getReturnType().equals(value.getClass())) {
          Class attrType = getter[getter.length - 1].getReturnType();
          if (attrType.equals(Integer.class) || attrType.equals(Integer.TYPE))
            value = new Integer(Double.valueOf(value.toString()).intValue());
          else if (attrType.equals(Double.class) || attrType.equals(Double.TYPE))
            value = new Double(value.toString());
          else if (attrType.equals(BigDecimal.class)) value = new BigDecimal(value.toString());
          else if (attrType.equals(Long.class) || attrType.equals(Long.TYPE))
            value = new Long(Double.valueOf(value.toString()).longValue());
          else if (attrType.equals(Short.class) || attrType.equals(Short.TYPE))
            value = new Short(Double.valueOf(value.toString()).shortValue());
          else if (attrType.equals(Float.class) || attrType.equals(Float.TYPE))
            value = new Float(Double.valueOf(value.toString()).floatValue());
        }
      } else if (value != null && value.equals("")) {
        if (!getter[getter.length - 1].getReturnType().equals(value.getClass())) value = null;
      }
      // test date compatibility...
      if (value != null && value.getClass().equals(java.util.Date.class)) {
        if (setter[setter.length - 1].getParameterTypes()[0].equals(java.sql.Date.class))
          value = new java.sql.Date(((java.util.Date) value).getTime());
        else if (setter[setter.length - 1].getParameterTypes()[0].equals(java.sql.Timestamp.class))
          value = new java.sql.Timestamp(((java.util.Date) value).getTime());
      }

      // retrieve inner v.o.: if not present then maybe create it, according to "createInnerVO"
      // property...
      Method[] m = (Method[]) voGetterMethods.get(attributeName);
      if (m == null)
        Logger.error(
            this.getClass().getName(),
            "setField",
            "No getter method for attribute name '" + attributeName + "'.",
            null);
      Object oldObj = obj;
      String auxAttr;
      for (int i = 0; i < m.length - 1; i++) {
        oldObj = obj;
        obj = (ValueObject) m[i].invoke(oldObj, new Object[0]);
        if (obj == null) {
          if (grids.getGridControl() == null || !grids.getGridControl().isCreateInnerVO()) return;
          else {
            obj = (ValueObject) m[i].getReturnType().newInstance();
            String[] attrs = attributeName.split("\\.");

            auxAttr = "";
            for (int k = 0; k <= i; k++) auxAttr += attrs[k] + ".";
            auxAttr = auxAttr.substring(0, auxAttr.length() - 1);
            Method aux = ((Method[]) voSetterMethods.get(auxAttr))[i];
            aux.invoke(oldObj, new Object[] {obj});
          }
        }
      }

      // avoid to set null for primitive types!
      if (value == null && setter[setter.length - 1].getParameterTypes()[0].equals(Long.TYPE))
        setter[setter.length - 1].invoke(obj, new Object[] {new Long(0)});
      if (value == null && setter[setter.length - 1].getParameterTypes()[0].equals(Integer.TYPE))
        setter[setter.length - 1].invoke(obj, new Object[] {new Integer(0)});
      if (value == null && setter[setter.length - 1].getParameterTypes()[0].equals(Short.TYPE))
        setter[setter.length - 1].invoke(obj, new Object[] {new Short((short) 0)});
      if (value == null && setter[setter.length - 1].getParameterTypes()[0].equals(Float.TYPE))
        setter[setter.length - 1].invoke(obj, new Object[] {new Float(0)});
      if (value == null && setter[setter.length - 1].getParameterTypes()[0].equals(Double.TYPE))
        setter[setter.length - 1].invoke(obj, new Object[] {new Double(0)});
      if (value == null && setter[setter.length - 1].getParameterTypes()[0].equals(Boolean.TYPE))
        setter[setter.length - 1].invoke(obj, new Object[] {Boolean.FALSE});
      else setter[setter.length - 1].invoke(obj, new Object[] {value});
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }