CmsParserUtil.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package net.mingsoft.cms.util;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.io.FileUtil;
  4. import cn.hutool.core.util.PageUtil;
  5. import freemarker.core.ParseException;
  6. import freemarker.template.MalformedTemplateNameException;
  7. import freemarker.template.TemplateNotFoundException;
  8. import net.mingsoft.base.constant.Const;
  9. import net.mingsoft.basic.util.BasicUtil;
  10. import net.mingsoft.basic.util.SpringUtil;
  11. import net.mingsoft.cms.bean.CategoryBean;
  12. import net.mingsoft.cms.entity.CategoryEntity;
  13. import net.mingsoft.mdiy.bean.PageBean;
  14. import net.mingsoft.mdiy.biz.IModelBiz;
  15. import net.mingsoft.mdiy.biz.impl.ModelBizImpl;
  16. import net.mingsoft.mdiy.entity.ModelEntity;
  17. import net.mingsoft.mdiy.util.ParserUtil;
  18. import org.apache.commons.lang3.StringUtils;
  19. import javax.servlet.http.HttpServletRequest;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.util.ArrayList;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.concurrent.ExecutorService;
  27. /**
  28. * 文章解析工具类
  29. */
  30. public class CmsParserUtil extends ParserUtil {
  31. /**
  32. * 封面
  33. */
  34. private static int COLUMN_TYPE_COVER = 2;
  35. private final static String FIELD="field";
  36. /**
  37. * 指定模板,指定路径进行生成静态页面,会自定识别pc与移动端
  38. *
  39. * @param templatePath
  40. * 模板路径
  41. * @param targetPath
  42. * 生成后的路径,默认生成的html文件,所以不能带.html后缀,
  43. * @throws IOException
  44. */
  45. public static void generate(String templatePath, String targetPath) throws IOException {
  46. Map<String, Object> map = new HashMap<String, Object>();
  47. map.put(IS_DO, false);
  48. CategoryEntity column = new CategoryEntity();
  49. //内容管理栏目编码
  50. map.put(COLUMN, column);
  51. String content = CmsParserUtil.generate(templatePath, map);
  52. FileUtil.writeString(content, ParserUtil.buildHtmlPath(targetPath), Const.UTF8);
  53. }
  54. /**
  55. * 生成静态列表页
  56. * @param column
  57. * @param articleIdTotal
  58. * @throws TemplateNotFoundException
  59. * @throws MalformedTemplateNameException
  60. * @throws ParseException
  61. * @throws IOException
  62. */
  63. public static void generateList(CategoryEntity column, int articleIdTotal)
  64. throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException {
  65. try{
  66. // 文章的栏目模型编号
  67. PageBean page = new PageBean();
  68. //获取列表中的size
  69. page.setSize(ParserUtil.getPageSize(column.getCategoryListUrl(),20));
  70. page.setRcount(articleIdTotal);
  71. int totalPageSize = PageUtil.totalPage(articleIdTotal, page.getSize());
  72. page.setTotal(totalPageSize);
  73. //获取模板中列表标签中的条件
  74. Map<String, Object> map = new HashMap<>();
  75. if (BasicUtil.getWebsiteApp() != null) {
  76. map.put(ParserUtil.APP_ID, BasicUtil.getWebsiteApp().getAppId());
  77. }
  78. map.put(ParserUtil.HTML, ParserUtil.HTML);
  79. map.put(ParserUtil.URL, BasicUtil.getUrl());
  80. map.put(ParserUtil.PAGE, page);
  81. String columnListPath;
  82. ModelEntity contentModel = null;
  83. // 判断当前栏目是否有自定义模型
  84. if (column.getMdiyModelId() != null) {
  85. // 通过栏目模型编号获取自定义模型实体
  86. contentModel = (ModelEntity) SpringUtil.getBean(ModelBizImpl.class).getEntity(column.getMdiyModelId());
  87. }
  88. //全局参数设置
  89. Map<String, Object> parserParams = new HashMap<String, Object>();
  90. parserParams.put(ParserUtil.PAGE, page);
  91. parserParams.put(COLUMN, column);
  92. //标签中使用field获取当前栏目
  93. parserParams.put(FIELD, column);
  94. parserParams.put(IS_DO, false);
  95. parserParams.put(HTML, HTML);
  96. if(BasicUtil.getWebsiteApp() != null){
  97. parserParams.put(APP_ID, BasicUtil.getWebsiteApp().getAppId());
  98. }
  99. if (contentModel!=null) {
  100. // 将自定义模型编号设置为key值
  101. parserParams.put(TABLE_NAME, contentModel.getModelTableName());
  102. }
  103. //如果单站点,就废弃站点地址
  104. if(ParserUtil.IS_SINGLE) {
  105. parserParams.put(ParserUtil.URL, BasicUtil.getUrl());
  106. }
  107. int pageNo = 1;
  108. //文章列表页没有写文章列表标签,总数为0
  109. if (totalPageSize <= 0) {
  110. // 数据库中第一页是从开始0*size
  111. columnListPath = ParserUtil.buildHtmlPath(column.getCategoryPath() + File.separator + ParserUtil.INDEX);
  112. // 设置分页的起始位置
  113. page.setPageNo(pageNo);
  114. String read = ParserUtil.rendering(File.separator + column.getCategoryListUrl(), parserParams);
  115. FileUtil.writeString(read, columnListPath, Const.UTF8);
  116. } else {
  117. // 遍历分页
  118. for (int i = 0; i < totalPageSize; i++) {
  119. if (i == 0) {
  120. // 数据库中第一页是从开始0*size
  121. // 首页路径index.html
  122. columnListPath = ParserUtil
  123. .buildHtmlPath(column.getCategoryPath() + File.separator + ParserUtil.INDEX);
  124. } else {
  125. // 其他路径list-2.html
  126. columnListPath = ParserUtil
  127. .buildHtmlPath(column.getCategoryPath() + File.separator + ParserUtil.PAGE_LIST + pageNo);
  128. }
  129. // 设置分页的起始位置
  130. page.setPageNo(pageNo);
  131. String read = ParserUtil.rendering(File.separator + column.getCategoryListUrl(), parserParams);
  132. FileUtil.writeString(read, columnListPath, Const.UTF8);
  133. pageNo++;
  134. }
  135. }
  136. }catch (Exception e){
  137. e.printStackTrace();
  138. }
  139. }
  140. /**
  141. * 生成内容
  142. *
  143. * @param articleIdList
  144. * 文章集合
  145. * @return
  146. * @throws IOException
  147. * @throws ParseException
  148. * @throws MalformedTemplateNameException
  149. * @throws TemplateNotFoundException
  150. */
  151. public static void generateBasic(List<CategoryBean> articleIdList) {
  152. Map<Object, Object> contentModelMap = new HashMap<Object, Object>();
  153. ModelEntity contentModel = null;
  154. // 记录已经生成了文章编号
  155. List<String> generateIds = new ArrayList<>();
  156. ExecutorService pool= SpringUtil.getBean(ExecutorService.class);
  157. // 生成文章
  158. for (int artId = 0; artId < articleIdList.size();) {
  159. String writePath = null;
  160. //设置分页类
  161. PageBean page = new PageBean();
  162. // 文章编号
  163. String articleId = articleIdList.get(artId).getArticleId();
  164. // 文章的栏目路径
  165. String articleColumnPath = articleIdList.get(artId).getCategoryPath();
  166. // 该文章相关分类
  167. String categoryParentId = articleIdList.get(artId).getId() ;
  168. if(StringUtils.isNotBlank(articleIdList.get(artId).getCategoryParentId())){
  169. categoryParentId += ','+articleIdList.get(artId).getCategoryParentId();
  170. }
  171. // 文章的模板路径
  172. String columnUrl = articleIdList.get(artId).getCategoryUrl();
  173. // 文章的栏目模型编号
  174. Integer columnContentModelId = null;
  175. if(articleIdList.get(artId).getMdiyModelId() != null && articleIdList.get(artId).getMdiyModelId() > 0){
  176. columnContentModelId = articleIdList.get(artId).getMdiyModelId();
  177. }
  178. // 文章是否已经生成了,生成了就跳过
  179. if (generateIds.contains(articleId)) {
  180. artId++;
  181. continue;
  182. }
  183. // 判断文件是否存在,若不存在弹出返回信息
  184. if (!FileUtil.exist(ParserUtil.buildTempletPath(columnUrl))||articleIdList.get(artId).getId()==null||articleIdList.get(artId).getCategoryType()==null) {
  185. artId++;
  186. continue;
  187. }
  188. // 将
  189. generateIds.add(articleId);
  190. //如果是封面就生成index.html
  191. if(Integer.parseInt(articleIdList.get(artId).getCategoryType()) == COLUMN_TYPE_COVER) {
  192. writePath = ParserUtil.buildHtmlPath(articleColumnPath + File.separator + ParserUtil.INDEX);
  193. } else {
  194. // 组合文章路径如:html/站点id/栏目id/文章id.html
  195. writePath = ParserUtil.buildHtmlPath(articleColumnPath + File.separator + articleId);
  196. }
  197. Map<String, Object> parserParams = new HashMap<String, Object>();
  198. parserParams.put(ParserUtil.COLUMN, articleIdList.get(artId));
  199. // 判断当前栏目是否有自定义模型
  200. if (columnContentModelId != null) {
  201. // 通过当前栏目的模型编号获取,自定义模型表名
  202. if (contentModelMap.containsKey(columnContentModelId)) {
  203. parserParams.put(TABLE_NAME, contentModel.getModelTableName());
  204. } else {
  205. // 通过栏目模型编号获取自定义模型实体
  206. contentModel = (ModelEntity) SpringUtil.getBean(IModelBiz.class)
  207. .getEntity(columnContentModelId);
  208. // 将自定义模型编号设置为key值
  209. contentModelMap.put(columnContentModelId, contentModel.getModelTableName());
  210. parserParams.put(TABLE_NAME, contentModel.getModelTableName());
  211. }
  212. }
  213. parserParams.put(ID, articleId);
  214. // 第一篇文章没有上一篇
  215. if (artId > 0) {
  216. CategoryBean preCaBean = articleIdList.get(artId - 1);
  217. //判断当前文档是否与上一页文章在同一栏目下,并且不能使用父栏目字符串,因为父栏目中没有所属栏目编号
  218. // if( categoryParentId.contains(preCaBean.getCategoryId()+"")){
  219. page.setPreId(preCaBean.getArticleId());
  220. // }
  221. }
  222. // 最后一篇文章没有下一篇
  223. if (artId + 1 < articleIdList.size()) {
  224. CategoryBean nextCaBean = articleIdList.get(artId + 1);
  225. //判断当前文档是否与下一页文章在同一栏目下并且不能使用父栏目字符串,因为父栏目中没有所属栏目编号
  226. // if(categoryParentId.contains(nextCaBean.getCategoryId()+"")){
  227. page.setNextId(nextCaBean.getArticleId());
  228. // }
  229. }
  230. parserParams.put(IS_DO, false);
  231. parserParams.put(ParserUtil.PAGE, page);
  232. String finalWritePath = writePath;
  233. HashMap<Object, Object> cloneMap = CollUtil.newHashMap();
  234. cloneMap.putAll(parserParams);
  235. HttpServletRequest request = SpringUtil.getRequest();
  236. pool.execute(() -> {
  237. String content = null;
  238. try {
  239. SpringUtil.setRequest(request);
  240. content = CmsParserUtil.generate(columnUrl, cloneMap);
  241. FileUtil.writeString(content, finalWritePath, Const.UTF8);
  242. } catch (IOException e) {
  243. e.printStackTrace();
  244. }
  245. });
  246. artId++;
  247. }
  248. }
  249. }