<%

/*
 * add.jsp
 *
 * Adds a new entry through EmployeeBean. 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 empNo, empName and
 * salary of the new record to be added. When 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 her.
 *
 */
%>
<%@ page import="bmpapp.*,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 all columns were passed to this page
String param_1 = "";
String param_2 = "";
String param_3 = "";

Integer empNo = null;
String empName = null;
Float salary = null;

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

// We will first attempt to get the reference to EmployeeHome from the
// session. The "list.jsp" page sets this attribute in the session.
EmployeeHome home = (EmployeeHome) session.getAttribute("bmp.EmployeeHome");
if (home == null) {
   error = "No previous connection to EmployeeBean.";
} else {

   // Attempt to get all 3 parameters from the session
   param_1 = request.getParameter("EMPNO");
   param_2 = request.getParameter("EMPNAME");
   param_3 = request.getParameter("SALARY");

   // 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 (param_1 != null && param_2 != null && param_3 != null) {
      param_1 = param_1.trim();
      param_2 = param_2.trim();
      param_3 = param_3.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 param_1, param_2 and/or param_3, then this will be noted
   // in the warning message.
   if ("".equals(param_1)) {
      warning = "Null param_1 specified. ";
   }
   if ("".equals(param_2)) {
      warning += "Null param_2 specified. ";
   }
   if ("".equals(salary)) {
      warning += "Null param_3 specified.";
   }

   // If we don't have a warning message yet, then we will attempt to create
   // a new record.
   if ("".equals(warning)) {
      try {
         empNo = new Integer(param_1);
         empName = new String(param_2);
         salary = new Float(param_3);
         Employee rec = (Employee) home.create(empNo, empName, salary);

         // empty columns after insert for effect
         param_1 = "";
         param_2 = "";
         param_3 = "";

         // 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 = "bmpapp | 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>EmpNo:</STRONG></TD>
   <TD><INPUT type="text" name="EMPNO" value="<%= param_1 %>"></INPUT></TD>
</TR>
<TR>
   <TD><STRONG>EmpName:</STRONG></TD>
   <TD><INPUT type="text" name="EMPNAME" value="<%= param_2 %>"></INPUT></TD>
</TR>
<TR>
   <TD><STRONG>Salary:</STRONG></TD>
   <TD><INPUT type="text" name="SALARY" value="<%= param_3 %>"></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>
