com.dlshouwen.tdjs.content.controller.TdjsArticleController.java Source code

Java tutorial

Introduction

Here is the source code for com.dlshouwen.tdjs.content.controller.TdjsArticleController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dlshouwen.tdjs.content.controller;

import com.dlshouwen.core.base.config.CONFIG;
import com.dlshouwen.core.base.extra.grid.model.Pager;
import com.dlshouwen.core.base.extra.grid.utils.PagerPropertyUtils;
import com.dlshouwen.core.base.http.FileUploadClient;
import com.dlshouwen.core.base.model.AjaxResponse;
import com.dlshouwen.core.base.model.OperationType;
import com.dlshouwen.core.base.model.SessionUser;
import com.dlshouwen.core.base.utils.GUID;
import com.dlshouwen.core.base.utils.LogUtils;
import com.dlshouwen.core.system.dao.UserDao;
import com.dlshouwen.core.system.model.User;
import com.dlshouwen.tdjs.content.dao.ArticleDao;
import com.dlshouwen.tdjs.content.model.Article;
import com.dlshouwen.tdjs.album.dao.AlbumDao;
import com.dlshouwen.tdjs.album.model.Album;
import com.dlshouwen.tdjs.channel.dao.ChannelDao;
import com.dlshouwen.tdjs.channel.model.Channel;
import com.dlshouwen.tdjs.picture.model.Picture;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
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.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**
 * 
 *
 * @author cui
 */
@Controller
@RequestMapping("/tdjs/tdjsArticle/article")
public class TdjsArticleController {

    /**
     * 
     */
    private String basePath = "tdjs/article/";

    private ArticleDao dao;

    private AlbumDao albumDao;

    private UserDao userDao;

    private ChannelDao channelDao;

    /**
     * ??
     *
     * @param dao ??
     */
    @Resource(name = "tdjsArticleDao")
    public void setDao(ArticleDao dao) {
        this.dao = dao;
    }

    @Resource(name = "TdjsAlbumDao")
    public void setAlbumDao(AlbumDao albumDao) {
        this.albumDao = albumDao;
    }

    @Resource(name = "userDao")
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Resource(name = "TdjsChannelDao")
    public void setChannelDao(ChannelDao channelDao) {
        this.channelDao = channelDao;
    }

    /**
     * ??
     *
     * @param request 
     * @return basePath + 'articleList'
     * @throws Exception 
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String goArticlePage(HttpServletRequest request) throws Exception {
        //       ?
        LogUtils.updateOperationLog(request, OperationType.VISIT, "??");

        //?     
        List<Map<String, Object>> channelList = channelDao.getChannelList();
        Map map1 = new HashMap();
        for (Map map : channelList) {
            String channel_id = (String) map.get("channel_id");
            String channel_name = (String) map.get("channel_name");
            map1.put(channel_id, channel_name);
        }
        request.setAttribute("channelList", JSONObject.fromObject(map1).toString());
        String selChannelId = request.getParameter("selChannelId");
        request.setAttribute("selChannelId", selChannelId);
        return basePath + "articleList";
    }

    /**
     * 
     *
     * @param album 
     * @param bindingResult 
     * @return ajax?
     * @throws Exception 
     */
    @RequestMapping(value = "/editAlbum", method = RequestMethod.POST)
    public void editArtAlbum(@Valid Album album, HttpServletRequest request, BindingResult bindingResult,
            HttpServletResponse response) throws Exception {
        //      AJAX?
        AjaxResponse ajaxResponse = new AjaxResponse();
        //      ??
        if (bindingResult.hasErrors()) {
            ajaxResponse.bindingResultHandler(bindingResult);
            //           ?
            LogUtils.updateOperationLog(request, OperationType.UPDATE,
                    "id" + album.getAlbum_id() + "??"
                            + album.getAlbum_name() + "?"
                            + AjaxResponse.getBindingResultMessage(bindingResult) + "");
            response.setContentType("text/html;charset=utf-8");
            JSONObject obj = JSONObject.fromObject(ajaxResponse);
            response.getWriter().write(obj.toString());
            return;
        }

        String path = "";
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile multipartFile = multipartRequest.getFile("picture");
        String orgFileName = multipartFile.getOriginalFilename();
        if (multipartFile != null && StringUtils.isNotEmpty(orgFileName)) {
            JSONObject jobj = FileUploadClient.upFile(request, orgFileName, multipartFile.getInputStream());
            if (jobj != null && jobj.getString("responseMessage").equals("OK")) {
                path = jobj.getString("fpath");
            }
        }
        album.setAlbum_coverpath(path);
        //      ????
        Date nowDate = new Date();
        //      ?
        album.setAlbum_updatedate(nowDate);
        album.setAlbum_flag("1");

        //      
        albumDao.updateAlbum(album);
        //      ????
        ajaxResponse.setSuccess(true);
        ajaxResponse.setSuccessMessage("??");

        //URL??
        Map map = new HashMap();
        map.put("URL", "tdjs/tdjsArticle/article");
        ajaxResponse.setExtParam(map);

        //      ?
        LogUtils.updateOperationLog(request, OperationType.UPDATE,
                "id" + album.getAlbum_id() + "??" + album.getAlbum_name());
        response.setContentType("text/html;charset=utf-8");
        JSONObject obj = JSONObject.fromObject(ajaxResponse);
        response.getWriter().write(obj.toString());
    }

