<%@ page import="javax.jms.*,com.eactive.eai.flowcontrol.jms.*"%>
<%@ page language="java" contentType="text/html;charset=utf-8"%>
<%
	String receiverName = request.getParameter("receiverName");
    String queueName = request.getParameter("queueName");
    String errorQueueName = request.getParameter("errorQueueName");
    String receiverCount = request.getParameter("receiverCount");
    String delayMs = request.getParameter("delayMs");
    String retryCnt = request.getParameter("retryCnt");
    String selector = request.getParameter("selector");
    String descr = request.getParameter("descr");
    
    String sreceiverName = request.getParameter("s_receiverName");
    String actionType = request.getParameter("actionType");

	String[][] monitorDataTotal = null;
	String[][] monitorData = null;

	FCQueueReceiverManager man = FCQueueReceiverManager.getInstance();
	System.out.println("actionType    = " + actionType);

	
	System.out.println("sreceiverName = " + sreceiverName);
	
	String errorMsg = null;
	
	String[] receiverNames = null;
	
	try {
		if("START".equals(actionType)) {
			System.out.println("START : receiverName = " + receiverName);
			int ireceiverCount = Integer.parseInt(receiverCount);
			int idelayMs = Integer.parseInt(delayMs);
			int iretryCnt = Integer.parseInt(retryCnt);
			FCQueueReceiverVO vo = new FCQueueReceiverVO();
			vo.setReceiverName(receiverName);
			vo.setQueueName(queueName); 
			vo.setErrorQueueName(errorQueueName);
			vo.setReceiverCount(ireceiverCount); 
			vo.setErrorDelayTimeMs(idelayMs); 
			vo.setRetryCount(iretryCnt); 
			vo.setMessageSelector(selector);
			vo.setDescription(descr);
			
			man.setFCQueueReceiver(vo);
		}
		else if("STOP".equals(actionType)) {
			System.out.println("STOP : receiverName = " + sreceiverName);
			man.removeFCQueueReceiver(sreceiverName);
		}
		else if("STATUS".equals(actionType)) {
			System.out.println("STATUS : receiverName = " + sreceiverName);
			monitorDataTotal = man.getMonitorData();
			monitorData      = man.getMonitorData(sreceiverName);
			FCQueueReceiverVO vo = man.getFCQueueReceiver(sreceiverName);
			receiverName   = vo.getReceiverName();
		    queueName      = vo.getQueueName();
		    errorQueueName = vo.getErrorQueueName();
		    receiverCount  = "" + vo.getReceiverCount();
		    delayMs        = "" + vo.getErrorDelayTimeMs();
		    retryCnt       = "" + vo.getRetryCount();
		    selector       = vo.getMessageSelector();
		    descr          = vo.getDescription();
		}
		else if("RESET".equals(actionType)) {
			System.out.println("RESET : receiverName = " + sreceiverName);
			man.resetCount(sreceiverName);
			
		}
		else {
			System.out.println("Unhandled Action " + actionType);
		}
		
		System.out.println("receiverName  = " + receiverName);
		System.out.println("queueName     = " + queueName);
		System.out.println("errorQueueName= " + errorQueueName);
		System.out.println("receiverCount = " + receiverCount);
		System.out.println("delayMs       = " + delayMs);
		System.out.println("retryCnt      = " + retryCnt);
		System.out.println("selector      = " + selector);
		System.out.println("descr         = " + descr);
		
	} catch(Exception e) {
		errorMsg = e.getMessage();
		//e.printStackTrace();
	}
	
	try {
		receiverNames = man.getAllReceiverNames();
	} catch(Exception e) {
		errorMsg = e.getMessage();
		//e.printStackTrace();
	}	
%>
<html>
    <head>
        <title>
            FC Queue Receiver Manager
        </title>
        <script>
        function doAction() {
            document.frm.submit();
        }

        function doStop() {
            document.frm.actionType.value="STOP";
            document.frm.submit();
        }

        function doStart() {
            document.frm.actionType.value="START";
            document.frm.submit();
        }

        function doStatus() {
            document.frm.actionType.value="STATUS";
            document.frm.submit();
        }
		
		function doReset() {
            document.frm.actionType.value="RESET";
            document.frm.submit();
        }
        </script>
    </head>
    <body>
        <p style="font-size:12pt;">
        <b>FC Queue Receiver Manager</b>
        </p>
        <form name="frm" action="">
        <input type="hidden" name="actionType" value="">
        <table border='0' width="800" >
        <tr>
        <td colspan="5" align='left'>
<%
	if(errorMsg != null) {
%>
<font color=red>Error : <%= errorMsg %></font>
<%
	}
