GeneraterAction.java 7.4 KB

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