/**
 * @(#)file    CommMatlCodeSectListBiz
 * @(#)author  lsm
 * @(#)version 1.0
 * @(#)date    2014. 3. 5
 * @(#)since   JDK1.4.1
 *
 * Copyright (c) 2002-2003 www.hwenc.com, Inc.  
 * All rights reserved.
 * This software is the proprietary information of hwenc, Inc.
 */
package kr.co.udapsoft.ebid.buyer.sycs.common.biz;

import com.udapsoft.modular.web.ibsheet7.IBSheet7Factory;
import com.udapsoft.modular.web.ibsheet7.util.IBSheet7Utils;
import com.udapsoft.waf.common.ui.HTMLMaker;
import kr.co.udapsoft.ebid.buyer.sycs.security.biz.SYSRightCommon;
import com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;
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.WAFSQLException;
import kr.co.hsnc.common.sql.search.Search;
import kr.co.hsnc.common.sql.search.SearchImpl;
import kr.co.hsnc.common.sql.util.RowSetUtility;
import kr.co.hsnc.common.util.ValueObject;

public class CommMatlCodeTreeList extends Biz {
    
    private static String MATERIALGROUP_TABLE     = "PMM_MATERIALGROUP";
    private static String MATREIAL_TABLE        = "PMM_COMM_MATERIAL_MASTER";
    private static String CODE_TABLE   = "COM911T";
    

    public CommMatlCodeTreeList(HandlerStorage storage) {

        super(storage);        
    }
    public void doBiz() throws Exception {

        String systemId   = params.get("system_id");
        
        if(storage.getEvent().equals("GET_CODE_LIST"))
        {
            /*
            RowSet rowSet = getMatlCodeSectList(params);
            
           
            storage.addRowSet("ListRowSet", rowSet);
            
//          Sheet 컬럼만큼 SheetTD Array
            
            int totalColCount = 5;
            int blankColCount = 2;
               

            SheetTD[] sheetTDs = SheetFactory.createSheetTD(totalColCount);  

            SheetTDUtil.setIsBlankValue(sheetTDs, true, 0, blankColCount); 
            

            SheetTDUtil.setRowSetKey(sheetTDs, "CODE_LIST_ROWSET", blankColCount, totalColCount); 
            
            SheetTDUtil.setRowSetColumnIndex(sheetTDs, 0, blankColCount, totalColCount);

            SheetTR sheetTR = new SheetTR();
            sheetTR.setSheetTDs(sheetTDs);
            
            //SheetData Class 
            SheetData sheetData = new SheetData();

            sheetData.setAttribute("TOTAL", ""+rowSet.size());
            sheetData.sheetTR = sheetTR;
            sheetData.setRowSet("CODE_LIST_ROWSET", rowSet);
            storage.setSheetData(sheetData);
            */
            
            
            params.set("sheet1", getMatlCodeSectList(params));
            storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
        }
    }
    
    
    
    private RowSet getMatlCodeSectList(ValueObject Params) throws Exception{
        Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        StringBuffer sqlstr = new StringBuffer();
        try {           
            
            sqlstr.append("           SELECT GUBUN_1 KIND_BIG, GUBUN_2 KIND_MID, GUBUN_3 KIND_SMALL                                                       \n")
                   .append("                              FROM                                                                                                                          \n")
                   .append("                              (                                                                                                                                                   \n")
                   .append("                              SELECT                                                                                                                                                                       \n")
                   .append("                                 MATERIAL_LEVEL,    MATERIALGROUPNAME  ,   MATERIALGROUP,                                                                                                             \n")
                   .append("                                 LAST_VALUE(GUBUN_1 IGNORE NULLS) OVER(ORDER BY MATERIALGROUP) GUBUN_1,                                                                                    \n")
                   .append("                                 LAST_VALUE(GUBUN_2 IGNORE NULLS) OVER(ORDER BY MATERIALGROUP) GUBUN_2,                                                                                    \n")
                   .append("                                 LAST_VALUE(GUBUN_3 IGNORE NULLS) OVER(ORDER BY MATERIALGROUP) GUBUN_3                                                                                     \n")
                   .append("                                 FROM                                                                                                                                                                                              \n")
                   .append("                                 (                                                                                                                                                                                                     \n")
                   .append("                                     SELECT LEVEL-1 MATERIAL_LEVEL,    MATERIALGROUPNAME  , MATERIALGROUP,                                                                                         \n")
                   .append("                                                 DECODE(LEVEL, '1', MATERIALGROUPNAME, '') GUBUN_1,                                                                                                            \n")
                   .append("                                                 DECODE(LEVEL, '2', MATERIALGROUPNAME, '') GUBUN_2,                                                                                                            \n")
                   .append("                                                 DECODE(LEVEL, '3', MATERIALGROUPNAME, '') GUBUN_3                                                                                                             \n")
                   .append("                                                 FROM(                                                                                                                                                                             \n")
                   .append("                                                         SELECT MATERIAL_ID,                                                                                                                                                \n")
                   .append("                                                                    MATERIALGROUP,                                                                                                                                            \n")
                   .append("                                                                    PARENT_LEVEL,                                                                                                                                              \n")
                   .append("                                                                    MATERIALLEVEL,                                                                                                                                             \n")
                   .append("                                                                    MATERIALGROUPNAME,                                                                                                                                     \n")
                   .append("                                                                    MATERIAL_GB                                                                                                                                                 \n")
                   .append("                                                                FROM PMM_MATERIALGROUP                                                                                                                                 \n")
                   .append("                                                                WHERE MATERIAL_GB = 'C'                                                                                                                \n")
                   .append("                                                         )                                                                                                                                                                             \n")
                   .append("                                          START WITH PARENT_LEVEL = 'C'                                                                                                                                                 \n")
                   .append("                                              CONNECT BY PRIOR MATERIALGROUP = PARENT_LEVEL                                                                                                                 \n")
                   .append("                                              AND LEVEL <= 3                                                                                                                                                                    \n")
                   .append("                                              ORDER SIBLINGS BY PARENT_LEVEL ASC, MATERIALGROUP ASC                                                                                                    \n")
                   .append("                                     )                                                                                                                                                                                  \n")
                   .append("                                 )                                                                                                                                                                                            \n")
                   .append("                                 WHERE MATERIAL_LEVEL = 2                                                                                                                                                        \n");
            
            
            
            System.out.println(sqlstr.toString());
            System.out.println(search.getParameters());
            
            search.setStatement(sqlstr);
            rowSet = search.execute();
        }
        catch(Exception e) {

            System.out.println(this.getClass().getName() + " --> getMatlCodeSectList() : " + e.getMessage());
            System.out.println("SQL : " + search.getStatement());
            System.out.println(e);
            return null;
        }
        finally {    
             if(rowSet == null) rowSet = new RowSetImpl();
             return rowSet;
        }
    }
}
