org.bisen.blog.web.BlogController.java Source code

Java tutorial

Introduction

Here is the source code for org.bisen.blog.web.BlogController.java

Source

/*
 * Copyright 2014 asikprad.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.bisen.blog.web;

import javax.validation.Valid;
import org.bisen.blog.model.Blog;
import org.bisen.blog.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefaults;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
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.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author asikprad
 */
@Controller
@SessionAttributes(types = Blog.class)
public class BlogController {

    private final BlogService blogService;

    @Autowired
    public BlogController(BlogService blogService) {
        this.blogService = blogService;
    }

    @RequestMapping(value = "/blogs/new", method = RequestMethod.GET)
    public String initCreationForm(Model model) {
        model.addAttribute(new Blog());
        return "blogs/createOrUpdateBlogForm";
    }

    @RequestMapping(value = "/blogs", method = RequestMethod.GET)
    public String blogsIndex(Model model,
            @PageableDefaults(pageNumber = 0, value = 2, sort = "lastModifiedDate_dt", sortDir = Sort.Direction.DESC) Pageable pageable) {

        model.addAttribute("blogs", blogService.findAll(pageable));
        return "blogs/welcome";
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(Model model,
            @PageableDefaults(pageNumber = 0, value = 2, sort = "lastModifiedDate_dt", sortDir = Sort.Direction.DESC) Pageable pageable) {

        model.addAttribute("blogs", blogService.findAll(pageable));
        return "blogs/welcome";
    }

    @RequestMapping(value = "/blogs/new", method = RequestMethod.POST)
    public String processCreationForm(@Valid Blog blog, BindingResult result, SessionStatus status) {
        if (result.hasErrors()) {
            return "blogs/createOrUpdateBlogForm";
        } else {
            this.blogService.save(blog);
            status.setComplete();
            return "redirect:/blogs/" + blog.getId();
        }
    }

    @RequestMapping(value = "/blogs/{blogId}/edit", method = RequestMethod.GET)
    public String initUpdateBlogForm(@PathVariable("blogId") int blogId, Model model) {
        Blog blog = this.blogService.find(blogId);
        model.addAttribute(blog);
        return "blogs/createOrUpdateBlogForm";
    }

    @RequestMapping(value = "/blogs/{blogId}/edit", method = RequestMethod.PUT)
    public String processUpdateBlogForm(@Valid Blog blog, BindingResult result, SessionStatus status) {
        if (result.hasErrors()) {
            return "blogs/createOrUpdateBlogForm";
        } else {
            this.blogService.save(blog);
            status.setComplete();
            return "redirect:/blogs/" + blog.getId();
        }
    }

    @RequestMapping(value = "/blogs/find", method = RequestMethod.GET)
    public String initFindForm(Model model) {
        model.addAttribute("blog", new Blog());
        return "blogs/findBlogs";
    }

    /**
     * Custom handler for displaying an owner.
     *
     * @param blogId the ID of the owner to display
     * @return a ModelMap with the model attributes for the view
     */
    @RequestMapping("/blogs/{blogId}")
    public ModelAndView showBlog(@PathVariable("blogId") int blogId) {
        ModelAndView mav = new ModelAndView("blogs/blogDetails");
        mav.addObject(this.blogService.find(blogId));
        return mav;
    }

    @RequestMapping("/blogs/search")
    public String searchBlogs(@RequestParam(value = "q", defaultValue = "") String query, Model model,
            @PageableDefaults(pageNumber = 0, value = 2) Pageable pageable) {

        model.addAttribute("blogs",
                "".equals(query) ? blogService.findAll(pageable) : blogService.highlight(query, pageable));
        model.addAttribute("q", query);

        return "blogs/blogsList";
    }

    @RequestMapping(value = "/blogs/login")
    public String initLogin(Model model) {
        return "blogs/login";
    }

    @RequestMapping(value = "/blogs/loginError")
    public String initLoginError(Model model) {
        model.addAttribute("loginError", true);
        return "blogs/login";
    }
}