<%@ 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 ("AddFund".equals(request.getParameter("action"))) {
      try {
        cal.addFund (request.getParameter("fundname"),
            Double.parseDouble(request.getParameter("fundrate")));
      } 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=addfund.jsp"></head>
<%
      return;
    }
%>

<body>

<p><form action="addfund.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="addfund.jsp" method="POST">
  投資会社名 : <input type="text" name="fundname" value="">
  年成長率 : <input type="text" name="fundrate" value="0.05">
  <input type="hidden" name="action" value="AddFund"><br/>
  <input type="submit" value="投資会社を追加する">
  <INPUT type="button" value="閉じる" onClick="window.close()">
</form><br/>

<%
  // Collection <Fund> funds = cal.getFunds();
  Collection funds = cal.getFunds();
%>

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

<table>
<tr>
<td>投資会社名</td>
<td>年成長率</td>
</tr>

<%
    for (Iterator iter = funds.iterator(); iter.hasNext();) {
    Fund fund = (Fund) iter.next();
%>

<tr>
<td><%=fund.getName()%></td>
<td><%=nf.format(fund.getGrowthrate())%></td>
</tr>

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

</body></html>