/**
 * @(#)file    CommCodeManager.java
 * @(#)author  chj
 * @(#)version 1.0
 * @(#)date    2014-06-24
 * @(#)since   JDK1.6
 *
 * Copyright (c) www.udapsoft.co.kr, Inc.
 */

package kr.co.udapsoft.common.code;

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

import kr.co.hsnc.common.sql.Row;
import kr.co.hsnc.common.sql.RowImpl;
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 kr.co.hsnc.common.util.ValueObject;
import com.udapsoft.waf.common.ui.HTMLMaker;

/**
* 공통코드 DB 접근 Beans.
* 
*
* @version 1.0 2003-05-29
* @author DaeJin Lee
*/
public class CommCodeManager {

    private static String D_TABLE = "EBID_COM911T";

    /**
     * Select 태그 Method
     * @param group_cd 검색 조건
     * @param inputName Select name
     * @param sytleStr Select style
     * @param defaultOption Option Default Value
     * @param selectedValue Option Selected Value
     * @param eventStr
     * @return
     * @throws Exception
     */
    public static String getSelectCodeList(String group_cd, String inputName, String sytleStr, String defaultOption, String selectedValue) throws Exception {

        return getSelectCodeList(group_cd, inputName, sytleStr, defaultOption, selectedValue, 10, "");
    }
    public static String getSelectCodeList(String group_cd, String inputName, String sytleStr, String defaultOption, String selectedValue, String eventStr) throws Exception {

        return getSelectCodeList(group_cd, inputName, sytleStr, defaultOption, selectedValue, 10, eventStr);
    }
    public static String getSelectCodeSearchList(String group_cd, String inputName, String sytleStr, String defaultOption, String selectedValue, String Search) throws Exception {
        return getSelectCodeList5(group_cd, inputName, sytleStr, defaultOption, selectedValue, 10, Search , "");
    }
    public static String getSelectCodeSearchList(String group_cd, String inputName, String sytleStr, String defaultOption, String selectedValue, String Search,String range) throws Exception {
        return getSelectCodeList5(group_cd, inputName, sytleStr, defaultOption, selectedValue, 10, Search, range);
    }
    
    /**
     * Select 태그 Method
     * @param group_cd 검색 조건
     * @param inputName Select name
     * @param sytleStr Select style
     * @param defaultOption Option Default Value
     * @param selectedValue Option Selected Value
     * @param eventStr
     * @return code name
     * @throws Exception
     */
    public static String getSelectCodeList2(String group_cd, String inputName, String sytleStr, String defaultOption, String selectedValue, String eventStr) throws Exception {
        return getSelectCodeList2(group_cd, inputName, sytleStr, defaultOption, selectedValue, 10, eventStr);
    }
    public static String getSelectCodeList3(String group_cd, String inputName, String sytleStr, String defaultOption, String selectedValue, String eventStr) throws Exception {
        return getSelectCodeList3(group_cd, inputName, sytleStr, defaultOption, selectedValue, 10, eventStr);
    }
    public static String getSelectCodeList4(String group_cd, String inputName, String sytleStr, String defaultOption, String selectedValue, String eventStr) throws Exception {
        return getSelectCodeList4(group_cd, inputName, sytleStr, defaultOption, selectedValue, 10, eventStr);
    }
    
    public static String getSelectCodeList(String group_cd, String inputName, String sytleStr, String defaultOption, String selectedValue, int size, String eventStr) throws Exception {
        return getSelectCodeList(group_cd, inputName, sytleStr, defaultOption, selectedValue, size, eventStr, "");
    }

    public static String getSelectCodeList2(String group_cd, String inputName, String sytleStr, String defaultOption, String selectedValue, int size, String eventStr) throws Exception {
        return getSelectCodeList2(group_cd, inputName, sytleStr, defaultOption, selectedValue, size, eventStr, "");
    }
    public static String getSelectCodeList3(String group_cd, String inputName, String sytleStr, String defaultOption, String selectedValue, int size, String eventStr) throws Exception {
        return getSelectCodeList3(group_cd, inputName, sytleStr, defaultOption, selectedValue, size, eventStr, "");
    }

    public static String getSelectCodeList4(String group_cd, String inputName, String sytleStr, String defaultOption, String selectedValue, int size, String eventStr) throws Exception {
        return getSelectCodeList4(group_cd, inputName, sytleStr, defaultOption, selectedValue, size, eventStr, "");
    }
    
