net.przemkovv.sphinx.web.TaskController.java Source code

Java tutorial

Introduction

Here is the source code for net.przemkovv.sphinx.web.TaskController.java

Source

/*
 * The MIT License
 *
 * Copyright 2013 Przemyslaw Walkowiak <przemyslaw.walkowiak@put.poznan.pl>.
 *
 * 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 net.przemkovv.sphinx.web;

import javax.validation.Valid;
import net.przemkovv.sphinx.model.Task;
import net.przemkovv.sphinx.service.TaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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;

/**
 *
 * @author Przemyslaw Walkowiak <przemyslaw.walkowiak@put.poznan.pl>
 */
@Controller
@RequestMapping(value = "/tasks")
public class TaskController {

    private static final Logger logger = LoggerFactory.getLogger(TaskController.class);

    @Autowired
    TaskService taskService;

    public TaskController() {
    }

    //************************** Views ********************************************    
    //########################## Tasks ###########################################
    @RequestMapping
    public String getTasks(Model model) {
        model.addAttribute("tasks", taskService.getTasks());
        return "view.tasks.list";
    }

    //########################## Task ###########################################
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String getAddTask(Model model) {
        model.addAttribute("new_task", new Task());
        return "view.tasks.add";
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String getViewTask(Model model, @PathVariable Long id) {
        model.addAttribute("task", taskService.getTask(id));
        return "view.tasks.view";
    }

    @RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
    public String getEditTask(Model model, @PathVariable Long id) {
        model.addAttribute("task", taskService.getTask(id));
        return "view.tasks.edit";
    }

    @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
    public String getDeleteTask(Model model, @PathVariable Long id) {
        model.addAttribute("task", taskService.getTask(id));
        return "view.tasks.delete";
    }

    //************************** REST ********************************************    
    //########################## Tasks ###########################################
    @RequestMapping(method = RequestMethod.POST)
    public String addTask(@Valid @ModelAttribute("new_task") Task task, BindingResult result) {
        if (result.hasErrors()) {
            return "view.tasks.add";
        }
        taskService.saveTask(task);
        return "redirect:/tasks";
    }

    //########################## Task ###########################################
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public String deleteTask(@PathVariable Long id) {
        taskService.deleteTask(id);
        return "redirect:/tasks";
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String updateTask(@PathVariable Long id, @Valid @ModelAttribute("task") Task task,
            BindingResult result) {
        task.setId(id);
        if (result.hasErrors()) {
            return "view.tasks.edit";
        } else {
            taskService.saveTask(task);
            return "redirect:/tasks";
        }
    }

}