/** * */ package com.fitbank.common.helper; import java.math.BigDecimal; import java.sql.Date; import java.sql.Timestamp; import java.text.MessageFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.hibernate.Hibernate; import org.hibernate.SQLQuery; import org.hibernate.type.Type; import com.fitbank.common.Helper; import com.fitbank.common.exception.FitbankException; import com.fitbank.common.logger.FitbankLogger; import org.hibernate.type.StandardBasicTypes; /** * Clase encargada de insertar información en una tabla dado un Query * * @author */ public class FillTableFromQuery { /** Constante TEMPLATE. */ public static final String TEMPLATE = "insert into {0}({1}){2}"; /** Constante TEMPLATE. */ public static final String TEMPLATE1 = "insert into {0}{1}"; /** Valor de table. */ private String table; /** Valor de sql. */ private String sql; /** Valor de fields. */ private List fields; /** Valor de query. */ private SQLQuery query; /** Map auxiliar empleado para el manejo de Par�metros. */ private Map map; /** * Crea una nueva instancia de FillTableFromQuery.java * * @param pTable Valor de table * @param pSql Valor de sql * @param pFields Valor de fields * @throws Exception the exception */ public FillTableFromQuery(String pTable, String pSql, List pFields) throws Exception { this.table = pTable; this.sql = pSql; this.fields = (pFields == null) ? new ArrayList() : pFields; this.map = new HashMap(); this.prepare(); } /** * Crea una nueva instancia de FillTableFromQuery.java * * @param pTable Valor de table * @param pSql Valor de sql * @param pFields Valor de fields * @throws Exception the exception */ public FillTableFromQuery(String pTable, String pSql, String... pFields) throws Exception { this.table = pTable; this.sql = pSql; this.fields = new ArrayList(); this.map = new HashMap(); if (pFields != null) { for (String field : pFields) { this.fields.add(field); } } this.prepare(); } /** * Execute. * * @return the int * @throws Exception the exception */ public int execute() throws Exception { String[] param = this.query.getNamedParameters(); for (String parameter : param) { Object obj = this.map.get(parameter); if (obj == null) { throw new FitbankException("HB006", "VALOR NO ENVIADO {0}", parameter); } else if (obj instanceof Type) { this.query.setParameter(parameter, null, (Type) obj); FitbankLogger.getLogger().debug("UtilHB.execute null " + parameter); } else { if (obj instanceof Date) { this.query.setDate(parameter, (Date) obj); } else if (obj instanceof Timestamp) { this.query.setTimestamp(parameter, (Timestamp) obj); } else { this.query.setParameter(parameter, obj); } } // qry.setParameter(parameter,obj); } return this.query.executeUpdate(); } /** * Metodo Empleado para Prepare. * * @throws Exception the exception */ private void prepare() throws Exception { StringBuffer sFields = new StringBuffer(); boolean first = true; for (String field : this.fields) { sFields.append((first) ? "" : ",").append(field); first = false; } String sentence = (this.fields.isEmpty()) ? MessageFormat.format(TEMPLATE1, this.table, this.sql) : MessageFormat.format(TEMPLATE, this.table, sFields.toString(), this.sql); this.query = Helper.createSQLQuery(sentence); } /** * Fija un criterio BigDecimal a la sentencia. El nombre del parametro debe ser igual al del where de la sentencia. * * @param pParameter Valor de parameter * @param pBigdecimal Valor de bigdecimal */ public void setBigDecimal(String pParameter, BigDecimal pBigdecimal) { if (pBigdecimal == null) { this.map.put(pParameter, StandardBasicTypes.BIG_DECIMAL); } else { this.map.put(pParameter, pBigdecimal); } } /** * Fija un criterio Data a la sentencia. El nombre del parametro debe ser igual al del where de la sentencia. * * @param pParameter Valor de parameter * @param pDate Valor de fecha */ public void setDate(String pParameter, Date pDate) { this.map.put(pParameter, pDate); } /** * Fija un criterio Integer a la sentencia. El nombre del parametro debe ser igual al del where de la sentencia. * * @param pParameter Valor de parameter * @param pInteger Valor de integer */ public void setInteger(String pParameter, Integer pInteger) { if (pInteger == null) { this.map.put(pParameter, StandardBasicTypes.INTEGER); } else { this.map.put(pParameter, pInteger); } } /** * Fija un criterio Integer a la sentencia. El nombre del parametro debe ser igual al del where de la sentencia. * * @param pParameter Valor de parameter * @param pLong Valor de long */ public void setLong(String pParameter, Long pLong) { if (pLong == null) { this.map.put(pParameter, StandardBasicTypes.LONG); } else { this.map.put(pParameter, pLong); } } /** * Fija un criterio string de la sentencia. El nombre del parametro debe ser igual al del where de la sentencia. * * @param pParameter Valor de parameter * @param pString Valor de string */ public void setString(String pParameter, String pString) { this.map.put(pParameter, pString); } /** * Fija un criterio Timestamp a la sentencia. El nombre del parametro debe ser igual al del where de la sentencia. * * @param pParameter Valor de parameter * @param pTimestamp Valor de timestamp */ public void setTimestamp(String pParameter, Timestamp pTimestamp) { this.map.put(pParameter, pTimestamp); } }