/**
 * @(#)file    PuSupplierBiz.java
 * @(#)author  이성민
 * @(#)version 1.0
 * @(#)date    2014.06.25
 *
 * Copyright (c) www.udapsoft.co.kr, Inc.
 * All rights reserved.
 * This software is the proprietary information of udapsoft Inc.
 */
package kr.co.udapsoft.ebid.buyer.test.biz;



import java.io.Reader;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.SQLException;

import kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.base.WAFMessages;
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.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 kr.co.hsnc.common.sql.util.RowSetUtility;
import kr.co.hsnc.common.util.ValueObject;
import kr.co.udapsoft.common.sender.MessageSend;

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






public class ContTemplatePopBiz extends Biz {

	public ContTemplatePopBiz(HandlerStorage storage) {
		super(storage);
	}

	/* (non-Javadoc)
	 * @see kr.co.hwenc.system.Biz#doBiz()
	 */
	public void doBiz() throws Exception {

		
		
		WAFLogger.debug("event==============>" + storage.getEvent());
		
		 ValueObject templateVo = new ValueObject();
		 templateVo = getTemplate(params);
		
		 storage.addValueObject("contTemplateVo", templateVo);
	}
	
	 public ValueObject getTemplate(ValueObject param) throws Exception {

	        Search search = new SearchImpl();
	        ValueObject valueObject = null;
	        StringBuffer sqlstr = new StringBuffer();
	        try {
	        	sqlstr.append(" SELECT DECODE(GUBUN,'P','구매','O','외주',GUBUN) GUBUN_NM, PUR_CONT_MOD_ID, DEPT_CD,DEPT_NAME,ORD_DOC_NO,CST_DOC_NAME,MOD_NO,SEQ,TEMPLATE_NAME,TEMPLATE_HTML,CHECK_YN, OPTION_YN         	\n");
	        	sqlstr.append("   FROM (       	\n");
	        	sqlstr.append("  SELECT 'P' GUBUN, A.PUR_CONT_MOD_ID, A.DEPT_CD, A.DEPT_NAME, A.ORD_DOC_NO, A.CST_DOC_NAME, A.MOD_NO, B.SEQ, B.TEMPLATE_NAME, B.TEMPLATE_HTML, B.CHECK_YN, B.OPTION_YN        	\n");
	        	sqlstr.append("    FROM PUR2000T A, CONT_PUR_HTML B         	\n");
	        	sqlstr.append("   WHERE A.PUR_CONT_MOD_ID = B.PUR_CONT_MOD_ID       	\n");
	        	sqlstr.append("   UNION ALL        	\n");
	        	sqlstr.append("  SELECT 'O' GUBUN, A.SUB_CONT_MOD_ID, A.DEPT_CD, A.DEPT_NAME, A.CON_DOC_NO, A.BUILD_NAME, A.MOD_NO, B.SEQ, B.TEMPLATE_NAME, B.TEMPLATE_HTML, B.CHECK_YN, B.OPTION_YN         	\n");
	        	sqlstr.append("    FROM SUB2000T A, CONT_OUT_HTML B				       	\n");
	        	sqlstr.append("   WHERE A.SUB_CONT_MOD_ID = B.SUB_CONT_MOD_ID) A        	\n");
	        	sqlstr.append("   WHERE GUBUN = '"+params.get("P_GUBUN")+"'       	\n");
	        	sqlstr.append("     AND PUR_CONT_MOD_ID = '"+params.get("P_CONT_MOD_ID")+"'        \n");
	        	sqlstr.append("     AND SEQ = '"+params.get("P_SEQ")+"'      \n");

	            search.setStatement(sqlstr);
	            System.out.println(param);
	            WAFLogger.debug(sqlstr.toString());
	            valueObject = RowSetUtility.getValueObject(search.execute().getRow(0));
	            valueObject.set("TEMPLATE_HTML",readClob(valueObject.getObject("TEMPLATE_HTML")));        
	        }
	        catch(WAFSQLException se) {
	        	super.doException(se);
	        	this.storage.setSuccess(false);
	        	this.storage.setMessage(se.getMessage());
	        	storage.setDetailMessage(se);
	        }
	        finally {

	            if( valueObject == null ) valueObject = new ValueObject();
	            return valueObject;
	        }
	    }
	 /**
	     * Clob Object에서 데이터를 읽어 리턴한다.
	     * @param object
	     * @return
	     * @throws Exception
	     */
	    private String readClob(Object object) throws Exception {

	        if( object == null ) {
	        
	            return "";
	        }

	        Clob clob = (Clob)object;
	    /*  -- 2006.09.14 changed by SMH  
	        Reader reader = clob.getCharacterStream();

	        int size = (int)clob.length();
	        char[] clobData = new char[size];
	        reader.read(clobData, 0, size);
	        reader.close();

	        StringBuffer temp = new StringBuffer(); 
	        temp.append(clobData);                

	        return temp.toString();
	    */    
	        return getClob(clob);
	    }
	    
	    /**
	     * Clob 데이터 유형을 String으로 변경하여 반환한다.
	     * @param clob
	     * @return
	     * @throws Exception
	     */
	    public static String getClob(Clob clob) throws Exception{
	        String strClob = "";
	        Reader reader = null;
	        try {
	            if(clob != null){              
	                reader = clob.getCharacterStream();
	                char[] buffer = new char[1024];
	                int length = 0;
	                while( (length = reader.read(buffer)) != -1 ) {                  
	                    for( int j=0; j<length; j++ ) {
	                        strClob = strClob+buffer[j];
	                    }
	                }
	            }        
	        }
	        finally {
	            if(reader != null) reader.close();                    
	        }   
	        return strClob;
	    }
}