<%@ page contentType="text/html; chareset=euc-kr" %>
<jsp:directive.page import="org.kldp.ebmsh.config.EbmsProperties"/>
<jsp:directive.page import="org.kldp.ebmsh.setup.EbmsSetupManager"/>
<jsp:directive.page import="org.kldp.ebmsh.setup.EbmsSetupUtils"/>
<jsp:directive.page import="org.kldp.ebmsh.setup.EbmsSetupException"/>
<jsp:directive.page import="org.kldp.util.log.LogUtil"/>

<%
	String action = request.getParameter("action");
	
	if( action == null ) {
		EbmsProperties newEbmsProperties = new EbmsProperties();				
		application.setAttribute("newEbmsProperties", newEbmsProperties);
%>
		<jsp:forward page="/setup/template/template.jsp">
			<jsp:param name="contentPage" value="/setup/view/setupView.jsp"/>
		</jsp:forward>
<%
	}
	
	else if( action.equals("dataBaseSetup") ) {
%>
		<jsp:forward page="/setup/template/template.jsp">
			<jsp:param name="contentPage" value="/setup/view/dataBaseSetupView.jsp"/>
		</jsp:forward>
<%	
	}
	
	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") ) {
%>
		<jsp:forward page="/setup/template/template.jsp">
			<jsp:param name="contentPage" value="/setup/view/queueSetupView.jsp"/>
		</jsp:forward>
<%	
	}
	
	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") ) {
%>
		<jsp:forward page="/setup/template/template.jsp">
			<jsp:param name="contentPage" value="/setup/view/setupConfirmView.jsp"/>
		</jsp:forward>
<%	
	}
	
	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);
%>
		<jsp:forward page="/setup/template/template.jsp">
			<jsp:param name="contentPage" value="/setup/view/setupResultView.jsp"/>
		</jsp:forward>
<%	
	}
%>