/**
 * @(#)file    AuthBatchListSaveBiz.java
 * @(#)author  gyeongmo an
 * @(#)date    2014.6.15
 *
 * Copyright (c) www.udapsoft.co.kr, Inc.
 * All rights reserved.
 */
package kr.co.udapsoft.ebid.buyer.sycs.security.biz;

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

import kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.util.ValueObject;

import com.udapsoft.modular.web.ibsheet7.IBSheet7Factory;
import com.udapsoft.modular.web.ibsheet7.util.IBSheet7Utils;
import com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

public class AuthBatchListSaveBiz extends Biz {

	public AuthBatchListSaveBiz(HandlerStorage storage) {
		super(storage);
	}

	public void doBiz() throws Exception {
		if (storage.compareEvent("SAVE_USER_LIST")) {
			boolean success = false;
			String[] status = super.gets("sheet1.STATUS");

			try {
				super.conn.begin();
				
				for (int i = 0; i < status.length; i++) {
					if (IBSheet7Utils.equalStatusUpdate(status[i])) {
						success = updUserRight(super.conn.getConnection(), IBSheet7Utils.getIBSheet7SqlParams("SHEET1", params, i));
					}
				}
				
				super.storage.setSuccess(success);
				storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
			} catch (Exception e) {
				WAFLogger.error(e);
				super.conn.setRollback();
				throw new Exception(e);
				
			} finally {
				super.conn.close();
			}
		}
	}

	public boolean updUserRight(Connection connection, ValueObject sqlParams) throws Exception {
		PreparedStatement pstmt = null;
		PreparedStatement pstmt2 = null;
		
		try{
			StringBuffer sql = new StringBuffer();
			StringBuffer sql_d = new StringBuffer();
			
			sql.append("	INSERT INTO PUB0350T                                         		\n");
         	sql.append("	       ( RIGHT_LIST_ID, RIGHT_ID, ACCT_ID, CRE_BY, CRE_DATE )		\n");
         	sql.append("	       VALUES                                                		\n");
         	sql.append("	       ( PUB0350T_S.NEXTVAL,                                 		\n");
         	sql.append("	                 ?,                                          		\n");
         	sql.append("	                 ?,                                          		\n");
         	sql.append("	                 ?,                                          		\n");
         	sql.append("	                 TO_CHAR(SYSDATE,'YYYYMMDDHH24MISS')  )      		\n");

         	sql_d.append("	DELETE PUB0350T   		\n");
         	sql_d.append("	WHERE  ACCT_ID = ?		\n");
         	
         	pstmt2 = connection.prepareStatement(sql.toString());
         	pstmt = connection.prepareStatement(sql_d.toString());
			int paramIdx = 1;
			int paramIdx2 = 1;
			
			String target_v = params.get("target_v");
            String[] arrTarget_v = target_v.split("/", 0);
            
			if("1".equals(sqlParams.get("CHECK")) && "U".equals(sqlParams.get("STATUS"))) {
				pstmt.setInt(paramIdx++, sqlParams.getInt("ACCT_ID"));
				pstmt.executeUpdate();
				
				for( int j = 0 ; j < arrTarget_v.length ; j++ ) {
					
					pstmt2.setString(paramIdx2++, arrTarget_v[j]);
					pstmt2.setInt(paramIdx2++, sqlParams.getInt("ACCT_ID"));
					pstmt2.setInt(paramIdx2++, storage.getUser().getInt("ACCT_ID"));
					pstmt2.executeUpdate();
				}
			}
			return true;
		} catch (Exception e) {
			WAFLogger.error(e);
			throw new Exception(e);
		} finally {
			try {
				if(pstmt != null) pstmt.close();
				if(pstmt2 != null) pstmt2.close();
			} catch(SQLException se) {
				WAFLogger.error(se);
			}
		}
    }
	
	
	
}
