Example usage for org.springframework.ui Model asMap

List of usage examples for org.springframework.ui Model asMap

Introduction

In this page you can find the example usage for org.springframework.ui Model asMap.

Prototype

Map<String, Object> asMap();

Source Link

Document

Return the current set of model attributes as a Map.

Usage

From source file:com.jd.survey.web.security.AuthorityController.java

@Secured({ "ROLE_ADMIN" })
@RequestMapping(method = RequestMethod.PUT, produces = "text/html")
public String update(@RequestParam(value = "_proceed", required = false) String proceed,
        @Valid Authority authority, BindingResult bindingResult, Principal principal, Model uiModel,
        HttpServletRequest httpServletRequest) {
    log.info("update(): handles PUT");
    try {//from  www.  j  a v a2 s  .c  o  m
        User user = userService.user_findByLogin(principal.getName());
        if (proceed != null) {
            if (bindingResult.hasErrors()) {
                populateEditForm(uiModel, authority, user);
                return "authorities/update";
            }
            if (!userService.authority_ValidateNameIsUnique(authority)) {
                bindingResult.rejectValue("name", "field_unique");
                populateEditForm(uiModel, authority, user);
                return "security/authorities/create";
            }
            uiModel.asMap().clear();
            authority = userService.authority_merge(authority);
            log.info("redirecting to: " + "redirect:/security/authorities/"
                    + encodeUrlPathSegment(authority.getId().toString(), httpServletRequest));
            return "redirect:/security/authorities/"
                    + encodeUrlPathSegment(authority.getId().toString(), httpServletRequest);

        } else {
            return "redirect:/security/authorities";
        }

    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw (new RuntimeException(e));
    }
}

From source file:com.jd.survey.web.security.AuthorityController.java

@Secured({ "ROLE_ADMIN" })
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = "text/html")
public String delete(@PathVariable("id") Long id, Principal principal, Model uiModel,
        HttpServletRequest httpServletRequest) {
    log.info("delete(): id=" + id);
    try {//from w w  w  .j a  va 2s . c o  m
        SecurityObject authority = userService.authority_findById(id);
        userService.authority_remove(authority);
        uiModel.asMap().clear();
        return "redirect:/security/authorities/";
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw (new RuntimeException(e));
    }
}

From source file:com.jd.survey.web.security.DepartmentController.java

@Secured({ "ROLE_ADMIN" })
@RequestMapping(method = RequestMethod.POST, produces = "text/html")
public String createPost(@RequestParam(value = "_proceed", required = false) String proceed,
        @Valid Department department, BindingResult bindingResult, Principal principal, Model uiModel,
        HttpServletRequest httpServletRequest) {
    try {/*from w  w w.  j ava  2s .c  o  m*/
        User user = userService.user_findByLogin(principal.getName());

        if (proceed != null) {

            if (bindingResult.hasErrors()) {
                populateEditForm(uiModel, department, user);
                return "security/departments/create";
            }
            if (surveySettingsService.department_findByName(department.getName()) != null) {
                bindingResult.rejectValue("name", "field_unique");
                populateEditForm(uiModel, department, user);
                return "security/departments/create";
            }
            uiModel.asMap().clear();
            department = surveySettingsService.department_merge(department);
            return "redirect:/security/departments/"
                    + encodeUrlPathSegment(department.getId().toString(), httpServletRequest);
        } else {

            return "redirect:/security/departments?page=1&size=10";

        }

    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw (new RuntimeException(e));
    }

}

From source file:com.jd.survey.web.security.DepartmentController.java

@Secured({ "ROLE_ADMIN" })
@RequestMapping(method = RequestMethod.PUT, produces = "text/html")
public String update(@RequestParam(value = "_proceed", required = false) String proceed,
        @Valid Department department, BindingResult bindingResult, Principal principal, Model uiModel,
        HttpServletRequest httpServletRequest) {
    log.info("update(): handles PUT");
    try {//  w  ww  . ja va 2 s  .  co m
        User user = userService.user_findByLogin(principal.getName());
        if (proceed != null) {

            if (bindingResult.hasErrors()) {
                populateEditForm(uiModel, department, user);
                return "security/departments/update";
            }
            if (surveySettingsService.department_findByName(department.getName()) != null
                    && !surveySettingsService.department_findByName(department.getName()).getId()
                            .equals(department.getId())) {
                bindingResult.rejectValue("name", "field_unique");
                populateEditForm(uiModel, department, user);
                return "security/departments/update";
            }
            uiModel.asMap().clear();
            department = surveySettingsService.department_merge(department);
            return "redirect:/security/departments/"
                    + encodeUrlPathSegment(department.getId().toString(), httpServletRequest);

        } else {

            return "redirect:/security/departments?page=1&size=10";

        }

    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw (new RuntimeException(e));
    }
}

From source file:com.jd.survey.web.security.GroupController.java

