<%

/*
 * add.jsp
 *
 * Adds a new entry to the address book. This is a JSP that serves 2
 * functions. First of all, when called with no arguments, it will display a
 * table with a few input fields. The user should enter the name, address and
 * city of the new address entry to be added. When he/she submits this
 * information, it is sent to this page again. If it is successful, then the
 * user can continue adding new entries. If it is not, then the old data will
 * be displayed, and a warning message will be shown to the user.
 *
 * Version: $Revision: 1.1 $ $Date: 2001/11/10 00:22:17 $
 * Authors: Ernst de Haan (ernst@jollem.com)
 */
%>
<%@ page import="addressbook.ejb.*,java.io.*,java.util.*,javax.naming.*" %>
<%

// 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 this variable.
String error = null;

// In a moment we will check if the name, address and city were passed to this
// page. If they were not, then these variables will be set to contain the
// empty string ("").
String name    = "";
String address = "";
String city    = "";

// This variable indicates what function of this page is currently used. If
// this page is called with parameters, then a new address entry should be
// added. In that case this variable is true.
boolean submitting = false;

// We will first attempt to get the reference to the address book from the
// session. The "list.jsp" page sets this attribute in the session.
AddressBook addressBook = (AddressBook) session.getAttribute("AddressBook");
if (addressBook == null) {
   error = "No connection with the AddressBook bean established.";
} else {

   // Attempt to get all 3 parameters from the session
name    = request.getParameter("name");
   address = request.getParameter("address");
   city    = request.getParameter("city");

   // If all 3 parameters are specified, then this is probably a submission by
// this very page. Note that if the user left one of the fields blank, then
// the corresponding parameter will be "", not null.
if (name != null && address != null && city != null) {
      name    = name.trim();
      address = address.trim();
      city    = city.trim();
      submitting = true;
   }
}

// In the following variable we will store a (non-fatal) warning message. This
// message will be displayed in the page, but so will the submission form.
String warning = null;

if (submitting) {
   warning = "";

   // If there is an empty name, address and/or city, then this will be noted
// in the warning message.
if ("".equals(name)) {
      warning = "No name specified. ";
   }
   if ("".equals(address)) {
      warning += "No address specified. ";
   }
   if ("".equals(city)) {
      warning += "No city specified.";
   }

   // If we don't have a warning message yet, then we will attempt to create
// the new address entry.
if ("".equals(warning)) {
      try {
         addressBook.create(name, address, city);

         // We will only get here if the previous statement succeeded. If it
// did succeed, then the fields on the page should be empty, and the
// warning message will be set to null, for easy comparison checking
// later on.
name    = "";
         address = "";
         city    = "";

         // If we got this far, then there was no problem detected.
warning = null;
      } catch (Exception e) {

         // Set the warning variable to indicate a problem.
warning = "Unable to create new entry, caught: \"" +
                   e.getClass().getName() + "\", message is: \"" +
                   e.getMessage() + "\".";
      }
   }
}

// Decide what the title will be.
String title;
if (error != null) {
   title = "Error";
} else {
   title = "Addressbook | Add entry";
}
%>

<!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>

<%
// If there was a fatal error, then display the error message
if (error != null) {
%>

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

<%
// Otherwise display a table with fields to be filled in.
} else {

      // If there was a warning, then display it.
if (warning != null) {
%>

<TABLE border="1" bgcolor="#FF2222">
<TR><TD><FONT color="#FFFFFF"><STRONG>Warning:&nbsp;<%= warning %></STRONG></FONT></TD></TR>
</TABLE>

<%
} /* if */

// Display the table with fields. There are two columns. The left column
// contains the names of the fields, while the right column contains the
// fields.
%>

<FORM action="add.jsp" method="GET">
<P><TABLE border="1">
<TR>
<TD><STRONG>Name:</STRONG></TD>
<TD><INPUT type="text" name="name" value="<%= name %>"></INPUT></TD>
</TR>
<TR>
<TD><STRONG>Address:</STRONG></TD>
<TD><INPUT type="text" name="address" value="<%= address %>"></INPUT></TD>
</TR>
<TR>
<TD><STRONG>City:</STRONG></TD>
<TD><INPUT type="text" name="city" value="<%= city %>"></INPUT></TD>
</TR>
<TR>
<TD colspan="3" align="center"><INPUT type="submit" value="Add this entry"></INPUT></TD>
</TR>
</TABLE>
</FORM>

<%
} /* else */
%>

<P><TABLE border="1">
<TR><TD><A href="list.jsp">Back&nbsp;to&nbsp;list</A></TD></TR>
</TABLE>
</BODY>
</HTML>