CmsParserUtil.java 9.2 KB

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