package com.medsocial.controller;

import java.util.EnumSet;
import java.util.Set;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.google.appengine.api.users.UserServiceFactory;
import com.medsocial.dao.UserRegistry;
import com.medsocial.model.GaeUser;
import com.medsocial.security.AppRole;
import com.medsocial.security.GaeUserAuthentication;
import com.medsocial.validation.RegistrationForm;

@Controller
@RequestMapping(value = "/register")
public class RegistrationController {

	private static final Logger logger = LoggerFactory.getLogger(RegistrationController.class);
	
	@Autowired
	private UserRegistry registry;

	@RequestMapping(method = RequestMethod.GET)
	public Model registrationForm() {
		Model model = new ExtendedModelMap();
		model.addAttribute(new RegistrationForm());
		return model;
	}

	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView register(@Valid RegistrationForm form, BindingResult result) {
		if (result.hasErrors()) {
			logger.debug("RegistrationForm has errors.");
			return null;
		}

		ModelAndView mav = new ModelAndView("redirect:/home");

		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		GaeUser currentUser = (GaeUser) authentication.getPrincipal();
		Set<AppRole> roles = EnumSet.of(AppRole.USER);
		roles.add(form.getAccountType().getRole());

		if (UserServiceFactory.getUserService().isUserAdmin()) {
			roles.add(AppRole.ADMIN);
		}

		GaeUser user = new GaeUser();
		user.setUserId(currentUser.getUserId());
		user.setNickname(currentUser.getNickname());
		user.setEmail(currentUser.getEmail());
		user.setForename(form.getForename());
		user.setSurname(form.getSurname());
		user.setAuthorities(roles);
		user.setEnabled(true);

		registry.registerUser(user);

		// Update the context with the full authentication
		SecurityContextHolder.getContext().setAuthentication(
				new GaeUserAuthentication(user, authentication.getDetails()));

		return mav;
	}
}

@@ -412,93 +412,8 @@
 er;%0A
-import org.springframework.ui.ExtendedModelMap;%0Aimport org.springframework.ui.Model;%0A
 impo
@@ -1202,16 +1202,23 @@
 ic Model
+AndView
  registr
@@ -1242,61 +1242,75 @@
 odel
- model = new ExtendedModelMap();%0A%09%09model.addAttribute
+AndView mav = new ModelAndView(%22newUser/register%22);%0A%09%09mav.addObject
 (new
@@ -1341,20 +1341,18 @@
 return m
-odel
+av
 ;%0A%09%7D%0A%0A%09@

Fix registration bug