package kr.co.udapsoft.ebid.buyer.common.lib.biz;

import java.sql.Connection;

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.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 oracle.toplink.internal.ox.StrBuffer;

public class ModifyUserInfoBiz extends Biz {

	public ModifyUserInfoBiz(HandlerStorage storage) {
		super(storage);
	}
 
    public void doBiz() throws Exception {
    	
    	if(storage.compareEvent("")){
    		storage.addValueObject("userInfo", getUserInfo());
    		
    	} else if (storage.compareEvent("SAVE")) {
    		boolean success = changePassWord();
    		super.storage.setSuccess(success);
    	}
    }

    public ValueObject getUserInfo() {

        Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        StringBuffer sql = new StringBuffer();
        ValueObject vo = new ValueObject();

        try {

			ValueObject user = storage.getUser();
			sql.append("SELECT  USER_SABUN		\n");
			sql.append("        ,USER_NAME		\n");
			sql.append("        ,GRADE_NAME		\n");
			sql.append("        ,DEPART_DEPT_NM	\n");
			sql.append("FROM    PUB0120T		\n");
			sql.append("WHERE   ACCT_ID = ?		\n");
        	search.setStatement(sql.toString());
        	search.addParameter(user.get("ACCT_ID"));
            rowSet = search.execute();
            
            if( rowSet != null && rowSet.size() > 0 ) {
            	vo = RowSetUtility.getValueObject(rowSet.getRow(0));
            }
            
        } catch (WAFSQLException se) {
            super.doException(se);
            this.storage.setSuccess(false);
            this.storage.setMessage(se.getMessage());
            storage.setDetailMessage(se);
        }
        
		return vo;
    }
    
    
    public boolean changePassWord() throws Exception {
    	
    	conn.begin();

    	try {
        	Persistent persistent = new PersistentImpl(conn.getConnection());
            StrBuffer sqlstr = new StrBuffer();
            
            sqlstr.append(" UPDATE  PUB0120T				\n");
        	sqlstr.append(" SET     USER_PW = F_SHA256(?)	\n");
        	sqlstr.append(" WHERE   ACCT_ID = ?				\n");
            persistent.setStatement(sqlstr.toString());
            persistent.addParameter(params.get("user_pw1"));
            persistent.addParameter(storage.getUser().get("ACCT_ID"));
            persistent.execute();
            
        	return true;
    	}catch(Exception e) {
    		WAFLogger.error(this.getClass().getName() + " --> changePassWord() : " + e.getMessage());
            WAFLogger.error(e);
            conn.setRollback();
            this.storage.setSuccess(false);  
            this.storage.setMessage(e.getMessage());            
            storage.setDetailMessage(e);
            return false;
    	}finally {
    		super.conn.end();
    	}
	}

    
}
