Browse Source


。。

WANG 6 years ago
parent
commit
47214676ce

+ 1 - 1
src/main/java/cn/com/qmth/examcloud/web/config/LogProperties.java

@@ -11,7 +11,7 @@ import org.springframework.stereotype.Component;
  * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
  */
 @Component
-@ConfigurationProperties("ocean.web.log")
+@ConfigurationProperties("examcloud.web.log")
 public class LogProperties {
 
 	/**

+ 19 - 41
src/main/java/cn/com/qmth/examcloud/web/config/SystemConfig.java

@@ -1,12 +1,8 @@
 package cn.com.qmth.examcloud.web.config;
 
-import org.apache.commons.lang3.StringUtils;
-import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.stereotype.Component;
 
-import cn.com.qmth.examcloud.commons.exception.StatusException;
-import cn.com.qmth.examcloud.commons.util.PropertiesUtil;
-
 /**
  * 系统配置
  *
@@ -15,45 +11,27 @@ import cn.com.qmth.examcloud.commons.util.PropertiesUtil;
  * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
  */
 @Component
+@ConfigurationProperties("examcloud.web.sys")
 public class SystemConfig {
 
-	@Value("${$dir}")
-	private String dir;
-
-	@Value("${$tempDir}")
-	private String tempDir;
-
-	/**
-	 * 获取数据目录
-	 *
-	 * @author WANGWEI
-	 * @return
-	 */
-	public static String getDataDir() {
-		String dir = PropertiesUtil.getString("$dir");
-		if (StringUtils.isBlank(dir)) {
-			throw new StatusException("370", "数据目录未配置");
-		}
-		if (dir.endsWith("/")) {
-			dir = dir.substring(0, dir.length() - 1);
-		}
-		return dir;
+	private String dataDir;
+
+	private String tempDataDir;
+
+	public String getDataDir() {
+		return dataDir;
+	}
+
+	public void setDataDir(String dataDir) {
+		this.dataDir = dataDir;
+	}
+
+	public String getTempDataDir() {
+		return tempDataDir;
 	}
 
-	/**
-	 * 获取临时数据目录
-	 *
-	 * @author WANGWEI
-	 * @return
-	 */
-	public static String getTempDataDir() {
-		String tempDir = PropertiesUtil.getString("$tempDir");
-		if (StringUtils.isBlank(tempDir)) {
-			throw new StatusException("370", "临时数据目录未配置");
-		}
-		if (tempDir.endsWith("/")) {
-			tempDir = tempDir.substring(0, tempDir.length() - 1);
-		}
-		return tempDir;
+	public void setTempDataDir(String tempDataDir) {
+		this.tempDataDir = tempDataDir;
 	}
+
 }

+ 70 - 0
src/main/java/cn/com/qmth/examcloud/web/jpa/DataIntegrityViolationTransverter.java

@@ -0,0 +1,70 @@
+package cn.com.qmth.examcloud.web.jpa;
+
+import java.sql.SQLIntegrityConstraintViolationException;
+import java.util.List;
+import java.util.Map;
+
+import org.springframework.dao.DataIntegrityViolationException;
+
+import com.google.common.collect.Maps;
+
+import cn.com.qmth.examcloud.commons.exception.StatusException;
+
+/**
+ * 数据完整性校验转换器
+ *
+ * @author WANGWEI
+ * @date 2019年1月30日
+ * @Copyright (c) 2018-2020 WANGWEI [QQ:522080330] All Rights Reserved.
+ */
+public class DataIntegrityViolationTransverter {
+
+	private static Map<String, UniqueRule> uniqueRuleMap = Maps.newHashMap();
+
+	public static void setUniqueRules(List<UniqueRule> rules) {
+		for (UniqueRule rule : rules) {
+			uniqueRuleMap.put(rule.getIndexName(), rule);
+		}
+	}
+
+	/**
+	 * 唯一性约束转换
+	 *
+	 * @author WANGWEI
+	 * @param e
+	 */
+	public static void throwIfDuplicateEntry(DataIntegrityViolationException e) {
+
+		Throwable cause = e.getCause();
+		if (null != cause) {
+			Throwable cause2 = cause.getCause();
+			if (cause instanceof org.hibernate.exception.ConstraintViolationException) {
+				if (null != cause2) {
+					if (cause2 instanceof java.sql.SQLIntegrityConstraintViolationException) {
+						throwIfDuplicateEntry(
+								(java.sql.SQLIntegrityConstraintViolationException) cause2);
+					}
+				}
+			}
+		}
+	}
+
+	/**
+	 * 唯一性约束转换
+	 *
+	 * @author WANGWEI
+	 * @param e
+	 */
+	public static void throwIfDuplicateEntry(SQLIntegrityConstraintViolationException e) {
+		String message = e.getMessage();
+		// Duplicate entry '*' for key '*'
+		if (message.startsWith("Duplicate entry") && message.contains("IDX_")) {
+			String index = message.substring(message.indexOf("IDX_"), message.length() - 1);
+			UniqueRule rule = uniqueRuleMap.get(index);
+			if (null != rule) {
+				throw new StatusException(rule.getCode(), rule.getDesc());
+			}
+		}
+	}
+
+}

+ 9 - 5
src/main/java/cn/com/qmth/examcloud/web/jpa/JpaEntity.java

@@ -5,6 +5,8 @@ import java.util.Date;
 import javax.persistence.Column;
 import javax.persistence.EntityListeners;
 import javax.persistence.MappedSuperclass;
+import javax.persistence.Temporal;
+import javax.persistence.TemporalType;
 
 import org.springframework.data.annotation.CreatedDate;
 import org.springframework.data.annotation.LastModifiedDate;
@@ -16,8 +18,8 @@ import cn.com.qmth.examcloud.api.commons.exchange.JsonSerializable;
  * JPA 数据库实体父类
  *
  * @author WANGWEI
- * @date 2018年5月24
- * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
+ * @date 2018年10月22
+ * @Copyright (c) 2018-2020 WANGWEI [QQ:522080330] All Rights Reserved.
  */
 @MappedSuperclass
 @EntityListeners(AuditingEntityListener.class)
@@ -29,14 +31,16 @@ public abstract class JpaEntity implements JsonSerializable {
 	 * 更新时间
 	 */
 	@LastModifiedDate
-	@Column(nullable = true)
+	@Column(nullable = false)
+	@Temporal(TemporalType.TIMESTAMP)
 	private Date updateTime;
 
 	/**
 	 * 创建时间
 	 */
 	@CreatedDate
-	@Column(nullable = true, updatable = false)
+	@Column(nullable = false, updatable = false)
+	@Temporal(TemporalType.TIMESTAMP)
 	private Date creationTime;
 
 	public Date getUpdateTime() {
@@ -55,4 +59,4 @@ public abstract class JpaEntity implements JsonSerializable {
 		this.creationTime = creationTime;
 	}
 
-}
+}

+ 49 - 0
src/main/java/cn/com/qmth/examcloud/web/jpa/UniqueRule.java

@@ -0,0 +1,49 @@
+package cn.com.qmth.examcloud.web.jpa;
+
+public final class UniqueRule {
+
+	private String indexName;
+
+	private String code;
+
+	private String desc;
+
+	/**
+	 * 构造函数
+	 *
+	 * @param indexName
+	 * @param code
+	 * @param desc
+	 */
+	public UniqueRule(String indexName, String code, String desc) {
+		super();
+		this.indexName = indexName;
+		this.code = code;
+		this.desc = desc;
+	}
+
+	public String getIndexName() {
+		return indexName;
+	}
+
+	public void setIndexName(String indexName) {
+		this.indexName = indexName;
+	}
+
+	public String getCode() {
+		return code;
+	}
+
+	public void setCode(String code) {
+		this.code = code;
+	}
+
+	public String getDesc() {
+		return desc;
+	}
+
+	public void setDesc(String desc) {
+		this.desc = desc;
+	}
+
+}

+ 32 - 0
src/main/java/cn/com/qmth/examcloud/web/jpa/WithIdAndStatusJpaEntity.java

@@ -0,0 +1,32 @@
+package cn.com.qmth.examcloud.web.jpa;
+
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.MappedSuperclass;
+
+/**
+ * ID and status
+ *
+ * @author WANGWEI
+ * @date 2019年2月20日
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
+ */
+@MappedSuperclass
+public abstract class WithIdAndStatusJpaEntity extends WithStatusJpaEntity {
+
+	private static final long serialVersionUID = 2909770208688213445L;
+
+	@Id
+	@GeneratedValue(strategy = GenerationType.IDENTITY)
+	private Long id;
+
+	public Long getId() {
+		return id;
+	}
+
+	public void setId(Long id) {
+		this.id = id;
+	}
+
+}

+ 32 - 0
src/main/java/cn/com/qmth/examcloud/web/jpa/WithIdJpaEntity.java

@@ -0,0 +1,32 @@
+package cn.com.qmth.examcloud.web.jpa;
+
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.MappedSuperclass;
+
+/**
+ * ID
+ *
+ * @author WANGWEI
+ * @date 2019年2月20日
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
+ */
+@MappedSuperclass
+public abstract class WithIdJpaEntity extends JpaEntity {
+
+	private static final long serialVersionUID = 2909770208688213445L;
+
+	@Id
+	@GeneratedValue(strategy = GenerationType.IDENTITY)
+	private Long id;
+
+	public Long getId() {
+		return id;
+	}
+
+	public void setId(Long id) {
+		this.id = id;
+	}
+
+}

+ 46 - 0
src/main/java/cn/com/qmth/examcloud/web/jpa/WithStatusJpaEntity.java

@@ -0,0 +1,46 @@
+package cn.com.qmth.examcloud.web.jpa;
+
+import javax.persistence.Column;
+import javax.persistence.MappedSuperclass;
+
+/**
+ * status
+ *
+ * @author WANGWEI
+ * @date 2019年2月20日
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
+ */
+@MappedSuperclass
+public abstract class WithStatusJpaEntity extends JpaEntity {
+
+	private static final long serialVersionUID = -5204331536862187353L;
+
+	/**
+	 * 是否可用
+	 */
+	@Column(nullable = false)
+	private Boolean enabled;
+
+	/**
+	 * 是否被销毁
+	 */
+	@Column(nullable = false)
+	private Boolean destroyed;
+
+	public Boolean getEnabled() {
+		return enabled;
+	}
+
+	public void setEnabled(Boolean enabled) {
+		this.enabled = enabled;
+	}
+
+	public Boolean getDestroyed() {
+		return destroyed;
+	}
+
+	public void setDestroyed(Boolean destroyed) {
+		this.destroyed = destroyed;
+	}
+
+}