package jeus_jspwork._setup;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import org.kldp.ebmsh.config.EbmsProperties;
import org.kldp.ebmsh.setup.EbmsSetupManager;
import org.kldp.ebmsh.setup.EbmsSetupUtils;
import org.kldp.ebmsh.setup.EbmsSetupException;
import org.kldp.util.log.LogUtil;

public final class _500_setup_5fjsp extends jeus.servlet.jsp2.runtime.HttpJspBase
    implements jeus.servlet.jsp2.runtime.JspSourceDependent {

  private static java.util.Vector _jspx_dependants;

  public java.util.List getDependants() {
    return _jspx_dependants;
  }

  public void _jspService(HttpServletRequest request, HttpServletResponse response)
        throws java.io.IOException, ServletException {

    JspFactory _jspxFactory = null;
    PageContext pageContext = null;
    HttpSession session = null;
    ServletContext application = null;
    ServletConfig config = null;
    JspWriter out = null;
    Object page = this;
    JspWriter _jspx_out = null;
    PageContext _jspx_page_context = null;


    try {
      _jspxFactory = JspFactory.getDefaultFactory();
      response.setContentType("text/html; chareset=euc-kr;charset=ISO-8859-1");
      pageContext = _jspxFactory.getPageContext(this, request, response,
      			null, true, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");

	String action = request.getParameter("action");
	
	if( action == null ) {
		EbmsProperties newEbmsProperties = new EbmsProperties();				
		application.setAttribute("newEbmsProperties", newEbmsProperties);

      out.write("\r\n");
      out.write("\t\t");
      if (true) {
        _jspx_page_context.forward("/setup/template/template.jsp" + (("/setup/template/template.jsp").indexOf('?')>0? '&': '?') + jeus.servlet.jsp2.runtime.JspRuntimeLibrary.URLEncode("contentPage", request.getCharacterEncoding())+ "=" + jeus.servlet.jsp2.runtime.JspRuntimeLibrary.URLEncode("/setup/view/setupView.jsp", request.getCharacterEncoding()));
        return;
      }
      out.write('\r');
      out.write('\n');

	}
	
	else if( action.equals("dataBaseSetup") ) {

      out.write("\r\n");
      out.write("\t\t");
      if (true) {
        _jspx_page_context.forward("/setup/template/template.jsp" + (("/setup/template/template.jsp").indexOf('?')>0? '&': '?') + jeus.servlet.jsp2.runtime.JspRuntimeLibrary.URLEncode("contentPage", request.getCharacterEncoding())+ "=" + jeus.servlet.jsp2.runtime.JspRuntimeLibrary.URLEncode("/setup/view/dataBaseSetupView.jsp", request.getCharacterEncoding()));
        return;
      }
      out.write('\r');
      out.write('\n');
	
	}
	
	else if( action.equals("dataBaseSetupComplete") ) {
		String dataSourceName = request.getParameter("dataSourceName");
		String databaseType = request.getParameter("databaseType");		
		String bizboxDatabaseHeaderName = request.getParameter("bizboxDatabaseHeaderName");
		
		EbmsProperties properties = (EbmsProperties)application.getAttribute("newEbmsProperties");
		
		properties.setDataSourceName(dataSourceName);
		properties.setDatabaseType(databaseType);
		properties.setBizboxDatabaseHeaderName(bizboxDatabaseHeaderName);
		
		response.sendRedirect("setup.jsp?action=queueSetup");
	}
	
	
	else if( action.equals("queueSetup") ) {

      out.write("\r\n");
      out.write("\t\t");
      if (true) {
        _jspx_page_context.forward("/setup/template/template.jsp" + (("/setup/template/template.jsp").indexOf('?')>0? '&': '?') + jeus.servlet.jsp2.runtime.JspRuntimeLibrary.URLEncode("contentPage", request.getCharacterEncoding())+ "=" + jeus.servlet.jsp2.runtime.JspRuntimeLibrary.URLEncode("/setup/view/queueSetupView.jsp", request.getCharacterEncoding()));
        return;
      }
      out.write('\r');
      out.write('\n');
	
	}
	
	else if( action.equals("queueSetupComplete") ) {
		String queueConnectionFactoryName = request.getParameter("queueConnectionFactoryName");	
		String queueJNDIHeaderName = request.getParameter("queueJNDIHeaderName");
		
		EbmsProperties properties = (EbmsProperties)application.getAttribute("newEbmsProperties");
		
		properties.setQueueConnectionFactoryName(queueConnectionFactoryName);
		properties.setQueueJNDIHeaderName(queueJNDIHeaderName);
//		properties.setSendQueueName(sendQueueName);
//		properties.setApplicationResultQueueName(applicationResultQueueName);
//		properties.setTrackerQueueName(trackerQueueName);
		
		response.sendRedirect("setup.jsp?action=setupConfirm");
	}
	
	else if( action.equals("setupConfirm") ) {

      out.write("\r\n");
      out.write("\t\t");
      if (true) {
        _jspx_page_context.forward("/setup/template/template.jsp" + (("/setup/template/template.jsp").indexOf('?')>0? '&': '?') + jeus.servlet.jsp2.runtime.JspRuntimeLibrary.URLEncode("contentPage", request.getCharacterEncoding())+ "=" + jeus.servlet.jsp2.runtime.JspRuntimeLibrary.URLEncode("/setup/view/setupConfirmView.jsp", request.getCharacterEncoding()));
        return;
      }
      out.write('\r');
      out.write('\n');
	
	}
	
	else if( action.equals("setupApply") ) {
		// BizboxSetupManager °´Ã¼ »ý¼º
		EbmsSetupManager bizboxSetupManager = new EbmsSetupManager();
	
		// Config ÆÄÀÏÀÇ °æ·Î¸¦ °¡Á®¿È
		String bizboxRootPath = EbmsSetupUtils.getEbmsRootPath(application);
		
		String bizboxSetupSqlPath = EbmsSetupUtils.getEbmsSetupSqlPath(application);
		
		String bizboxConfigPath = EbmsSetupUtils.getEbmsConfigPath(application);
		
		EbmsProperties properties = (EbmsProperties)application.getAttribute("newEbmsProperties");
		
		
		/*//CPA µð·ºÅä¸® »ý¼º
		String cpaSetupPath = properties.getCpaPath();
		String cpaSetupRealPath = "";
			
		// Cpa °æ·Î°¡ Àý´ë°æ·ÎÀÎÁö »ó´ë °æ·ÎÀÎÁö ÆÇ´ÜÇÕ´Ï´Ù.
		if( FileIoUtil.isAbsolutePath(cpaSetupPath) ) {
			cpaSetupRealPath = cpaSetupPath;
		}
		else {
			cpaSetupRealPath = bizboxRootPath + cpaSetupPath;
		}
		
		request.setAttribute("cpaSetupPath", cpaSetupRealPath);
		
		try {
			bizboxSetupManager.createCpaDirectory(cpaSetupRealPath);
			
			request.setAttribute("cpaSetupResult", "success");
		}
		catch(EbmsSetupException e) {
			request.setAttribute("cpaSetupResult", "fail");
			request.setAttribute("cpaSetupError", LogUtil.stackTraceToString(e));
		}
		
		
		// Message µð·ºÅä¸® »ý¼º
		String messageSetupPath = properties.getMessagePath();
		String messageSetupRealPath = "";
		
		// Message °æ·Î°¡ Àý´ë°æ·ÎÀÎÁö »ó´ë °æ·ÎÀÎÁö ÆÇ´ÜÇÕ´Ï´Ù.
		if( FileIoUtil.isAbsolutePath(messageSetupRealPath) ) {
			messageSetupRealPath = messageSetupPath;
		}
		else {
			messageSetupRealPath = bizboxRootPath + messageSetupPath;
		}
		
		request.setAttribute("messageSetupPath", messageSetupRealPath);
		
		try {
			bizboxSetupManager.createMessageDirectory(messageSetupRealPath);
			
			request.setAttribute("messageSetupResult", "success");
		}
		catch(EbmsSetupException e) {
			request.setAttribute("messageSetupResult", "fail");
			request.setAttribute("messageSetupError", LogUtil.stackTraceToString(e));
		}*/
		
		
		// µ¥ÀÌÅÍº£ÀÌ½º »ý¼º
		String dataSourceName = properties.getDataSourceName();
		String databaseType = properties.getDatabaseType();
		String bizboxDatabaseHaderName = properties.getBizboxDatabaseHeaderName();
		
		try {
			bizboxSetupManager.createDatabase(bizboxSetupSqlPath, dataSourceName, databaseType, bizboxDatabaseHaderName);
			
			request.setAttribute("databaseSetupResult", "success");
		}
		catch(EbmsSetupException e) {
			request.setAttribute("databaseSetupResult", "fail");
			request.setAttribute("databaseSetupError", LogUtil.stackTraceToString(e));
		}
		
		// ¼³Á¤ ÆÄÀÏ ÀúÀå
		if( request.getAttribute("databaseSetupResult").equals("success") )
			properties.saveToFile(bizboxConfigPath);

      out.write("\r\n");
      out.write("\t\t");
      if (true) {
        _jspx_page_context.forward("/setup/template/template.jsp" + (("/setup/template/template.jsp").indexOf('?')>0? '&': '?') + jeus.servlet.jsp2.runtime.JspRuntimeLibrary.URLEncode("contentPage", request.getCharacterEncoding())+ "=" + jeus.servlet.jsp2.runtime.JspRuntimeLibrary.URLEncode("/setup/view/setupResultView.jsp", request.getCharacterEncoding()));
        return;
      }
      out.write('\r');
      out.write('\n');
	
	}

    } catch (Throwable t) {
      if (!(t instanceof SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          out.clearBuffer();
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
      }
    } finally {
      if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
}
