CmsParserUtil.java 9.8 KB

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