@RequestMapping(
      value = {"/updateapplication/{id}"},
      method = {RequestMethod.POST})
  public ModelAndView updateapplication(
      @PathVariable Long id,
      @ModelAttribute("application") Application application,
      Model model,
      BindingResult bindingResult,
      HttpSession session,
      Authentication auth) {
    ModelAndView mav = new ModelAndView();
    Application application1 = mechanicService.getApplicationById(id);
    applicationValidator.validate(application, bindingResult);
    if (bindingResult.hasErrors()) {

      UserPrincipal user = userService.getUserByName(auth.getName());
      Number size1 = directorService.getSizeMechanicOnSto(application1.getSto());
      int size = Integer.parseInt(size1.toString());
      mav.addObject("application", application1);
      mav.addObject("statuss", directorService.getStatus());
      mav.addObject("mechanics", directorService.getMechanicsOnSto(application1.getSto(), 0, size));
      mav.addObject("user", user);
      mav.setViewName("director.updateapplication");
      return mav;
    }

    application1.setMechanic(application.getMechanic());
    application1.setStatus(application.getStatus());
    clientService.addOrUpdateApplication(application1);
    mav.setViewName("redirect:/home");
    return mav;
  }
  @PreAuthorize("isFullyAuthenticated()")
  @RequestMapping(value = "/getapplication", method = RequestMethod.GET)
  public ModelAndView getapplicationlist(
      @RequestParam(value = "page", required = false) Integer page,
      HttpSession session,
      Authentication auth) {
    ModelAndView mav = new ModelAndView();
    UserPrincipal user = userService.getUserByName(auth.getName());
    Status status = clientService.getStatusByName("zajavka ozhidaet obrabotku");
    if (page == null) page = 1;
    Integer pageSize = 3;
    Integer startPage = page;
    Integer endPage = page + 5;

    Number size1 = directorService.getSizeApplicationByStatus(status);
    int size = Integer.parseInt(size1.toString());

    Integer lastPage = (size + (pageSize - 1)) / pageSize;

    if (endPage >= lastPage) endPage = lastPage;
    if (endPage >= 5 && (endPage - startPage) < 5) startPage = endPage - 5;

    mav.addObject("page", page);
    mav.addObject("startpage", startPage);
    mav.addObject("endpage", endPage);

    mav.addObject("user", user);
    mav.addObject(
        "application",
        directorService.getApplicationByStatus(status, (page - 1) * pageSize, pageSize));
    mav.setViewName("director.applicationlist");
    return mav;
  };
  @RequestMapping(
      value = {"/updateapplicationdetail/{id}"},
      method = {RequestMethod.POST})
  public ModelAndView updateapplicationdetail(
      @PathVariable Long id,
      @ModelAttribute("applicationdetails") ApplicationDetail applicationDetail,
      BindingResult bindingResult,
      Model model,
      HttpSession session,
      Authentication auth) {
    ModelAndView mav = new ModelAndView();
    applicationDetailValidator.validate(applicationDetail, bindingResult);
    if (bindingResult.hasErrors()) {
      UserPrincipal user = userService.getUserByName(auth.getName());
      mav.addObject("statuss", directorService.getStatus());
      mav.addObject("applicationdetail", directorService.getApplicationDetailById(id));
      mav.addObject("user", user);
      mav.setViewName("director.updateapplicationdetail");
      return mav;
    }

    ApplicationDetail applicationDetail1 =
        directorService.getApplicationDetailById(applicationDetail.getId());
    applicationDetail1.setStatus(applicationDetail.getStatus());
    applicationDetail1.setDateDelivery(applicationDetail.getDateDelivery());
    // applicationDetail.setId(1l);
    directorService.saveApplicationDetail(applicationDetail1);
    mav.setViewName("redirect:/home");
    return mav;
  }
 @RequestMapping(
     value = {"/addmechanic"},
     method = {RequestMethod.POST})
 public ModelAndView addmechanic(
     @ModelAttribute("mechanic") Mechanic mechanic,
     BindingResult bindingResult,
     Model model,
     HttpSession session,
     Authentication auth) {
   ModelAndView mav = new ModelAndView();
   mechanicValidator.validate(mechanic, bindingResult);
   if (bindingResult.hasErrors()) {
     UserPrincipal user = userService.getUserByName(auth.getName());
     mav.addObject("user", user);
     mav.addObject("stos", directorService.getSto());
     mav.setViewName("director.addmechanic");
     return mav;
   }
   mechanic.setLogin(mechanic.getName());
   mechanic.setRating((float) 0);
   mechanic.setRole(UserRole.MECHANIC);
   directorService.saveOrUpdateMechanic(mechanic);
   mav.setViewName("redirect:/home");
   return mav;
 }
  @PreAuthorize("isFullyAuthenticated()")
  @RequestMapping(value = "/mechaniclistbysto/{id}", method = RequestMethod.GET)
  public ModelAndView getmechaniclistbysto(
      @RequestParam(value = "page", required = false) Integer page,
      @PathVariable Long id,
      HttpSession session,
      Authentication auth) {
    ModelAndView mav = new ModelAndView();
    UserPrincipal user = userService.getUserByName(auth.getName());
    Sto sto = directorService.getStoById(id);
    Number size1 = directorService.getSizeMechanicOnSto(sto);
    int size = Integer.parseInt(size1.toString());
    System.out.println("Test test test" + sto.getName());
    if (page == null) page = 1;
    Integer pageSize = 3;
    Integer startPage = page;
    Integer endPage = page + 5;
    Integer lastPage = (size + (pageSize - 1)) / pageSize;

    if (endPage >= lastPage) endPage = lastPage;
    if (endPage >= 5 && (endPage - startPage) < 5) startPage = endPage - 5;

    mav.addObject("page", page);
    mav.addObject("startpage", startPage);
    mav.addObject("endpage", endPage);

    mav.addObject("user", user);
    mav.addObject(
        "mechanic", directorService.getMechanicsOnSto(sto, (page - 1) * pageSize, pageSize));
    mav.setViewName("director.mechaniclistbysto");
    return mav;
  };
  @RequestMapping(
      value = {"/updatesto/{id}"},
      method = {RequestMethod.POST})
  public ModelAndView updatesto(
      @PathVariable Long id,
      @ModelAttribute("sto") Sto sto,
      BindingResult bindingResult,
      Model model,
      HttpSession session,
      Authentication auth) {
    ModelAndView mav = new ModelAndView();
    rentValidator.validate(sto, bindingResult);
    if (bindingResult.hasErrors()) {
      logger.info("Returning updatesto.jsp page");
      UserPrincipal user = userService.getUserByName(auth.getName());
      mav.addObject("user", user);
      mav.addObject("sto", directorService.getStoById(id));
      mav.setViewName("director.updatesto");
      return mav;
    }

    Sto sto1 = directorService.getStoById(id);
    sto1.setName(sto.getName());
    sto1.setPrice(sto.getPrice());
    directorService.addSto(sto1);
    mav.setViewName("redirect:/home");
    return mav;
  }
  @PreAuthorize("isFullyAuthenticated()")
  @RequestMapping(value = "/getmechanics", method = RequestMethod.GET)
  public ModelAndView getmechaniclist(
      @RequestParam(value = "page", required = false) Integer page,
      HttpSession session,
      Authentication auth) {
    ModelAndView mav = new ModelAndView();
    UserPrincipal user = userService.getUserByName(auth.getName());
    if (page == null) page = 1;
    Integer pageSize = 4;
    Integer startPage = page;
    Integer endPage = page + 5;

    Number size1 = directorService.getSizeAllMechanic();
    int size = Integer.parseInt(size1.toString());

    Integer lastPage = (size + (pageSize - 1)) / pageSize;

    if (endPage >= lastPage) endPage = lastPage;
    if (endPage >= 5 && (endPage - startPage) < 5) startPage = endPage - 5;

    mav.addObject("page", page);
    mav.addObject("startpage", startPage);
    mav.addObject("endpage", endPage);

    mav.addObject("user", user);
    mav.addObject("mechanic", directorService.getMechanicsToPage((page - 1) * pageSize, pageSize));
    mav.setViewName("director.mechanicslist");
    return mav;
  };
 @RequestMapping(
     value = {"/deletemechanic/{id}"},
     method = {RequestMethod.GET})
 public String deleteMechanic(@PathVariable Long id, Model model, HttpSession session) {
   Mechanic mechanic = directorService.getMechanicById(id);
   directorService.deleteMechanic(mechanic);
   return "redirect:/home";
 }
 @PreAuthorize("isFullyAuthenticated()")
 @RequestMapping(value = "/updateapplicationdetail/{id}", method = RequestMethod.GET)
 public ModelAndView updateapplicationdetail(
     @PathVariable Long id, HttpSession session, Authentication auth) {
   ModelAndView mav = new ModelAndView();
   UserPrincipal user = userService.getUserByName(auth.getName());
   mav.addObject("statuss", directorService.getStatus());
   mav.addObject("applicationdetail", directorService.getApplicationDetailById(id));
   mav.addObject("user", user);
   mav.setViewName("director.updateapplicationdetail");
   return mav;
 };
 @RequestMapping(
     value = {"/addsalary/{id}"},
     method = {RequestMethod.GET})
 public String addsalaryByMechanic(@PathVariable Long id, Model model, HttpSession session) {
   Mechanic mechanic = directorService.getMechanicById(id);
   Salary salary = new Salary();
   salary.setMechanic(mechanic);
   salary.setSumma(new Float(mechanic.getSalary()));
   salary.setDate(new Date());
   directorService.addSalary(salary);
   return "redirect:/home";
 }
 @PreAuthorize("isFullyAuthenticated()")
 @RequestMapping(value = "/updateapplication/{id}", method = RequestMethod.GET)
 public ModelAndView updateapplication(
     @PathVariable Long id, HttpSession session, Authentication auth) {
   ModelAndView mav = new ModelAndView();
   UserPrincipal user = userService.getUserByName(auth.getName());
   Application application = mechanicService.getApplicationById(id);
   Number size1 = directorService.getSizeMechanicOnSto(application.getSto());
   int size = Integer.parseInt(size1.toString());
   mav.addObject("application", application);
   mav.addObject("statuss", directorService.getStatus());
   mav.addObject("mechanics", directorService.getMechanicsOnSto(application.getSto(), 0, size));
   mav.addObject("user", user);
   mav.setViewName("director.updateapplication");
   return mav;
 };
  @RequestMapping(
      value = {"/addrent"},
      method = {RequestMethod.POST})
  public String addrent(@ModelAttribute("rent") Rent rent, Model model, HttpSession session) {
    rent.setDateStart(new Date());
    rent.setPrice(rent.getSto().getPrice());
    directorService.addRent(rent);

    return "redirect:/home";
  }
  @RequestMapping(
      value = {"/getreport"},
      method = {RequestMethod.POST})
  public ModelAndView getreport(
      @ModelAttribute("reportinfo") ReportInfo reportinfo,
      BindingResult bindingResult,
      Model model,
      HttpSession session,
      Authentication auth) {
    ModelAndView mav = new ModelAndView();
    reportValidator.validate(reportinfo, bindingResult);
    if (bindingResult.hasErrors()) {
      logger.info("Returning getreport.jsp page");
      UserPrincipal user = userService.getUserByName(auth.getName());
      mav.addObject("user", user);

      mav.addObject("stos", directorService.getSto());
      mav.setViewName("director.getreport");
      return mav;
    } else {

      ModelAndView mave = new ModelAndView();
      UserPrincipal user = userService.getUserByName(auth.getName());
      mave.addObject("user", user);
      if (reportinfo.getWhom().equals("sto")) {
        mave.addObject(
            "report",
            directorService.getreportSto(
                reportinfo.getSto(), reportinfo.getDateStart(), reportinfo.getDateFinish()));
        mave.addObject("reportinfo", reportinfo);
        mave.setViewName("director.reportsto");
      } else {
        mave.addObject(
            "report",
            directorService.getreportAll(reportinfo.getDateStart(), reportinfo.getDateFinish()));
        mave.addObject("reportinfo", reportinfo);
        mave.setViewName("director.reportall");
      }

      return mave;
    }
  }
 @PreAuthorize("isFullyAuthenticated()")
 @RequestMapping(value = "/addmechanic", method = RequestMethod.GET)
 public ModelAndView addmechanic(HttpSession session, Authentication auth) {
   ModelAndView mav = new ModelAndView();
   UserPrincipal user = userService.getUserByName(auth.getName());
   mav.addObject("user", user);
   mav.addObject("mechanic", new Mechanic());
   mav.addObject("stos", directorService.getSto());
   mav.setViewName("director.addmechanic");
   return mav;
 };
  @RequestMapping(
      value = {"/addservice"},
      method = {RequestMethod.POST})
  public ModelAndView addservice(
      @ModelAttribute("service") Service service,
      BindingResult bindingResult,
      Model model,
      HttpSession session,
      Authentication auth) {
    ModelAndView mav = new ModelAndView();
    serviceValidator.validate(service, bindingResult);
    if (bindingResult.hasErrors()) {
      logger.info("Returning addservice.jsp page");
      UserPrincipal user = userService.getUserByName(auth.getName());
      mav.addObject("user", user);
      mav.setViewName("director.addservice");
      return mav;
    }

    directorService.addService(service);
    mav.setViewName("redirect:/home");
    return mav;
  }