<%@ page contentType="text/html; charset=Shift_JIS" %>
<%@ page import="trail.mdpojo.*, javax.naming.*, java.text.*,
                 org.jboss.ejb3.mdb.*"%>

<%
  if ("send".equals(request.getParameter ("action"))) {

    int start = Integer.parseInt(request.getParameter ("start"));
    int end = Integer.parseInt(request.getParameter ("end"));
    double growthrate = Double.parseDouble(request.getParameter ("growthrate"));
    double saving = Double.parseDouble(request.getParameter ("saving"));

    // The sent timestamp acts as the message's ID
    long sent = System.currentTimeMillis();

    Calculator cal = null;
    ProducerManager manager = null;
    try {

      InitialContext ctx = new InitialContext();
      cal = (Calculator) ctx.lookup(
                  Calculator.class.getName());
      // cal = (Calculator) ctx.lookup(
      //             "EJB3Trail/MdpojoCalculator/remote");
      ProducerObject po = (ProducerObject) cal;
      manager = po.getProducerManager();

    } catch (Exception e) {
      e.printStackTrace ();
    }

    manager.connect(); // internally create a JMS connection
    try {
      cal.doCalculation(sent, start, end,
                        growthrate, saving);
    } finally {
      manager.close(); // clean up the JMS connection
    }

%>

<html>
<head><meta http-equiv="REFRESH" content="3;
                URL=check.jsp?sent=<%=sent%>"></head>
  <body>
    しばらくお待ちください。メッセージが届いているか確認中です。<br/>
    <a href="calculator.jsp">計算プログラムに戻る</a>
  </body>
</html>

<%
    return;

  } else {

    int start = 25;
    int end = 65;
    double growthrate = 0.08;
    double saving = 300.0;
%>

<html>
<body>
<p>投資計算プログラム<br/>
<form action="calculator.jsp" method="POST">
  <input type="hidden" name="action" value="send">
  開始年齢 = <input type="text" name="start" value="<%=start%>"><br/>
  終了年齢 = <input type="text" name="end" value="<%=end%>"><br/>
  年成長率 = <input type="text" name="growthrate" value="<%=growthrate%>"><br/>
  月掛金額 = <input type="text" name="saving" value="<%=saving%>"><br/>
  <input type="submit" value="計算">
  <INPUT type="button" value="閉じる" onClick="window.close()">
</form>
</p>
</body>
</html>

<%
    return;
  }
%>