GeneraterAction.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. The MIT License (MIT) * Copyright (c) 2016 铭飞科技(mingsoft.net)
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  4. * this software and associated documentation files (the "Software"), to deal in
  5. * the Software without restriction, including without limitation the rights to
  6. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  7. * the Software, and to permit persons to whom the Software is furnished to do so,
  8. * subject to the following conditions:
  9. * The above copyright notice and this permission notice shall be included in all
  10. * copies or substantial portions of the Software.
  11. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  13. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  14. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  15. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. */
  18. package net.mingsoft.cms.action;
  19. import cn.hutool.core.bean.BeanUtil;
  20. import cn.hutool.core.bean.copier.CopyOptions;
  21. import cn.hutool.core.io.FileUtil;
  22. import net.mingsoft.basic.biz.IModelBiz;
  23. import net.mingsoft.basic.entity.AppEntity;
  24. import net.mingsoft.basic.util.BasicUtil;
  25. import net.mingsoft.cms.bean.CategoryBean;
  26. import net.mingsoft.cms.biz.ICategoryBiz;
  27. import net.mingsoft.cms.biz.IContentBiz;
  28. import net.mingsoft.cms.entity.CategoryEntity;
  29. import net.mingsoft.cms.util.CmsParserUtil;
  30. import net.mingsoft.mdiy.util.ParserUtil;
  31. import org.apache.shiro.authz.annotation.RequiresPermissions;
  32. import org.slf4j.Logger;
  33. import org.slf4j.LoggerFactory;
  34. import org.springframework.beans.factory.annotation.Autowired;
  35. import org.springframework.beans.factory.annotation.Value;
  36. import org.springframework.context.annotation.Scope;
  37. import org.springframework.stereotype.Controller;
  38. import org.springframework.ui.ModelMap;
  39. import org.springframework.web.bind.annotation.PathVariable;
  40. import org.springframework.web.bind.annotation.RequestMapping;
  41. import org.springframework.web.bind.annotation.ResponseBody;
  42. import javax.servlet.http.HttpServletRequest;
  43. import javax.servlet.http.HttpServletResponse;
  44. import java.io.File;
  45. import java.io.IOException;
  46. import java.util.ArrayList;
  47. import java.util.List;
  48. /**
  49. *
  50. * @ClassName: GeneraterAction
  51. * @Description:TODO 生成器
  52. * @author: 铭飞开发团队
  53. * @date: 2018年1月31日 下午2:52:07
  54. *
  55. * @Copyright: 2018 www.mingsoft.net Inc. All rights reserved.
  56. */
  57. @Controller("cmsGenerater")
  58. @RequestMapping("/${ms.manager.path}/cms/generate")
  59. @Scope("request")
  60. public class GeneraterAction extends BaseAction {
  61. /*
  62. * log4j日志记录
  63. */
  64. protected final Logger LOG = LoggerFactory.getLogger(this.getClass());
  65. /**
  66. * 文章管理业务层
  67. */
  68. @Autowired
  69. private IContentBiz contentBiz;
  70. /**
  71. * 栏目管理业务层
  72. */
  73. @Autowired
  74. private ICategoryBiz categoryBiz;
  75. /**
  76. * 模块管理业务层
  77. */
  78. @Autowired
  79. private IModelBiz modelBiz;
  80. @Value("${ms.manager.path}")
  81. private String managerPath;
  82. /**
  83. /**
  84. * 更新主页
  85. *
  86. * @return
  87. */
  88. @RequestMapping("/index")
  89. public String index(HttpServletRequest request, ModelMap model) {
  90. return "/cms/generate/index";
  91. }
  92. /**
  93. * 生成主页
  94. *
  95. * @param request
  96. * @param response
  97. */
  98. @RequestMapping("/generateIndex")
  99. @RequiresPermissions("cms:generate:index")
  100. @ResponseBody
  101. public void generateIndex(HttpServletRequest request, HttpServletResponse response) {
  102. // 模版文件名称
  103. String tmpFileName = request.getParameter("url");
  104. // 生成后的文件名称
  105. String generateFileName = request.getParameter("position");
  106. // 获取文件所在路径 首先判断用户输入的模版文件是否存在
  107. if (!FileUtil.exist(ParserUtil.buildTempletPath())) {
  108. this.outJson(response, false, getResString("templet.file"));
  109. } else {
  110. try {
  111. CmsParserUtil.generate(tmpFileName, generateFileName);
  112. this.outJson(response, true);
  113. } catch (IOException e) {
  114. e.printStackTrace();
  115. this.outJson(response, false);
  116. }
  117. }
  118. }
  119. /**
  120. * 生成列表的静态页面
  121. *
  122. * @param request
  123. * @param response
  124. * @param CategoryId
  125. */
  126. @RequestMapping("/{CategoryId}/genernateColumn")
  127. @RequiresPermissions("cms:generate:column")
  128. @ResponseBody
  129. public void genernateColumn(HttpServletRequest request, HttpServletResponse response, @PathVariable int CategoryId) {
  130. // 获取站点id
  131. AppEntity app = BasicUtil.getApp();
  132. List<CategoryEntity> columns = new ArrayList<CategoryEntity>();
  133. // 如果栏目id小于0则更新所有的栏目,否则只更新选中的栏目
  134. if (CategoryId>0) {
  135. CategoryEntity categoryEntity = new CategoryEntity();
  136. categoryEntity.setId(CategoryId+"");
  137. categoryEntity.setAppId(app.getAppId());
  138. columns = categoryBiz.queryChilds(categoryEntity);
  139. } else {
  140. // 获取所有的内容管理栏目
  141. CategoryEntity categoryEntity=new CategoryEntity();
  142. categoryEntity.setAppId(app.getAppId());
  143. columns = categoryBiz.query(categoryEntity);
  144. }
  145. List<CategoryBean> articleIdList = null;
  146. try {
  147. // 1、设置模板文件夹路径
  148. // 获取栏目列表模版
  149. for (CategoryEntity column : columns) {
  150. // 判断模板文件是否存在
  151. if (!FileUtil.exist(ParserUtil.buildTempletPath(column.getCategoryUrl()))) {
  152. LOG.error("模板不存在:{}",column.getCategoryUrl());
  153. continue;
  154. }
  155. articleIdList = contentBiz.queryIdsByCategoryIdForParser(column.getId(), null, null);
  156. // 判断列表类型
  157. switch (column.getCategoryType()) {
  158. //TODO 暂时先用字符串代替
  159. case "1": // 列表
  160. CmsParserUtil.generateList(column, articleIdList.size());
  161. break;
  162. case "2":// 单页
  163. if(articleIdList.size()==0){
  164. CategoryBean columnArticleIdBean=new CategoryBean();
  165. CopyOptions copyOptions=CopyOptions.create();
  166. copyOptions.setIgnoreError(true);
  167. BeanUtil.copyProperties(column,columnArticleIdBean,copyOptions);
  168. articleIdList.add(columnArticleIdBean);
  169. }
  170. CmsParserUtil.generateBasic(articleIdList);
  171. break;
  172. }
  173. }
  174. } catch (IOException e) {
  175. e.printStackTrace();
  176. this.outJson(response, false);
  177. }
  178. this.outJson(response, true);
  179. }
  180. /**
  181. * 根据栏目id更新所有的文章
  182. *
  183. * @param request
  184. * @param response
  185. * @param columnId
  186. */
  187. @RequestMapping("/{columnId}/generateArticle")
  188. @RequiresPermissions("cms:generate:article")
  189. @ResponseBody
  190. public void generateArticle(HttpServletRequest request, HttpServletResponse response, @PathVariable String columnId) {
  191. String dateTime = request.getParameter("dateTime");
  192. // 网站风格物理路径
  193. List<CategoryBean> articleIdList = null;
  194. // 查出所有文章(根据选择栏目)包括子栏目
  195. articleIdList = contentBiz.queryIdsByCategoryIdForParser(columnId, dateTime, null);
  196. // 有符合条件的新闻就更新
  197. if (articleIdList.size() > 0) {
  198. CmsParserUtil.generateBasic(articleIdList);
  199. }
  200. this.outJson(response, true);
  201. }
  202. /**
  203. * 用户预览主页
  204. *
  205. * @param request
  206. * @return
  207. */
  208. @RequestMapping("/{position}/viewIndex")
  209. public String viewIndex(HttpServletRequest request, @PathVariable String position, HttpServletResponse response) {
  210. AppEntity app = BasicUtil.getApp();
  211. // 组织主页预览地址
  212. String indexPosition = app.getAppHostUrl() + File.separator + ParserUtil.HTML + File.separator + app.getAppId()
  213. + File.separator + position + ParserUtil.HTML_SUFFIX;
  214. return "redirect:" + indexPosition;
  215. }
  216. }