com.dlshouwen.tdjs.album.controller.TdjsAlbumController.java Source code

Java tutorial

Introduction

Here is the source code for com.dlshouwen.tdjs.album.controller.TdjsAlbumController.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.album.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.album.dao.AlbumDao;
import com.dlshouwen.tdjs.album.model.Album;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
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/tdjsAlbum/album")
public class TdjsAlbumController {

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

    private AlbumDao dao;
    private UserDao userDao;

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

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

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

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

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

    /**
     * 
     *
     * @param album 
     * @param bindingResult 
     * @param request 
     * @return ajax?
     * @throws Exception 
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public void addAlbum(@Valid Album album, BindingResult bindingResult, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        AjaxResponse ajaxResponse = new AjaxResponse();

        if (bindingResult.hasErrors()) {
            ajaxResponse.bindingResultHandler(bindingResult);
            LogUtils.updateOperationLog(request, OperationType.INSERT,
                    "??" + 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);

        //      ????
        SessionUser sessionUser = (SessionUser) request.getSession().getAttribute(CONFIG.SESSION_USER);
        String userName = sessionUser.getUser_name();
        String userId = sessionUser.getUser_id();
        Date nowDate = new Date();
        //      ?????
        album.setAlbum_id(new GUID().toString());
        album.setAlbum_createuser(userName);
        album.setAlbum_createuserbyid(userId);
        album.setAlbum_createdate(nowDate);

        //      
        dao.insertAlbum(album);
        //      ????
        ajaxResponse.setSuccess(true);
        ajaxResponse.setSuccessMessage("??");

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

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

    /**
     * ?
     *
     * @param albumId ??
     * @param model ??
     * @param request 
     * @return base + 'editAlbum'
     * @throws Exception 
     */
    @RequestMapping(value = "/{albumId}/edit", method = RequestMethod.GET)
    public String editAlbumPage(@PathVariable String albumId, Model model, HttpServletRequest request)
            throws Exception {
        Album album = dao.getAlbumById(albumId);
        //      ??
        model.addAttribute("album", album);
        //      ?
        LogUtils.updateOperationLog(request, OperationType.VISIT, "?id"
                + album.getAlbum_id() + "??" + album.getAlbum_name());
        //      ?
        return basePath + "editAlbum";
    }

    /**
     * 
     *
     * @param album 
     * @param bindingResult 
     * @return ajax?
     * @throws Exception 
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public void editAlbum(@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);

        //      
        dao.updateAlbum(album);
        //      ????
        ajaxResponse.setSuccess(true);
        ajaxResponse.setSuccessMessage("??");
        //?
        Map map = new HashMap();
        map.put("URL", "tdjs/tdjsAlbum/album");
        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 albumIds ? 
     * @param request 
     * @return ajax?
     * @throws Exception 
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse deleteAlbum(String[] albumIds, HttpServletRequest request) throws Exception {
        for (int i = 0; i < albumIds.length; i++) {
            //?id 
            dao.deletePicByAlbId(albumIds[i]);
            //
            dao.deleteAlbum(albumIds[i]);
        }
        //      ?
        AjaxResponse ajaxResponse = new AjaxResponse();
        ajaxResponse.setSuccess(true);
        ajaxResponse.setSuccessMessage("??");
        //      ?
        LogUtils.updateOperationLog(request, OperationType.DELETE, "id" + albumIds);
        return ajaxResponse;
    }

    /**
     * 
     *
     * @param albumId
     * @param model
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/{albumId}/openAlbum", method = RequestMethod.GET)
    public String openAlbum(@PathVariable String albumId, Model model, HttpServletRequest request)
            throws Exception {
        Album album = dao.getAlbumById(albumId);
        List pic = dao.findPicByAlbumId(albumId);
        model.addAttribute("pic", pic);
        //      ??
        model.addAttribute("album", album);
        //      ?
        LogUtils.updateOperationLog(request, OperationType.VISIT, "?id"
                + album.getAlbum_id() + "??" + album.getAlbum_name());
        //      ?
        return basePath + "openAlbum";
    }

    /**
     * ??
     *
     * @param albumId id
     * @param albCoverpath ??
     * @param request
     */
    @RequestMapping(value = "/updateAlbCover", method = RequestMethod.POST)
    public void updateAlbCover(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String albumId = request.getParameter("albumId");
        String albCoverPath = request.getParameter("albCoverPath");
        dao.setAlbCover(albumId, albCoverPath);
        //      ?
        AjaxResponse ajaxResponse = new AjaxResponse();
        ajaxResponse.setSuccess(true);
        ajaxResponse.setSuccessMessage("??");
        //      ?
        LogUtils.updateOperationLog(request, OperationType.DELETE,
                "id" + albumId + ",??" + albCoverPath);
        response.setContentType("text/html;charset=utf-8");
        JSONObject obj = JSONObject.fromObject(ajaxResponse);
        response.getWriter().write(obj.toString());
    }

}