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

<%!
  private NumberFormat nf = null;

  public void jspInit () {
    nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(2);
  }
%>

<%
  Calculator cal =
      (Calculator) session.getAttribute("apptrans_cal");
  if (cal == null) {
    try {
      InitialContext ctx = new InitialContext();
      cal = (Calculator) ctx.lookup(
                  "EJB3Trail/ApptransCalculator/local");
      session.setAttribute ("apptrans_cal", cal);
    } catch (Exception e) {
      e.printStackTrace ();
    }
  }

  if ("Update".equals(request.getParameter("action"))) {
    cal.updateExchangeRate(
          Double.parseDouble(
              request.getParameter("newrate")));
%>
<html>
<head><meta http-equiv="REFRESH" content="3; URL=update2.jsp"></head>
  <body>
    しばらくお待ちください...
  </body>
</html>
<%
  } else {
%>

<html><body>

<p>新しい為替で計算記録を更新する<br/>
<form action="update.jsp" method="POST">
  変換レート = <input type="text" name="newrate" value="1.1">
  <input type="hidden" name="action" value="Update"><br/>
  <input type="submit" value="更新">
  <INPUT type="button" value="閉じる" onClick="window.close()">
</form><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>

<%
  }
%>