    /**
     * ??
     *
     * @param gridPager ?
     * @param request 
     * @param response ?
     * @throws Exception 
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public void getArticleList(String gridPager, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String selChannelId = request.getParameter("selChannelId");
        //          ??
        SessionUser sessionUser = (SessionUser) request.getSession().getAttribute(CONFIG.SESSION_USER);
        User user = userDao.getUserById(sessionUser.getUser_id());
        //          Pager
        Pager pager = PagerPropertyUtils.copy(JSONObject.fromObject(gridPager));
        //          ??(?)
        if (StringUtils.isNotEmpty(user.getIdentity()) && user.getIdentity().equals("1")) {
            if (StringUtils.isEmpty(user.getTeam_id())) {//?
                dao.getArticleList(pager, request, response, selChannelId);
            } else {//?
                dao.getArticleList(pager, request, response, true, user.getTeam_id(), user.getUser_id(),
                        selChannelId);
            }
        } else if (StringUtils.isNotEmpty(user.getIdentity()) && user.getIdentity().equals("0")
                && StringUtils.isNotEmpty(user.getTeam_id())) {//
            dao.getArticleList(pager, request, response, false, user.getTeam_id(), user.getUser_id(), selChannelId);
        }

        //          ?
        LogUtils.updateOperationLog(request, OperationType.SEARCH,
                "????");
    }

    /**
     * ?
     *
     * @param model ??
     * @param request 
     * @return basePath + 'addArticle'
     * @throws Exception 
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addArticlePage(Model model, HttpServletRequest request) throws Exception {

        //          
        Article article = new Article();

        //?id
        String channelId = request.getParameter("selChannelId");

        //channleid ???
        if (channelId != null && channelId != "") {
            Channel channel = channelDao.getChannelById(channelId);
            String channel_name = channel.getChannel_name();
            String team_id = channel.getTeam_id();
            String team_name = channel.getTeam_name();
            article.setTeam_id(team_id);
            article.setTeam_name(team_name);
            article.setChannel_name(channel_name);
            article.setChannel_id(channelId);
        }

        //          ??
        SessionUser sessionUser = (SessionUser) request.getSession().getAttribute(CONFIG.SESSION_USER);
        String publisher = sessionUser == null ? "" : sessionUser.getUser_name();

        article.setArticle_id(new GUID().toString());

        article.setArticle_limit("2");

        //          ???
        article.setPublisher(publisher);
        //            ?
        article.setPublish_time(new Date());
        //???
        article.setEdit_time(new Date());
        //          ??
        model.addAttribute("article", article);
        //          ?
        LogUtils.updateOperationLog(request, OperationType.VISIT, "?");
        return basePath + "addArticle";
    }

    /**
     * 
     *
     * @param article 
     * @param bindingResult 
     * @param request 
     * @return ajax?
     * @throws Exception 
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse addArticle(@Valid Article article, BindingResult bindingResult, HttpServletRequest request)
            throws Exception {
        //      AJAX?
        AjaxResponse ajaxResponse = new AjaxResponse();
        //      ??
        if (bindingResult.hasErrors()) {
            ajaxResponse.bindingResultHandler(bindingResult);
            //         ?
            LogUtils.updateOperationLog(request, OperationType.INSERT,
                    "???"
                            + AjaxResponse.getBindingResultMessage(bindingResult) + "");
            return ajaxResponse;
        }
        //?ServletContext
        ServletContext context = request.getSession().getServletContext();

        //      ????
        SessionUser sessionUser = (SessionUser) request.getSession().getAttribute(CONFIG.SESSION_USER);
        String userId = sessionUser.getUser_id();
        Date nowDate = new Date();
        //       ?????
        if (null == article.getPublish_time()) {
            article.setPublish_time(nowDate);
        }
        article.setCreator(userId);
        article.setCreate_time(nowDate);
        article.setEditor(userId);
        article.setEdit_time(nowDate);

        article.setArticle_type("2");
        //      
        dao.insertArticle(article);

        //              ???
        Album album = new Album();
        album.setAlbum_id(article.getArticle_id());
        album.setAlbum_name(article.getTitle());
        album.setAlbum_createuserbyid(userId);
        // 1 2
        album.setAlbum_flag("1");//
        album.setAlbum_description("");
        album.setAlbum_createuser(sessionUser.getUser_name());
        album.setAlbum_createdate(nowDate);
        album.setAlbum_updatedate(nowDate);
        //teamId 
        album.setTeam_id(article.getTeam_id());
        album.setTeam_name(article.getTeam_name());
        //              
        albumDao.insertAlbum(album);

        //      ????
        ajaxResponse.setSuccess(true);
        ajaxResponse.setSuccessMessage("??");
        //      ?
        LogUtils.updateOperationLog(request, OperationType.INSERT, "?"
                + article.getArticle_id() + "" + article.getTitle());
        return ajaxResponse;
    }

    /**
     * ?
     *
     * @param articleId ?
     * @param model ??
     * @param request 
     * @return base + 'editAnnouncement'
     * @throws Exception 
     */
    @RequestMapping(value = "/{articleId}/edit", method = RequestMethod.GET)
    public String editArticle(@PathVariable String articleId, Model model, HttpServletRequest request)
            throws Exception {
        //      ??
        Article article = dao.getArticleById(articleId);
        //      ??
        model.addAttribute("article", article);
        //      ?
        LogUtils.updateOperationLog(request, OperationType.VISIT, "?");
        //      ?
        return basePath + "editArticle";
    }