    /**
     * 파라미터로 받은 공통코드의 코드목록을 select.js를 이용하는 HTML SELECT Tag로 리턴한다.
     * @param group_cd : 공통코드
     * @param inputName : SELECT Tag의 'name' 속성명
     * @return <code>String</code>
     * @throws Exception
     *  
     */
    
    public static String getSelectCodeList(String group_cd, String inputName, String sytleStr, String defaultOption, String selectedValue, int size, String eventStr, String strClassNm) throws Exception {

        ValueObject rowObject = null;

        CommCodeManager commCodeManager = new CommCodeManager();
        RowSet commCodeRowSet = commCodeManager.getCodeRowSet(group_cd);

        /*
         * 수정. 황선재
         * 수정사항 : eventStr 인자 제거
         */

        return HTMLMaker.getSelectTag(commCodeRowSet, "CODE", "NAME", inputName, sytleStr, defaultOption, selectedValue, size, strClassNm, eventStr);
    }

    /**
     * 파라미터로 받은 공통코드의 코드목록을 select.js를 이용하는 HTML SELECT Tag로 리턴한다.
     * @param group_cd : 공통코드
     * @param inputName : SELECT Tag의 'code' 속성명
     * @return <code>String</code>
     * @throws Exception
     */
    public static String getSelectCodeList2(String group_cd, String inputName, String sytleStr, String defaultOption, String selectedValue, int size, String eventStr, String strClassNm) throws Exception {

        ValueObject rowObject = null;

        CommCodeManager commCodeManager = new CommCodeManager();
        RowSet commCodeRowSet = commCodeManager.getCodeRowSet2(group_cd);

        /*
         * 수정. 황선재
         * 수정사항 : eventStr 인자 제거
         */

        return HTMLMaker.getSelectTag2(commCodeRowSet, "CODE", "NAME", inputName, sytleStr, defaultOption, selectedValue, size, strClassNm);
    }
    /**
     * 파라미터로 받은 공통코드의 코드목록을 select.js를 이용하는 HTML SELECT Tag로 리턴한다.
     * @param group_cd : 공통코드
     * @param inputName : SELECT Tag의 'code' 속성명
     * @return <code>String</code>
     * @throws Exception
     */
    public static String getSelectCodeList3(String group_cd, String inputName, String sytleStr, String defaultOption, String selectedValue, int size, String eventStr, String strClassNm) throws Exception {

        ValueObject rowObject = null;

        CommCodeManager commCodeManager = new CommCodeManager();
        RowSet commCodeRowSet = commCodeManager.getCodeRowSet(group_cd);

        /*
         * 수정. 황선재
         * 수정사항 : eventStr 인자 제거
         */

        return HTMLMaker.getSelectTag(commCodeRowSet, "NAME", "NAME", inputName, sytleStr, defaultOption, selectedValue, size, strClassNm);
    }

    /**
     * 파라미터로 받은 공통코드의 코드목록을 select.js를 이용하는 HTML SELECT Tag로 리턴한다.
     * @param group_cd : 공통코드
     * @param inputName : SELECT Tag의 'code' 속성명
     * @return <code>String</code>
     * @throws Exception
     */
    public static String getSelectCodeList4(String group_cd, String inputName, String sytleStr, String defaultOption, String selectedValue, int size, String eventStr, String strClassNm) throws Exception {

        ValueObject rowObject = null;

        CommCodeManager commCodeManager = new CommCodeManager();
        RowSet commCodeRowSet = commCodeManager.getCodeRowSet3(group_cd);

        /*
         * 수정. 황선재
         * 수정사항 : eventStr 인자 제거
         */

        return HTMLMaker.getSelectTag(commCodeRowSet, "CODE", "NAME", inputName, sytleStr, defaultOption, selectedValue, size, strClassNm);
    }
    
    /**
     * 파라미터로 받은 공통코드의 코드목록을 select.js를 이용하는 HTML SELECT Tag로 리턴한다.
     * @param group_cd : 공통코드
     * @param inputName : SELECT Tag의 'name' 속성명
     * @return <code>String</code>
     * @throws Exception
     *  
     */
    public static String getSelectCodeList5(String group_cd, String inputName, String sytleStr, String defaultOption, String selectedValue, int size, String Search, String range) throws Exception {
        ValueObject rowObject = null;
        CommCodeManager commCodeManager = new CommCodeManager();
        /**
         * Search : 쿼리에 검색 조건을 추가 합니다.
         * range : 정렬 방식을 추가 합니다.
         */
        RowSet commCodeRowSet = commCodeManager.getSearchCodeRowSet(group_cd,Search,range);
        return HTMLMaker.getSelectTag(commCodeRowSet, "CODE", "NAME", inputName, sytleStr, defaultOption, selectedValue, size, "", "");
    }
    
