<%@ 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>

<%
    if ("AddInvestor".equals(request.getParameter("action"))) {
      try {
        cal.addInvestor (request.getParameter("investorname"),
            Integer.parseInt(request.getParameter("investorstart")),
            Integer.parseInt(request.getParameter("investorend")));
      } catch (Exception e) {
%>
<head><meta http-equiv="REFRESH" content="0; URL=error.html"></head>
<%
        return;
      }
    } else if ("Logout".equals(request.getParameter("action"))) {
      ((HttpSession) request.getSession()).invalidate ();
      SecurityAssociation.clear ();
%>
<head><meta http-equiv="REFRESH" content="0; URL=addinvestor.jsp"></head>
<%
      return;
    }
%>

<body>

<p><form action="addinvestor.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="addinvestor.jsp" method="POST">
  名前 : <input type="text" name="investorname" value=""><br/>
  開始年齢 = <input type="text" name="investorstart" value="25">
  終了年齢 = <input type="text" name="investorend" value="65">
  <input type="hidden" name="action" value="AddInvestor"><br/>
  <input type="submit" value="個人投資家を追加する">
  <INPUT type="button" value="閉じる" onClick="window.close()">
</form><br/>

<%
  // Collection <Investor> investors = cal.getInvestors();
  Collection investors = cal.getInvestors();
%>

<b><%=investors.size()%></b>人の個人投資家がデータベースに登録されています。<br/>

<table>
<tr>
<td>名前</td>
<td>開始年齢</td>
<td>終了年齢</td>
</tr>

<%
    for (Iterator iter = investors.iterator(); iter.hasNext();) {
    Investor investor = (Investor) iter.next();
%>

<tr>
<td><%=investor.getName()%></td>
<td><%=investor.getStartAge()%></td>
<td><%=investor.getEndAge()%></td>
</tr>

<%
  }
%>
</table></p>

</body></html>