www.pudn.com > employees.rar > GetEmployeeAction.java


package com.wrox;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;

public class GetEmployeeAction extends Action {

  protected ActionForm buildEmployeeForm(String username, HttpServletRequest request)
    throws Exception {

    EmployeeForm form = null;

    Employee employee = EmployeeData.getEmployee(username, getDataSource(request));

    if ( employee != null ) {

      form = new EmployeeForm();

      form.setUsername(employee.getUsername());
      form.setPassword(employee.getPassword());
      form.setDepid((employee.getDepid()).toString());
      form.setRoleid(employee.getRoleid().toString());
      form.setName(employee.getName());
      form.setPhone(employee.getPhone());
      form.setEmail(employee.getEmail());
    }
    else {

      throw new Exception("Employee " + username + " not found!");
    }
    return form;
  }

  public ActionForward execute(ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException {

    // Default target to success
    String target = new String("success");

    if ( isCancelled(request) ) {

      // Cancel pressed back to employee list
      return (mapping.findForward(target));
    }

    try {

      form = buildEmployeeForm(request.getParameter("username"), request);

      if ( "request".equals(mapping.getScope()) ) {

        request.setAttribute(mapping.getAttribute(), form);
      }
      else {

        HttpSession session = request.getSession();
        session.setAttribute(mapping.getAttribute(), form);
      }
    }
    catch ( Exception e ) {

      System.err.println("Setting target to error");
      System.err.println("---->" + e.getMessage() + "<----");
      target = new String("error");
      ActionErrors errors = new ActionErrors();

      errors.add(ActionErrors.GLOBAL_ERROR,
        new ActionError("errors.database.error", e.getMessage()));

      // Report any errors
      if ( !errors.isEmpty() ) {

        saveErrors(request, errors);
      }
    }
    // Forward to the appropriate View
    return (mapping.findForward(target));
  }
}