de.berlios.jhelpdesk.web.manager.users.UserEditController.java Source code

Java tutorial

Introduction

Here is the source code for de.berlios.jhelpdesk.web.manager.users.UserEditController.java

Source

/*
 * This program 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, version 3 of the License.
 *
 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * Copyright: (C) 2006 jHelpdesk Developers Team
 */
package de.berlios.jhelpdesk.web.manager.users;

import java.text.NumberFormat;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomBooleanEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import de.berlios.jhelpdesk.dao.DAOException;
import de.berlios.jhelpdesk.dao.UserDAO;
import de.berlios.jhelpdesk.model.Role;
import de.berlios.jhelpdesk.model.User;
import de.berlios.jhelpdesk.web.tools.RoleEditor;
import de.berlios.jhelpdesk.web.tools.UserValidator;

/**
 * Kontroler obsugujacy dodawanie nowych oraz edycj istniejcych uytkownikw systemu.
 * Zosta wydzielony z klasy UserController ze wzgldu na konieczno dodatkowych
 * wywoa initBinder(...) i populateRoles() dla kadego dania, co niepotrzebnie
 * obciao wszystkie dania (tak te, ktre nie wymagay tych dziaa).
 *
 *
 * @see User
 * @see UserDAO
 * @see UserValidator
 * 
 * @author jjhop
 */
@Controller
public class UserEditController {

    private final static Logger log = LoggerFactory.getLogger(UserEditController.class);

    @Autowired
    private UserDAO userDAO;

    @Autowired
    private UserValidator validator;

    @Autowired
    private RoleEditor roleEditor;

    /**
     * Rejestruje edytory waciwoci niezbdne podczas edycji danych
     * uytkownika.
     *
     * @param binder
     *
     * @see WebDataBinder
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Role.class, "userRole", roleEditor);
        binder.registerCustomEditor(Long.class, null,
                new CustomNumberEditor(Long.class, NumberFormat.getNumberInstance(), true));
        binder.registerCustomEditor(Boolean.class, null, new CustomBooleanEditor(true));
    }

    /**
     * Docza do modelu widoku list rl dostpnych w systemie.
     *
     * @return lista rl dostpnych w systemie
     *
     * @see Role
     */
    @ModelAttribute("roles")
    public List<Role> populateRoles() {
        return Role.getRoles();
    }

    /**
     * Przygotowuje formularz edycji uytkownika.
     *
     * @param userId identyfikator uytkownika do edycji
     * @param map model widoku
     * @return identyfikator widoku
     */
    @RequestMapping(value = "/manage/users/{userId}/edit.html", method = RequestMethod.GET)
    protected String prepareForm(@PathVariable("userId") Long userId, ModelMap map) {
        try {
            map.addAttribute("user", userDAO.getById(userId));
        } catch (DAOException ex) {
            log.error("Komunikat....", ex);
            throw new RuntimeException(ex);
        }
        return "manager/users/edit";
    }

    @RequestMapping(value = "/manage/users/new.html", method = RequestMethod.GET)
    protected String prepareForm(ModelMap map) {
        map.addAttribute(new User());
        return "manager/users/edit";
    }

    /**
     * Obsuguje wysany formularz i zapisuje podany obiekt. Decyzj o tym czy jest
     * to update czy dodanie nowego podejmuje stosowny obiekt DAO.
     *
     * @param user obiekt uytkownika do zapisania
     * @param result
     * @return identyfikator widoku do wywietlenia
     * 
     */
    @RequestMapping(value = "/manage/users/save.html", method = RequestMethod.POST)
    protected String processSubmit(@ModelAttribute("user") User user, BindingResult result) {
        validator.validate(user, result);
        if (result.hasErrors()) {
            return "manager/users/edit";
        }
        try {
            userDAO.saveOrUpdate(user);
        } catch (DAOException ex) {
            log.error("Komunikat....", ex);
            throw new RuntimeException(ex);
        }
        return "redirect:/manage/users/" + user.getUserId() + "/show.html";
    }
}