%>        	
        	</td>
        </tr>
        <tr>
        <td width="200" align='left'>* Receiver Name</td>
        <td align='left'><input type="text" name="receiverName" size='40' value="<%= receiverName==null?"":receiverName %>" ></td>
        </tr>
        
        <tr>
        <td width="200" align='left'>* Queue Name</td>
        <td align='left'><input type="text" name="queueName" size='40' value="<%= queueName==null?"":queueName %>" ></td>
        </tr>
        <td width="200" align='left'>* Error Queue Name</td>
        <td align='left'><input type="text" name="errorQueueName" size='40' value="<%= errorQueueName==null?"":errorQueueName %>" ></td>
        </tr>
        <tr>
        <td width="200" align='left'>* Queue Thread Count</td>
        <td align='left'><input type="text" name="receiverCount" size='40' value="<%= receiverCount==null?"":receiverCount %>" ></td>
        </tr>
        <tr>
        <td width="200" align='left'>* Error Delay Time(ms)</td>
        <td align='left'><input type="text" name="delayMs" size='40' value="<%= delayMs==null?"":delayMs %>" ></td>
        </tr>
        <td width="200" align='left'>* Retry Count</td>
        <td align='left'><input type="text" name="retryCnt" size='40' value="<%= retryCnt==null?"":retryCnt %>" ></td>
        </tr>
        <tr>
        <td width="200" align='left'>Selector</td>
        <td align='left'><input type="text" name="selector" size='40' value="<%= selector==null?"":selector %>" ></td>
        </tr>
        <tr>
        <td colspan="2" align='left'>Selector keys( EAIBWKCLS, EAISVCCD, OSIDINSTCD )</td>
        </tr>
        <tr>
        <td width="200" align='left'>Description</td>
        <td align='left'><input type="text" name="descr" size='40' value="<%= descr==null?"":descr %>" ></td>
        </tr>
        <tr>
  		<td align='left'>
            <select name="s_receiverName">
<%
	for(int i=0; i<receiverNames.length; i++) {
%>
            	<option value="<%=receiverNames[i]%>" <%= receiverNames[i].equals(sreceiverName)?"selected":"" %> ><%=receiverNames[i]%>
<%
	}
%>			</select>
            
        </td>
        <td align='right'>
           <input type="button" value="STOP" onclick="doStop()">
            <input type="button" value="START" onclick="doStart()">
            <input type="button" value="STATUS" onclick="doStatus()">
            <input type="button" value="RESET" onclick="doReset()">
        </td>
        </tr>
        
        </table><br>
        <table width="800" border=0 bgcolor='black' cellpadding="1" cellspacing="1">
        <tr bgcolor='white'>
           <td align='center' width='400' bgcolor="#ccccff">Receiver Name</td>
           <td align='center' width='200' bgcolor="#ccccff">MSG COUNT</td>
           <td align='center' width='200' bgcolor="#ccccff">SUCCESS</td>
        </tr>
<%
	if(monitorDataTotal != null) {
		for(int i=0; i<monitorDataTotal.length; i++) {
%>
        <tr bgcolor='white'>
            <td align='right' width='400' bgcolor="#ffffff"><%= monitorDataTotal[i][0] %>&nbsp;</td>
            <td align='right' width='200' bgcolor="#ffffff"><%= monitorDataTotal[i][1] %>&nbsp;</td>
            <td align='right' width='200' bgcolor="#ffffff"><%= monitorDataTotal[i][2] %>&nbsp;</td>
        </tr>
<%
		}
	}
%>
        </table><br>
        Detail : <%= sreceiverName %><br>
        STATUS : 0 -Wait, 1-Runnung, 2- Retry<br>
        <table width="800" border=0 bgcolor='black' cellpadding="1" cellspacing="1">
        <tr bgcolor='white'>
           <td align='center' width='500' bgcolor="#ccccff">Receiver Name</td>
           <td align='center' width='100' bgcolor="#ccccff">MSG COUNT</td>
           <td align='center' width='100' bgcolor="#ccccff">SUCCESS</td>
           <td align='center' width='100' bgcolor="#ccccff">STATUS</td>
        </tr>
<%
	if(monitorData != null) {
		for(int i=0; i<monitorData.length; i++) {
%>
        <tr bgcolor='white'>
            <td align='right' width='400' bgcolor="#ffffff"><%= monitorData[i][0] %>&nbsp;</td>
            <td align='right' width='200' bgcolor="#ffffff"><%= monitorData[i][1] %>&nbsp;</td>
            <td align='right' width='200' bgcolor="#ffffff"><%= monitorData[i][2] %>&nbsp;</td>
            <td align='right' width='200' bgcolor="#ffffff"><%= monitorData[i][3] %>&nbsp;</td>
        </tr>
<%
		}
	}
%>
        </table>
        </form>
    </body>
</html>