com.jee.shiro.web.task.TaskController.java Source code

Java tutorial

Introduction

Here is the source code for com.jee.shiro.web.task.TaskController.java

Source

/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.jee.shiro.web.task;

import java.util.Map;

import javax.servlet.ServletRequest;
import javax.validation.Valid;

import com.jee.shiro.entity.Task;
import com.jee.shiro.entity.User;
import com.jee.shiro.service.account.ShiroDbRealm;
import com.jee.shiro.service.task.TaskService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.mvc.support.RedirectAttributes;

import com.google.common.collect.Maps;
import org.springside.modules.web.Servlets;

/**
 * Task?Controller, RestfulUrls:
 * 
 * List page : GET /task/
 * Create page : GET /task/create
 * Create action : POST /task/create
 * Update page : GET /task/update/{id}
 * Update action : POST /task/update
 * Delete action : GET /task/delete/{id}
 * 
 * @author calvin
 */
@Controller
@RequestMapping(value = "/task")
public class TaskController {

    private static final String PAGE_SIZE = "3";

    private static Map<String, String> sortTypes = Maps.newLinkedHashMap();
    static {
        sortTypes.put("auto", "");
        sortTypes.put("title", "");
    }

    @Autowired
    private TaskService taskService;

    @RequestMapping(method = RequestMethod.GET)
    public String list(@RequestParam(value = "page", defaultValue = "1") int pageNumber,
            @RequestParam(value = "page.size", defaultValue = PAGE_SIZE) int pageSize,
            @RequestParam(value = "sortType", defaultValue = "auto") String sortType, Model model,
            ServletRequest request) {
        Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
        Long userId = getCurrentUserId();

        Page<Task> tasks = taskService.getUserTask(userId, searchParams, pageNumber, pageSize, sortType);

        model.addAttribute("tasks", tasks);
        model.addAttribute("content", tasks.getContent());
        model.addAttribute("sortType", sortType);
        model.addAttribute("sortTypes", sortTypes);
        // ?????URL
        model.addAttribute("searchParams", Servlets.encodeParameterStringWithPrefix(searchParams, "search_"));

        return "task/taskList";
    }

    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String createForm(Model model) {
        model.addAttribute("task", new Task());
        model.addAttribute("action", "create");
        return "task/taskForm";
    }

    @RequestMapping(value = "create", method = RequestMethod.POST)
    public String create(@Valid Task newTask, RedirectAttributes redirectAttributes) {
        User user = new User(getCurrentUserId());
        newTask.setUser(user);

        taskService.saveTask(newTask);
        redirectAttributes.addFlashAttribute("message", "?");
        return "redirect:/task/";
    }

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

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String update(@Valid @ModelAttribute("task") Task task, RedirectAttributes redirectAttributes) {
        taskService.saveTask(task);
        redirectAttributes.addFlashAttribute("message", "?");
        return "redirect:/task/";
    }

    @RequestMapping(value = "delete/{id}")
    public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
        taskService.deleteTask(id);
        redirectAttributes.addFlashAttribute("message", "?");
        return "redirect:/task/";
    }

    /**
     * RequestMapping?Model, Struts2 Preparable,?formid?Task,?Form??
     * update()formidupdate.
     */
    @ModelAttribute
    public void getTask(@RequestParam(value = "id", defaultValue = "-1") Long id, Model model) {
        if (id != -1) {
            model.addAttribute("task", taskService.getTask(id));
        }
    }

    /**
     * ?Shiro?Id.
     */
    private Long getCurrentUserId() {
        ShiroDbRealm.ShiroUser user = (ShiroDbRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
        return user.id;
    }
}