소스 검색

修复审核中文章前台旧版展示

发起文章变动时,为已发布正文和编辑正文生成历史快照并写入变更日志,避免审核稿覆盖发布稿后无法回看。

前台文章详情和列表在文章处于审核中(变动)时,从最近的发布版变更日志恢复标题、摘要、缩略图、发布时间、正文路径和 URL;审核通过后仍按原有已发布记录展示新版本。

Validation:

- javac -encoding UTF-8 -cp WebRoot/WEB-INF/classes:WebRoot/WEB-INF/lib/* -d WebRoot/WEB-INF/classes src/space/apps/moudules/website/services/ArticleService.java src/space/apps/moudules/website/template/util/WebsiteTemplateArticleUtil.java

- ./local-run/bin/start-full-local.sh

- curl http://ffkj-screen-red.localhost:4188/xiaoyuanxinwen/yixiandongtai/260626/FFKJEDU00001.htm => 200

- 栏目列表 /xiaoyuanxinwen/yixiandongtai/ 输出 FFKJEDU00001.htm 链接
非繁 5 달 전
부모
커밋
c6ad43a603

+ 60 - 1
src/space/apps/moudules/website/services/ArticleService.java

@@ -12,6 +12,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.jsoup.Jsoup;
@@ -61,6 +63,9 @@ public class ArticleService extends BaseTransaction {
     private static final long serialVersionUID = 1L;
 
     private static final String ARTICLE_PREVIEW_SESSION_PREFIX = "articlePreviewPath_";
+    private static final String ARTICLE_PUBLISHED_HISTORY_ROOT = "/web/upload/article-history/";
+    private static final String PUBLISHED_CONTENT_SNAPSHOT_FIELD = "publishedContentSnapshot";
+    private static final String PUBLISHED_EDIT_CONTENT_SNAPSHOT_FIELD = "publishedEditContentSnapshot";
 
     /**
      * 转换文章内容 html 里的链接
@@ -541,7 +546,8 @@ public class ArticleService extends BaseTransaction {
         Article a2 = new Article();
         a2.setArticleid(article.getArticleid());
         gdba.selectById(a2);
-        ChangeLogUtil.addChangeLog(gdba, appUser, "Article", a2.getArticleid(), GsonUtil.toJson(a2));
+        ChangeLogUtil.addChangeLog(gdba, appUser, "Article", a2.getArticleid(),
+                buildArticleChangeLogContent(a2, nowTimestamp));
 
         verifyArticleUrl(article, a2, wn);
 
@@ -582,6 +588,59 @@ public class ArticleService extends BaseTransaction {
         return TIPS_FORWARD;
     }
 
+    private String buildArticleChangeLogContent(Article article, Timestamp snapshotTime) throws Exception {
+        JsonObject logContent = new JsonParser().parse(GsonUtil.toJson(article)).getAsJsonObject();
+        if (!isPublishedArticle(article)) {
+            return logContent.toString();
+        }
+
+        String contentSnapshot = copyPublishedArticleFile(article.getArticleid(), article.getContent(), "content",
+                snapshotTime);
+        if (StringUtil.notNull(contentSnapshot)) {
+            logContent.addProperty(PUBLISHED_CONTENT_SNAPSHOT_FIELD, contentSnapshot);
+        }
+
+        String editContentSnapshot = copyPublishedArticleFile(article.getArticleid(), article.getEditContent(),
+                "edit-content", snapshotTime);
+        if (StringUtil.notNull(editContentSnapshot)) {
+            logContent.addProperty(PUBLISHED_EDIT_CONTENT_SNAPSHOT_FIELD, editContentSnapshot);
+        }
+
+        return logContent.toString();
+    }
+
+    private boolean isPublishedArticle(Article article) {
+        return Long.valueOf(dataStatusCode.USE.getCode()).equals(article.getDcode())
+                && articleStatusCode.HAS_RELEASE.getCode().equals(article.getAscode());
+    }
+
+    private String copyPublishedArticleFile(Long articleid, String filePath, String fileType, Timestamp snapshotTime)
+            throws Exception {
+        if (StringUtil.isNull(filePath)) {
+            return null;
+        }
+
+        File source = new File(getWebRootPath() + filePath);
+        if (!source.exists() || !source.isFile()) {
+            return null;
+        }
+
+        String snapshotDir = ARTICLE_PUBLISHED_HISTORY_ROOT + articleid + "/";
+        FileUtil.makeDirectory(new File(getWebRootPath() + snapshotDir));
+
+        String fileExt = FileUtil.getFileExt(filePath);
+        if (StringUtil.isNull(fileExt)) {
+            fileExt = "html";
+        } else if (fileExt.startsWith(".")) {
+            fileExt = fileExt.substring(1);
+        }
+
+        String snapshotPath = snapshotDir + DateTimeUtil.formatDate(snapshotTime, "yyyyMMddHHmmssSSS") + "-"
+                + fileType + "." + fileExt;
+        FileUtil.copyFile(source.getAbsolutePath(), getWebRootPath() + snapshotPath);
+        return snapshotPath;
+    }
+
     /**
      * 删除指定文章的所有抄送记录
      *

+ 222 - 12
src/space/apps/moudules/website/template/util/WebsiteTemplateArticleUtil.java

@@ -1,6 +1,11 @@
 package space.apps.moudules.website.template.util;
 
+import java.sql.Timestamp;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Iterator;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 
 import javax.servlet.http.HttpServletRequest;
@@ -13,6 +18,7 @@ import space.common.exception.SpaceNullPointerException;
 import space.core.common.CommonVars;
 import space.core.common.utils.DateTimeUtil;
 import space.core.common.utils.FileUtil;
+import space.core.common.utils.GsonUtil;
 import space.core.common.utils.StringUtil;
 import space.core.db.bean.base.PageBean;
 import space.core.db.jdbc.GeneralDBAccess;
@@ -26,6 +32,13 @@ import space.core.db.jdbc.SqlCommandImpl;
  */
 public class WebsiteTemplateArticleUtil {
 
+	private static final Long ARTICLE_DCODE_USE = 61L;
+	private static final Long ARTICLE_DCODE_CHANGING = 31L;
+	private static final Long ARTICLE_ASCODE_RELEASED = 10010L;
+	private static final String CHANGE_LOG_OBJECT_ARTICLE = "Article";
+	private static final String PUBLISHED_CONTENT_SNAPSHOT_FIELD = "publishedContentSnapshot";
+	private static final String PUBLISHED_EDIT_CONTENT_SNAPSHOT_FIELD = "publishedEditContentSnapshot";
+
 	/**
 	 * 获取栏目的子栏目文章列表
 	 * 
@@ -41,17 +54,20 @@ public class WebsiteTemplateArticleUtil {
 	public static List<Map<String, Object>> getAllChildrenArticleList(GeneralDBAccess gdba, Long navId,
 			boolean isThumbnailFlag, PageBean pb) throws Exception {
 
-		String sql = "SELECT a.*, w.protocol, w.port, w.domains FROM article a left join website w on a.websiteid = w.websiteid where w.dcode = 61 and a.dcode = 61 "
-				+ " and a.ascode = '10010' and (a.navigationid in (Select navigationid from website_navigation where parent_nid = :navigationid1 ) "
-				+ " or a.articleid in (Select ac.articleid from article_copy ac, article a where a.articleid = ac.articleid and ac.dcode = 61 "
-				+ " and a.dcode = 61 and a.ascode = '10010' and ac.navigationid in (Select navigationid from website_navigation where parent_nid = :navigationid2 ) ))"
+		String sql = "SELECT a.*, w.protocol, w.port, w.domains FROM article a left join website w on a.websiteid = w.websiteid where w.dcode = 61 and "
+				+ getPublishedOrChangingArticleSql("a")
+				+ " and (a.navigationid in (Select navigationid from website_navigation where parent_nid = :navigationid1 ) "
+				+ " or a.articleid in (Select ac.articleid from article_copy ac, article ca where ca.articleid = ac.articleid and ac.dcode = 61 "
+				+ " and " + getPublishedOrChangingArticleSql("ca")
+				+ " and ac.navigationid in (Select navigationid from website_navigation where parent_nid = :navigationid2 ) ))"
 				+ (isThumbnailFlag ? " and a.thumbnail is not null" : "") // thumbnail
-				+ "order by a.sticktop desc, a.release_time desc, a.articleid desc";
+				+ " order by a.sticktop desc, a.release_time desc, a.articleid desc";
 
 		SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
 		sqlCmd.setString("navigationid1", navId).setString("navigationid2", navId);
 		List<Map<String, Object>> articleList = gdba.selectAsMapList(sqlCmd, pb);
 
+		restoreChangingArticleListPublishedVersion(gdba, articleList);
 		transformArticleURL(articleList);
 
 		return articleList;
@@ -90,7 +106,7 @@ public class WebsiteTemplateArticleUtil {
 		SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
 		sqlCmd.setString("websiteid", website.getWebsiteid()).setString("uri", uri);
 		Article a = (Article) gdba.selectAsBean(sqlCmd, Article.class);
-		return a;
+		return a == null ? getChangingArticlePublishedVersionByURI(gdba, website, uri) : a;
 	}
 
 	/**
@@ -138,9 +154,11 @@ public class WebsiteTemplateArticleUtil {
 			PageBean pb) throws Exception {
 
 		String sql = "SELECT a.*, w.protocol, w.port, w.domains FROM article a left join website w on a.websiteid = w.websiteid "
-				+ "where w.dcode = 61 and a.dcode = 61 and a.ascode = '10010' and (a.navigationid = :navigationid1 "
-				+ "or a.articleid in (Select ac.articleid from article_copy ac, article a "
-				+ "where a.articleid = ac.articleid and ac.dcode = 61 and a.dcode = 61 and a.ascode = '10010' and ac.navigationid = :navigationid2 ))"
+				+ "where w.dcode = 61 and " + getPublishedOrChangingArticleSql("a")
+				+ " and (a.navigationid = :navigationid1 "
+				+ "or a.articleid in (Select ac.articleid from article_copy ac, article ca "
+				+ "where ca.articleid = ac.articleid and ac.dcode = 61 and "
+				+ getPublishedOrChangingArticleSql("ca") + " and ac.navigationid = :navigationid2 ))"
 				+ (isThumbnailFlag ? " and a.thumbnail is not null" : "") // thumbnail
 				+ " order by a.sticktop desc, a.release_time desc ";
 
@@ -148,6 +166,7 @@ public class WebsiteTemplateArticleUtil {
 		sqlCmd.setString("navigationid1", navId).setString("navigationid2", navId);
 		List<Map<String, Object>> articleList = gdba.selectAsMapList(sqlCmd, pb);
 
+		restoreChangingArticleListPublishedVersion(gdba, articleList);
 		transformArticleURL(articleList);
 
 		return articleList;
@@ -183,13 +202,15 @@ public class WebsiteTemplateArticleUtil {
 	public static List<Map<String, Object>> getArticleList(GeneralDBAccess gdba, String keyword, PageBean pb)
 			throws Exception {
 
-		String sql = "SELECT a.*, w.protocol, w.port, w.domains FROM article a left join website w on a.websiteid = w.websiteid where w.dcode = 61 and a.dcode = 61"
-				+ " and a.ascode = '10010' and title like :keyword order by a.sticktop desc, a.release_time desc";
+		String sql = "SELECT a.*, w.protocol, w.port, w.domains FROM article a left join website w on a.websiteid = w.websiteid where w.dcode = 61 and "
+				+ getPublishedOrChangingArticleSql("a")
+				+ " and title like :keyword order by a.sticktop desc, a.release_time desc";
 
 		SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
 		sqlCmd.setString("keyword", "%" + keyword + "%");
 		List<Map<String, Object>> articleList = gdba.selectAsMapList(sqlCmd, pb);
 
+		restoreChangingArticleListPublishedVersion(gdba, articleList);
 		transformArticleURL(articleList);
 
 		return articleList;
@@ -213,7 +234,7 @@ public class WebsiteTemplateArticleUtil {
 				+ "FROM article a "
 				+ "left join website w on a.websiteid = w.websiteid "
 				+ "left join website_navigation wn on a.navigationid = wn.navigationid "
-				+ "where w.dcode = 61 and a.dcode = 61 and a.ascode = '10010' "
+				+ "where w.dcode = 61 and " + getPublishedOrChangingArticleSql("a") + " "
 				+ "and (w.websiteid = :rootWebsiteId1 "
 				+ "or w.parent_wid = :rootWebsiteId2 "
 				+ "or w.parent_wid in (select sw.websiteid from website sw where sw.dcode = 61 and sw.parent_wid = :rootWebsiteId3) "
@@ -228,11 +249,200 @@ public class WebsiteTemplateArticleUtil {
 				.setString("keyword1", "%" + keyword + "%").setString("keyword2", "%" + keyword + "%");
 		List<Map<String, Object>> articleList = gdba.selectAsMapList(sqlCmd, pb);
 
+		restoreChangingArticleListPublishedVersion(gdba, articleList);
 		transformArticleURL(articleList);
 
 		return articleList;
 	}
 
+	private static String getPublishedOrChangingArticleSql(String alias) {
+		return "((" + alias + ".dcode = 61 and " + alias + ".ascode = '10010') or " + alias + ".dcode = 31)";
+	}
+
+	private static Article getChangingArticlePublishedVersionByURI(GeneralDBAccess gdba, Website website, String uri)
+			throws Exception {
+		String sql = "SELECT cl.content FROM change_log cl where cl.object = :object and cl.content like :uriLike "
+				+ "and cl.objectid in (select ca.articleid from article ca where ca.dcode = :changingDcode) "
+				+ "order by cl.change_logid desc limit 0,20";
+		SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
+		sqlCmd.setString("object", CHANGE_LOG_OBJECT_ARTICLE).setString("uriLike", "%" + uri + "%")
+				.setString("changingDcode", ARTICLE_DCODE_CHANGING);
+		List<Map<String, Object>> logList = gdba.selectAsMapList(sqlCmd);
+		if (logList == null || logList.size() == 0) {
+			return null;
+		}
+
+		for (Map<String, Object> log : logList) {
+			Article article = buildPublishedArticleFromChangeLog(log.get("content"), website.getWebsiteid(), uri);
+			if (article != null) {
+				return article;
+			}
+		}
+		return null;
+	}
+
+	private static Article getChangingArticlePublishedVersionByArticleID(GeneralDBAccess gdba, Long articleid)
+			throws Exception {
+		String sql = "SELECT content FROM change_log where object = :object and objectid = :articleid "
+				+ "order by change_logid desc limit 0,20";
+		SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
+		sqlCmd.setString("object", CHANGE_LOG_OBJECT_ARTICLE).setString("articleid", articleid);
+		List<Map<String, Object>> logList = gdba.selectAsMapList(sqlCmd);
+		if (logList == null || logList.size() == 0) {
+			return null;
+		}
+
+		for (Map<String, Object> log : logList) {
+			Article article = buildPublishedArticleFromChangeLog(log.get("content"), null, null);
+			if (article != null) {
+				return article;
+			}
+		}
+		return null;
+	}
+
+	private static Article buildPublishedArticleFromChangeLog(Object logContent, Long websiteid, String uri) {
+		if (StringUtil.isNull(logContent)) {
+			return null;
+		}
+
+		Map<String, Object> articleMap = GsonUtil.jsonToMap(logContent.toString());
+		if (!ARTICLE_DCODE_USE.equals(getLongValue(articleMap, "dcode"))
+				|| !ARTICLE_ASCODE_RELEASED.equals(getLongValue(articleMap, "ascode"))) {
+			return null;
+		}
+		if (websiteid != null && !websiteid.equals(getLongValue(articleMap, "websiteid"))) {
+			return null;
+		}
+		if (uri != null && !uri.equals(getStringValue(articleMap, "url"))) {
+			return null;
+		}
+
+		Article article = new Article();
+		article.setArticleid(getLongValue(articleMap, "articleid"));
+		article.setDcode(ARTICLE_DCODE_USE);
+		article.setWebsiteid(getLongValue(articleMap, "websiteid"));
+		article.setUnitid(getLongValue(articleMap, "unitid"));
+		article.setAscode(ARTICLE_ASCODE_RELEASED);
+		article.setNavigationid(getLongValue(articleMap, "navigationid"));
+		article.setTitle(getStringValue(articleMap, "title"));
+		article.setAjtcode(getLongValue(articleMap, "ajtcode"));
+		article.setDesc(getStringValue(articleMap, "desc"));
+		article.setSource(getStringValue(articleMap, "source"));
+		article.setThumbnail(getStringValue(articleMap, "thumbnail"));
+		article.setSticktop(getLongValue(articleMap, "sticktop"));
+		article.setSticktopEndTime(getTimestampValue(articleMap, "sticktopEndTime"));
+		article.setSticktopSchedulesid(getLongValue(articleMap, "sticktopSchedulesid"));
+		article.setRelease(getLongValue(articleMap, "release"));
+		article.setReleaseTime(getTimestampValue(articleMap, "releaseTime"));
+		article.setReleaseTaskSchedulesid(getLongValue(articleMap, "releaseTaskSchedulesid"));
+		article.setContent(getStringValue(articleMap, PUBLISHED_CONTENT_SNAPSHOT_FIELD));
+		if (StringUtil.isNull(article.getContent())) {
+			article.setContent(getStringValue(articleMap, "content"));
+		}
+		article.setEditContent(getStringValue(articleMap, PUBLISHED_EDIT_CONTENT_SNAPSHOT_FIELD));
+		if (StringUtil.isNull(article.getEditContent())) {
+			article.setEditContent(getStringValue(articleMap, "editContent"));
+		}
+		article.setUrl(getStringValue(articleMap, "url"));
+		article.setReadCount(getLongValue(articleMap, "readCount"));
+		article.setCreateTime(getTimestampValue(articleMap, "createTime"));
+		article.setLastChangeTime(getTimestampValue(articleMap, "lastChangeTime"));
+		article.setCreatePid(getLongValue(articleMap, "createPid"));
+		article.setLastChangePid(getLongValue(articleMap, "lastChangePid"));
+		return article;
+	}
+
+	private static void restoreChangingArticleListPublishedVersion(GeneralDBAccess gdba,
+			List<Map<String, Object>> articleList) throws Exception {
+		if (articleList == null || articleList.size() == 0) {
+			return;
+		}
+
+		Iterator<Map<String, Object>> iterator = articleList.iterator();
+		while (iterator.hasNext()) {
+			Map<String, Object> articleMap = iterator.next();
+			if (!ARTICLE_DCODE_CHANGING.equals(getLongValue(articleMap, "dcode"))) {
+				continue;
+			}
+
+			Article article = getChangingArticlePublishedVersionByArticleID(gdba, getLongValue(articleMap, "articleid"));
+			if (article == null) {
+				iterator.remove();
+				continue;
+			}
+
+			putArticleValueToMap(articleMap, article);
+		}
+	}
+
+	private static void putArticleValueToMap(Map<String, Object> articleMap, Article article) {
+		articleMap.put("articleid", article.getArticleid());
+		articleMap.put("dcode", article.getDcode());
+		articleMap.put("websiteid", article.getWebsiteid());
+		articleMap.put("unitid", article.getUnitid());
+		articleMap.put("ascode", article.getAscode());
+		articleMap.put("navigationid", article.getNavigationid());
+		articleMap.put("title", article.getTitle());
+		articleMap.put("ajtcode", article.getAjtcode());
+		articleMap.put("desc", article.getDesc());
+		articleMap.put("source", article.getSource());
+		articleMap.put("thumbnail", article.getThumbnail());
+		articleMap.put("sticktop", article.getSticktop());
+		articleMap.put("sticktop_end_time", article.getSticktopEndTime());
+		articleMap.put("sticktop_schedulesid", article.getSticktopSchedulesid());
+		articleMap.put("release", article.getRelease());
+		articleMap.put("release_time", article.getReleaseTime());
+		articleMap.put("release_task_schedulesid", article.getReleaseTaskSchedulesid());
+		articleMap.put("content", article.getContent());
+		articleMap.put("edit_content", article.getEditContent());
+		articleMap.put("url", article.getUrl());
+		articleMap.put("read_count", article.getReadCount());
+		articleMap.put("create_time", article.getCreateTime());
+		articleMap.put("last_change_time", article.getLastChangeTime());
+		articleMap.put("create_pid", article.getCreatePid());
+		articleMap.put("last_change_pid", article.getLastChangePid());
+	}
+
+	private static String getStringValue(Map<String, Object> map, String key) {
+		Object value = map.get(key);
+		return StringUtil.isNull(value) ? null : value.toString();
+	}
+
+	private static Long getLongValue(Map<String, Object> map, String key) {
+		Object value = map.get(key);
+		if (StringUtil.isNull(value)) {
+			return null;
+		}
+		if (value instanceof Number) {
+			return ((Number) value).longValue();
+		}
+		return Long.valueOf(value.toString());
+	}
+
+	private static Timestamp getTimestampValue(Map<String, Object> map, String key) {
+		String value = getStringValue(map, key);
+		if (StringUtil.isNull(value)) {
+			return null;
+		}
+
+		String[] patterns = new String[] { "MMM d, yyyy h:mm:ss a", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm" };
+		for (String pattern : patterns) {
+			try {
+				Date date = new SimpleDateFormat(pattern, Locale.ENGLISH).parse(value);
+				return new Timestamp(date.getTime());
+			} catch (Exception e) {
+				// 尝试下一种历史日志里的时间格式。
+			}
+		}
+
+		try {
+			return Timestamp.valueOf(value);
+		} catch (Exception e) {
+			return null;
+		}
+	}
+
 	/**
 	 * 获取文章列表
 	 *