@Secured({ "ROLE_ADMIN" })
@RequestMapping(method = RequestMethod.POST, produces = "text/html")
public String createPost(@RequestParam(value = "_proceed", required = false) String proceed, @Valid Group group,
        BindingResult bindingResult, Principal principal, Model uiModel,
        HttpServletRequest httpServletRequest) {
    log.info("create(): handles " + RequestMethod.POST.toString());
    try {//from   w  ww .  j  a  va2  s .co m
        User user = userService.user_findByLogin(principal.getName());
        if (proceed != null) {
            if (bindingResult.hasErrors()) {
                populateEditForm(uiModel, group, user);
                return "security/groups/create";
            }
            if (!userService.group_ValidateNameIsUnique(group)) {
                bindingResult.rejectValue("name", "field_unique");
                populateEditForm(uiModel, group, user);
                return "security/groups/create";
            }

            uiModel.asMap().clear();
            group = userService.group_merge(group);

            return "redirect:/security/groups/"
                    + encodeUrlPathSegment(group.getId().toString(), httpServletRequest);

        } else {
            return "redirect:/security/groups";
        }

    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw (new RuntimeException(e));
    }

}

From source file:com.jd.survey.web.security.GroupController.java

@Secured({ "ROLE_ADMIN" })
@RequestMapping(method = RequestMethod.PUT, produces = "text/html")
public String update(@RequestParam(value = "_proceed", required = false) String proceed, @Valid Group group,
        BindingResult bindingResult, Principal principal, Model uiModel,
        HttpServletRequest httpServletRequest) {
    log.info("update(): handles PUT");
    try {/*from www  .  j  a va2  s.  com*/
        User user = userService.user_findByLogin(principal.getName());
        if (proceed != null) {
            if (bindingResult.hasErrors()) {
                populateEditForm(uiModel, group, user);
                return "security/groups/update";
            }
            if (!userService.group_ValidateNameIsUnique(group)) {
                bindingResult.rejectValue("name", "field_unique");
                populateEditForm(uiModel, group, user);
                return "security/groups/update";
            }

            if (!userService.group_ValidateGroupEmpty(group)) {
                bindingResult.rejectValue("authorities", "field_validation_checkboxes");
                populateEditForm(uiModel, group, user);
                return "security/groups/update";
            }
            uiModel.asMap().clear();
            group = userService.group_merge(group);
            log.info("redirecting to: " + "redirect:/security/groups/"
                    + encodeUrlPathSegment(group.getId().toString(), httpServletRequest));
            return "redirect:/security/groups/"
                    + encodeUrlPathSegment(group.getId().toString(), httpServletRequest);

        } else {
            return "redirect:/security/groups";
        }

    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw (new RuntimeException(e));
    }
}

From source file:com.jd.survey.web.security.GroupController.java

@Secured({ "ROLE_ADMIN" })
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = "text/html")
public String delete(@PathVariable("id") Long id, Principal principal, Model uiModel,
        HttpServletRequest httpServletRequest) {
    log.info("delete(): id=" + id);
    try {/*from  w  ww .  j  ava2 s.c  om*/
        Group group = userService.group_findById(id);
        Long groupId = group.getId();
        userService.group_remove(group);
        uiModel.asMap().clear();
        return "redirect:/security/groups";
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw (new RuntimeException(e));
    }
}

From source file:com.jd.survey.web.security.LoginController.java

