// Path: src/main/java/org/fjorum/model/service/RoleService.java
// public interface RoleService {
//     List<Role> getAllRoles();
// 
//     Role addRole(Role role);
// 
//     Optional<Role> findRole(String name);
// }


import org.fjorum.controller.form.*;
import org.fjorum.model.entity.User;
import org.fjorum.model.service.RoleService;
import org.fjorum.model.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.validation.Valid;
import java.util.Comparator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;
        binder.addValidators(userCreateValidator);
    }

    @RequestMapping(method = RequestMethod.GET)
    @Transactional(readOnly = true)
    public String getAdminPage(Model model) {
        List<UserEditForm> editForms = userService.getAll(new Sort("name")).stream()
                .map(UserEditForm::new)
                .collect(Collectors.toList());

        //model.addAttribute("users", userService.getAll());
        model.addAttribute("roles", roleService.getAllRoles());
        model.addAttribute(UserCreateForm.NAME, new UserCreateForm());
        model.addAttribute(UserEditForm.NAME + "s", editForms);
        //model.addAttribute(UserRightsForm.NAME, new UserRightsForm());
        //model.addAttribute(USER_DELETE_FORM_NAME, new DeleteForm());
        return ADMIN_PAGE;
    }

    @RequestMapping(value = "/userCreate", method = RequestMethod.POST)
    public String handleUserCreateForm(
            @Valid UserCreateForm userCreateForm,
            BindingResult bindingResult,
            RedirectAttributes redirectAttributes) {
        if (bindingResult.hasErrors()) {
            FlashMessage.ERROR.put(redirectAttributes, "user.create.failure");
        } else {
            try {
                userService.create(userCreateForm);
                FlashMessage.SUCCESS.put(redirectAttributes, "user.create.success");
// Path: src/main/java/org/fjorum/model/service/RoleService.java
// public interface RoleService {
//     List<Role> getAllRoles();
// 
//     Role addRole(Role role);
// 
//     Optional<Role> findRole(String name);
// }


// Path: src/main/java/org/fjorum/controller/AdminController.java
import org.fjorum.controller.form.*;
import org.fjorum.model.entity.User;
import org.fjorum.model.service.RoleService;
import org.fjorum.model.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.validation.Valid;
import java.util.Comparator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;

        binder.addValidators(userCreateValidator);
    }

    @RequestMapping(method = RequestMethod.GET)
    @Transactional(readOnly = true)
    public String getAdminPage(Model model) {
        List<UserEditForm> editForms = userService.getAll(new Sort("name")).stream()
                .map(UserEditForm::new)
                .collect(Collectors.toList());

        //model.addAttribute("users", userService.getAll());
        model.addAttribute("roles", roleService.getAllRoles());
        model.addAttribute(UserCreateForm.NAME, new UserCreateForm());
        model.addAttribute(UserEditForm.NAME + "s", editForms);
        //model.addAttribute(UserRightsForm.NAME, new UserRightsForm());
        //model.addAttribute(USER_DELETE_FORM_NAME, new DeleteForm());
        return ADMIN_PAGE;
    }

    @RequestMapping(value = "/userCreate", method = RequestMethod.POST)
    public String handleUserCreateForm(
            @Valid UserCreateForm userCreateForm,
            BindingResult bindingResult,
            RedirectAttributes redirectAttributes) {
        if (bindingResult.hasErrors()) {
            FlashMessage.ERROR.put(redirectAttributes, "user.create.failure");
        } else {
            try {
                userService.create(userCreateForm);
                FlashMessage.SUCCESS.put(redirectAttributes, "user.create.success");
            } catch (DataIntegrityViolationException e) {