CmsParserUtil.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. Integer columnContentModelId = column.getMdiyModelId();
  68. PageBean page = new PageBean();
  69. page.setSize(10);
  70. //获取分页数量
  71. //获取列表页显示的文章数量
  72. //获取总数
  73. String columnListPath;
  74. ModelEntity contentModel = null;
  75. // 判断当前栏目是否有自定义模型
  76. if (columnContentModelId != null) {
  77. // 通过栏目模型编号获取自定义模型实体
  78. contentModel = (ModelEntity) SpringUtil.getBean(ModelBizImpl.class).getEntity(columnContentModelId);
  79. }
  80. int pageNo = 1;
  81. //全局参数设置
  82. Map<String, Object> parserParams = new HashMap<String, Object>();
  83. parserParams.put(COLUMN, column);
  84. //标签中使用field获取当前栏目
  85. parserParams.put(FIELD, column);
  86. parserParams.put(IS_DO, false);
  87. parserParams.put(HTML, HTML);
  88. if(BasicUtil.getWebsiteApp() != null){
  89. parserParams.put(APP_ID, BasicUtil.getWebsiteApp().getAppId());
  90. }
  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. ParserUtil.read(File.separator + column.getCategoryListUrl(),parserParams, page);
  101. int totalPageSize = PageUtil.totalPage(articleIdTotal, page.getSize());
  102. page.setTotal(totalPageSize);
  103. //文章列表页没有写文章列表标签,总数为0
  104. if (totalPageSize <= 0) {
  105. // 数据库中第一页是从开始0*size
  106. columnListPath = ParserUtil.buildHtmlPath(column.getCategoryPath() + File.separator + ParserUtil.INDEX);
  107. // 设置分页的起始位置
  108. page.setPageNo(pageNo);
  109. String read = ParserUtil.read(File.separator + column.getCategoryListUrl(), parserParams);
  110. FileUtil.writeString(read, columnListPath, Const.UTF8);
  111. } else {
  112. // 遍历分页
  113. for (int i = 0; i < totalPageSize; i++) {
  114. if (i == 0) {
  115. // 数据库中第一页是从开始0*size
  116. // 首页路径index.html
  117. columnListPath = ParserUtil
  118. .buildHtmlPath(column.getCategoryPath() + File.separator + ParserUtil.INDEX);
  119. } else {
  120. // 其他路径list-2.html
  121. columnListPath = ParserUtil
  122. .buildHtmlPath(column.getCategoryPath() + File.separator + ParserUtil.PAGE_LIST + pageNo);
  123. }
  124. // 设置分页的起始位置
  125. page.setPageNo(pageNo);
  126. String read = ParserUtil.read(File.separator + column.getCategoryListUrl(), parserParams);
  127. FileUtil.writeString(read, columnListPath, Const.UTF8);
  128. pageNo++;
  129. }
  130. }
  131. }catch (Exception e){
  132. e.printStackTrace();
  133. }
  134. }
  135. /**
  136. * 生成内容
  137. *
  138. * @param articleIdList
  139. * 文章集合
  140. * @return
  141. * @throws IOException
  142. * @throws ParseException
  143. * @throws MalformedTemplateNameException
  144. * @throws TemplateNotFoundException
  145. */
  146. public static void generateBasic(List<CategoryBean> articleIdList) {
  147. Map<Object, Object> contentModelMap = new HashMap<Object, Object>();
  148. ModelEntity contentModel = null;
  149. // 记录已经生成了文章编号
  150. List<String> generateIds = new ArrayList<>();
  151. ExecutorService pool= SpringUtil.getBean(ExecutorService.class);
  152. // 生成文章
  153. for (int artId = 0; artId < articleIdList.size();) {
  154. String writePath = null;
  155. //设置分页类
  156. PageBean page = new PageBean();
  157. // 文章编号
  158. String articleId = articleIdList.get(artId).getArticleId();
  159. // 文章的栏目路径
  160. String articleColumnPath = articleIdList.get(artId).getCategoryPath();
  161. // 该文章相关分类
  162. String categoryParentId = articleIdList.get(artId).getId() ;
  163. if(StringUtils.isNotBlank(articleIdList.get(artId).getCategoryParentId())){
  164. categoryParentId += ','+articleIdList.get(artId).getCategoryParentId();
  165. }
  166. // 文章的模板路径
  167. String columnUrl = articleIdList.get(artId).getCategoryUrl();
  168. // 文章的栏目模型编号
  169. Integer columnContentModelId = null;
  170. if(articleIdList.get(artId).getMdiyModelId() != null && articleIdList.get(artId).getMdiyModelId() > 0){
  171. columnContentModelId = articleIdList.get(artId).getMdiyModelId();
  172. }
  173. // 文章是否已经生成了,生成了就跳过
  174. if (generateIds.contains(articleId)) {
  175. artId++;
  176. continue;
  177. }
  178. // 判断文件是否存在,若不存在弹出返回信息
  179. if (!FileUtil.exist(ParserUtil.buildTempletPath(columnUrl))||articleIdList.get(artId).getId()==null||articleIdList.get(artId).getCategoryType()==null) {
  180. artId++;
  181. continue;
  182. }
  183. // 将
  184. generateIds.add(articleId);
  185. //如果是封面就生成index.html
  186. if(Integer.parseInt(articleIdList.get(artId).getCategoryType()) == COLUMN_TYPE_COVER) {
  187. writePath = ParserUtil.buildHtmlPath(articleColumnPath + File.separator + ParserUtil.INDEX);
  188. }else {
  189. // 组合文章路径如:html/站点id/栏目id/文章id.html
  190. writePath = ParserUtil.buildHtmlPath(articleColumnPath + File.separator + articleId);
  191. }
  192. Map<String, Object> parserParams = new HashMap<String, Object>();
  193. parserParams.put(ParserUtil.COLUMN, articleIdList.get(artId));
  194. // 判断当前栏目是否有自定义模型
  195. if (columnContentModelId != null) {
  196. // 通过当前栏目的模型编号获取,自定义模型表名
  197. if (contentModelMap.containsKey(columnContentModelId)) {
  198. parserParams.put(TABLE_NAME, contentModel.getModelTableName());
  199. } else {
  200. // 通过栏目模型编号获取自定义模型实体
  201. contentModel = (ModelEntity) SpringUtil.getBean(IModelBiz.class)
  202. .getEntity(columnContentModelId);
  203. // 将自定义模型编号设置为key值
  204. contentModelMap.put(columnContentModelId, contentModel.getModelTableName());
  205. parserParams.put(TABLE_NAME, contentModel.getModelTableName());
  206. }
  207. }
  208. parserParams.put(ID, articleId);
  209. // 第一篇文章没有上一篇
  210. if (artId > 0) {
  211. CategoryBean preCaBean = articleIdList.get(artId - 1);
  212. //判断当前文档是否与上一页文章在同一栏目下,并且不能使用父栏目字符串,因为父栏目中没有所属栏目编号
  213. // if( categoryParentId.contains(preCaBean.getCategoryId()+"")){
  214. page.setPreId(preCaBean.getArticleId());
  215. // }
  216. }
  217. // 最后一篇文章没有下一篇
  218. if (artId + 1 < articleIdList.size()) {
  219. CategoryBean nextCaBean = articleIdList.get(artId + 1);
  220. //判断当前文档是否与下一页文章在同一栏目下并且不能使用父栏目字符串,因为父栏目中没有所属栏目编号
  221. // if(categoryParentId.contains(nextCaBean.getCategoryId()+"")){
  222. page.setNextId(nextCaBean.getArticleId());
  223. // }
  224. }
  225. parserParams.put(IS_DO, false);
  226. parserParams.put(ParserUtil.PAGE, page);
  227. String finalWritePath = writePath;
  228. HashMap<Object, Object> cloneMap = CollUtil.newHashMap();
  229. cloneMap.putAll(parserParams);
  230. HttpServletRequest request = SpringUtil.getRequest();
  231. pool.execute(() -> {
  232. String content = null;
  233. try {
  234. SpringUtil.setRequest(request);
  235. content = CmsParserUtil.generate(columnUrl, cloneMap);
  236. FileUtil.writeString(content, finalWritePath, Const.UTF8);
  237. } catch (IOException e) {
  238. e.printStackTrace();
  239. }
  240. });
  241. artId++;
  242. }
  243. }
  244. }