Advertisement
Guest User

Untitled

a guest
Jul 31st, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. @RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
  2.     public ModelAndView edditingUser(Model model, @ModelAttribute User user,
  3.             BindingResult result, @PathVariable Long id) {
  4.         ModelAndView modelAndView = null;
  5.  
  6.         try { // tymczasowe, TODO: ustawienie avatara podczas edycji usera
  7.             user.setAvatar(userService.getUser(user.getId()).getAvatar());
  8.         } catch (BusinessException e) {
  9.             log.error("setting old avatar", e);
  10.         }
  11.  
  12.         UsersValidator userValidator = new UsersValidator();
  13.         userValidator.validate(user, result);
  14.         if (result.hasErrors()) {
  15.             modelAndView = editUserPage(model, id);
  16.             return modelAndView;
  17.         }
  18.        
  19.         User temp = new User();
  20.         temp.setId(user.getId());
  21.         temp.setBossUser(user.getBossUser());
  22.        
  23.         try {
  24.            
  25.            
  26.             if (userService.getUser(id).getId() == user.getBossUser().getId())
  27.                 throw new Exception("new boss, same as the user");
  28.            
  29.             //TODO: Aktualnie wyszukujemy po wszystkich userach, trzeba to zmienic, najlepiej utworzyc relacje dwustronna
  30.             while(temp.getBossUser() != null)
  31.             {
  32.                
  33.                 if(temp.getId() == userService.getUser(id).getId())
  34.                         throw new Exception("hierarchy error");
  35.                 temp = temp.getBossUser();
  36.             }
  37.            
  38.        
  39.             userService.updateUser(user);
  40.             modelAndView = new ModelAndView("redirect:/user/list");
  41.             String message = "User was successfully edited.";
  42.             modelAndView.addObject("message", message);
  43.  
  44.         } catch (BusinessException e) {
  45.             log.error("edditingUser", e);
  46.             String message = "We were unable to update user id:" + id
  47.                     + " due to: " + e.getLocalizedMessage();
  48.             modelAndView = editUserPage(model, id);
  49.             modelAndView.addObject("message", message);
  50.         } catch (Exception e) {
  51.             String message = "We were unable to update user id:" + id
  52.                     + " due to: " + e.getLocalizedMessage();
  53.             modelAndView = editUserPage(model, id);
  54.             modelAndView.addObject("message", message);
  55.         }
  56.         return modelAndView;
  57.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement