<%

/*
 * edit.jsp
 *
 * Allows the user to edit an existing entry. This is a JSP that
 * serves 2 functions, similar to "add.jsp". First of all, when called with
 * only a "name" argument, it will display a table with a few input fields.
 * One for empName, and one for salary. These fields are filled in, and
 * they can be changed by the user.
 *
 * When she submits this information, it is sent to this page again. If the
 * data can successfully be changed, then the user will see the new data in
 * this page, with no error message.
 *
 */
%>
<%@ 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. We will send a few log
// messages to System.out.
PrintStream errorStream = System.err;

// If we find any fatal error, we will store it in the "error" variable. If we
// need to store the exception too, we will store it in "exception".
String error        = null;
Exception exception = null;

// 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;

// 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;

// 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-1
if (home == null) {
   error = "No previous connection to bean.";
} else {

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

   // list.jsp URL must have at least pk as param
   if (param_1 == null) {
      error = "No key specified as a parameter.";
   } else if (param_2 == null || param_3 == null) {
         // get missing columns using pk
         try {
            empNo = new Integer(param_1);
            Employee rec = home.findByPrimaryKey(new EmployeePK(empNo));
            empName = rec.getEmpName();
            salary = rec.getSalary();
         } catch (Exception e) {
      
            exception = e;
            error = "Caught \"" + exception.getClass().getName() +
                    "\" while attempting to get entry with " +
                    "key \"" + empNo + "\".";
            errorStream.println(error);
            exception.printStackTrace(errorStream);
         }

   }

   // edit.jsp(submit) URL must have all updated params
   if (param_1 != null && param_2 != null & param_3 !=null) {
   
      // set no warning   
      warning = "";
      param_1 = param_1.trim();

      if ("".equals(param_1)) {
         warning = "EmpNo not specified. ";
      }
      if ("".equals(param_2)) {
         warning = "EmpName not specified. ";
      }
      if ("".equals(param_3)) {
         warning = "Salary not specified. ";
      }
      
      if ("".equals(warning)) {
         // reset warning
         warning = null;
         try {
            empNo = new Integer(param_1);
            empName = new String(param_2);
            salary = new Float(param_3);
            Employee rec = home.findByPrimaryKey(new EmployeePK(empNo));
            rec.setEmpName(empName);
            rec.setSalary(salary);
         } catch (Exception e) {
      
            exception = e;
            error = "Caught \"" + exception.getClass().getName() +
                    "\" while attempting to get entry with " +
                    "key \"" + empNo + "\".";
            errorStream.println(error);
            exception.printStackTrace(errorStream);
         }
      }
   }
} //end-if-1

// Decide what the title will be.
String title;
// if-2
if (error != null) {
   title = "Error";
} else {
   title = "bmpapp | Edit entry";
}// end-if-2
%>

<!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-3
if (error != null) {
%>

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

<%
  // Otherwise display a table with fields to be filled in.
} else { //else-3
   //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>

<%
    }
      
    // 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="edit.jsp" method="GET">
<INPUT type="hidden" name="EMPNO" value="<%= empNo %>"></INPUT>
<P><TABLE border="1">
<TR>
   <TD><STRONG>EmpNo:</STRONG></TD>
   <TD><%= empNo %></TD>
</TR>
<TR>
   <TD><STRONG>EmpName:</STRONG></TD>
   <TD><INPUT type="text" name="EMPNAME" value="<%= empName %>"></INPUT></TD>
</TR>
<TR>
   <TD><STRONG>Salary:</STRONG></TD>
   <TD><INPUT type="text" name="SALARY" value="<%= salary %>"></INPUT></TD>
</TR>
<TR>
   <TD colspan="3" align="center"><INPUT type="submit" value="Update this entry"></INPUT></TD>
</TR>
</TABLE>
</FORM>

<%
} //end-if-3
%>

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