@Transactional(rollbackForClassName = "java.lang.Exception")
  @RequestMapping(
      value = "updateAccount",
      params = {"delete"},
      method = RequestMethod.POST)
  public ModelAndView deleteAccount(@Valid User user, HttpServletRequest request)
      throws IllegalRequestException {

    Integer authUserId = userCookieGenerator.getUserId(request);
    if (!authUserId.equals(user.getId())) throw new IllegalRequestException();

    userProfileMapper.deleteProfile(authUserId);
    userMasterMapper.deleteUser(authUserId);

    ConnectionRepository connectionRepository =
        usersConnectionRepository.createConnectionRepository(authUserId.toString());
    Connection connection = connectionRepository.findPrimaryConnection(Facebook.class);
    if (connection != null) connectionRepository.removeConnection(connection.getKey());
    connection = connectionRepository.findPrimaryConnection(Twitter.class);
    if (connection != null) connectionRepository.removeConnection(connection.getKey());

    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("forward:logout");
    return modelAndView;
  }
  @Transactional(rollbackForClassName = "java.lang.Exception")
  @RequestMapping(value = "updateAccount", method = RequestMethod.GET)
  public ModelAndView updateAccount(HttpServletRequest request) throws IllegalRequestException {

    User user = userMasterMapper.getUserById(userCookieGenerator.getUserId(request));
    if (user == null) throw new IllegalRequestException();

    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("user", user);
    modelAndView.setViewName("user/editAccount");
    return modelAndView;
  }
  @Transactional(rollbackForClassName = "java.lang.Exception")
  @RequestMapping(
      value = "updateMyPage",
      method = {RequestMethod.POST})
  public ModelAndView updateMyPage(
      @Valid UserProfile userProfile, BindingResult result, HttpServletRequest request)
      throws IllegalRequestException {

    Integer authUserId = userCookieGenerator.getUserId(request);
    if (!authUserId.equals(userProfile.getUserId())) throw new IllegalRequestException();

    try {
      if (result.hasErrors()) {
        throw new InvalidInputException();
      }
      if (!userProfile.validForEditingProfile()) {
        Map<String, String> rejectValueMap = userProfile.getRejectValueMap();
        for (Map.Entry<String, String> entry : rejectValueMap.entrySet()) {
          result.rejectValue(entry.getKey(), entry.getValue());
        }
        throw new InvalidInputException();
      }
    } catch (InvalidInputException e) {
      ModelAndView modelAndView = new ModelAndView();
      modelAndView.addObject(
          "userProfileDisplay", userProfileMapper.getUserProfileById(authUserId));
      modelAndView.addObject("userProfile", userProfile);
      modelAndView.addObject("userStickyList", userStickyMapper.listByUserId(authUserId));
      modelAndView.addObject("editMode", true);
      modelAndView.setViewName("user/show");
      return modelAndView;
    }

    userProfileMapper.updateUserProfile(userProfile);
    userProfile = userProfileMapper.getUserProfileById(authUserId);

    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("userProfileDisplay", userProfile);
    modelAndView.addObject("userProfile", userProfile);
    modelAndView.addObject("userStickyList", userStickyMapper.listByUserId(authUserId));
    modelAndView.addObject("editMode", true);
    modelAndView.addObject("updated", true);
    modelAndView.setViewName("user/show");
    return modelAndView;
  }
  @Transactional(rollbackForClassName = "java.lang.Exception")
  @RequestMapping(
      value = "updateAccount",
      params = {"update"},
      method = RequestMethod.POST)
  public ModelAndView updateAccount(
      @Valid User user, BindingResult result, HttpServletRequest request)
      throws IllegalRequestException {

    if (!userCookieGenerator.getUserId(request).equals(user.getId()))
      throw new IllegalRequestException();

    try {
      if (result.hasErrors()) {
        throw new InvalidInputException();
      }
      if (!user.validForEditingAccount()) {
        Map<String, String> rejectValueMap = user.getRejectValueMap();
        for (Map.Entry<String, String> entry : rejectValueMap.entrySet()) {
          result.rejectValue(entry.getKey(), entry.getValue());
        }
        throw new InvalidInputException();
      }
      if (userMasterMapper.countUserByEmail(user.getEmail(), user.getId()) != 0) {
        result.rejectValue("email", "error.email.exists");
        throw new InvalidInputException();
      }
    } catch (InvalidInputException e) {
      ModelAndView modelAndView = new ModelAndView();
      modelAndView.addObject("activeTab", "account");
      modelAndView.addObject("user", user);
      modelAndView.setViewName("user/edit");
      return modelAndView;
    }

    userMasterMapper.updateAccount(user.getId(), user.getEmail());

    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("activeTab", "account");
    modelAndView.addObject("updated", true);
    modelAndView.setViewName("user/edit");
    return modelAndView;
  }
  @Transactional(rollbackForClassName = "java.lang.Exception")
  @RequestMapping(
      value = "gotoMyPage",
      method = {RequestMethod.GET})
  public ModelAndView gotoMyPage(HttpServletRequest request) throws IllegalRequestException {

    Integer authUserId = userCookieGenerator.getUserId(request);

    UserProfile userProfile = userProfileMapper.getUserProfileById(authUserId);
    if (userProfile == null) throw new IllegalRequestException();

    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("userProfileDisplay", userProfile);
    modelAndView.addObject("userProfile", userProfile);
    modelAndView.addObject("userStickyList", userStickyMapper.listByUserId(authUserId));
    modelAndView.addObject("editMode", true);
    modelAndView.setViewName("user/mypage");
    return modelAndView;
  }
  @Transactional(rollbackForClassName = "java.lang.Exception")
  @RequestMapping(value = "updateSocial", method = RequestMethod.GET)
  public ModelAndView updateSocial(HttpServletRequest request) throws IllegalRequestException {

    Integer authUserId = userCookieGenerator.getUserId(request);

    ConnectionRepository connectionRepository =
        usersConnectionRepository.createConnectionRepository(authUserId.toString());
    boolean facebookConnected =
        ((connectionRepository.findPrimaryConnection(Facebook.class)) != null);
    boolean twitterConnected =
        ((connectionRepository.findPrimaryConnection(Twitter.class)) != null);

    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("facebookConnected", facebookConnected);
    modelAndView.addObject("twitterConnected", twitterConnected);
    modelAndView.setViewName("user/editSocial");
    return modelAndView;
  }
  @Transactional(rollbackForClassName = "java.lang.Exception")
  @RequestMapping(value = "disconnect", method = RequestMethod.POST)
  public ModelAndView disconnect(HttpServletRequest request) throws IllegalRequestException {

    Integer authUserId = userCookieGenerator.getUserId(request);

    ConnectionRepository connectionRepository =
        usersConnectionRepository.createConnectionRepository(authUserId.toString());
    if (request.getParameter("disconnectFacebook") != null) {
      Connection connection = connectionRepository.findPrimaryConnection(Facebook.class);
      if (connection != null) connectionRepository.removeConnection(connection.getKey());
    }
    if (request.getParameter("disconnectTwitter") != null) {
      Connection connection = connectionRepository.findPrimaryConnection(Twitter.class);
      if (connection != null) connectionRepository.removeConnection(connection.getKey());
    }
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("activeTab", "social");
    modelAndView.addObject("updated", true);
    modelAndView.setViewName("user/edit");
    return modelAndView;
  }
  @Transactional(rollbackForClassName = "java.lang.Exception")
  @RequestMapping(value = "updateProfile", method = RequestMethod.POST)
  public ModelAndView updateProfile(
      @Valid UserProfile userProfile, BindingResult result, HttpServletRequest request)
      throws IllegalRequestException {

    if (!userCookieGenerator.getUserId(request).equals(userProfile.getUserId()))
      throw new IllegalRequestException();

    try {
      if (result.hasErrors()) {
        throw new InvalidInputException();
      }
      if (!userProfile.validForEditingProfile()) {
        Map<String, String> rejectValueMap = userProfile.getRejectValueMap();
        for (Map.Entry<String, String> entry : rejectValueMap.entrySet()) {
          result.rejectValue(entry.getKey(), entry.getValue());
        }
        throw new InvalidInputException();
      }
    } catch (InvalidInputException e) {
      ModelAndView modelAndView = new ModelAndView();
      modelAndView.addObject("activeTab", "profile");
      modelAndView.addObject("userProfile", userProfile);
      modelAndView.setViewName("user/editProfile");
      return modelAndView;
    }

    userProfileMapper.updateUserProfile(userProfile);

    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("activeTab", "profile");
    modelAndView.addObject("updated", true);
    modelAndView.setViewName("user/edit");
    return modelAndView;
  }