/**
 * @(#)file    SYSRightPgmListGrSortFormWorkerBiz.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 kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.sql.persistent.Persistent;
import kr.co.hsnc.common.sql.persistent.PersistentImpl;
import com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

/**
 * 프로그램그룹 순서 변경 처리
 * 
 * @author DaeJin Lee
 */
public class SYSRightPgmListGrSortFormWorkerBiz extends Biz  {
	public SYSRightPgmListGrSortFormWorkerBiz(HandlerStorage storage) {
		super(storage);
	}

	public void doBiz() throws Exception {

        String   rightID = params.get("right_id");
        String[] pgmGrID = storage.gets("pgm_gr_id");

        boolean success = updPgmListGrSeq(rightID, pgmGrID);
        storage.setSuccess(success);
	}

    /**
     * 프로그램 그룹의 순서를 변경한다.
     * @param pgmListID
     * @return
     */
    private boolean updPgmListGrSeq(String rightID, String[] pgmGrID) {

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

            sqlstr = " UPDATE PUB0300T \n"
                   + " SET    PGM_GR_SEQ  = ? \n"
                   + " WHERE  PGM_GR_ID   = ? \n" 
                   + " AND    RIGHT_ID    = ? \n";

            Persistent persistent = new PersistentImpl(conn.getConnection());
            persistent.setStatement(sqlstr);
            for( int i = 0 ; i < pgmGrID.length ; i++ ) {

                persistent.addParameter(i + 1);
                persistent.addParameter(pgmGrID[i]);
                persistent.addParameter(rightID);
                persistent.execute();
                persistent.clearParameters();
            }
            
            return true;
        }
        catch(Exception e) {

            WAFLogger.error(this.getClass().getName() + " --> updPgmListGrSeq() : " + e.getMessage());
            WAFLogger.error("SQL : " + sqlstr);
            WAFLogger.error(e);
            conn.setRollback();
            storage.setDetailMessage(e);
            return false;
        }
        finally {

            conn.end();
        }
    }
}