    /**
     * 
     *
     * @param article 
     * @param bindingResult 
     * @param request 
     * @return ajax?
     * @throws Exception 
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse editArticlePag(@Valid Article article, BindingResult bindingResult,
            HttpServletRequest request) throws Exception {
        //      AJAX?
        AjaxResponse ajaxResponse = new AjaxResponse();
        //      ??
        if (bindingResult.hasErrors()) {
            ajaxResponse.bindingResultHandler(bindingResult);
            //         ?
            LogUtils.updateOperationLog(request, OperationType.UPDATE,
                    "???"
                            + AjaxResponse.getBindingResultMessage(bindingResult) + "");
            return ajaxResponse;
        }
        //      ????
        SessionUser sessionUser = (SessionUser) request.getSession().getAttribute(CONFIG.SESSION_USER);
        String userId = sessionUser.getUser_id();
        Date nowDate = new Date();
        //      ?
        article.setEditor(userId);
        article.setEdit_time(nowDate);

        //?? 2
        article.setArticle_type("2");

        //      
        dao.updateArticle(article);
        //      ????
        ajaxResponse.setSuccess(true);
        ajaxResponse.setSuccessMessage("??");
        //URL??
        Map map = new HashMap();
        map.put("URL", "tdjs/tdjsArticle/article");
        ajaxResponse.setExtParam(map);
        //      ?
        LogUtils.updateOperationLog(request, OperationType.UPDATE, "?"
                + article.getArticle_id() + "" + article.getTitle());
        return ajaxResponse;
    }

    /**
     * 
     *
     * @param articleIds ?
     * @param request 
     * @return ajax?
     * @throws Exception 
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse deleteArticle(String articleIds, HttpServletRequest request) throws Exception {
        //      
        dao.deleteArticel(articleIds);
        //              
        albumDao.deletePicByAlbId(articleIds);
        //              
        albumDao.deleteAlbum(articleIds);
        //      ?
        AjaxResponse ajaxResponse = new AjaxResponse();
        ajaxResponse.setSuccess(true);
        ajaxResponse.setSuccessMessage("??");
        //      ?
        LogUtils.updateOperationLog(request, OperationType.DELETE,
                "?" + articleIds);
        return ajaxResponse;
    }

    /**
     * ?
     *
     * @param articleIds ?
     * @param request 
     * @return ajax?
     * @throws Exception 
     */
    @RequestMapping(value = "/open", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse openArticle(String articleIds, HttpServletRequest request) throws Exception {
        //      ?
        dao.openArticle(articleIds);
        //      ?
        AjaxResponse ajaxResponse = new AjaxResponse();
        ajaxResponse.setSuccess(true);
        ajaxResponse.setSuccessMessage("???");
        //      ?
        LogUtils.updateOperationLog(request, OperationType.UPDATE,
                "??" + articleIds);
        return ajaxResponse;
    }

    /**
     * ?
     *
     * @param articleIds ?
     * @param request 
     * @return ajax?
     * @throws Exception 
     */
    @RequestMapping(value = "/close", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse closeArticle(String articleIds, HttpServletRequest request) throws Exception {
        //      ?
        dao.closeArticle(articleIds);
        //      ?
        AjaxResponse ajaxResponse = new AjaxResponse();
        ajaxResponse.setSuccess(true);
        ajaxResponse.setSuccessMessage("???");
        //      ?
        LogUtils.updateOperationLog(request, OperationType.UPDATE,
                "??" + articleIds);
        return ajaxResponse;
    }

    /**
     * 
     *
     * @param articleId
     * @param model
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/{articleId}/openArtAlubm", method = RequestMethod.GET)
    public String openArtAlubm(@PathVariable String articleId, Model model, HttpServletRequest request)
            throws Exception {
        //
        Album album = albumDao.getAlbumById(articleId);
        List<Picture> pic = albumDao.findPicByAlbumId(articleId);
        //?
        model.addAttribute("pic", pic);
        model.addAttribute("album", album);
        LogUtils.updateOperationLog(request, OperationType.VISIT,
                "?id" + articleId);
        return basePath + "openArtAlbum";
    }

    /**
     * ?
     *
     * @param articleId
     * @param model
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/{articleId}/editArtAlubm", method = RequestMethod.GET)
    public String editArtAlubmPag(@PathVariable String articleId, Model model, HttpServletRequest request)
            throws Exception {
        //
        Album album = albumDao.getAlbumById(articleId);
        model.addAttribute("album", album);
        LogUtils.updateOperationLog(request, OperationType.VISIT,
                "?id" + articleId);
        return basePath + "editArtAlbum";
    }

    /**
     * ??
     *
     * @param article
     * @param bindingResult
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/saveDraft", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse saveDraft(@Valid Article article, BindingResult bindingResult, HttpServletRequest request)
            throws Exception {
        //      AJAX?
        AjaxResponse ajaxResponse = new AjaxResponse();
        //      ??
        if (bindingResult.hasErrors()) {
            ajaxResponse.bindingResultHandler(bindingResult);
            //         ?
            LogUtils.updateOperationLog(request, OperationType.UPDATE,
                    "???"
                            + AjaxResponse.getBindingResultMessage(bindingResult) + "");
            return ajaxResponse;
        }
        //      ????
        SessionUser sessionUser = (SessionUser) request.getSession().getAttribute(CONFIG.SESSION_USER);
        String userId = sessionUser.getUser_id();
        Date nowDate = new Date();
        //      ?
        article.setEditor(userId);
        article.setEdit_time(nowDate);

        //?? 2
        article.setArticle_type("4");

        //      
        dao.updateArticle(article);
        //      ????
        ajaxResponse.setSuccess(true);
        ajaxResponse.setSuccessMessage("???");
        //URL??
        Map map = new HashMap();
        map.put("URL", "tdjs/tdjsArticle/article");
        ajaxResponse.setExtParam(map);
        //      ?
        LogUtils.updateOperationLog(request, OperationType.UPDATE, "?"
                + article.getArticle_id() + "" + article.getTitle());
        return ajaxResponse;
    }

}