GeneraterAction.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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.base.entity.ResultData;
  23. import net.mingsoft.basic.annotation.LogAnn;
  24. import net.mingsoft.basic.biz.IModelBiz;
  25. import net.mingsoft.basic.constant.e.BusinessTypeEnum;
  26. import net.mingsoft.basic.entity.AppEntity;
  27. import net.mingsoft.basic.util.BasicUtil;
  28. import net.mingsoft.cms.bean.CategoryBean;
  29. import net.mingsoft.cms.bean.ContentBean;
  30. import net.mingsoft.cms.biz.ICategoryBiz;
  31. import net.mingsoft.cms.biz.IContentBiz;
  32. import net.mingsoft.cms.entity.CategoryEntity;
  33. import net.mingsoft.cms.util.CmsParserUtil;
  34. import net.mingsoft.mdiy.bean.PageBean;
  35. import net.mingsoft.mdiy.util.ParserUtil;
  36. import org.apache.commons.lang3.StringUtils;
  37. import org.apache.shiro.authz.annotation.RequiresPermissions;
  38. import org.slf4j.Logger;
  39. import org.slf4j.LoggerFactory;
  40. import org.springframework.beans.factory.annotation.Autowired;
  41. import org.springframework.beans.factory.annotation.Value;
  42. import org.springframework.context.annotation.Scope;
  43. import org.springframework.stereotype.Controller;
  44. import org.springframework.ui.ModelMap;
  45. import org.springframework.web.bind.annotation.PathVariable;
  46. import org.springframework.web.bind.annotation.RequestMapping;
  47. import org.springframework.web.bind.annotation.ResponseBody;
  48. import javax.servlet.http.HttpServletRequest;
  49. import javax.servlet.http.HttpServletResponse;
  50. import java.io.File;
  51. import java.io.IOException;
  52. import java.util.ArrayList;
  53. import java.util.HashMap;
  54. import java.util.List;
  55. import java.util.Map;
  56. /**
  57. *
  58. * @ClassName: GeneraterAction
  59. * @Description:TODO 生成器
  60. * @author: 铭飞开发团队
  61. * @date: 2018年1月31日 下午2:52:07
  62. *
  63. * @Copyright: 2018 www.mingsoft.net Inc. All rights reserved.
  64. */
  65. @Controller("cmsGenerater")
  66. @RequestMapping("/${ms.manager.path}/cms/generate")
  67. @Scope("request")
  68. public class GeneraterAction extends BaseAction {
  69. /*
  70. * log4j日志记录
  71. */
  72. protected final Logger LOG = LoggerFactory.getLogger(this.getClass());
  73. /**
  74. * 文章管理业务层
  75. */
  76. @Autowired
  77. private IContentBiz contentBiz;
  78. /**
  79. * 栏目管理业务层
  80. */
  81. @Autowired
  82. private ICategoryBiz categoryBiz;
  83. /**
  84. * 模块管理业务层
  85. */
  86. @Autowired
  87. private IModelBiz modelBiz;
  88. @Value("${ms.manager.path}")
  89. private String managerPath;
  90. /**
  91. /**
  92. * 更新主页
  93. *
  94. * @return
  95. */
  96. @RequestMapping("/index")
  97. public String index(HttpServletRequest request, ModelMap model) {
  98. return "/cms/generate/index";
  99. }
  100. /**
  101. * 生成主页
  102. *
  103. * @param request
  104. * @param response
  105. */
  106. @RequestMapping("/generateIndex")
  107. @RequiresPermissions("cms:generate:index")
  108. @LogAnn(title = "生成主页", businessType = BusinessTypeEnum.UPDATE)
  109. @ResponseBody
  110. public ResultData generateIndex(HttpServletRequest request, HttpServletResponse response) throws IOException {
  111. // 模版文件名称
  112. String tmpFileName = request.getParameter("url");
  113. // 生成后的文件名称
  114. String generateFileName = request.getParameter("position");
  115. // 获取文件所在路径 首先判断用户输入的模版文件是否存在
  116. if (!FileUtil.exist(ParserUtil.buildTempletPath())) {
  117. return ResultData.build().error(getResString("templet.file"));
  118. } else {
  119. CmsParserUtil.generate(tmpFileName, generateFileName);
  120. return ResultData.build().success();
  121. }
  122. }
  123. /**
  124. * 生成列表的静态页面
  125. *
  126. * @param request
  127. * @param response
  128. * @param categoryId
  129. */
  130. @RequestMapping("/{categoryId}/genernateColumn")
  131. @LogAnn(title = "生成栏目", businessType = BusinessTypeEnum.UPDATE)
  132. @RequiresPermissions("cms:generate:column")
  133. @ResponseBody
  134. public ResultData genernateColumn(HttpServletRequest request, HttpServletResponse response, @PathVariable String categoryId) throws IOException {
  135. // 获取站点id
  136. AppEntity app = BasicUtil.getApp();
  137. List<CategoryEntity> columns = new ArrayList<CategoryEntity>();
  138. // 如果栏目id小于0则更新所有的栏目,否则只更新选中的栏目
  139. if (!"0".equals(categoryId)) {
  140. CategoryEntity categoryEntity = new CategoryEntity();
  141. categoryEntity.setId(categoryId);
  142. columns = categoryBiz.queryChilds(categoryEntity);
  143. } else {
  144. // 获取所有的内容管理栏目
  145. CategoryEntity categoryEntity=new CategoryEntity();
  146. columns = categoryBiz.query(categoryEntity);
  147. }
  148. List<CategoryBean> articleIdList = null;
  149. // 1、设置模板文件夹路径
  150. // 获取栏目列表模版
  151. for (CategoryEntity column : columns) {
  152. ContentBean contentBean = new ContentBean();
  153. contentBean.setCategoryId(column.getId());
  154. // 分类是列表
  155. if(column.getCategoryType().equals("1")) {
  156. // 判断模板文件是否存在
  157. if (!FileUtil.exist(ParserUtil.buildTempletPath(column.getCategoryListUrl()))) {
  158. LOG.error("模板不存在:{}", column.getCategoryUrl());
  159. continue;
  160. }
  161. //获取模板中列表标签中的条件
  162. Map<String, Object> map = new HashMap<>();
  163. if(BasicUtil.getWebsiteApp() != null){
  164. map.put(ParserUtil.APP_ID, BasicUtil.getWebsiteApp().getAppId());
  165. }
  166. PageBean page = new PageBean();
  167. map.put(ParserUtil.HTML, ParserUtil.HTML);
  168. map.put(ParserUtil.URL, BasicUtil.getUrl());
  169. map.put(ParserUtil.PAGE, page);
  170. }
  171. articleIdList = contentBiz.queryIdsByCategoryIdForParser(contentBean);
  172. // 判断列表类型
  173. switch (column.getCategoryType()) {
  174. //TODO 暂时先用字符串代替
  175. case "1": // 列表
  176. CmsParserUtil.generateList(column, articleIdList.size());
  177. break;
  178. case "2":// 单页
  179. if(articleIdList.size()==0){
  180. CategoryBean columnArticleIdBean=new CategoryBean();
  181. CopyOptions copyOptions=CopyOptions.create();
  182. copyOptions.setIgnoreError(true);
  183. BeanUtil.copyProperties(column,columnArticleIdBean,copyOptions);
  184. articleIdList.add(columnArticleIdBean);
  185. }
  186. CmsParserUtil.generateBasic(articleIdList);
  187. break;
  188. }
  189. }
  190. return ResultData.build().success();
  191. }
  192. /**
  193. * 根据栏目id更新所有的文章
  194. *
  195. * @param request
  196. * @param response
  197. * @param columnId
  198. */
  199. @RequestMapping("/{columnId}/generateArticle")
  200. @RequiresPermissions("cms:generate:article")
  201. @LogAnn(title = "生成文章", businessType = BusinessTypeEnum.UPDATE)
  202. @ResponseBody
  203. public ResultData generateArticle(HttpServletRequest request, HttpServletResponse response, @PathVariable String columnId) throws IOException {
  204. String dateTime = request.getParameter("dateTime");
  205. // 网站风格物理路径
  206. List<CategoryBean> articleIdList = null;
  207. List<CategoryEntity> categoryList = null;
  208. ContentBean contentBean = new ContentBean();
  209. contentBean.setBeginTime(dateTime);
  210. Map<String, Object> map = new HashMap<>();
  211. if(BasicUtil.getWebsiteApp() != null){
  212. map.put(ParserUtil.APP_ID, BasicUtil.getWebsiteApp().getAppId());
  213. }
  214. PageBean page = new PageBean();
  215. map.put(ParserUtil.HTML, ParserUtil.HTML);
  216. map.put(ParserUtil.URL, BasicUtil.getUrl());
  217. map.put(ParserUtil.PAGE, page);
  218. // 生成所有栏目的文章
  219. if("0".equals(columnId)){
  220. CategoryEntity categoryEntity = new CategoryEntity();
  221. categoryList = categoryBiz.query(categoryEntity);
  222. for(CategoryEntity category : categoryList){
  223. contentBean.setCategoryId(category.getId());
  224. // 分类是列表
  225. if(category.getCategoryType().equals("1")){
  226. // 判断模板文件是否存在
  227. if (!FileUtil.exist(ParserUtil.buildTempletPath(category.getCategoryListUrl())) || StringUtils.isEmpty(category.getCategoryListUrl())) {
  228. LOG.error("模板不存在:{}",category.getCategoryUrl());
  229. continue;
  230. }
  231. }
  232. //将文章列表标签中的中的参数
  233. articleIdList = contentBiz.queryIdsByCategoryIdForParser(contentBean);
  234. // 有符合条件的就更新
  235. if (articleIdList.size() > 0) {
  236. CmsParserUtil.generateBasic(articleIdList);
  237. }
  238. }
  239. }else {
  240. CategoryEntity category = (CategoryEntity) categoryBiz.getById(columnId);
  241. contentBean.setCategoryId(columnId);
  242. // 分类是列表
  243. if(category.getCategoryType().equals("1")){
  244. // 获取文章列表表属性
  245. // 判断模板文件是否存在
  246. if (!FileUtil.exist(ParserUtil.buildTempletPath(category.getCategoryUrl()))) {
  247. LOG.error("模板不存在:{}",category.getCategoryUrl());
  248. return ResultData.build().error(getResString("templet.file"));
  249. }
  250. }
  251. articleIdList = contentBiz.queryIdsByCategoryIdForParser(contentBean);
  252. // 有符合条件的就更新
  253. if (articleIdList.size() > 0) {
  254. CmsParserUtil.generateBasic(articleIdList);
  255. }
  256. }
  257. return ResultData.build().success();
  258. }
  259. /**
  260. * 用户预览主页
  261. *
  262. * @param request
  263. * @return
  264. */
  265. @RequestMapping("/{position}/viewIndex")
  266. public String viewIndex(HttpServletRequest request, @PathVariable String position, HttpServletResponse response) {
  267. AppEntity app = BasicUtil.getApp();
  268. // 组织主页预览地址
  269. String indexPosition = app.getAppHostUrl() + File.separator + ParserUtil.HTML + File.separator + app.getAppId()
  270. + File.separator + position + ParserUtil.HTML_SUFFIX;
  271. return "redirect:" + indexPosition;
  272. }
  273. }