/*
 * 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 ImportVOWrapper {
    
    public static final String getSelectFieldsClause(String table) {
      return ""+table+".UTI_LOGIN, "+table+".IMPORT_ID, "+table+".FILE_NAME, "+table+".FILE_TYPE, "+table+".SUBMIT_DATE, "+table+".END_DATE, "+table+".STATUS, "+table+".COLLECTIVITE, "+table+".BUDGET, "+table+".COLLECTIVITE_LIB, "+table+".BUDGET_LIB, "+table+".ERROR_MESSAGE";
    }

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

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

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

    public static ImportVOFull getImportVOFullByPk(Connection con, ImportVOPK data) throws SQLException {
        return getImportVOFullByPk(con, data, ImportVOFull.class);
    }
    
    public static ImportVOFull getImportVOFullByPk(Connection con, ImportVOPK data, Class clazz) throws SQLException {
        String sql = "SELECT " + getSelectFieldsClause() + " FROM XEM_IMPORTS WHERE IMPORT_ID=?";
        return getImportVOFullBy(con, clazz, sql, new PStmtBinder.SimplePStmtBinderBuilder().add(data.importId).toPStmtBinder());
    }
    
    public static void deleteImportVO(Connection con, ImportVOPK data) throws SQLException {
        String sql = "DELETE FROM XEM_IMPORTS WHERE IMPORT_ID=?";
            JdbcUtils.executeUpdate(con,sql,new PStmtBinder.SimplePStmtBinderBuilder().add(data.importId==null ? (java.lang.String)null : data.importId).toPStmtBinder());
    }
    
    
    public static void insertImportVO(Connection con, ImportVOFull data) throws SQLException {
        int colCount=0;
        StringBuilder sql = new StringBuilder("INSERT INTO XEM_IMPORTS(");
        
        
        if(data.isUtiLoginSet | data.isUtiLoginModified) {
            sql.append("UTI_LOGIN,");
            colCount++;
        }
        
        if(data.isImportIdSet | data.isImportIdModified) {
            sql.append("IMPORT_ID,");
            colCount++;
        }
        
        if(data.isFileNameSet | data.isFileNameModified) {
            sql.append("FILE_NAME,");
            colCount++;
        }
        
        if(data.isFileTypeSet | data.isFileTypeModified) {
            sql.append("FILE_TYPE,");
            colCount++;
        }
        
        if(data.isSubmitDateSet | data.isSubmitDateModified) {
            sql.append("SUBMIT_DATE,");
            colCount++;
        }
        
        if(data.isEndDateSet | data.isEndDateModified) {
            sql.append("END_DATE,");
            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.isUtiLoginSet | data.isUtiLoginModified) {
            
                  if(data.utiLogin==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.utiLogin);
                  
                
        }
        
        if(data.isImportIdSet | data.isImportIdModified) {
            
                  if(data.importId==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.importId);
                  
                
        }
        
        if(data.isFileNameSet | data.isFileNameModified) {
            
                  if(data.fileName==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.fileName);
                  
                
        }
        
        if(data.isFileTypeSet | data.isFileTypeModified) {
            
                  if(data.fileType==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.fileType);
                  
                
        }
        
        if(data.isSubmitDateSet | data.isSubmitDateModified) {
            
                  if(data.submitDate==null) builder=builder.add((java.sql.Timestamp)null); else builder=builder.add(data.submitDate);
                  
                
        }
        
        if(data.isEndDateSet | data.isEndDateModified) {
            
                  if(data.endDate==null) builder=builder.add((java.sql.Timestamp)null); else builder=builder.add(data.endDate);
                  
                
        }
        
        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 updateImportVO(Connection con, ImportVOFull data) throws SQLException {
        PreparedStatement ps = null;
        StringBuilder sql=new StringBuilder("UPDATE XEM_IMPORTS SET ");
        if(data.isUtiLoginModified) sql.append("UTI_LOGIN=?,");
        if(data.isFileNameModified) sql.append("FILE_NAME=?,");
        if(data.isFileTypeModified) sql.append("FILE_TYPE=?,");
        if(data.isSubmitDateModified) sql.append("SUBMIT_DATE=?,");
        if(data.isEndDateModified) sql.append("END_DATE=?,");
        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=?");
        PStmtBinder.SimplePStmtBinderBuilder builder = new PStmtBinder.SimplePStmtBinderBuilder();
        
        if(data.isUtiLoginModified) {
            
                  if(data.utiLogin==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.utiLogin);
                  
                
        }
        if(data.isFileNameModified) {
            
                  if(data.fileName==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.fileName);
                  
                
        }
        if(data.isFileTypeModified) {
            
                  if(data.fileType==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.fileType);
                  
                
        }
        if(data.isSubmitDateModified) {
            
                  if(data.submitDate==null) builder=builder.add((java.sql.Timestamp)null); else builder=builder.add(data.submitDate);
                  
                
        }
        if(data.isEndDateModified) {
            
                  if(data.endDate==null) builder=builder.add((java.sql.Timestamp)null); else builder=builder.add(data.endDate);
                  
                
        }
        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);
        
        JdbcUtils.executeUpdate(con,sql.toString(),builder.toPStmtBinder());
    }
            
}
        