/*
 * Copyright
 *   2009 axYus - www.axyus.com
 *   2009 C.Marchand - christophe.marchand@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package fr.gouv.finances.dgfip.xemelios.batch.imports.utils;

    

import fr.gouv.finances.dgfip.xemelios.data.utils.jdbc.JdbcUtils;
import fr.gouv.finances.dgfip.xemelios.data.utils.jdbc.PStmtBinder;
import fr.gouv.finances.dgfip.xemelios.data.utils.jdbc.RowMapper;
import java.util.List;

import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.Connection;


public class XemImportsFilesVOWrapper {
    
    public static final String getSelectFieldsClause(String table) {
      return ""+table+".IMPORT_ID, "+table+".SUB_FILE_NAME, "+table+".FILE_TYPE, "+table+".STATUS, "+table+".COLLECTIVITE, "+table+".BUDGET, "+table+".COLLECTIVITE_LIB, "+table+".BUDGET_LIB, "+table+".ERROR_MESSAGE";
    }

    public static final String getSelectFieldsClause() {
      return "IMPORT_ID, SUB_FILE_NAME, FILE_TYPE, STATUS, COLLECTIVITE, BUDGET, COLLECTIVITE_LIB, BUDGET_LIB, ERROR_MESSAGE";
    }
    
    public static XemImportsFilesVOFull getXemImportsFilesVOFullBy(Connection con, Class clazz, String sql, PStmtBinder binder) throws SQLException {
      return (XemImportsFilesVOFull)JdbcUtils.queryObject(con, sql, new RowMapper.ClassRowMapper(clazz), binder);
    }

    public static List getAllXemImportsFilesVOFullBy(Connection con, Class clazz, String sql, PStmtBinder binder) throws SQLException {
      return JdbcUtils.queryObjects(con, sql, new RowMapper.ClassRowMapper(clazz), binder);
    }

    public static List getAllXemImportsFilesVOFullBy(Connection con, Class clazz, String sql) throws SQLException {
      return JdbcUtils.queryObjects(con, sql, new RowMapper.ClassRowMapper(clazz), null);
    }

    public static XemImportsFilesVOFull getXemImportsFilesVOFullByPk(Connection con, XemImportsFilesVOPK data) throws SQLException {
        return getXemImportsFilesVOFullByPk(con, data, XemImportsFilesVOFull.class);
    }
    
    public static XemImportsFilesVOFull getXemImportsFilesVOFullByPk(Connection con, XemImportsFilesVOPK data, Class clazz) throws SQLException {
        String sql = "SELECT " + getSelectFieldsClause() + " FROM XEM_IMPORTS_FILES WHERE IMPORT_ID=? AND SUB_FILE_NAME=?";
        return getXemImportsFilesVOFullBy(con, clazz, sql, new PStmtBinder.SimplePStmtBinderBuilder().add(data.importId).add(data.subFileName).toPStmtBinder());
    }
    
    public static void deleteXemImportsFilesVO(Connection con, XemImportsFilesVOPK data) throws SQLException {
        String sql = "DELETE FROM XEM_IMPORTS_FILES WHERE IMPORT_ID=? AND SUB_FILE_NAME=?";
            JdbcUtils.executeUpdate(con,sql,new PStmtBinder.SimplePStmtBinderBuilder().add(data.importId==null ? (java.lang.String)null : data.importId).add(data.subFileName==null ? (java.lang.String)null : data.subFileName).toPStmtBinder());
    }
    
    
    public static void insertXemImportsFilesVO(Connection con, XemImportsFilesVOFull data) throws SQLException {
        int colCount=0;
        StringBuilder sql = new StringBuilder("INSERT INTO XEM_IMPORTS_FILES(");
        
        
        if(data.isImportIdSet | data.isImportIdModified) {
            sql.append("IMPORT_ID,");
            colCount++;
        }
        
        if(data.isSubFileNameSet | data.isSubFileNameModified) {
            sql.append("SUB_FILE_NAME,");
            colCount++;
        }
        
        if(data.isFileTypeSet | data.isFileTypeModified) {
            sql.append("FILE_TYPE,");
            colCount++;
        }
        
        if(data.isStatusSet | data.isStatusModified) {
            sql.append("STATUS,");
            colCount++;
        }
        
        if(data.isCollectiviteSet | data.isCollectiviteModified) {
            sql.append("COLLECTIVITE,");
            colCount++;
        }
        
        if(data.isBudgetSet | data.isBudgetModified) {
            sql.append("BUDGET,");
            colCount++;
        }
        
        if(data.isCollectiviteLibSet | data.isCollectiviteLibModified) {
            sql.append("COLLECTIVITE_LIB,");
            colCount++;
        }
        
        if(data.isBudgetLibSet | data.isBudgetLibModified) {
            sql.append("BUDGET_LIB,");
            colCount++;
        }
        
        if(data.isErrorMessageSet | data.isErrorMessageModified) {
            sql.append("ERROR_MESSAGE,");
            colCount++;
        }
        if(colCount==0) throw new SQLException("No data to insert");
        sql.deleteCharAt(sql.length() - 1);
        sql.append(") VALUES (");
        for(int i=0;i<colCount;i++) sql.append("?,");
        sql.deleteCharAt(sql.length() - 1);
        sql.append(")");
        PStmtBinder.SimplePStmtBinderBuilder builder = new PStmtBinder.SimplePStmtBinderBuilder();
        
        if(data.isImportIdSet | data.isImportIdModified) {
            
                  if(data.importId==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.importId);
                  
                
        }
        
        if(data.isSubFileNameSet | data.isSubFileNameModified) {
            
                  if(data.subFileName==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.subFileName);
                  
                
        }
        
        if(data.isFileTypeSet | data.isFileTypeModified) {
            
                  if(data.fileType==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.fileType);
                  
                
        }
        
        if(data.isStatusSet | data.isStatusModified) {
            
                  if(data.status==null) builder=builder.add((java.lang.Integer)null); else builder=builder.add(data.status);
                  
                
        }
        
        if(data.isCollectiviteSet | data.isCollectiviteModified) {
            
                  if(data.collectivite==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.collectivite);
                  
                
        }
        
        if(data.isBudgetSet | data.isBudgetModified) {
            
                  if(data.budget==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.budget);
                  
                
        }
        
        if(data.isCollectiviteLibSet | data.isCollectiviteLibModified) {
            
                  if(data.collectiviteLib==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.collectiviteLib);
                  
                
        }
        
        if(data.isBudgetLibSet | data.isBudgetLibModified) {
            
                  if(data.budgetLib==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.budgetLib);
                  
                
        }
        
        if(data.isErrorMessageSet | data.isErrorMessageModified) {
            
                  if(data.errorMessage==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.errorMessage);
                  
                
        }
        
        
        JdbcUtils.executeUpdate(con,sql.toString(),builder.toPStmtBinder());
        
        
        
    }
    
    public static void updateXemImportsFilesVO(Connection con, XemImportsFilesVOFull data) throws SQLException {
        PreparedStatement ps = null;
        StringBuilder sql=new StringBuilder("UPDATE XEM_IMPORTS_FILES SET ");
        if(data.isFileTypeModified) sql.append("FILE_TYPE=?,");
        if(data.isStatusModified) sql.append("STATUS=?,");
        if(data.isCollectiviteModified) sql.append("COLLECTIVITE=?,");
        if(data.isBudgetModified) sql.append("BUDGET=?,");
        if(data.isCollectiviteLibModified) sql.append("COLLECTIVITE_LIB=?,");
        if(data.isBudgetLibModified) sql.append("BUDGET_LIB=?,");
        if(data.isErrorMessageModified) sql.append("ERROR_MESSAGE=?,");
        
        sql.deleteCharAt(sql.length()-1);
        sql.append(" WHERE IMPORT_ID=? AND SUB_FILE_NAME=?");
        PStmtBinder.SimplePStmtBinderBuilder builder = new PStmtBinder.SimplePStmtBinderBuilder();
        
        if(data.isFileTypeModified) {
            
                  if(data.fileType==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.fileType);
                  
                
        }
        if(data.isStatusModified) {
            
                  if(data.status==null) builder=builder.add((java.lang.Integer)null); else builder=builder.add(data.status);
                  
                
        }
        if(data.isCollectiviteModified) {
            
                  if(data.collectivite==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.collectivite);
                  
                
        }
        if(data.isBudgetModified) {
            
                  if(data.budget==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.budget);
                  
                
        }
        if(data.isCollectiviteLibModified) {
            
                  if(data.collectiviteLib==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.collectiviteLib);
                  
                
        }
        if(data.isBudgetLibModified) {
            
                  if(data.budgetLib==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.budgetLib);
                  
                
        }
        if(data.isErrorMessageModified) {
            
                  if(data.errorMessage==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.errorMessage);
                  
                
        }
        
            builder=builder.add(data.importId);
        
            builder=builder.add(data.subFileName);
        
        JdbcUtils.executeUpdate(con,sql.toString(),builder.toPStmtBinder());
    }
            
                
                
                
    public static List<XemImportsFilesFL> getChildren(Connection con, String request, Class clazz, PStmtBinder binder) throws SQLException {
        return (List<XemImportsFilesFL>)JdbcUtils.queryObjects(con,request, new RowMapper.ClassRowMapper(clazz), binder);
    }
            
}
        