cn.itganhuo.app.web.controller.ArticleController.java Source code

Java tutorial

Introduction

Here is the source code for cn.itganhuo.app.web.controller.ArticleController.java

Source

/*
 * Copyright 2014-2024 the https://github.com/xiaoxing598/itganhuo.
 *
 * 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
 *
 * This project consists of JAVA private school online learning community group Friends co-creator [QQ group 329232140].
 * JAVA???[QQ329232140];
 * See the list of IT dry technology sharing network [http://www.itganhuo.cn/teams].
 * ????IT[http://www.itganhuo.cn/teams];
 * The author does not guarantee the quality of the project and its stability, reliability, and security does not bear any responsibility.
 * ????????.
 */
package cn.itganhuo.app.web.controller;

import cn.itganhuo.app.common.page.Pagination;
import cn.itganhuo.app.common.pool.ConfigPool;
import cn.itganhuo.app.common.pool.ConstantPool;
import cn.itganhuo.app.common.utils.DateUtil;
import cn.itganhuo.app.common.utils.StringUtil;
import cn.itganhuo.app.entity.*;
import cn.itganhuo.app.service.ArticleService;
import cn.itganhuo.app.service.AttentionService;
import cn.itganhuo.app.service.CommentService;
import cn.itganhuo.app.service.ReplyService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <h2></h2>
 * <dl>
 * <dt>??</dt>
 * <dd></dd>
 * <dt></dt>
 * <dd></dd>
 * </dl>
 *
 * @author -?-?
 * @version 0.0.1-SNAPSHOT
 */
@Controller
public class ArticleController {

    private static final Logger log = LogManager.getLogger(ArticleController.class.getName());

    @Autowired
    private ArticleService articleService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private ReplyService replyService;
    @Autowired
    private AttentionService attentionService;

