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

import kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.sql.persistent.Persistent;
import kr.co.hsnc.common.sql.persistent.PersistentImpl;
import kr.co.hsnc.common.sql.search.Search;
import kr.co.hsnc.common.sql.search.SearchImpl;
import com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

public class SYSRightPgmListAddFormWorkerBiz extends Biz {
	public SYSRightPgmListAddFormWorkerBiz(HandlerStorage storage) {
		super(storage);
	}

	public void doBiz() throws Exception {
		String pgmGrID = "";
        int acctID = 0;
        
        if( storage.getUser() != null ) {

            acctID = storage.getUser().getInt("ACCT_ID");
        }

        String rightID  = params.get("right_id");
        String pgmGrSeq = params.get("pgm_gr_seq");
        String gubun    = params.get("gubun");
        String[] pgmID   =  storage.gets("pgm_id");

        if( pgmGrSeq.indexOf("NEW_") != -1 ) {
            pgmGrID = pgmGrSeq.substring(4, pgmGrSeq.length());
            pgmGrSeq = getNextPgmGrSeq(rightID);
        }
        else {

            pgmGrID = getPgmGrID(rightID, pgmGrSeq);
        }

        if( gubun.equals("L") ) {

            String[] temp = { "" };
            pgmID = temp;
        }
            

        boolean success = addPgmList(gubun, pgmGrID, pgmGrSeq, pgmID, rightID, acctID);

        storage.setSuccess(success);
    }

    /**
     * 권한별 프로그램 목록을 등록한다.
	 * @param gubun
	 * @param pgmGrID
	 * @param pgmGrSeq
	 * @param pgmID
	 * @param pgmSeq
	 * @param rightID
	 * @param acctID
	 * @return
	 */
	private boolean addPgmList(String gubun, String pgmGrID, String pgmGrSeq, String[] pgmID, String rightID, int acctID) {

        String sqlstr = "";
        try {
            conn.begin();

            sqlstr = " INSERT INTO PUB0300T                   \n"
                   + " ( PGM_LIST_ID, GUBUN, PGM_GR_ID, PGM_GR_SEQ, PGM_ID, \n"
                   + "   PGM_SEQ, RIGHT_ID, CRE_BY, CRE_DATE )              \n"
                   + " VALUES                                               \n"
                   + " ( PUB0300T_S.NEXTVAL, ?, ?, ?, ?,                    \n"
                   + "   ?, ?, ?, TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') )                                 \n" ;

            Persistent persistent = new PersistentImpl(conn.getConnection());
            persistent.setStatement(sqlstr);

            int pgmSeq = getNextPgmSeq(rightID, pgmGrID, pgmGrSeq);

            for( int i = 0 ; i < pgmID.length ; i++ ) {

                persistent.addParameter(gubun);
                persistent.addParameter(pgmGrID);
                persistent.addParameter(pgmGrSeq);
                persistent.addParameter(pgmID[i]);

                persistent.addParameter(pgmSeq++);
                persistent.addParameter(rightID);
                persistent.addParameter(acctID);
                persistent.execute();
                persistent.clearParameters();
            }
            return true;
        }
        catch(Exception e) {
            WAFLogger.error(this.getClass().getName() + " --> addPgmList() : " + e.getMessage());
            WAFLogger.error("SQL : " + sqlstr);
            WAFLogger.error(e);
            conn.setRollback();
            storage.setDetailMessage(e);
            return false;
        }
        finally {
            conn.end();
        }
    }

    /**
     * 프로그램 그룹 일련번호를 입력받아 프로그램 그룹 ID를 리턴한다.
	 * @param rightID
	 * @param pgmGrSeq
	 * @return
	 */
	private String getPgmGrID(String rightID, String pgmGrSeq) {
        
        Search search = new SearchImpl();
        String sqlstr = "";
        try {
            sqlstr = " SELECT PGM_GR_ID              \n"
                   + " FROM   PUB0300T \n"
                   + " WHERE  RIGHT_ID   = ?         \n"
                   + " AND    PGM_GR_SEQ = ?         \n"
                   + " ORDER BY PGM_GR_ID DESC       \n" ;
            search.setStatement(sqlstr);
            
            search.addParameter(rightID);
            search.addParameter(pgmGrSeq);

            return search.execute().getRow(0).get("PGM_GR_ID");
        }
        catch(Exception e) {

            WAFLogger.error(this.getClass().getName() + " --> getPgmList() : " + e.getMessage());
            WAFLogger.error("SQL : " + search.getStatement());
            WAFLogger.error(e);
            return null;
        }
    }

    /**
     * 새로운(Next) 프로그램 그룹의 일련번호를 리턴한다.
	 * @param rightID
	 * @return
	 */
	private String getNextPgmGrSeq(String rightID) {
        
        Search search = new SearchImpl();
        String sqlstr = "";
        try {
            sqlstr = " SELECT NVL(MAX(PGM_GR_SEQ), 0) + 1 AS NEXT_PGM_GR_SEQ \n"
                   + " FROM   PUB0300T                         \n"
                   + " WHERE  RIGHT_ID = ?                                   \n" ;

            search.setStatement(sqlstr);
            search.addParameter(rightID);

            return search.execute().getRow(0).get("NEXT_PGM_GR_SEQ");
        }
        catch(Exception e) {

            WAFLogger.error(this.getClass().getName() + " --> getNextPgmGrSeq() : " + e.getMessage());
            WAFLogger.error("SQL : " + search.getStatement());
            WAFLogger.error(e);
            return null;
        }
    }

    /**
     * 새로운(Next) 프로그램 목록의 일련번호를 리턴한다.
	 * @param rightID
	 * @param pgmGrID
	 * @param pgmGrSeq
	 * @return
	 */
	private int getNextPgmSeq(String rightID, String pgmGrID, String pgmGrSeq) throws Exception {
        
        Search search = new SearchImpl();
        String sqlstr = "";
        try {
            sqlstr = " SELECT NVL(MAX(PGM_SEQ), 0) + 1 AS NEXT_PGM_SEQ  \n"
                   + " FROM   PUB0300T                    \n"
                   + " WHERE  RIGHT_ID = ?                              \n"
                   + " AND    PGM_GR_ID = ?                             \n"
                   + " AND    PGM_GR_SEQ = ?                            \n" ;
            search.setStatement(sqlstr);
            
            search.addParameter(rightID);
            search.addParameter(pgmGrID);
            search.addParameter(pgmGrSeq);

            return search.execute().getRow(0).getInt("NEXT_PGM_SEQ");
        }
        catch(Exception e) {

            WAFLogger.error(this.getClass().getName() + " --> getPgmList() : " + e.getMessage());
            WAFLogger.error("SQL : " + search.getStatement());
            WAFLogger.error(e);
            throw e;
        }
    }
}
