com.klm.workshop.controller.host.manage.SessionController.java Source code

Java tutorial

Introduction

Here is the source code for com.klm.workshop.controller.host.manage.SessionController.java

Source

/*
 * The MIT License
 *
 * Copyright 2015 Tijme Gommers <t.gommers@jetcat.nl>.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.klm.workshop.controller.host.manage;

import com.klm.workshop.dao.SessionDAO;
import com.klm.workshop.dao.WorkshopDAO;
import com.klm.workshop.editor.SessionWorkshopEditor;
import com.klm.workshop.model.Session;
import com.klm.workshop.model.Workshop;
import java.util.Locale;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.support.PagedListHolder;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
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 org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * Host manage sessions controller
 * 
 * @author Tijme Gommers <t.gommers@jetcat.nl>
 */
@Controller("hostManageSessionController")
@RequestMapping(value = "/host/manage")
public class SessionController {

    /**
     * Session data access object
     */
    @Autowired
    private SessionDAO sessionDAO;

    /**
     * Workshop data access object
     */
    @Autowired
    private WorkshopDAO workshopDAO;

    /**
     * Translations
     */
    @Autowired
    private MessageSource messageSource;

    /**
     * Register editors to cast input strings to objects. E.g. String ID to a
     * Session object
     * 
     * @param binder The request data binder
     */
    @InitBinder
    public void dataBinding(WebDataBinder binder) {
        binder.registerCustomEditor(Workshop.class, "workshop", new SessionWorkshopEditor(workshopDAO));
    }

    /**
     * List of sessions
     * 
     * @param model Objects and view
     * @param page Current pagination page
     * @param search Search value
     * @return The sessions list view
     */
    @RequestMapping(value = "/sessions/index", method = RequestMethod.GET)
    public ModelAndView index(ModelAndView model,
            @RequestParam(name = "p", required = false, defaultValue = "0") int page,
            @RequestParam(name = "search", required = false, defaultValue = "") String search) {
        PagedListHolder pagedListHolder = new PagedListHolder();
        pagedListHolder.setSource(sessionDAO.searchAll(search));
        pagedListHolder.setPage(page);

        model.addObject("pagedListHolder", pagedListHolder);
        model.setViewName("host/manage/sessions/index");
        return model;
    }

    /**
    * Show create sessions form
    * 
    * @param model Objects and view
    * @return Form to create a session
    */
    @RequestMapping(value = "/sessions/create", method = RequestMethod.GET)
    public ModelAndView getCreate(ModelAndView model) {
        model.addObject("session", new Session());
        model.addObject("workshops", workshopDAO.findAll());
        model.setViewName("host/manage/sessions/create");
        return model;
    }

    /**
     * Create sessions, and show create sessions form. On error, show errors. 
     * On success, redirect to index and show success message.
     * 
     * @param model Objects and view
     * @param session The posted session
     * @param result Binded validation
     * @param redirect The redirect attributes
     * @param locale The current client locale
     * @return Form to create a sessions, or a redirect (if session was created successfully)
     */
    @RequestMapping(value = "/sessions/create", method = RequestMethod.POST)
    public ModelAndView postCreate(ModelAndView model, @ModelAttribute("session") @Valid Session session,
            BindingResult result, RedirectAttributes redirect, Locale locale) {
        if (result.hasErrors()) {
            model.addObject("workshops", workshopDAO.findAll());
            model.setViewName("host/manage/sessions/create");
        } else {
            sessionDAO.create(session);

            String success = messageSource.getMessage("general.create_success",
                    new String[] { session.getWorkshop().getName() }, locale);

            redirect.addFlashAttribute("alertSuccess", success);
            model.setViewName("redirect:/host/manage/sessions/index");
        }

        return model;
    }

    /**
     * Show update session form
     * 
     * @param id The ID of the session to update
     * @param model Objects and view
     * @return Form to create a session
     */
    @RequestMapping(value = "/sessions/update/{id}", method = RequestMethod.GET)
    public ModelAndView getUpdate(ModelAndView model, @PathVariable int id) {
        model.addObject("session", (Session) sessionDAO.findById(id));
        model.addObject("workshops", workshopDAO.findAll());
        model.setViewName("host/manage/sessions/update");
        return model;
    }

    /**
     * Update session, and show update session form. On error, show errors. On
     * success, redirect to index and show success message.
     * 
     * @param model Objects and view
     * @param id The ID of the session to update
     * @param session The posted session
     * @param result Binded validation
     * @param redirect The redirect attributes
     * @param locale The current client locale
     * @return Form to update a session, or a redirect (if session was updated successfully)
     */
    @RequestMapping(value = "/sessions/update/{id}", method = RequestMethod.POST)
    public ModelAndView postUpdate(ModelAndView model, @PathVariable int id,
            @ModelAttribute("session") @Valid Session session, BindingResult result, RedirectAttributes redirect,
            Locale locale) {
        if (result.hasErrors()) {
            model.addObject("workshops", workshopDAO.findAll());
            model.setViewName("host/manage/sessions/update");
        } else {
            sessionDAO.update(session);

            String success = messageSource.getMessage("general.update_success",
                    new String[] { session.getWorkshop().getName() }, locale);

            redirect.addFlashAttribute("alertSuccess", success);
            model.setViewName("redirect:/host/manage/sessions/index");
        }

        return model;
    }

    /**
     * Delete session, based on the given session id. Then redirects to the 
     * index, with a alert message.
     * 
     * @param id The ID of the session to delete
     * @param redirect The redirect attributes
     * @param locale The current client locale
     * @return Redirect to the index
     */
    @RequestMapping(value = "/sessions/delete/{id}", method = RequestMethod.POST)
    public String getDelete(@PathVariable int id, RedirectAttributes redirect, Locale locale) {
        Session session = (Session) sessionDAO.findById(id);

        String success = messageSource.getMessage("general.delete_success",
                new String[] { session.getWorkshop().getName() }, locale);

        sessionDAO.delete(session);

        redirect.addFlashAttribute("alertSuccess", success);
        return "redirect:/host/manage/sessions/index";
    }

}