package com.jspdev.ch17;

import org.apache.struts.action.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public final class AddEmployeeAction extends Action {

    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
        throws Exception {
        EmployeeBean service = new EmployeeBean();
        EmployeeForm employeeForm = (EmployeeForm) form;
        Employee employee = new Employee();
        employee=employeeForm.getEmployee();
        try {
            service.addEmployee( employee);
            ActionMessages messages = new ActionMessages();
            ActionMessage message = new ActionMessage("message.employee.insert.success",employee.getName() );
            messages.add( ActionMessages.GLOBAL_MESSAGE, message );
            saveMessages( request, messages );
            request.setAttribute("employee",employee);
            return (mapping.findForward("success"));
        }
        catch( Exception de ) {
            ActionErrors errors = new ActionErrors();
            ActionError error = new ActionError("error.employee.databaseException");
            errors.add( ActionErrors.GLOBAL_ERROR, error );
            saveErrors( request, errors );
            return (mapping.findForward("error"));
        }
    }
}