/**
 * @(#)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.supplier.outside.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 OutSupplierEmailBiz extends Biz {

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

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

		
		
		WAFLogger.debug("event==============>" + storage.getEvent());
		
		 ValueObject emailVo = new ValueObject();
		 emailVo = getSubEmail();
		
		 storage.addValueObject("emailVo", emailVo);
	}
	
	 public ValueObject getSubEmail() throws Exception {

	        Search search = new SearchImpl();
	        ValueObject valueObject = null;
	        StringBuffer sqlstr = new StringBuffer();
	        try {

	            sqlstr.append( " SELECT  *                                            \n");
	            sqlstr.append("  FROM    SUPP_EMAIL                                 \n");
	            sqlstr.append("  WHERE JOB_DIV = 'O'                                \n");

	            search.setStatement(sqlstr);
	            valueObject = RowSetUtility.getValueObject(search.execute().getRow(0));
	            valueObject.set("SEND_EMAIL",readClob(valueObject.getObject("SEND_EMAIL")));        
	        }
	        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;
	    }
}