GeneraterAction.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.ContentBean;
  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.springframework.beans.factory.annotation.Autowired;
  33. import org.springframework.beans.factory.annotation.Value;
  34. import org.springframework.context.annotation.Scope;
  35. import org.springframework.stereotype.Controller;
  36. import org.springframework.ui.ModelMap;
  37. import org.springframework.web.bind.annotation.PathVariable;
  38. import org.springframework.web.bind.annotation.RequestMapping;
  39. import org.springframework.web.bind.annotation.ResponseBody;
  40. import javax.servlet.http.HttpServletRequest;
  41. import javax.servlet.http.HttpServletResponse;
  42. import java.io.File;
  43. import java.io.IOException;
  44. import java.util.ArrayList;
  45. import java.util.List;
  46. /**
  47. *
  48. * @ClassName: GeneraterAction
  49. * @Description:TODO 生成器
  50. * @author: 铭飞开发团队
  51. * @date: 2018年1月31日 下午2:52:07
  52. *
  53. * @Copyright: 2018 www.mingsoft.net Inc. All rights reserved.
  54. */
  55. @Controller("cmsGenerater")
  56. @RequestMapping("/${ms.manager.path}/cms/generate")
  57. @Scope("request")
  58. public class GeneraterAction extends BaseAction {
  59. /**
  60. * 文章管理业务层
  61. */
  62. @Autowired
  63. private IContentBiz contentBiz;
  64. /**
  65. * 栏目管理业务层
  66. */
  67. @Autowired
  68. private ICategoryBiz categoryBiz;
  69. /**
  70. * 模块管理业务层
  71. */
  72. @Autowired
  73. private IModelBiz modelBiz;
  74. @Value("${ms.manager.path}")
  75. private String managerPath;
  76. /**
  77. /**
  78. * 更新主页
  79. *
  80. * @return
  81. */
  82. @RequestMapping("/index")
  83. public String index(HttpServletRequest request, ModelMap model) {
  84. return "/cms/generate/index";
  85. }
  86. /**
  87. * 生成主页
  88. *
  89. * @param request
  90. * @param response
  91. */
  92. @RequestMapping("/generateIndex")
  93. @RequiresPermissions("cms:generate:index")
  94. @ResponseBody
  95. public void generateIndex(HttpServletRequest request, HttpServletResponse response) {
  96. // 模版文件名称
  97. String tmpFileName = request.getParameter("url");
  98. // 生成后的文件名称
  99. String generateFileName = request.getParameter("position");
  100. // 获取文件所在路径 首先判断用户输入的模版文件是否存在
  101. if (!FileUtil.exist(ParserUtil.buildTempletPath())) {
  102. this.outJson(response, false, getResString("templet.file"));
  103. } else {
  104. try {
  105. CmsParserUtil.generate(tmpFileName, generateFileName);
  106. this.outJson(response, true);
  107. } catch (IOException e) {
  108. e.printStackTrace();
  109. this.outJson(response, false);
  110. }
  111. }
  112. }
  113. /**
  114. * 生成列表的静态页面
  115. *
  116. * @param request
  117. * @param response
  118. * @param CategoryId
  119. */
  120. @RequestMapping("/{CategoryId}/genernateColumn")
  121. @RequiresPermissions("cms:generate:column")
  122. @ResponseBody
  123. public void genernateColumn(HttpServletRequest request, HttpServletResponse response, @PathVariable int CategoryId) {
  124. // 获取站点id
  125. AppEntity app = BasicUtil.getApp();
  126. List<CategoryEntity> columns = new ArrayList<CategoryEntity>();
  127. // 如果栏目id小于0则更新所有的栏目,否则只更新选中的栏目
  128. if (CategoryId>0) {
  129. CategoryEntity categoryEntity = new CategoryEntity();
  130. categoryEntity.setId(CategoryId+"");
  131. categoryEntity.setAppId(app.getAppId());
  132. columns = categoryBiz.queryChilds(categoryEntity);
  133. } else {
  134. // 获取所有的内容管理栏目
  135. CategoryEntity categoryEntity=new CategoryEntity();
  136. categoryEntity.setAppId(app.getAppId());
  137. columns = categoryBiz.query(categoryEntity);
  138. }
  139. List<ContentBean> articleIdList = null;
  140. try {
  141. // 1、设置模板文件夹路径
  142. // 获取栏目列表模版
  143. for (CategoryEntity column : columns) {
  144. // 判断模板文件是否存在
  145. if (!FileUtil.exist(ParserUtil.buildTempletPath(column.getCategoryUrl()))) {
  146. continue;
  147. }
  148. articleIdList = contentBiz.queryIdsByCategoryIdForParser(column.getId(), null, null);
  149. // 判断列表类型
  150. switch (column.getCategoryType()) {
  151. //TODO 暂时先用字符串代替
  152. case "1": // 列表
  153. CmsParserUtil.generateList(column, articleIdList.size());
  154. break;
  155. case "2":// 单页
  156. if(articleIdList.size()==0){
  157. ContentBean columnArticleIdBean=new ContentBean();
  158. CopyOptions copyOptions=CopyOptions.create();
  159. copyOptions.setIgnoreError(true);
  160. BeanUtil.copyProperties(column,columnArticleIdBean,copyOptions);
  161. articleIdList.add(columnArticleIdBean);
  162. }
  163. CmsParserUtil.generateBasic(articleIdList);
  164. break;
  165. }
  166. }
  167. } catch (IOException e) {
  168. e.printStackTrace();
  169. this.outJson(response, false);
  170. }
  171. this.outJson(response, true);
  172. }
  173. /**
  174. * 根据栏目id更新所有的文章
  175. *
  176. * @param request
  177. * @param response
  178. * @param columnId
  179. */
  180. @RequestMapping("/{columnId}/generateArticle")
  181. @RequiresPermissions("cms:generate:article")
  182. @ResponseBody
  183. public void generateArticle(HttpServletRequest request, HttpServletResponse response, @PathVariable String columnId) {
  184. String dateTime = request.getParameter("dateTime");
  185. // 网站风格物理路径
  186. List<ContentBean> articleIdList = null;
  187. try {
  188. // 查出所有文章(根据选择栏目)包括子栏目
  189. articleIdList = contentBiz.queryIdsByCategoryIdForParser(columnId, dateTime, null);
  190. // 有符合条件的新闻就更新
  191. if (articleIdList.size() > 0) {
  192. CmsParserUtil.generateBasic(articleIdList);
  193. }
  194. this.outJson(response, true);
  195. } catch (IOException e) {
  196. e.printStackTrace();
  197. this.outJson(response, false);
  198. }
  199. }
  200. /**
  201. * 用户预览主页
  202. *
  203. * @param request
  204. * @return
  205. */
  206. @RequestMapping("/{position}/viewIndex")
  207. public String viewIndex(HttpServletRequest request, @PathVariable String position, HttpServletResponse response) {
  208. AppEntity app = BasicUtil.getApp();
  209. // 组织主页预览地址
  210. String indexPosition = app.getAppHostUrl() + File.separator + ParserUtil.HTML + File.separator + app.getAppId()
  211. + File.separator + position + ParserUtil.HTML_SUFFIX;
  212. return "redirect:" + indexPosition;
  213. }
  214. }