/**
 * @(#)file    SYSRightRegPgmListSaveBiz.java
 * @(#)author  gyeongmo an
 * @(#)date    2014.6.22
 *
 * 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 SYSRightRegPgmListSaveBiz extends Biz {
	
	public SYSRightRegPgmListSaveBiz(HandlerStorage storage) {
		super(storage);
	}

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

			try {
				super.conn.begin();
				
				for (int i = 0; i < status.length; i++) {
					if (IBSheet7Utils.equalStatusInsert(status[i])) {
						success = saveData(super.conn.getConnection(), IBSheet7Utils.getIBSheet7SqlParams("SHEET1", params, i));
					}
					
					if (IBSheet7Utils.equalStatusUpdate(status[i])) {
						success = updateData(super.conn.getConnection(), IBSheet7Utils.getIBSheet7SqlParams("SHEET1", params, i));
					}
					
					if (IBSheet7Utils.equalStatusDelete(status[i])) {
						success = deleteData(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();
			}
		}
	}

	private boolean saveData(Connection connection, ValueObject sqlParams) throws Exception {
		PreparedStatement pstmt = null;
		
		try {
			StringBuffer sql = new StringBuffer();
			sql.append("	INSERT INTO PUB0320T			\n ");
			sql.append("	(                   			\n ");
			sql.append("		PGM_ID            			\n ");
			sql.append("		, PGM_NAME        			\n ");
			sql.append("		, SCREEN_NAME     			\n ");
			sql.append("		, SYSTEM_ID       			\n ");
			sql.append("		, SYSTEM_CODE     			\n ");
			sql.append("		, DSCR            			\n ");
			sql.append("		, CRE_BY          			\n ");
			sql.append("		, CRE_DATE        			\n ");
			sql.append("		) VALUES (        			\n ");
			sql.append("		PUB0320T_S.NEXTVAL                 			\n ");
			sql.append("		,?                			\n ");
			sql.append("		,?                			\n ");
			sql.append("		,?                			\n ");
			sql.append("		,?                			\n ");
			sql.append("		,?                			\n ");
			sql.append("		,?                			\n ");
			sql.append("		,TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS')          			\n ");
			sql.append("		)                 			\n ");
			
			pstmt = connection.prepareStatement(sql.toString());
			int paramIdx = 1;
			String[] system_code = sqlParams.get("SYSTEM_ID").split("[@]");
			
			pstmt.setString(paramIdx++, sqlParams.get("PGM_NAME"));
			pstmt.setString(paramIdx++, sqlParams.get("SCREEN_NAME"));
			pstmt.setInt(paramIdx++, Integer.parseInt(system_code[0]));
			pstmt.setString(paramIdx++, system_code[1]);
			pstmt.setString(paramIdx++, sqlParams.get("DSCR"));
			pstmt.setInt(paramIdx++, storage.getUser().getInt("ACCT_ID"));
			
			pstmt.executeUpdate();
			return true;
		} catch (Exception e) {
			WAFLogger.error(e);
			throw new Exception(e);
		} finally {
			try {
				if(pstmt != null) pstmt.close();
			} catch(SQLException se) {
				WAFLogger.error(se);
			}
		}
	}
	
	private boolean updateData(Connection connection, ValueObject sqlParams) throws Exception {
		PreparedStatement pstmt = null;
		
		try {
			StringBuffer sql = new StringBuffer();
			sql.append("	UPDATE PUB0320T SET   	\n");
			sql.append("		PGM_NAME = ?        	\n");
			sql.append("		, SCREEN_NAME = ?   	\n");
			sql.append("		, SYSTEM_CODE = ?   	\n");
			sql.append("		, SYSTEM_ID = ?   	\n");
			sql.append("		, DSCR = ?          	\n");
			sql.append("		, UPD_BY = ?        	\n");
			sql.append("		, UPD_DATE = TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS')	\n");
			sql.append("	WHERE PGM_ID = ?	\n");
			
			pstmt = connection.prepareStatement(sql.toString());
			int paramIdx = 1;
			String[] system_code = sqlParams.get("SYSTEM_ID").split("[@]");
			
			pstmt.setString(paramIdx++, sqlParams.get("PGM_NAME"));
			pstmt.setString(paramIdx++, sqlParams.get("SCREEN_NAME"));
			pstmt.setString(paramIdx++, system_code[1]);
			pstmt.setInt(paramIdx++, Integer.parseInt(system_code[0]));
			pstmt.setString(paramIdx++, sqlParams.get("DSCR"));
			pstmt.setInt(paramIdx++, storage.getUser().getInt("ACCT_ID"));
			pstmt.setInt(paramIdx++, sqlParams.getInt("PGM_ID"));
			
			pstmt.executeUpdate();
			return true;
		} catch (Exception e) {
			WAFLogger.error(e);
			throw new Exception(e);
		} finally {
			try {
				if(pstmt != null) pstmt.close();
			} catch(SQLException se) {
				WAFLogger.error(se);
			}
		}
	}
	
	private boolean deleteData(Connection connection, ValueObject sqlParams) throws Exception {
		PreparedStatement pstmt = null;
		
		try {
			StringBuffer sql = new StringBuffer();
			sql.append("	DELETE FROM PUB0320T WHERE PGM_ID = ?			\n ");
			
			pstmt = connection.prepareStatement(sql.toString());
			int paramIdx = 1;
			pstmt.setInt(paramIdx++, sqlParams.getInt("PGM_ID"));
			
			pstmt.executeUpdate();
			return true;
		} catch (Exception e) {
			WAFLogger.error(e);
			throw new Exception(e);
		} finally {
			try {
				if(pstmt != null) pstmt.close();
			} catch(SQLException se) {
				WAFLogger.error(se);
			}
		}
	}
}
