com.dlshouwen.wzgl.album.controller.AlbumController.java Source code

Java tutorial

Introduction

Here is the source code for com.dlshouwen.wzgl.album.controller.AlbumController.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.wzgl.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.wzgl.album.dao.AlbumDao;
import com.dlshouwen.wzgl.album.model.Album;
import com.dlshouwen.wzgl.picture.dao.PictureDao;
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("/wzgl/album/album")
public class AlbumController {

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

    private AlbumDao dao;

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

    //?
    @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));
        dao.getAlbumList(pager, request, response);
        //          ?
        LogUtils.updateOperationLog(request, OperationType.SEARCH,
                "????");
    }

    /**
     * ?
     *
     * @param model ??
     * @param request 
     * @return basePath + 'addAlbum'
     * @throws Exception 
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addAlbum(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;
        }
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile multipartFile = multipartRequest.getFile("picture");
        String fileName = multipartFile.getOriginalFilename();
        //?
        String path = null;
        if (null != multipartFile && StringUtils.isNotEmpty(multipartFile.getOriginalFilename())) {
            JSONObject jobj = FileUploadClient.upFile(request, fileName, multipartFile.getInputStream());
            if (jobj.getString("responseMessage").equals("OK")) {
                path = jobj.getString("fpath");
            }
        }
        /*
        if (fileName.trim().length() > 0) {
        int pos = fileName.lastIndexOf(".");
        fileName = fileName.substring(pos);
        String fileDirPath = request.getSession().getServletContext().getRealPath("/");
        String path = fileDirPath.substring(0, fileDirPath.lastIndexOf(File.separator)) + CONFIG.UPLOAD_PIC_PATH;
        Date date = new Date();
        fileName = String.valueOf(date.getTime()) + fileName;
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        file = new File(path + "/" + fileName);
        if (!file.exists()) {
            file.createNewFile();
        }
        path = path + "/" + fileName;
        FileOutputStream fos = null;
        InputStream s = null;
        try {
            fos = new FileOutputStream(file);
            s = multipartFile.getInputStream();
            byte[] buffer = new byte[1024];
            int read = 0;
            while ((read = s.read(buffer)) != -1) {
                fos.write(buffer, 0, read);
            }
            fos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                fos.close();
            }
            if (s != null) {
                s.close();
            }
        }
        path = path.replaceAll("\\\\", "/");
        album.setAlbum_coverpath(path);
        }
        */
        if (null != path) {
            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", basePath + "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;
        }
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile multipartFile = multipartRequest.getFile("picture");
        String fileName = multipartFile.getOriginalFilename();
        //?
        String path = null;
        if (null != multipartFile && StringUtils.isNotEmpty(multipartFile.getOriginalFilename())) {
            JSONObject jobj = FileUploadClient.upFile(request, fileName, multipartFile.getInputStream());
            if (jobj.getString("responseMessage").equals("OK")) {
                path = jobj.getString("fpath");
            }
        }
        /*
        if (fileName.trim().length() > 0) {
        int pos = fileName.lastIndexOf(".");
        fileName = fileName.substring(pos);
        String fileDirPath = request.getSession().getServletContext().getRealPath("/");
        String path = fileDirPath.substring(0, fileDirPath.lastIndexOf(File.separator)) + CONFIG.UPLOAD_PIC_PATH;
        Date date = new Date();
        fileName = String.valueOf(date.getTime()) + fileName;
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        file = new File(path + "/" + fileName);
        if (!file.exists()) {
            file.createNewFile();
        }
        path = path + "/" + fileName;
        FileOutputStream fos = null;
        InputStream s = null;
        try {
            fos = new FileOutputStream(file);
            s = multipartFile.getInputStream();
            byte[] buffer = new byte[1024];
            int read = 0;
            while ((read = s.read(buffer)) != -1) {
                fos.write(buffer, 0, read);
            }
            fos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                fos.close();
            }
            if (s != null) {
                s.close();
            }
        }
        path = path.replaceAll("\\\\", "/");
        album.setAlbum_coverpath(path);
        }
        */
        if (null != path) {
            album.setAlbum_coverpath(path);
        }
        //      ????
        Date nowDate = new Date();
        //      ?
        album.setAlbum_updatedate(nowDate);
        album.setAlbum_flag("1");
        //      
        dao.updateAlbum(album);
        //      ????
        ajaxResponse.setSuccess(true);
        ajaxResponse.setSuccessMessage("??");
        //?
        Map map = new HashMap();
        map.put("URL", basePath + "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());
    }

}