@RequestMapping(method = RequestMethod.POST, value = "/rpass", produces = "text/html")
public String forgotPasswordPost(@RequestParam(value = "password", required = true) String password,
        @RequestParam(value = "cpassword", required = true) String cpassword,
        @RequestParam(value = "key", required = true) String key,
        @RequestParam(value = "_proceed", required = false) String proceed, Model uiModel,
        HttpServletRequest httpServletRequest) {
    try {/*w w w .j ava 2 s . c  o m*/
        if (proceed != null) {
            //validate the passed key
            if (!userService.user_validateForgotPasswordKey(key)) {
                log.warn("Attempt to reset password with invalid key, Not successful");
                uiModel.addAttribute("status", "E"); //Error
                throw (new RuntimeException("Attempt to reset password with invalid key, Not successful"));
            }

            //check that passwords match    
            if (!password.equals(cpassword)) {
                uiModel.asMap().clear();
                uiModel.addAttribute("key", key);
                uiModel.addAttribute("status", "U"); //Unmatching Passwords
                return "public/rpass";
            }

            GlobalSettings globalSettings = applicationSettingsService.getSettings();

            //Check new password strength 
            if (!GenericValidator.matchRegexp(password, globalSettings.getPasswordEnforcementRegex())) {
                uiModel.asMap().clear();
                uiModel.addAttribute("key", key);
                uiModel.addAttribute("status", "I"); //Unmatching Passwords
                uiModel.addAttribute("passwordPolicyMsg", globalSettings.getPasswordEnforcementMessage());
                return "public/rpass";
            }

            //All validations passed, save the HASH of the password in the database
            PasswordResetRequest passwordResetRequest = userService.passwordResetRequest_findByHash(key);
            User user = userService.user_findByLogin(passwordResetRequest.getLogin());
            user.setPassword(password);
            userService.user_updatePassword(user, passwordResetRequest);
            uiModel.addAttribute("status", "S");//success
            return "public/rpass";
        } else {
            //cancel button
            return "public/login";
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw (new RuntimeException(e));
    }
}

From source file:com.jd.survey.web.security.UserController.java

/**
 * Creates a new user/*from  w w  w .ja v a 2s.  c om*/
 * @param proceed
 * @param user
 * @param bindingResult
 * @param principal
 * @param uiModel
 * @param httpServletRequest
 * @return
 */
@Secured({ "ROLE_ADMIN" })
@RequestMapping(method = RequestMethod.POST, produces = "text/html")
public String createPost(@RequestParam(value = "_proceed", required = false) String proceed,
        @Validated({ User.UserInfo.class, User.Password.class }) User user, BindingResult bindingResult,
        Principal principal, Model uiModel, HttpServletRequest httpServletRequest) {
    try {
        User loggedInUser = userService.user_findByLogin(principal.getName());
        if (proceed != null) {
            if (bindingResult.hasErrors()) {
                populateEditForm(uiModel, user, loggedInUser);
                return "security/users/create";
            }
            //check that login is unique
            if (userService.user_findByLogin(user.getLogin()) != null
                    && userService.user_ValidateLoginIsUnique(user) == true) {
                bindingResult.rejectValue("login", "field_unique");
                populateEditForm(uiModel, user, loggedInUser);
                return "security/users/create";
            }
            //check that email is unique
            if (userService.user_findByEmail(user.getEmail()) != null
                    && userService.user_ValidateEmailIsUnique(user) == true) {
                bindingResult.rejectValue("email", "field_unique");
                populateEditForm(uiModel, user, loggedInUser);
                return "security/users/create";
            }
            //check that passwords match
            if (!user.getPassword().equals(user.getConfirmPassword())) {
                bindingResult.rejectValue("confirmPassword",
                        "security_password_reset_confirm_passwords_unmatching");
                populateEditForm(uiModel, user, loggedInUser);
                return "security/users/create";
            }
            if (!user.getConfirmPassword().matches(globalSettings.getPasswordEnforcementRegex())) {
                bindingResult.rejectValue("confirmPassword", globalSettings.getPasswordEnforcementMessage(),
                        this.globalSettings.getPasswordEnforcementMessage());
                populateEditForm(uiModel, user, loggedInUser);
                return "security/users/create";
            }
            uiModel.asMap().clear();
            user = userService.user_merge(user);
            return "redirect:/security/users/"
                    + encodeUrlPathSegment(user.getId().toString(), httpServletRequest);
        } else {
            if (user.getType().equals(SecurityType.I)) {
                return "redirect:/security/users/internal";
            }
            if (user.getType().equals(SecurityType.E)) {
                return "redirect:/security/users/external";
            }
        }
        return "redirect:/security";
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw (new RuntimeException(e));
    }
}

From source file:com.jd.survey.web.security.UserController.java

/**
 * Updates the user information, except password
 * @param proceed/*from  w ww. jav a 2 s .  c o  m*/
 * @param user
 * @param bindingResult
 * @param principal
 * @param uiModel
 * @param httpServletRequest
 * @return
 */
@Secured({ "ROLE_ADMIN" })
@RequestMapping(method = RequestMethod.PUT, produces = "text/html")
public String update(@RequestParam(value = "_proceed", required = false) String proceed,
        @Validated({ User.UserInfo.class }) User user, BindingResult bindingResult, Principal principal,
        Model uiModel, HttpServletRequest httpServletRequest) {
    log.info("update(): handles PUT");
    try {
        User loggedInUser = userService.user_findByLogin(principal.getName());
        if (proceed != null) {
            if (bindingResult.hasErrors()) {
                populateEditForm(uiModel, user, loggedInUser);
                return "security/users/update";
            }
            if (userService.user_findByLogin(user.getLogin()) != null
                    && userService.user_ValidateLoginIsUnique(user) == true) {
                bindingResult.rejectValue("login", "field_unique");
                populateEditForm(uiModel, user, loggedInUser);
                return "security/users/update";
            }
            if (userService.user_findByEmail(user.getEmail()) != null
                    && userService.user_ValidateEmailIsUnique(user) == true) {
                bindingResult.rejectValue("email", "field_unique");
                populateEditForm(uiModel, user, loggedInUser);
                return "security/users/update";
            }
            uiModel.asMap().clear();
            user = userService.user_merge(user);
            return "redirect:/security/users/"
                    + encodeUrlPathSegment(user.getId().toString(), httpServletRequest);

        } else {

            if (user.getType().equals(SecurityType.I)) {
                return "redirect:/security/users/internal";
            }

            if (user.getType().equals(SecurityType.E)) {
                return "redirect:/security/users/external";
            }
        }
        return "redirect:/security";
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw (new RuntimeException(e));
    }
}