<%

/*
 * list.jsp
 *
 * Lists all the entries stored through EmployeeBean. This JSP is the only JSP
 * that will actually connect to the entity bean. On success, it will save a
 * reference to the entity bean in the session. So there will be one reference
 * to the bean per session.
 *
 */
%>
<%@ page import="cmpapp.*,java.io.*,java.net.*,java.util.*,javax.naming.*,javax.rmi.*" %>
<%

// Make sure this page will not be cached by the browser
response.addHeader("Pragma", "no-cache");
response.addHeader("Cache-Control", "no-store");

// We will send error messages to System.err, for verbosity. In a real
// application you will probably not want this.
PrintStream errorStream = System.err;

// If we find any fatal error, we will store it in the "error" variable. If
// an exception is caught that corresponds with this error message, then we
// will store it in the "exception" variable.
String error        = null;
Exception exception = null;

// First check if the reference to the EJB is already stored in the session.
EmployeeHome home = (EmployeeHome) session.getAttribute("cmp.EmployeeHome");

// If not, then attempt to get the initial JNDI context.
if (home == null) {

   // When attempting to connect to JNDI, we store the reference to the
   // initial JNDI context in this variable. We will use it to lookup the
   // entity bean.
   Context context = null;

   try {
      context = new InitialContext();
   } catch (Exception e) {
      exception = e;
      error = "Caught \"" + exception.getClass().getName() + "\" while " +
              "attempting to create the initial JNDI context.";
      errorStream.println(error);
      exception.printStackTrace(errorStream);
   }

   // We have specified "EmployeeBean" in the web.xml file as the name
   // by which we would like to contact the EmployeeBean home interface. We will
   // have to prepend "java:comp/env/", the root `directory' for enterprise
   // beans.
   final String location = "java:comp/env/EmployeeBean";
   if (error == null) {
      try {
         // Attempt to lookup an object at the specified location in the JNDI
         // context.
         Object boundObject = context.lookup(location);

         // Try to convert it to an instance of EmployeeBean, the home
         // interface for our bean.
         home = (EmployeeHome) PortableRemoteObject.narrow(boundObject,
                                                           EmployeeHome.class);

         // If we got this far, we've done it, let's save the reference to the
         // Employee home interface in the session for future use by both
         // this page and the other JSP pages.
         session.setAttribute("cmp.EmployeeHome", home);

      } catch (Exception e) {
         exception = e;
         error = "Caught \"" + exception.getClass().getName() + "\" while " + 
                 "attempting to lookup the Employee bean at \"" +
                 location + "\".";
         errorStream.println(error);
         exception.printStackTrace(errorStream);
      }
   }
}

// This is the variable we will store all records in.
Collection recs = null;

if (error == null) {
   try {
      recs = home.findAll();
   } catch (Exception e) {
      exception = e;
      error = "Caught \"" + exception.getClass().getName() + "\" while " + 
              "attempting to find all EmployeeBean entries.";
      errorStream.println(error);
      exception.printStackTrace(errorStream);
   }

}

// Decide what the title will be.
String title;
if (error != null) {
   title = "Error";
} else {
   title = "cmpapp | List of entries";
}
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>
<TITLE><%= title %></TITLE>
</HEAD>
<BODY bgcolor="#FFFFFF">
<H1><%= title %></H1>

<%
   // Display the error message, if any.
   if (error != null) {
%>

<P><BLOCKQUOTE><%= error %></BLOCKQUOTE>

<%
      // Display the exception message, if any.
      if (exception != null) {
%>

<P><BLOCKQUOTE><CODE><%= exception %></CODE></BLOCKQUOTE>

<%
      } /* if */

   } else {

      // If there are no recs to be displayed, display a descriptive text.
      if (recs.size() == 0) {
%>

<P><BLOCKQUOTE>No entries found.</BLOCKQUOTE>

<%
      // Otherwise display a table with all columns, and
      // display two extra choices: "Edit" and "Delete".
      } else {
%>

<P><TABLE border="1" width="100%">
<TR>
   <TD><STRONG>EmpNo (Integer)</STRONG></TD>
   <TD><STRONG>EmpName (String)</STRONG></TD>
   <TD><STRONG>Salary (Float)</STRONG></TD>
   <TD><STRONG>Actions</STRONG></TD>
</TR>

<%
         Iterator iterator = recs.iterator();
         while(iterator.hasNext()) {
	   Employee rec =
	     (Employee) PortableRemoteObject.narrow(iterator.next(),
                                                    Employee.class);
            Integer empNo    = rec.getEmpNo();
            String empName     = rec.getEmpName();
            Float salary      = rec.getSalary();

            // put all pk columns in hashtable for URLEncoding
            Hashtable cols = new Hashtable();
            cols.put("EMPNO", empNo.toString());

            // URLEncode columns as params to JSP
            StringBuffer buf = new StringBuffer();
            Enumeration params = cols.keys();
            while (params.hasMoreElements()) {
              String param = (String)params.nextElement();
              String value = (String)cols.get(param);
              buf.append(URLEncoder.encode(param) +
                "=" + URLEncoder.encode(value));
              if (params.hasMoreElements())
                buf.append("&");
            }

            String editURL   = "edit.jsp?"   + buf;
            String deleteURL = "delete.jsp?" + buf;
%>

<TR>
   <TD><%= empNo %></TD>
   <TD><%= empName %></TD>
   <TD><%= salary %></TD>
   <TD><A href="<%= editURL %>">Edit</A>&nbsp;<A href="<%= deleteURL %>">Delete</A></TD>
</TR>

<%
         } /* for */
%>

</TABLE>

<%
      } /* else */
   } /* else */

   // Finally display a link to the page that allows the user to add an entry
   // to the address book.
%>

<P><TABLE border="1">
<TR><TD><A href="add.jsp">Add entry</A></TD></TR>
</TABLE>
</BODY>
</HTML>
