Java tutorial
/* * 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.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.AttributeUtils; 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.channel.dao.ChannelDao; import com.dlshouwen.wzgl.channel.model.Channel; import com.dlshouwen.wzgl.content.dao.ArticleDao; import com.dlshouwen.wzgl.content.model.Article; import com.dlshouwen.wzgl.picture.model.Picture; 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; import org.springframework.web.servlet.ModelAndView; /** * * * @author cui */ @Controller @RequestMapping("/wzgl/article/article") public class ArticleController { /** * */ private String basePath = "wzgl/article/"; /** * ?? */ private ArticleDao dao; /** * ?? */ private AlbumDao albumDao; /** * ?? * * @param dao ?? */ @Resource(name = "articleDao") public void setDao(ArticleDao dao) { this.dao = dao; } /** * ?? */ @Resource(name = "ChannelDao") private ChannelDao channelDao; public void setChannelDao(ChannelDao channelDao) { this.channelDao = channelDao; } /** * ?? * * @param dao ?? */ @Resource(name = "albumDao") public void setAlbumDao(AlbumDao albumDao) { this.albumDao = albumDao; } /** * ?? * * @param request * @return basePath + 'articleList' * @throws Exception */ @RequestMapping(value = "", method = RequestMethod.GET) public String goArticlePage(HttpServletRequest request) throws Exception { //? 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); //// ? // LogUtils.updateOperationLog(request, OperationType.VISIT, "??"); return basePath + "articleList"; } /** * ?? * * @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 { // Pager Pager pager = PagerPropertyUtils.copy(JSONObject.fromObject(gridPager)); // ?? dao.getArticleList(pager, request, response); // ? LogUtils.updateOperationLog(request, OperationType.SEARCH, "????"); } /** * ? * * @param model ?? * @param request * @return basePath + 'addArticle' * @throws Exception */ @RequestMapping(value = "/add", method = RequestMethod.GET) public String addArticle(Model model, HttpServletRequest request) throws Exception { // Article article = new Article(); // ?? SessionUser sessionUser = (SessionUser) request.getSession().getAttribute(CONFIG.SESSION_USER); String publisher = sessionUser == null ? "" : sessionUser.getUser_name(); article.setArticle_id(new GUID().toString()); // ? // ? article.setStatus("1"); // ? article.setTopset("0"); // ??? article.setPublisher(publisher); // ? article.setPublish_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; } // ???? 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); // dao.insertArticle(article); // ??? Album album = new Album(); album.setAlbum_id(article.getArticle_id()); album.setAlbum_name(article.getTitle()); album.setAlbum_createuserbyid(userId); album.setAlbum_flag("1");// album.setAlbum_description(""); album.setAlbum_createuser(sessionUser.getUser_name()); album.setAlbum_createdate(nowDate); album.setAlbum_updatedate(nowDate); // 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 editArticle(@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); // dao.updateArticle(article); // ???? ajaxResponse.setSuccess(true); ajaxResponse.setSuccessMessage("??"); //URL?? Map map = new HashMap(); map.put("URL", "wzgl/article/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 editArtAlubm(@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 articleId ? * @param model ?? * @param request * @return base + 'editAnnouncement' * @throws Exception */ @RequestMapping(value = "/{articleId}/check", method = RequestMethod.GET) public ModelAndView checkArticle(@PathVariable String articleId, HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView view = new ModelAndView("/wzgl/content/articleDetail.btl"); String sourcePath = AttributeUtils.getAttributeContent(request.getServletContext(), "source_webapp_file_postion"); // ?? Article article = dao.getArticleById(articleId); String channel_id = article.getChannel_id(); Channel channelNow = channelDao.getChannelById(channel_id); List<Map<String, Object>> secChannelList = null; if (channelNow.getChannel_parent().equals("top")) { secChannelList = channelDao.getSecChannelList(channelNow.getChannel_id()); view.addObject("topChannelNow", channelNow); view.addObject("channelNow", null); view.addObject("secChannelList", secChannelList); } else { Channel channelTopNow = channelDao.getChannelById(channelNow.getChannel_parent()); secChannelList = channelDao.getSecChannelList(channelTopNow.getChannel_id()); view.addObject("topChannelNow", channelTopNow); view.addObject("channelNow", channelNow); view.addObject("secChannelList", secChannelList); } List<Map<String, Object>> allChannel = channelDao.getFirstChannel(); List<Picture> pics = albumDao.findPicByAlbumId(articleId); if (null == pics || pics.size() == 0) { pics = null; } view.addObject("allChannel", allChannel); view.addObject("article", article); view.addObject("pictureList", pics); view.addObject("SOURCEPATH", sourcePath); // ? LogUtils.updateOperationLog(request, OperationType.VISIT, "?"); return view; } /** * * * @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; } 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, multipartFile.getOriginalFilename(), 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"); // albumDao.updateAlbum(album); // ???? ajaxResponse.setSuccess(true); ajaxResponse.setSuccessMessage("??"); //URL?? Map map = new HashMap(); map.put("URL", "wzgl/article/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()); } }