com.dlshouwen.tdjs.video.controller.TdjsVideoController.java Source code

Java tutorial

Introduction

Here is the source code for com.dlshouwen.tdjs.video.controller.TdjsVideoController.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.video.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.AttributeUtils;
import com.dlshouwen.core.base.utils.ConvertVideo;
import com.dlshouwen.core.base.utils.GUID;
import com.dlshouwen.core.base.utils.LogUtils;
import com.dlshouwen.core.base.utils.WebUtil;
import com.dlshouwen.core.system.dao.UserDao;
import com.dlshouwen.core.system.model.User;
import com.dlshouwen.tdjs.video.dao.TdjsVideoDao;
import com.dlshouwen.tdjs.video.model.Video;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
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;

/**
 * controller
 *
 * @author admin
 */
@Controller
@RequestMapping("/tdjs/tdjsVideo/video")
public class TdjsVideoController {

    //
    private String basePath = "tdjs/video/";

    private TdjsVideoDao dao;
    private UserDao userDao;

    //dao
    @Resource(name = "tdjsVideoDao")
    public void setDao(TdjsVideoDao dao) {
        this.dao = dao;
    }

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

    //?
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String goVideolPage(HttpServletRequest request, Model model) throws Exception {
        LogUtils.updateOperationLog(request, OperationType.VISIT, "??");
        //?
        return basePath + "videoList";
    }

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

    /**
     * ?
     *
     * @param model ??
     * @param request 
     * @return basePath + 'addVideo'
     * @throws Exception 
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addVideo(Model model, HttpServletRequest request) throws Exception {
        Video video = new Video();
        video.setVd_isdisplay("0");
        model.addAttribute("video", video);
        //      ?
        LogUtils.updateOperationLog(request, OperationType.VISIT, "?");
        return basePath + "addVideo";
    }

    /**
     * 
     *
     * @param video 
     * @param bindingResult 
     * @param request 
     * @return ajax?
     * @throws Exception 
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse addVideo(@Valid Video video, BindingResult bindingResult, HttpServletRequest request)
            throws Exception {

        AjaxResponse ajaxResponse = new AjaxResponse();
        String localPath = WebUtil.getCookie(request, "videoPath");

        String videoTime = WebUtil.getCookie(request, "videoTime");
        String coverPath = WebUtil.getCookie(request, "coverPath");

        if (localPath == null || localPath.trim().length() == 0) {
            ajaxResponse.setWarning(true);
            ajaxResponse.setWarningMessage(
                    "?<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;??");
            return ajaxResponse;
        }
        if (bindingResult.hasErrors()) {
            ajaxResponse.bindingResultHandler(bindingResult);
            LogUtils.updateOperationLog(request, OperationType.INSERT,
                    "??" + video.getVd_name() + "?"
                            + AjaxResponse.getBindingResultMessage(bindingResult) + "");
            return ajaxResponse;
        }
        //      ????
        SessionUser sessionUser = (SessionUser) request.getSession().getAttribute(CONFIG.SESSION_USER);
        String userName = sessionUser.getUser_name();
        Date nowDate = new Date();
        //      ?????
        video.setVd_id(new GUID().toString());
        video.setVd_uploaduser(userName);
        video.setVd_uploaddate(nowDate);
        video.setVd_thumbnailspath(coverPath);
        video.setVd_savepath(localPath);
        video.setVd_status("1");
        video.setVd_time(videoTime);
        //      
        dao.insertVideo(video);
        String toPath = localPath.substring(0, localPath.lastIndexOf("/"));
        Thread thread = new Thread(new SimpleThread(localPath, toPath, video.getVd_id()));
        thread.start();
        //      ????
        ajaxResponse.setSuccess(true);
        ajaxResponse.setSuccessMessage("??");
        //?
        Map map = new HashMap();
        map.put("URL", "tdjs/tdjsVideo/video");
        ajaxResponse.setExtParam(map);

        //      ?
        LogUtils.updateOperationLog(request, OperationType.INSERT,
                "id" + video.getVd_id() + "??" + video.getVd_name());
        return ajaxResponse;
    }

    /**
     * ?
     *
     * @param videoId ??
     * @param model ??
     * @param request 
     * @return base + 'editVideo'
     * @throws Exception 
     */
    @RequestMapping(value = "/{videoId}/edit", method = RequestMethod.GET)
    public String editVideo(@PathVariable String videoId, Model model, HttpServletRequest request)
            throws Exception {
        Video video = dao.getVideoById(videoId);
        //      ??
        model.addAttribute("video", video);
        //      ?
        LogUtils.updateOperationLog(request, OperationType.VISIT, "?id"
                + video.getVd_id() + "??" + video.getVd_name());
        //      ?
        return basePath + "editVideo";
    }

