Package com.cosmo.orm.annotations

Examples of com.cosmo.orm.annotations.CormFieldSetter


    * @throws IllegalArgumentException
    */
   public static Object getObjectFromRequest(Class<?> ormClass, HttpServletRequest request) throws InvalidMappingException, OrmException
   {
      Object instance = null;
      CormFieldSetter cfs;
      Class<?> paramType;

      // Comprueba si el objeto proporcionado es un objeto CORM v�lido
      if (!OrmFactory.isValidCormObject(ormClass))
      {
         throw new InvalidMappingException(ormClass.getName() + " is not a CORM object.");
      }

      // Genera la instancia de la clase
      try
      {
         Constructor<?>[] ctors = ormClass.getDeclaredConstructors();
         for (Constructor<?> ctor : ctors)
         {
            if (ctor.getGenericParameterTypes().length == 0)
            {
               ctor.setAccessible(true);
               instance = ctor.newInstance();
            }
         }
      }
      catch (Exception ex)
      {
         throw new OrmException(ex.getMessage(), ex);
      }

      // Establece los valores de las propiedades con el contenido de los par�metros contenidos en el Request
      try
      {
         for (Method method : ormClass.getMethods())
         {
            if (method.isAnnotationPresent(CormFieldSetter.class))
            {
               method.setAccessible(true);
               cfs = method.getAnnotation(CormFieldSetter.class);
               paramType = method.getParameterTypes()[0];

               // Establece el valor seg�n el tipo de datos
               if ((paramType == String.class) || (paramType == char.class))
               {
                  method.invoke(instance, HttpRequestUtils.getValue(request, cfs.dbTableColumn(), ""));
               }
               else if ((paramType == Integer.class) || (paramType == int.class) ||
                        (paramType == Long.class) || (paramType == long.class) ||
                        (paramType == Short.class) || (paramType == short.class) ||
                        (paramType == Byte.class) || (paramType == byte.class))
               {
                  method.invoke(instance, HttpRequestUtils.getInt(request, cfs.dbTableColumn()));
               }
               else if ((paramType == Float.class) || (paramType == float.class) ||
                        (paramType == Double.class) || (paramType == double.class))
               {
                  method.invoke(instance, HttpRequestUtils.getDouble(request, cfs.dbTableColumn()));
               }
               else if ((paramType == Boolean.class) || (paramType == boolean.class))
               {
                  method.invoke(instance, HttpRequestUtils.getBoolean(request, cfs.dbTableColumn()));
               }
               else if (paramType == Date.class)
               {
                  method.invoke(instance, HttpRequestUtils.getDate(request, cfs.dbTableColumn()));
               }
            }
         }
      }
      catch (Exception ex)
View Full Code Here


   public Object get(Object data) throws InvalidMappingException, SQLException, DataException, Exception
   {
      boolean first;
      StringBuilder sql = new StringBuilder();
      CormObjectField cf;
      CormFieldSetter cfs;
      ResultSet rs;

      // Comprueba si el objeto proporcionado es un objeto CORM v�lido
      if (!OrmFactory.isValidCormObject(data.getClass()))
      {
         throw new InvalidMappingException(data.getClass() + " is not a CORM object.");
      }

      // Configura la cl�usula SELECT
      sql.append(SQL_SELECT);
      sql.append(" ");

      first = true;
      for (Method method : data.getClass().getMethods())
      {
         cf = method.getAnnotation(CormObjectField.class);
         if (cf != null)
         {
            sql.append((first ? "" : ", "));
            sql.append(cf.dbTableColumn());

            first = false;
         }
      }
      sql.append(" ");

      // Configura la cl�usula FROM
      sql.append(SQL_FROM);
      sql.append(" ");
      sql.append(OrmFactory.getDbTableName(data.getClass()));
      sql.append(" ");

      // Genera la cl�usua WHERE
      sql.append(getFilter(data));

      // Memoriza la sent�ncia SQL generada
      this.setLastSqlSentence(sql.toString());

      // Ejecuta la sent�ncia SQL
      this.getConnection().connect();
      rs = this.getConnection().executeSql(sql.toString());
      this.getConnection().disconnect();

      // Recopila los datos en el objeto
      if (rs.next())
      {
         for (Method method : data.getClass().getMethods())
         {
            if (method.isAnnotationPresent(CormFieldSetter.class))
            {
               cfs = method.getAnnotation(CormFieldSetter.class);

               Class<?>[] argTypes = method.getParameterTypes();
               if (argTypes.length > 0)
               {
                  // Texto
                  if ((argTypes[0] == String.class) || (argTypes[0] == char.class))
                  {
                     method.invoke(data, rs.getString(cfs.dbTableColumn()));
                  }
                  // Enteros
                  else if (argTypes[0] == Integer.class || argTypes[0] == int.class)
                  {
                     method.invoke(data, rs.getInt(cfs.dbTableColumn()));
                  }
                  else if (argTypes[0] == Long.class || argTypes[0] == long.class)
                  {
                     method.invoke(data, rs.getLong(cfs.dbTableColumn()));
                  }
                  else if (argTypes[0] == Short.class || argTypes[0] == short.class)
                  {
                     method.invoke(data, rs.getShort(cfs.dbTableColumn()));
                  }
                  else if (argTypes[0] == Byte.class || argTypes[0] == byte.class)
                  {
                     method.invoke(data, rs.getByte(cfs.dbTableColumn()));
                  }
                  // Decimales
                  else if (argTypes[0] == Double.class || argTypes[0] == double.class)
                  {
                     method.invoke(data, rs.getDouble(cfs.dbTableColumn()));
                  }
                  else if (argTypes[0] == Float.class || argTypes[0] == float.class)
                  {
                     method.invoke(data, rs.getFloat(cfs.dbTableColumn()));
                  }
                  else if (argTypes[0] == BigDecimal.class)
                  {
                     method.invoke(data, rs.getBigDecimal(cfs.dbTableColumn()));
                  }
                  // Fechas y horas
                  else if (argTypes[0] == Date.class)
                  {
                     method.invoke(data, rs.getDate(cfs.dbTableColumn()));
                  }
                  else if (argTypes[0] == Time.class)
                  {
                     method.invoke(data, rs.getTime(cfs.dbTableColumn()));
                  }
                  else if (argTypes[0] == Timestamp.class)
                  {
                     method.invoke(data, rs.getTimestamp(cfs.dbTableColumn()));
                  }
                  // Booleanos
                  else if (argTypes[0] == boolean.class || argTypes[0] == Boolean.class)
                  {
                     method.invoke(data, rs.getBoolean(cfs.dbTableColumn()));
                  }
               }
            }
         }
View Full Code Here

TOP

Related Classes of com.cosmo.orm.annotations.CormFieldSetter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.