<%@ page import="java.io.*, java.util.*"%>
<%@ page language="java" contentType="text/html;charset=utf-8"%>
<%@ page import="com.eactive.eai.adapter.AdapterVO"%>
<%@ page import="com.eactive.eai.agent.command.Command"%>
<%@ page import="com.eactive.eai.agent.testcall.TestCallCommand"%>
<%@ page import="com.eactive.eai.agent.AgentUtil"%>
<%@ page import="com.eactive.eai.common.property.PropManager"%>
<%@ page import="com.eactive.eai.common.testcall.TestCall"%>
<%@ page import="com.eactive.eai.common.testcall.TestCallManager"%>

<%
    TestCallManager   manager   = TestCallManager.getInstance();
    ArrayList         names     = new ArrayList();
    String            action    = request.getParameter("actionType");
    String            period    = request.getParameter("period");
    Properties        prop      = new Properties();
        
    Iterator it = manager.getAllAdapterNames();
    while(it.hasNext()) {
        names.add(it.next());   
    }
    Collections.sort(names);
    
    if(period == null|| period.length()<=0){
        
        PropManager pmanager = PropManager.getInstance();
        long temp = 1000*60*10;
        try {
            temp = Long.parseLong(pmanager.getProperty(manager.PROP_GROUP, manager.PROP_PERIOD));
            
        } catch(Exception e) {}
        
        period  =  ""+temp;
    }
    String run = "";
    
    if("UPDATE".equals(action)) {
        prop.setProperty("PERIOD", period);     
        Command command = new TestCallCommand();
        command.setArgs(prop);
        AgentUtil.broadcast(command);
    }
    if("START".equals(action)){
        run = "START";
        
        prop.setProperty("RUN",run);
        Command command = new TestCallCommand();
        command.setArgs(prop);
        AgentUtil.broadcast(command);   
    }
    if("STOP".equals(action)){
        run = "STOP";
        
        prop.setProperty("RUN",run);
        Command command = new TestCallCommand();
        command.setArgs(prop);
        AgentUtil.broadcast(command);
    }
    
    String status = "";
        
    if(manager.isStarted())
        status = "시작됨";
    else
        status = "종료됨";
%>

<html>
    <head>
        <title>
            TestCall 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 doUpdate() {
            document.frm.actionType.value="UPDATE";
            document.frm.submit();
        }
          
        </script>
    </head>
    <body>
        <p style="font-size:12pt;">
        <b>TestCall Configuration</b> <br><br>
        </p>
        <form name="frm" action="">
        <input type="hidden" name="actionType" value="">
        <table border='0' width="800" >
        <tr>
        <td align='left'>
        <table width="450" border=0 bgcolor='black' cellpadding="1" cellspacing="1">
        <tr bgcolor='white'>
            <td align='right' width='180' bgcolor="#ccccff"><label value="동작주기 : "/>&nbsp;</td>
            <td align='left'>&nbsp;<input type="text" name="period" size='20' value="<%=period%>" ></td>
            <td align='left'> <input type="button" value="UPDATE" onclick="doUpdate()">
        </td>                                                              
        </tr>
        </table>
        </td>
        </tr>
        <tr>
  
        <td align='left'>
            <input type="text" name="period" size='20' value="<%=status%>" readonly >
        </td>
        <td align='left'>
           <input type="button" value="STOP" onclick="doStop()">
        </td>
        <td align='left'>
            <input type="button" value="START" onclick="doStart()">
        </td>
        <td></td><td></td>
        </tr>
        </table><br>
        <table width="800" border=0 bgcolor='black' cellpadding="1" cellspacing="1">
        <tr bgcolor='white'>
            <td align='right' width='350' bgcolor="#003366">&nbsp; <font color="yellow"><b>항목</b> &nbsp;</font></td>
            <td align='left' bgcolor="#003366">&nbsp;<font color="yellow">&nbsp; <b>내용</b></font></td>
        </tr>
<%
    if(names != null || names.size() > 0 ) {
        int length = names.size();
        
        for(int i=0; i<length; i++){
            TestCall tester = manager.getTestCall((String)names.get(i));
            AdapterVO vo    = tester.getAdapterInfo();
%>

        <tr bgcolor='white'>                                                                                                                                                                
            <td align='right' width='350' bgcolor="#ccccff">&nbsp; Adapter Name : &nbsp;</td>                                                                                              
            <td align='left'>&nbsp;<input type="text" name="name" size='60' value="<%=vo.getName()%>" readonly></td>                                                              
        </tr>   
                              
         <tr bgcolor='white'>                                                                                                                                                                
            <td align='right' width='350' bgcolor="#ccccff">&nbsp; Adapter 상태 : &nbsp;</td>                                                                                              
            <td align='left'>&nbsp;<input type="text" name="name" size='60' value="<%=tester.isAlive()%>" readonly></td>                                                              
        </tr>   
 <% 
    } 
}
%>    
        </table>
        </form>
    </body>
</html>