    /**
     * 
     *
     * @param video 
     * @param bindingResult 
     * @return ajax?
     * @throws Exception 
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse editVideo(@Valid Video video, HttpServletRequest request, BindingResult bindingResult)
            throws Exception {
        //      AJAX?
        AjaxResponse ajaxResponse = new AjaxResponse();
        //      ??
        if (bindingResult.hasErrors()) {
            ajaxResponse.bindingResultHandler(bindingResult);
            //         ?
            LogUtils.updateOperationLog(request, OperationType.UPDATE,
                    "id" + video.getVd_id() + "??"
                            + video.getVd_name() + "?"
                            + AjaxResponse.getBindingResultMessage(bindingResult) + "");
            return ajaxResponse;
        }
        //      ????
        SessionUser sessionUser = (SessionUser) request.getSession().getAttribute(CONFIG.SESSION_USER);
        String userName = sessionUser.getUser_name();
        Date nowDate = new Date();
        //      ?
        video.setVd_updatedate(nowDate);
        video.setVd_updateuser(userName);
        //      
        dao.updateVideo(video);
        //      ????
        ajaxResponse.setSuccess(true);
        ajaxResponse.setSuccessMessage("??");
        //?
        Map map = new HashMap();
        map.put("URL", "tdjs/tdjsVideo/video");
        ajaxResponse.setExtParam(map);

        //      ?
        LogUtils.updateOperationLog(request, OperationType.UPDATE,
                "id" + video.getVd_id() + "??" + video.getVd_name());
        return ajaxResponse;
    }

    /**
     * 
     *
     * @param videoIds ?
     * @param request 
     * @return ajax?
     * @throws Exception 
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse deleteVideo(String videoIds, HttpServletRequest request) throws Exception {

        dao.deleteVideo(videoIds);
        //      ?
        AjaxResponse ajaxResponse = new AjaxResponse();
        ajaxResponse.setSuccess(true);
        ajaxResponse.setSuccessMessage("??");
        //      ?
        LogUtils.updateOperationLog(request, OperationType.DELETE, "id" + videoIds);
        return ajaxResponse;
    }

    //?
    @RequestMapping(value = "/open", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse openVideo(String videoIds, HttpServletRequest request) throws Exception {
        dao.openVideo(videoIds);

        //      ?
        AjaxResponse ajaxResponse = new AjaxResponse();
        ajaxResponse.setSuccess(true);
        ajaxResponse.setSuccessMessage("???");
        //      ?
        LogUtils.updateOperationLog(request, OperationType.UPDATE,
                "??" + videoIds);
        return ajaxResponse;
    }

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

    /**
     * 
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse uploadVideo(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String path = "";
        String videoTime = "";
        String coverPath = "";
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile multipartFile = multipartRequest.getFile("file");
        String fileName = multipartFile.getOriginalFilename();
        if (multipartFile != null && StringUtils.isNotEmpty(fileName)) {
            JSONObject jobj = FileUploadClient.upFile(request, fileName, multipartFile.getInputStream());
            if (jobj != null && jobj.getString("responseMessage").equals("OK")) {
                path = jobj.getString("fpath");
                //
                videoTime = jobj.getString("videoTime");
                coverPath = jobj.getString("videoCoverPath");
            }
        }
        WebUtil.addCookie(request, response, "videoPath", path, -1);
        WebUtil.addCookie(request, response, "videoTime", videoTime, -1);
        WebUtil.addCookie(request, response, "coverPath", coverPath, -1);

        //      ?
        AjaxResponse ajaxResponse = new AjaxResponse();
        ajaxResponse.setSuccess(true);
        ajaxResponse.setSuccessMessage("???");
        //      ?
        LogUtils.updateOperationLog(request, OperationType.UPDATE,
                "??" + fileName);
        return ajaxResponse;
    }

    /**
     * ?
     *
     * @param model ??
     * @param request 
     * @return basePath + 'addVideo'
     * @throws Exception 
     */
    @RequestMapping(value = "/preview", method = RequestMethod.GET)
    public String previewVideo(String videoId, HttpServletRequest request) throws Exception {
        Video video = dao.getVideoById(videoId);
        request.setAttribute("videoPath", video.getVd_savepath().replaceAll("\\\\", "/"));
        String videoName = "";
        if (video.getVd_savepath() != null) {
            videoName = video.getVd_savepath().substring(video.getVd_savepath().lastIndexOf("\\") + 1);
        }
        request.setAttribute("imagePath", video.getVd_thumbnailspath().replaceAll("\\\\", "/"));
        request.setAttribute("videoName", videoName);
        //      ?
        LogUtils.updateOperationLog(request, OperationType.VISIT, "?");
        return basePath + "preview";
    }
}