<%@ 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">
The current user is <b><%=((Principal) SecurityAssociation.getPrincipal()).getName()%></b>
  <input type="hidden" name="action" value="Logout"><br/>
  <input type="submit" value="Change user">
</form></p>

<p>Add a new investor:<br/>
<form action="addinvestor.jsp" method="POST">
  Name : <input type="text" name="investorname" value=""><br/>
  Start age = <input type="text" name="investorstart" value="25">
  End age = <input type="text" name="investorend" value="65">
  <input type="hidden" name="action" value="AddInvestor"><br/>
  <input type="submit" value="Add Investor">
  <INPUT type="button" value="Close Window" onClick="window.close()">
</form><br/>

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

There are <b><%=investors.size()%></b> investors in the database.<br/>

<table>
<tr>
<td>Name</td>
<td>Start age</td>
<td>End age</td>
</tr>

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

<tr>
<td><%=investor.getName()%></td>
<td><%=investor.getStart()%></td>
<td><%=investor.getEnd()%></td>
</tr>

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

</body></html>