Kaynağa Gözat

content内容未修改时用户提交表单不操作数据库

huangct 8 yıl önce
ebeveyn
işleme
4389adf923

+ 69 - 0
jpress-commons/src/main/java/io/jpress/utils/MyCollectionUtils.java

@@ -0,0 +1,69 @@
+package io.jpress.utils;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ * Created by 黄诚天 on 2017-10-13.
+ */
+public class MyCollectionUtils {
+    private static final Integer INTEGER_ONE = 1;
+
+    public static boolean isEqualCollection(Collection a, Collection b) {
+        if (a == null && b == null) {
+            return true;
+        } else if (a == null && b != null) {
+            return false;
+        } else if (a != null && b == null) {
+            return false;
+        }
+
+        if (a.size() != b.size()) {  // size是最简单的相等条件
+            return false;
+        }
+        Map mapa = getCardinalityMap(a);
+        Map mapb = getCardinalityMap(b);
+
+        // 转换map后,能去掉重复的,这时候size就是非重复项,也是先决条件
+        if (mapa.size() != mapb.size()) {
+            return false;
+        }
+        Iterator it = mapa.keySet().iterator();
+        while (it.hasNext()) {
+            Object obj = it.next();
+            // 查询同一个obj,首先两边都要有,而且还要校验重复个数,就是map.value
+            if (getFreq(obj, mapa) != getFreq(obj, mapb)) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
+     * 以obj为key,可以防止重复,如果重复就value++
+     * 这样实际上记录了元素以及出现的次数
+     */
+    public static Map getCardinalityMap(Collection coll) {
+        Map count = new HashMap();
+        for (Iterator it = coll.iterator(); it.hasNext(); ) {
+            Object obj = it.next();
+            Integer c = (Integer) count.get(obj);
+            if (c == null)
+                count.put(obj, INTEGER_ONE);
+            else {
+                count.put(obj, new Integer(c.intValue() + 1));
+            }
+        }
+        return count;
+    }
+
+    private static final int getFreq(Object obj, Map freqMap) {
+        Integer count = (Integer) freqMap.get(obj);
+        if (count != null) {
+            return count.intValue();
+        }
+        return 0;
+    }
+}

+ 19 - 4
jpress-commons/src/main/java/io/jpress/utils/StringUtils.java

@@ -15,6 +15,9 @@
  */
 package io.jpress.utils;
 
+import com.jfinal.core.JFinal;
+import com.jfinal.log.Log;
+
 import java.io.UnsupportedEncodingException;
 import java.math.BigInteger;
 import java.net.URLDecoder;
@@ -22,9 +25,6 @@ import java.net.URLEncoder;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-import com.jfinal.core.JFinal;
-import com.jfinal.log.Log;
-
 public class StringUtils {
 	private static final Log log = Log.getLog(StringUtils.class);
 
@@ -163,6 +163,21 @@ public class StringUtils {
 
 		return texts;
 	}
-	
+
+	public static boolean isEqual(String a, String b) {
+		if (a == null && b == null) {
+			return true;
+		} else if (a == null && b != null) {
+			return false;
+		} else if (a != null && b == null) {
+			return false;
+		}
+
+		if (a.equals(b)) {
+			return true;
+		}
+
+		return false;
+	}
 
 }

+ 36 - 5
jpress-web-admin/src/main/java/io/jpress/admin/controller/_ContentController.java

@@ -35,10 +35,7 @@ import io.jpress.router.RouterNotAllowConvert;
 import io.jpress.template.TemplateManager;
 import io.jpress.template.TplModule;
 import io.jpress.template.TplTaxonomyType;
-import io.jpress.utils.HttpUtils;
-import io.jpress.utils.JsoupUtils;
-import io.jpress.utils.MyEncryptUtils;
-import io.jpress.utils.StringUtils;
+import io.jpress.utils.*;
 
 import java.math.BigInteger;
 import java.sql.SQLException;
@@ -413,9 +410,43 @@ public class _ContentController extends JBaseCRUDController<Content> {
 			@Override
 			public boolean run() throws SQLException {
 
+				boolean isSameAsOld = true;
 				Content oldContent = null;
 				if (content.getId() != null) {
 					oldContent = ContentQuery.me().findById(content.getId());
+
+					//判断内容是否改变,否则不提交到数据库
+					//TODO content 与 oldContent 数据一致了
+					if (!StringUtils.isEqual(content.getAuthor(),oldContent.getAuthor())) {
+						isSameAsOld = false;
+					} else if (!MyCollectionUtils.isEqualCollection(content.getMetadatas(),oldContent.getMetadatas())) {
+						isSameAsOld = false;
+					} else if (!MyCollectionUtils.isEqualCollection(content.getTaxonomys(),oldContent.getTaxonomys())) {
+						isSameAsOld = false;
+					} else if (!StringUtils.isEqual(content.getTitle(),oldContent.getTitle())) {
+						isSameAsOld = false;
+					} else if (!StringUtils.isEqual(content.getSlug(),oldContent.getSlug())) {
+						isSameAsOld = false;
+					} else if (!StringUtils.isEqual(content.getText(),oldContent.getText())) {
+						isSameAsOld = false;
+					} else if (!StringUtils.isEqual(content.getSummary(),oldContent.getSummary())) {
+						isSameAsOld = false;
+					} else if (content.getViewCount() != oldContent.getViewCount()) {
+						isSameAsOld = false;
+					} else if (content.getVoteUp() != oldContent.getVoteUp()) {
+						isSameAsOld = false;
+					} else if (!StringUtils.isEqual(content.getCommentStatus(),oldContent.getCommentStatus())) {
+						isSameAsOld = false;
+					} else if (!StringUtils.isEqual(content.getThumbnail(),oldContent.getThumbnail())) {
+						isSameAsOld = false;
+					} else if (!StringUtils.isEqual(content.getSmallThumbnail(),oldContent.getSmallThumbnail())) {
+						isSameAsOld = false;
+					}
+
+					if (isSameAsOld) {
+						return false;
+					}
+
 				}
 
 				if (!content.saveOrUpdate()) {
@@ -455,7 +486,7 @@ public class _ContentController extends JBaseCRUDController<Content> {
 		});
 
 		if (!saved) {
-			renderAjaxResultForError();
+			renderAjaxResultForError("保存失败或未修改内容!");
 			return;
 		}