<%@ page contentType="text/html; charset=Shift_JIS" %>
<%@ page import="javax.naming.*,
                 java.text.*,
                 java.util.*, org.jboss.security.*,
                 trail.entity.beans.*, trail.security.*,
                 java.security.Principal"%>

<%!
  private Calculator cal = null;
  private NumberFormat nf = null;

  public void jspInit () {
    try {
      InitialContext ctx = new InitialContext();
      cal = (Calculator) ctx.lookup(
                  "EJB3Trail/SecureCalculator/local");
    } catch (Exception e) {
      e.printStackTrace ();
    }

    nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(2);
  }
%>

<html>

<%
    String result = "Not Valid";
    if ("Calculate".equals(request.getParameter("action"))) {
        double res = -1;
        res = cal.calculate (Integer.parseInt(request.getParameter("fund")),
                             Integer.parseInt(request.getParameter("investor")),
                             Double.parseDouble(request.getParameter("saving")));
        if (res != -1) {
            result = nf.format(res);
        }
    } else if ("Logout".equals(request.getParameter("action"))) {
      ((HttpSession) request.getSession()).invalidate ();
      SecurityAssociation.clear ();
%>
<head><meta http-equiv="REFRESH" content="0; URL=calculator.jsp"></head>
<%
      return;
    }
%>

<body>

<p><form action="calculator.jsp" method="POST">
現在のユーザは <b><%=((Principal) SecurityAssociation.getPrincipal()).getName()%></b> です
  <input type="hidden" name="action" value="Logout"><br/>
  <input type="submit" value="ユーザを変更する">
</form></p>

<p>投資計算プログラム<br/>
<form action="calculator.jsp" method="POST">
  投資会社:
  <select name="fund">
<%
  Collection funds = cal.getFunds ();
  for (Iterator iter = funds.iterator(); iter.hasNext();) {
    Fund fund = (Fund) iter.next();
%>
    <option value="<%=fund.getId()%>"><%=fund.getName()%></option>
<%
  }
%>
  </select>
  と個人投資家:
  <select name="investor">
<%
  Collection investors = cal.getInvestors ();
  for (Iterator iter = investors.iterator(); iter.hasNext();) {
    Investor investor = (Investor) iter.next();
%>
    <option value="<%=investor.getId()%>"><%=investor.getName()%></option>
<%
  }
%>
  </select> を選択する<br/>
  月掛金額 = <input type="text" name="saving" value="100">
  <input type="hidden" name="action" value="Calculate">
  <input type="submit" value="計算">
  <INPUT type="button" value="閉じる" onClick="window.close()">
</form><br/>
合計投資額は <%=result%> です<br/>
<br/>
データベース内の過去の計算記録<br/>
<table>
<tr>
<td>タイムスタンプ</td>
<td>投資会社</td>
<td>個人投資家</td>
<td>月掛金額</td>
<td><b>合計投資額</b></td>
</tr>

<%
    Collection records = cal.getRecords ();
    for (Iterator iter = records.iterator(); iter.hasNext();) {
        TimedRecord record = (TimedRecord) iter.next();
%>

<tr>
<td><%=record.getTs()%></td>
<td><%=record.getFund().getName()%></td>
<td><%=record.getInvestor().getName()%></td>
<td><%=nf.format(record.getSaving())%></td>
<td><%=nf.format(record.getResult())%></td>
</tr>

<%
    }
%>
</table>

</p>

</body></html>