package com.udapsoft.waf.system.handler;

import javax.servlet.http.HttpServletRequest;

import kr.co.hsnc.common.logger.Logger;
import kr.co.hsnc.j2ee.waf.controller.web.action.DefaultActionHandler;

import com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

public abstract class BizDefaultActionHandler extends DefaultActionHandler {

	private static final long serialVersionUID = -8809726763354543402L;
	protected String screen = null;
	protected HandlerStorage storage = null;
	
	private Object handler = null;
	
	protected Biz biz = null;
	
	/**
	 * 
	 */
	public BizDefaultActionHandler() {
		super();
	}

    public void doStart(HttpServletRequest request) {
        super.doStart(request);
        request.setAttribute("SCREEN_NAME", super.getScreenName());
    }

    protected void doReady(Object handler, HttpServletRequest request) {
    	doReady(handler, request, "");
    }
    
    protected void doReady(Object handler, HttpServletRequest request, String defaultEvent) {
    	this.handler = handler;
        if( getRequest() == null ) {
        	super.doStart(request);
        }
    	this.storage = new HandlerStorage(request);

    	if( this.storage.getEvent() == null || this.storage.getEvent().equals("") ) {
    		storage.getParams().set("event", defaultEvent);
    	}
    }

    public void doEnd(HttpServletRequest request) {
    	if( this.storage.getWAFConnection().isTrx() ) {
    		Logger.err.println("Transaction not end.");
    		Logger.err.println("Handler Class : " + this.handler.getClass().getName());
    		Logger.err.println("Biz Class : " + this.biz.getClass().getName());
    	}

    	String storageKey = (String)request.getAttribute(HandlerStorage.KEY);
    	request.setAttribute(storageKey, this.storage);
    }

    protected void doException(Throwable e) {
        StackTraceElement element = e.getStackTrace()[1];
    	Logger.err.println(element.getClassName() + " --> " + element.getMethodName() + " : " + e.getMessage());
        Logger.err.println("URI : " + storage.getCurrentURI());
        Logger.err.println("EVENT : " + storage.getEvent());
        Logger.err.println("File  Name  : " + element.getFileName());
        Logger.err.println("Line Number : " + element.getLineNumber());
        Logger.err.printStackTrace((Exception)e);
    }

    protected String getEvent() {
    	return this.storage.getEvent();
    }

    public abstract String doHandle(HttpServletRequest request);
}