    /**
     * <h2></h2>
     * <dl>
     * <dt>??</dt>
     * <dd>??</dd>
     * <dt></dt>
     * <dd></dd>
     * </dl>
     *
     * @param article     ??
     * @param search_type ?type=1?type=2?type=3
     * @param now_page    ??1
     * @param request     Http
     * @return ??
     * @version 0.0.1-SNAPSHOT
     * @author -?
     */
    @RequestMapping(value = "/articles/{search_type}/{now_page}", method = RequestMethod.GET)
    public ModelAndView articles(Article article, @PathVariable String search_type, @PathVariable String now_page,
            HttpServletRequest request) {
        // 
        String request_get_context_path = request.getContextPath();

        // ?
        Paging paging = new Paging();
        if (!search_type.matches("^[123]?$")) {
            search_type = "1";
        }
        if ("1".equalsIgnoreCase(search_type)) { // 
            paging.setSort("id");
            paging.setOrder("DESC");
        } else if ("2".equalsIgnoreCase(search_type)) { // 
            paging.setSort("visitorNum");
            paging.setOrder("DESC");
        } else if ("3".equalsIgnoreCase(search_type)) { // 
            paging.setSort("visitorNum");
            paging.setOrder("ASC");
        }
        if (now_page.matches("^([1-9]|[1-9][0-9]+)+$")) {
            paging.setPage(StringUtil.getInt(now_page, 1));
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("article", article);
        map.put("paging", paging);

        // ??
        List<Article> articles = articleService.findArticleByCondition(map);
        int total = articleService.countArticleRows(null);
        Pagination pagination = new Pagination(StringUtil.getInt(now_page, 1), paging.getRows(), 5, total,
                request_get_context_path.concat("/articles"), search_type);

        // ?
        ModelAndView mav = new ModelAndView();
        mav.addObject("articles", articles);
        mav.addObject("pagination", pagination);
        mav.addObject("search_type", search_type);
        mav.addObject("path", request.getContextPath());
        mav.addObject("servletPath", request_get_context_path);
        mav.setViewName("article_list");
        return mav;
    }

    /**
     * <h2>???</h2>
     * <dl>
     * <dt>??</dt>
     * <dd>??</dd>
     * <dt></dt>
     * <dd></dd>
     * </dl>
     *
     * @param article     ??
     * @param search_type ?type=1?type=2?type=3
     * @param now_page    ??1
     * @param label_id    ID
     * @param request     Http
     * @return ??
     * @version 0.0.1-SNAPSHOT
     * @author -?
     */
    @RequestMapping(value = "/articles/{search_type}/{now_page}/{label_id}", method = RequestMethod.GET)
    public ModelAndView articlesByLabelId(Article article, @PathVariable String search_type,
            @PathVariable String now_page, @PathVariable String label_id, HttpServletRequest request) {
        // 
        String request_get_context_path = request.getContextPath();

        // ?
        Paging paging = new Paging();
        if (!search_type.matches("^[123]?$")) {
            search_type = "1";
        }
        if ("1".equalsIgnoreCase(search_type)) { // 
            paging.setSort("id");
            paging.setOrder("DESC");
        } else if ("2".equalsIgnoreCase(search_type)) { // 
            paging.setSort("visitorNum");
            paging.setOrder("DESC");
        } else if ("3".equalsIgnoreCase(search_type)) { // 
            paging.setSort("visitorNum");
            paging.setOrder("ASC");
        }
        if (now_page.matches("^([1-9]|[1-9][0-9]+)+$")) {
            paging.setPage(StringUtil.getInt(now_page, 1));
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("article", article);
        map.put("paging", paging);
        map.put("labelId", label_id);

        // ??
        List<Article> articles = articleService.findArticleByCondition(map);
        int total = articleService.countArticleRows(map);
        Pagination pagination = new Pagination(StringUtil.getInt(now_page, 1), paging.getRows(), 5, total,
                request_get_context_path.concat("/articles"), search_type, label_id);

        // ?
        ModelAndView mav = new ModelAndView();
        mav.addObject("articles", articles);
        mav.addObject("pagination", pagination);
        mav.addObject("search_type", search_type);
        mav.addObject("path", request.getContextPath());
        mav.addObject("servletPath", request_get_context_path);
        mav.addObject("label_id", label_id);
        mav.setViewName("article_list");
        return mav;
    }

    /**
     * <h2></h2>
     * <dl>
     * <dt>??</dt>
     * <dd></dd>
     * <dt></dt>
     * <dd></dd>
     * </dl>
     *
     * @param id ID
     * @return ?
     * @version 0.0.1-SNAPSHOT
     * @author -?
     */
    @RequestMapping(value = "/article/{ymd}/{id}", method = RequestMethod.GET)
    public ModelAndView getArticleById(@PathVariable(value = "ymd") String ymd,
            @PathVariable(value = "id") Integer id, HttpServletRequest request) {
        ModelAndView mav = articleService.getArticleById(ymd, id, request);
        return mav;
    }

    /**
     * ????t_reply
     *
     * @param reply
     * @return JSON???
     * @author -?(504487927)
     * @version 2014-11-21
     */
    @RequiresAuthentication
    @Transactional
    @RequestMapping(value = "/article/saveReply")
    @ResponseBody
    public RespMsg saveReply(Reply reply, @RequestParam Integer comment_id) {
        // ?
        String content = StringUtil.ifContainsSpecialStrReplace(reply.getContent());
        reply.setContent(content);
        // ???
        Subject current_user = SecurityUtils.getSubject();
        User user_model = (User) current_user.getSession().getAttribute(ConstantPool.USER_SHIRO_SESSION_ID);
        // ???
        reply.setUserId(user_model.getId());
        reply.setCommentId(comment_id);
        reply.setPostDate(DateUtil.getNowDateTimeStr(null));
        reply.setParentId(0);
        RespMsg respMsg = new RespMsg();
        if (replyService.addReply(reply) > 0) {
            respMsg.setStatus("0000");
            respMsg.setMessage(ConfigPool.getString("respMsg.reply.SaveReplySuccess"));
        } else {
            respMsg.setStatus("9999");
            respMsg.setMessage(ConfigPool.getString("respMsg.reply.SaveReplyFailure"));
        }
        return respMsg;
    }

    /**
     * <h2></h2>
     * <dl>
     * <dt>??</dt>
     * <dd></dd>
     * <dt></dt>
     * <dd></dd>
     * </dl>
     *
     * @param id   ID
     * @param type 1,2,3,4,5
     * @return
     * @version 0.0.1-SNAPSHOT
     * @author -?
     */
    @RequiresAuthentication
    @Transactional
    @RequestMapping(value = "/article/appraise", method = RequestMethod.POST)
    @ResponseBody
    public RespMsg addUsefulById(@RequestParam Integer id, @RequestParam Integer type) {
        RespMsg respMsg = new RespMsg();
        // ??
        Subject current_user = SecurityUtils.getSubject();
        if (current_user == null) {
            respMsg.setStatus("9000");
            return respMsg;
        }
        User user = (User) current_user.getSession().getAttribute(ConstantPool.USER_SHIRO_SESSION_ID);
        if (user == null) {
            respMsg.setStatus("9000");
            return respMsg;
        }
        // 
        if (2 == type || 3 == type || 4 == type || 5 == type) {
            // ?
            Article article = articleService.getArticleById(id);
            // ??
            if (!article.getUserId().equals(user.getId())) {
                // ???
                if (!commentService.isInvolvedComment(id, user.getId())) {
                    // 
                    Comment comment_model = new Comment();
                    comment_model.setType(type);
                    comment_model.setObjId(id);
                    comment_model.setUserId(user.getId());
                    comment_model.setPostDate(DateUtil.getNowDateTimeStr(null));
                    if (2 == type) {
                        comment_model.setContent("");
                    } else if (3 == type) {
                        comment_model.setContent("");
                    } else if (4 == type) {
                        comment_model.setContent("");
                    } else if (5 == type) {
                        comment_model.setContent("");
                    }
                    commentService.addComment(comment_model);
                    // 
                    if (2 == type) {
                        articleService.addPraiseNum(id);
                    } else if (3 == type) {
                        articleService.addTrampleNum(id);
                    } else if (4 == type) {
                        commentService.addPraiseById(id);
                    } else if (5 == type) {
                        commentService.addTrampleById(id);
                    }
                } else {
                    respMsg.setStatus("1001");
                    respMsg.setMessage(
                            ConfigPool.getString("respMsg.comment.AddUsefulOrUseless.RepetitiveOperation"));
                }
            } else {
                respMsg.setStatus("1000");
                respMsg.setMessage(ConfigPool.getString("respMsg.comment.AddUsefulOrUseless.SamePerson"));
            }
        } else {
            respMsg.setStatus("9999");
            respMsg.setMessage(ConfigPool.getString("respMsg.EvaluationTypeIncorrect"));
        }
        return respMsg;
    }

    /**
     * ????
     *
     * @param attention ?
     * @return ???
     */
    @RequiresAuthentication
    @Transactional
    @RequestMapping(value = "/article/saveAttentionInfo", method = RequestMethod.POST)
    @ResponseBody
    public RespMsg saveAttentionInfo(Attention attention) {
        RespMsg respMsg = attentionService.saveAttentionInfo(attention);
        return respMsg;
    }

}