com.izerui.user.UserController.java Source code

Java tutorial

Introduction

Here is the source code for com.izerui.user.UserController.java

Source

/**
 * Copyright (C) 2014 lyh (liuyuhua69@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.izerui.user;

import javax.inject.Inject;
import javax.validation.groups.Default;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.izerui.framework.BeanMapper;
import com.izerui.user.UserForm.UserCreateGroup;
import com.izerui.user.UserForm.UserDeleteGroup;
import com.izerui.user.UserForm.UserUpdateGroup;
import com.izerui.user.domain.model.User;
import com.izerui.user.domain.service.UserService;

@Controller
@RequestMapping("user")
public class UserController {
    @Inject
    protected UserService userService;

    @ModelAttribute
    public UserForm setUpUserForm() {
        return new UserForm();
    }

    // create flow

    @RequestMapping(value = "create", params = "form", method = RequestMethod.GET)
    public String createForm(UserForm form) {
        return "user/createForm";
    }

    @RequestMapping(value = "create", params = "confirm", method = RequestMethod.POST)
    public String createConfirm(@Validated({ Default.class, UserCreateGroup.class }) UserForm form,
            BindingResult result) {
        if (result.hasErrors()) {
            return "user/createForm";
        }
        return "user/createConfirm";
    }

    @RequestMapping(value = "create", params = "redo", method = RequestMethod.POST)
    public String createRedo(UserForm form) {
        // reset password
        form.setPassword("");
        form.setConfirmPassword("");
        return "user/createForm";
    }

    @RequestMapping(value = "create", method = RequestMethod.POST)
    public String create(@Validated({ Default.class, UserCreateGroup.class }) UserForm form, BindingResult result) {
        if (result.hasErrors()) {
            return "user/createForm";
        }

        //        User user = beanMapper.map(form, User.class);
        User user = new BeanMapper().map(form, User.class);
        userService.save(user, form.getPassword());

        return "redirect:/user/create?complete";
    }

    @RequestMapping(value = "create", params = "complete", method = RequestMethod.GET)
    public String createComplete() {
        return "user/createComplete";
    }

    // update flow

    @RequestMapping(value = "update", params = "form", method = RequestMethod.GET)
    public String updateForm(@RequestParam("id") Integer id, UserForm form, Model model) {

        User user = userService.findOne(id);
        //        beanMapper.map(user, form, "userExcludePassword");
        user = new BeanMapper().map(form, User.class);
        model.addAttribute(user);

        return "user/updateForm";
    }

    @RequestMapping(value = "update", params = "confirm", method = RequestMethod.POST)
    public String updateConfirm(@Validated({ Default.class, UserUpdateGroup.class }) UserForm form,
            BindingResult result) {
        if (result.hasErrors()) {
            return "user/updateForm";
        }
        return "user/updateConfirm";
    }

    @RequestMapping(value = "update", params = "redo", method = RequestMethod.POST)
    public String updateRedo(@RequestParam("id") Integer id, UserForm form, Model model) {

        // reset password
        form.setPassword("");
        form.setConfirmPassword("");

        return "user/updateForm";
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String update(@Validated({ Default.class, UserUpdateGroup.class }) UserForm form, BindingResult result) {
        if (result.hasErrors()) {
            return "user/updateForm";
        }

        User user = userService.findOne(form.getId());
        new BeanMapper().map(form, user);
        //        beanMapper.map(form, user);
        userService.save(user, form.getPassword());

        return "redirect:/user/update?complete";
    }

    @RequestMapping(value = "update", params = "complete", method = RequestMethod.GET)
    public String updateComplete() {
        return "user/updateComplete";
    }

    // delete flow

    @RequestMapping(value = "delete", params = "form", method = RequestMethod.GET)
    public String deleteForm(@RequestParam("id") Integer id, UserForm form, Model model) {

        User user = userService.findOne(id);
        //        beanMapper.map(user, form);
        new BeanMapper().map(user, form);

        model.addAttribute(user);
        return "user/deleteForm";
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public String delete(@Validated({ Default.class, UserDeleteGroup.class }) UserForm form, BindingResult result,
            RedirectAttributes attr) {
        if (result.hasErrors()) {
            attr.addFlashAttribute("errorMessage", "Illegal Access!");
            return "redirect:/user/list";
        }

        User user = userService.findOne(form.getId());
        new BeanMapper().map(form, user);

        userService.delete(user);
        return "redirect:/user/delete?complete";
    }

    @RequestMapping(value = "delete", params = "complete")
    public String deleteComplete() {
        return "user/deleteComplete";
    }

    @RequestMapping(value = { "create", "update", "delete" }, params = "redirectToList")
    public String redirectToList() {
        return "redirect:/user/list";
    }
}