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

Java tutorial

Introduction

Here is the source code for com.klm.workshop.controller.host.manage.WorkshopController.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.WorkshopDAO;
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.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 workshops controller
 * 
 * @author Tijme Gommers <t.gommers@jetcat.nl>
 */
@Controller("hostManageWorkshopController")
@RequestMapping(value = "/host/manage")
public class WorkshopController {

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

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

    /**
     * List of workshops
     * 
     * @param model Objects and view
     * @param page Current pagination page
     * @param search Search value
     * @return The workshops list view
     */
    @RequestMapping(value = "/workshops/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(workshopDAO.searchAll(search));
        pagedListHolder.setPage(page);

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

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

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

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

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

        return model;
    }

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

    /**
     * Update workshop, and show update workshop 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 workshop to update
     * @param workshop The posted workshop
     * @param result Binded validation
     * @param redirect The redirect attributes
     * @param locale The current client locale
     * @return Form to update a workshop, or a redirect (if workshop was updated successfully)
     */
    @RequestMapping(value = "/workshops/update/{id}", method = RequestMethod.POST)
    public ModelAndView postUpdate(ModelAndView model, @PathVariable int id,
            @ModelAttribute("workshop") @Valid Workshop workshop, BindingResult result, RedirectAttributes redirect,
            Locale locale) {
        if (result.hasErrors()) {
            model.setViewName("host/manage/workshops/update");
        } else {
            workshopDAO.update(workshop);

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

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

        return model;
    }

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

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

        workshopDAO.delete(workshop);

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

}