package net.reumann.demo.action;
import net.reumann.demo.Constants;
import net.reumann.demo.form.EmployeeForm;
import net.reumann.demo.service.DepartmentDaoService;
import net.reumann.demo.service.DepartmentService;
import net.reumann.demo.service.EmployeeDaoService;
import net.reumann.demo.service.EmployeeService;
import net.reumann.demo.vo.Employee;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.*;
import org.apache.struts.actions.DispatchAction;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
public class EmployeeAction extends DispatchAction {
private Log logger = LogFactory.getLog(this.getClass());
private static EmployeeService empService = new EmployeeDaoService();
private static DepartmentService deptService = new DepartmentDaoService();
public ActionForward getEmployees(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
logger.debug("getEmployees");
populateEmployees(request);
return mapping.findForward(Constants.SUCCESS);
}
public ActionForward setUpForInsertOrUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
logger.debug("setUpForInsertOrUpdate");
EmployeeForm employeeForm = (EmployeeForm)form;
if (isUpdate(request, employeeForm)) {
Integer id = Integer.valueOf(employeeForm.getEmployeeId());
Employee employee = empService.getEmployee(id);
BeanUtils.copyProperties(employeeForm, employee);
}
prep(request);
return mapping.findForward(Constants.SUCCESS);
}
public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
logger.debug("delete");
EmployeeForm employeeForm = (EmployeeForm)form;
Integer id = Integer.valueOf(employeeForm.getEmployeeId());
empService.deleteEmployee(id);
populateEmployees(request);
return mapping.findForward(Constants.SUCCESS);
}
public ActionForward insertOrUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
logger.debug("insertOrUpdate");
EmployeeForm employeeForm = (EmployeeForm)form;
if (validationSuccessful(request, employeeForm)) {
Employee employee = new Employee();
BeanUtils.copyProperties(employee, employeeForm);
if (isUpdate(request, employeeForm)) {
logger.debug("update");
empService.updateEmployee(employee);
} else {
logger.debug("insert");
empService.insertEmployee(employee);
}
populateEmployees(request);
return mapping.findForward(Constants.SUCCESS);
} else {
prep(request);
return mapping.findForward(Constants.FAILURE);
}
}
private void populateEmployees(HttpServletRequest request) {
List employees = empService.getAllEmployees();
request.setAttribute(Constants.EMPLOYEES, employees);
prep(request);
}
private void prep(HttpServletRequest request) {
request.setAttribute(Constants.DEPARTMENTS, deptService.getAllDepartments());
}
private boolean isUpdate(HttpServletRequest request, EmployeeForm empForm) {
boolean updateFlag = true;
//if ID is null or 0 we know we are doing an insert. You could check other
//things to decide, like a dispatch param
//It's annoying that BeanUtils will convert nulls to 0 so have to do 0 check also,
//or you could register a converter, which is the preferred way to handle it, but goes
//beyond this demo
String id = empForm.getEmployeeId();
if (id == null || id.trim().length() == 0 || Integer.parseInt(id) == 0) {
updateFlag = false;
}
request.setAttribute("updateFlag", Boolean.valueOf(updateFlag));
return updateFlag;
}
private boolean validationSuccessful(HttpServletRequest request, EmployeeForm form) {
//if you really like using the validation framework stuff, you can just
//call ActionErrors errors = form.validate( mapping, request ); in this method
//and check for errors being empty, if not save them and you're done.
//I end up finding the validation framework a bit annoying to work with, so I do it
//old-Skool way. Inevitably in a more complex app you end up having to perform
//more complex validation than the validation framework provides, so I just assume
//keep it all here in one place, versus having some handled by xml configuration and
//some hardcoded.
boolean isOk = true;
ActionMessages errors = new ActionMessages();
if (form.getAge() == null || form.getAge().trim().length() == 0) {
errors.add("age", new ActionMessage("errors.required", "Age"));
} else {
try {
Integer.parseInt(form.getAge());
} catch (NumberFormatException e) {
errors.add("age", new ActionMessage("errors.number", "Age"));
}
}
if (form.getFirstName() == null || form.getFirstName().trim().length() == 0) {
errors.add("firstName", new ActionMessage("errors.required", "First Name"));
}
if (form.getLastName() == null || form.getLastName().trim().length() == 0) {
errors.add("lastName", new ActionMessage("errors.required", "Last Name"));
}
if (!errors.isEmpty()) {
saveErrors(request, errors);
isOk = false;
}
return isOk;
}
}
评论0