WANG 6 ani în urmă
părinte
comite
8a5d75ecfa

+ 14 - 0
src/main/java/cn/com/qmth/examcloud/commons/helpers/BasicDataType.java

@@ -0,0 +1,14 @@
+package cn.com.qmth.examcloud.commons.helpers;
+
+/**
+ * 基础数据类型
+ *
+ * @author WANGWEI
+ * @date 2019年2月14日
+ * @Copyright (c) 2018-2020 WANGWEI [QQ:522080330] All Rights Reserved.
+ */
+public enum BasicDataType {
+
+	STRING, INTEGER, BOOLEAN, LONG;
+
+}

+ 33 - 8
src/main/java/cn/com/qmth/examcloud/commons/helpers/DynamicEnum.java

@@ -4,6 +4,8 @@ import java.io.Serializable;
 
 import org.apache.commons.lang3.StringUtils;
 
+import cn.com.qmth.examcloud.commons.util.StringUtil;
+
 /**
  * 动态枚举
  *
@@ -31,9 +33,9 @@ public final class DynamicEnum implements Serializable {
 	private String desc;
 
 	/**
-	 * 值匹配正则表达式
+	 * 值类型
 	 */
-	private String pattern;
+	private String valueType;
 
 	public Long getId() {
 		return id;
@@ -59,12 +61,12 @@ public final class DynamicEnum implements Serializable {
 		this.desc = desc;
 	}
 
-	public String getPattern() {
-		return pattern;
+	public String getValueType() {
+		return valueType;
 	}
 
-	public void setPattern(String pattern) {
-		this.pattern = pattern;
+	public void setValueType(String valueType) {
+		this.valueType = valueType;
 	}
 
 	/**
@@ -75,9 +77,32 @@ public final class DynamicEnum implements Serializable {
 	 * @return
 	 */
 	public Boolean isLegal(String value) {
-		if (StringUtils.isNotBlank(value) && StringUtils.isNotBlank(pattern)) {
-			return value.matches(pattern);
+		if (StringUtils.isNotBlank(value) && null != valueType) {
+			BasicDataType dataType = BasicDataType.valueOf(valueType);
+			if (dataType.equals(BasicDataType.BOOLEAN)) {
+				try {
+					StringUtil.toBoolean(value);
+					return true;
+				} catch (Exception e) {
+					return false;
+				}
+			} else if (dataType.equals(BasicDataType.INTEGER)) {
+				try {
+					StringUtil.toInteger(value);
+					return true;
+				} catch (Exception e) {
+					return false;
+				}
+			} else if (dataType.equals(BasicDataType.LONG)) {
+				try {
+					StringUtil.toLong(value);
+					return true;
+				} catch (Exception e) {
+					return false;
+				}
+			}
 		}
+
 		return true;
 	}
 

+ 14 - 0
src/main/java/cn/com/qmth/examcloud/commons/helpers/DynamicEnumManager.java

@@ -4,6 +4,8 @@ import java.io.File;
 import java.util.List;
 import java.util.Map;
 
+import org.apache.commons.lang3.StringUtils;
+
 import com.google.common.collect.Maps;
 import com.thoughtworks.xstream.XStream;
 
@@ -59,6 +61,18 @@ public class DynamicEnumManager {
 		manager.idIndex = Maps.newHashMap();
 
 		for (DynamicEnum cur : manager.enums) {
+			String valueTpye = cur.getValueType();
+			if (StringUtils.isBlank(valueTpye)) {
+				cur.setValueType(null);
+			} else {
+				try {
+					BasicDataType dataType = BasicDataType.valueOf(valueTpye);
+					cur.setValueType(dataType.name());
+				} catch (Exception e) {
+					throw new RuntimeException("valueTpye is wrong. valueTpye=" + valueTpye);
+				}
+
+			}
 			cur.setName(cur.getName().trim());
 			cur.setDesc(cur.getDesc().trim());
 			manager.nameIndex.put(cur.getName(), cur);

+ 1 - 1
src/main/java/cn/com/qmth/examcloud/commons/util/StringUtil.java

@@ -323,7 +323,7 @@ public class StringUtil {
 		} else if (s.equals("false")) {
 			return false;
 		} else {
-			throw new ExamCloudRuntimeException("[" + s + "] must be null, \"true\\\",\\\"false\\\"");
+			throw new ExamCloudRuntimeException("[" + s + "] must be  \"true\\\" or \\\"false\\\"");
 		}
 	}