GeneraterAction.java 11 KB

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