    /**
     * 파라미터로 받은 공통코드의 코드목록을 리턴한다.
     * @param commCode 공통코드
     * @return <code>RowSet</code>
     * @throws Exception
     */
    public static RowSet getCodeRowsetStatic(String group_cd){

    

        CommCodeManager commCodeManager = new CommCodeManager();
        RowSet commCodeRowSet = commCodeManager.getCodeRowSet(group_cd);
        return commCodeRowSet;
    }
    
    public RowSet getCodeRowSet(String group_cd) {

        Search search = new SearchImpl();
        RowSet rowSet = null;
        String sqlstr = "";
        try {

            sqlstr = " SELECT    DETAILCD AS CODE ,DETAILNM AS NAME, SORT_NO AS SEQ        	\n"
                   + " FROM      " + D_TABLE + "       										\n"
                   + " WHERE     BASECD = ?          										\n"
                   + " ORDER BY  SORT_NO              	   									\n";

            search.setStatement(sqlstr);
            search.addParameter(group_cd);
            rowSet = search.execute();
        }
        catch(Exception e) {

            WAFLogger.error(this.getClass().getName() + " --> getCodeRowSet() : " + e.getMessage());
            WAFLogger.error("SQL : " + search.getStatement());
            WAFLogger.error(e);
        }
        finally {
            if( rowSet == null ) rowSet = new RowSetImpl();
           
        }
        return rowSet;
    }
    
    public RowSet getSearchCodeRowSet(String group_cd, String search_terms, String range) {

        Search search = new SearchImpl();
        RowSet rowSet = null;
        if(range=="")range = "SORT_NO";
        String sqlstr = "";
        try {

            sqlstr = " SELECT    DETAILCD AS CODE ,DETAILNM AS NAME, SORT_NO AS SEQ        	\n"
                   + " FROM      " + D_TABLE + "       										\n"
                   + " WHERE     BASECD = ?          										\n"
                   + search_terms
                   + " ORDER BY  "+range+"              	   									\n";

            search.setStatement(sqlstr);
            search.addParameter(group_cd);
            rowSet = search.execute();
        }
        catch(Exception e) {

            WAFLogger.error(this.getClass().getName() + " --> getCodeRowSet() : " + e.getMessage());
            WAFLogger.error("SQL : " + search.getStatement());
            WAFLogger.error(e);
        }
        finally {
            if( rowSet == null ) rowSet = new RowSetImpl();
           
        }
        return rowSet;
    }
    
    

    /**
     * 파라미터로 받은 공통코드의 코드목록을 리턴한다.
     * @param commCode 공통코드
     * @return <code>RowSet</code>
     * @throws Exception
     */
    public static RowSet getCodeRowsetStatic(String group_cd, String column, String code){

    

        CommCodeManager commCodeManager = new CommCodeManager();
        RowSet commCodeRowSet = commCodeManager.getCodeRowSet(group_cd, column, code);
        return commCodeRowSet;
    }
    
    public RowSet getCodeRowSet(String group_cd, String column, String code) {
    	
    	Search search = new SearchImpl();
    	RowSet rowSet = null;
    	String sqlstr = "";
    	try {
    		
    		sqlstr = " SELECT    DETAILCD, DETAILNM, CHG_CODE01, CHG_CODE02, CHG_CODE03, CHG_CODE04, CHG_CODE05, CHG_CODE06, REMARK, SORT_NO, DETAILSORTNM        \n"
    				+ " FROM      " + D_TABLE + "       \n"
    				+ " WHERE     BASECD = ?          \n"
    				+ " AND     "+column+" = ?          \n"
    				+ " ORDER BY  SORT_NO              	   \n";
    		
    		search.setStatement(sqlstr);
    		search.addParameter(group_cd);
    		search.addParameter(code);
    		rowSet = search.execute();
    	}
    	catch(Exception e) {
    		
    		WAFLogger.error(this.getClass().getName() + " --> getCodeRowSet() : " + e.getMessage());
    		WAFLogger.error("SQL : " + search.getStatement());
    		WAFLogger.error(e);
    	}
    	finally {
    		if( rowSet == null ) rowSet = new RowSetImpl();
    		
    	}
    	return rowSet;
    }
        
    
    
