com.gantzgulch.sharing.controller.UserEditController.java Source code

Java tutorial

Introduction

Here is the source code for com.gantzgulch.sharing.controller.UserEditController.java

Source

/*
 * Copyright 2011 GantzGulch, Inc.
 * 
 * This file is part of GantzFileSharing.
 * 
 * GantzFileSharing is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GantzFileSharing is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GantzFileSharing.  If not, see <http://www.gnu.org/licenses/>. 
 */
package com.gantzgulch.sharing.controller;

import java.util.Map;

import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
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.ModelAndView;

import com.gantzgulch.sharing.domain.User;
import com.gantzgulch.sharing.domain.UserRole;
import com.gantzgulch.sharing.form.UserForm;
import com.gantzgulch.sharing.service.UserManager;

@Controller
@RequestMapping("/secure/admin/userEdit.htm")
public class UserEditController {

    private final UserManager userManager;

    @Autowired
    public UserEditController(UserManager userManager) {
        this.userManager = userManager;
    }

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView showEditForm(@RequestParam(required = false, defaultValue = "") final String id) {

        ModelAndView modelAndView = null;
        User user = null;
        UserForm form;

        if (StringUtils.isNotBlank(id)) {
            user = userManager.findById(id);

            if (user == null) {
                modelAndView = new ModelAndView("redirect:/secure/admin/users.htm");
            }
        }

        form = UserForm.create(user);

        modelAndView = modelAndView == null ? new ModelAndView(".userEdit", "userForm", form) : modelAndView;

        modelAndView.addObject("roleList", UserRole.getRoleList());
        return modelAndView;
    }

    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView processForm(@Valid UserForm userForm, BindingResult result, Map<String, Object> model) {

        if (result.hasErrors()) {
            ModelAndView modelAndView = new ModelAndView(".userEdit");
            modelAndView.addObject("roleList", UserRole.getRoleList());
            return modelAndView;
        }

        if (StringUtils.isBlank(userForm.getId())) {
            userManager.create(userForm);
        } else {
            userManager.update(userForm);
        }

        return new ModelAndView("redirect:/secure/admin/user.htm", "id", userForm.getId());
    }
}