1:package web;
   2:
   3:import business.DefectsManager;
   4:import business.data.Defect;
   5:import java.util.Date;
   6:import javax.servlet.http.HttpServletRequest;
   7:import javax.servlet.http.HttpServletResponse;
   8:import org.apache.commons.beanutils.ConvertUtils;
   9:import org.apache.struts.action.ActionForm;
  10:import org.apache.struts.action.ActionForward;
  11:import org.apache.struts.action.ActionMapping;
  12:import org.apache.struts.action.ActionMessages;
  13:import org.springframework.web.context.WebApplicationContext;
  14:import org.springframework.web.struts.DispatchActionSupport;
  15:
  16:public class DefectsAction extends DispatchActionSupport {
  17:    
  18:    //Needed in Struts1 to successfully pass date from UI to model:
  19:    static
  20:    {
  21:        ConvertUtils.register(new DateConverter(),Date.class);
  22:    }
  23:    
  24:    public ActionForward edit(
  25:            ActionMapping mapping,
  26:            ActionForm form,
  27:            HttpServletRequest request,
  28:            HttpServletResponse response) {
  29:        DefectsActionForm defectsActionForm = (DefectsActionForm) form;
  30:        
  31:        long id = Long.parseLong(request.getParameter("id"));
  32:        
  33:        //Engage business layer...
  34:        WebApplicationContext ctx = getWebApplicationContext();
  35:        DefectsManager mgr = (DefectsManager) ctx.getBean("defectsManager");
  36:        defectsActionForm.setDefect(mgr.getDefect(id));
  37:        
  38:        return mapping.findForward("edit");
  39:    }
  40:    
  41:    public ActionForward delete(
  42:            ActionMapping mapping,
  43:            ActionForm form,
  44:            HttpServletRequest request,
  45:            HttpServletResponse response) {
  46:        
  47:        long id = Long.parseLong(request.getParameter("id"));
  48:        
  49:        WebApplicationContext ctx = getWebApplicationContext();
  50:        DefectsManager mgr = (DefectsManager) ctx.getBean("defectsManager");
  51:        mgr.removeDefect(id);
  52:        
  53:        return mapping.findForward("list");
  54:    }
  55:    
  56:    public ActionForward add(
  57:            ActionMapping mapping,
  58:            ActionForm form,
  59:            HttpServletRequest request,
  60:            HttpServletResponse response) {
  61:        
  62:        return mapping.findForward("add");
  63:    }
  64:    
  65:    public ActionForward save(
  66:            ActionMapping mapping,
  67:            ActionForm form,
  68:            HttpServletRequest request,
  69:            HttpServletResponse response) {
  70:        DefectsActionForm defectsActionForm = (DefectsActionForm) form;
  71:        
  72:        //Validate form...
  73:        ActionMessages errors = form.validate(mapping, request);
  74:        if (!errors.isEmpty()) {
  75:            saveErrors(request, errors);
  76:            
  77:            if (((DefectsActionForm) form).getDefect().getId() == 0) {
  78:                return mapping.findForward("add");
  79:            } else {
  80:                return mapping.findForward("edit");
  81:            }
  82:        } else {
  83:            Defect defect = (Defect)defectsActionForm.getDefect();
  84:            
  85:            WebApplicationContext ctx = getWebApplicationContext();
  86:            DefectsManager mgr = (DefectsManager) ctx.getBean("defectsManager");
  87:            mgr.saveDefect(defect);
  88:        }
  89:        
  90:        return mapping.findForward("list");
  91:    }
  92:    
  93:    /**Cancel the form*/
  94:    public ActionForward cancelled(
  95:            ActionMapping mapping,
  96:            ActionForm form,
  97:            HttpServletRequest request,
  98:            HttpServletResponse response) {
  99:        
 100:        return mapping.findForward("list");
 101:    }
 102:    
 103:}