/* (non-Javadoc)
   * @see org.apache.struts.actions.DispatchAction#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
   */
  @Override
  public ActionForward execute(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {

    if (request.getSession().getAttribute("userId") == null) {

      System.out.println("userid123" + request.getSession().getAttribute("userId"));
      response.sendRedirect("/sec/Login.do");
    }
    Long aptid = (Long) request.getSession().getAttribute("aptID");
    MaintenanceRequestForm maintenfrm = (MaintenanceRequestForm) form;
    ApartmentDAO aptDAO = new ApartmentDAO();
    Apartment Apt = aptDAO.findById(aptid);
    maintenfrm.setApartment(Apt.getApartmentNumber());
    HttpSession session = request.getSession();
    session.setAttribute("ApartmentNo", maintenfrm.getApartment());
    System.out.println("userid" + request.getSession().getAttribute("userId"));

    System.out.println("a:" + request.getParameter("r:" + mapping.getParameter()));
    if (FormUtil.isNotNull(request.getParameter(mapping.getParameter()))
        && request.getParameter(mapping.getParameter()).equals("Submit")) {
      System.out.println("/////" + request.getParameter(mapping.getParameter()));
      return this.submit(mapping, form, request, response);
    } else {
      UserDAO userDAO = new UserDAO();
      User user = userDAO.findById((Long) request.getSession().getAttribute("userId"));
      MaintenanceRequestForm requestForm = new MaintenanceRequestForm();
      requestForm = (MaintenanceRequestForm) form;
      requestForm.setContactNo(user.getHomePhone());
      return mapping.findForward("input");
    }
  }
  /**
   * @param maintenanceRequestForm
   * @param maintenanceRequest
   * @param aptid
   * @param userName
   * @return
   */
  private MaintenanceRequest mapFormToVO(
      MaintenanceRequestForm maintenanceRequestForm,
      MaintenanceRequest maintenanceRequest,
      Long aptid,
      String userName) {

    ApartmentDAO aptDAO = new ApartmentDAO();
    Apartment apartment = aptDAO.findById(aptid);
    maintenanceRequest.setApartment(apartment);
    maintenanceRequest.setProperty(apartment.getBuilding().getProperty());
    maintenanceRequest.setMaintenanceDay(FormUtil.getDate(new Date()));
    maintenanceRequest.setMaintenanceMonth(FormUtil.getMonth(new Date()));
    maintenanceRequest.setMaintenanceYear(FormUtil.getYear(new Date()));
    maintenanceRequest.setProblem(maintenanceRequestForm.getDescription());
    maintenanceRequest.setMaintenanceLocation(maintenanceRequestForm.getMaintenanceLocation());
    maintenanceRequest.setDayTimeContactNo(maintenanceRequestForm.getContactNo());
    maintenanceRequest.setMaintenanceRequest(maintenanceRequestForm.getMaintenanceRequest());
    maintenanceRequest.setEntryPermission(maintenanceRequestForm.isPermission());
    maintenanceRequest.setCreatedBy(userName);
    maintenanceRequest.setCreatedDate(new Timestamp(new Date().getTime()));
    maintenanceRequest.setModifiedBy(userName);
    maintenanceRequest.setModifiedDate(new Timestamp(new Date().getTime()));
    return maintenanceRequest;
  }