    /**
     * 파라미터로 받은 공통코드의 코드목록을 리턴한다.
     * @param commCode 공통코드
     * @return <code>RowSet</code>
     * @throws Exception
     */
    public RowSet getCodeRowSet2(String group_cd) {

        Search search = new SearchImpl();
        RowSet rowSet = null;
        String sqlstr = "";
        try {

            sqlstr = " SELECT    DETAILCD AS CODE ,DETAILCD||': '||DETAILNM as name  	\n"
	               + " FROM      " + D_TABLE + "       									\n"
	               + " WHERE     BASECD = ?          									\n"
	               + " ORDER BY  SORT_NO              	   								\n";

            search.setStatement(sqlstr);
            search.addParameter(group_cd);
            rowSet = search.execute();
        }
        catch(Exception e) {

            WAFLogger.error(this.getClass().getName() + " --> getCodeRowSet() : " + e.getMessage());
            WAFLogger.error("SQL : " + search.getStatement());
            WAFLogger.error(e);
        }
        finally {
            if( rowSet == null ) rowSet = new RowSetImpl();
            return rowSet;
        }
    }
    
    
    /**
     * 파라미터로 받은 공통코드의 코드목록을 리턴한다.
     * @param commCode 공통코드
     * @return <code>RowSet</code>
     * @throws Exception
     */
    public RowSet getCodeRowSet3(String group_cd) {

        Search search = new SearchImpl();
        RowSet rowSet = null;
        String sqlstr = "";
        try {

            sqlstr = " SELECT DETAILNM || ';' || DETAILCD AS  CODE, DETAILNM AS NAME  	\n"
                   + " FROM      " + D_TABLE + "       									\n"
                   + " WHERE     BASECD = ?          									\n"
                   + " ORDER BY  SORT_NO              	   								\n";

            search.setStatement(sqlstr);
            search.addParameter(group_cd);
            rowSet = search.execute();
        }
        catch(Exception e) {

            WAFLogger.error(this.getClass().getName() + " --> getCodeRowSet() : " + e.getMessage());
            WAFLogger.error("SQL : " + search.getStatement());
            WAFLogger.error(e);
        }
        finally {
            if( rowSet == null ) rowSet = new RowSetImpl();
            return rowSet;
        }
    }    
    /**
     * 정렬방법 RowSet을 리턴
	 * @return
	 */
	public static RowSet getSortMethodRowSet() {

		String[] SORT_METHOD = {"순번", "코드명", "한글명", "영문명", "등록일시"};
        RowSet rowSet = new RowSetImpl();

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

            Row row = new RowImpl();
            row.addColumn("SEQ" , Integer.toString(i + 1));
            row.addColumn("NAME", SORT_METHOD[i]);
            rowSet.addRow(row);
        }
        return rowSet;
    }
	/**
	 * 
	 * @param commCode
	 * @return RowSet
	 * @throws Exception
	 */
	public static RowSet getCodeList(String commCode) throws Exception {

        CommCodeManager commCodeManager = new CommCodeManager();
        return commCodeManager.getCodeRowSet(commCode);
    }
	
	/**
	 * 결재 후처리할 클레스를 넘겨준다.
	 * @param signGB
	 * @return Sign After Control Class
	 * @throws Exception
	 */
	public static String getSignClass(String signGB) throws Exception {

        CommCodeManager commCodeManager = new CommCodeManager();
        return commCodeManager.getSignAfterClass(signGB);
    }
	/**
	 * 결재 후처리할 클레스를 넘겨준다.
	 * @param signGB
	 * @return
	 */
    private String getSignAfterClass(String signGB) {

        Search search = new SearchImpl();
        RowSet rowSet = null;
        String sqlstr = "";
        String ret_val = "";
        try {

            sqlstr = " SELECT   DETAILNM   AS NAME               	\n"
                   + " FROM     " + D_TABLE + "       				\n"
                   + " WHERE    BASECD = 'DRAFT'    				\n"
                   + " AND     	DETAILCD = ?              			\n";
        	
            search.setStatement(sqlstr);
            search.addParameter(signGB);
            rowSet = search.execute();
            if(rowSet.size() > 0){
            	ret_val = rowSet.getRow(0).get("NAME");  
            }
        }
        catch(Exception e) {

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