/**
 * @(#)file    SYSRightPgmSearchFormWorkerBiz.java
 * @(#)author  gyeongmo an
 * @(#)date    2014.6.17
 *
 * 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.RowSet;
import kr.co.hsnc.common.sql.RowSetImpl;
import kr.co.hsnc.common.sql.search.Search;
import kr.co.hsnc.common.sql.search.SearchImpl;

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

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

    public void doBiz() throws Exception {
        String systemId   = params.get("system_id");
        String pgmName    = params.get("pgm_name");
        String screenName = params.get("screen_name");
        String searchGb   = params.get("search_gb");
        String rightID    = params.get("right_id");

        if(storage.compareEvent("GET_SYS_RIGHT_PGM_SEARCH_FORM_WORKER")){
        	params.set("sheet1", gePgmList(systemId, pgmName, screenName, searchGb, rightID));
        	storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
        }
    }

    private RowSet gePgmList(String systemId, String pgmName, String screenName, String searchGb, String rightID) {
        Search search = new SearchImpl();
        RowSet listRowSet = null;
        String sqlstr = "";
        try {
            sqlstr = " SELECT A.*, B.NAME                   \n"
                   + " FROM   PUB0320T A ,     \n"
                   + "        PUB0390T B       \n"
                   + " WHERE  A.SYSTEM_ID = B.SYSTEM_ID \n" ;

            if( !systemId.equals("") )            
                sqlstr += " AND    A.SYSTEM_ID = '" + systemId + "' \n";

            if( !pgmName.equals("") )
                sqlstr += " AND    A.PGM_NAME LIKE '%" + pgmName + "%' \n";

            if( !screenName.equals("") )
                sqlstr += " AND    A.SCREEN_NAME LIKE '%" + screenName + "%' \n";

             if( searchGb.equalsIgnoreCase("REG") ) {	// 화면 검색조건 type 의add

                sqlstr += " AND    A.PGM_ID IN ( SELECT DISTINCT PGM_ID              \n"
                        + "                      FROM   PUB0300T       \n"
                        + "                      WHERE  RIGHT_ID = " + rightID + "   \n"
                        + "                      AND    PGM_ID IS NOT NULL         ) \n";
            }
            else if( searchGb.equalsIgnoreCase("NOT") ) {	// 화면 검색조건 type 의 미등록

                sqlstr += " AND    A.PGM_ID NOT IN ( SELECT DISTINCT PGM_ID              \n"
                        + "                          FROM   PUB0300T       \n"
                        + "                          WHERE  RIGHT_ID = " + rightID + "   \n"
                        + "                          AND    PGM_ID IS NOT NULL         ) \n";
            }

            sqlstr += " ORDER BY A.PGM_NAME ";
            
            search.setStatement(sqlstr);

            listRowSet = search.execute();
        }
        catch(Exception e) {

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

            if( listRowSet == null ) listRowSet = new RowSetImpl();
            return listRowSet;
        }
    }
}

