浏览代码

优化验证器逻辑

zhouhao 7 年之前
父节点
当前提交
2596e456fc

+ 19 - 3
hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/main/java/org/hswebframework/web/service/AbstractService.java

@@ -1,16 +1,19 @@
 package org.hswebframework.web.service;
 package org.hswebframework.web.service;
 
 
+import org.hswebframework.utils.ClassUtils;
 import org.hswebframework.web.NotFoundException;
 import org.hswebframework.web.NotFoundException;
 import org.hswebframework.web.commons.entity.Entity;
 import org.hswebframework.web.commons.entity.Entity;
 import org.hswebframework.web.commons.entity.factory.EntityFactory;
 import org.hswebframework.web.commons.entity.factory.EntityFactory;
 import org.hswebframework.web.validate.SimpleValidateResults;
 import org.hswebframework.web.validate.SimpleValidateResults;
 import org.hswebframework.web.validate.ValidationException;
 import org.hswebframework.web.validate.ValidationException;
-import org.hswebframework.utils.ClassUtils;
 import org.slf4j.Logger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 
 
+import javax.validation.ConstraintViolation;
 import javax.validation.Validator;
 import javax.validation.Validator;
+import java.util.Set;
+import java.util.function.Supplier;
 
 
 /**
 /**
  * 抽象服务类,提供通用模板方法、类,如验证器,实体工厂等
  * 抽象服务类,提供通用模板方法、类,如验证器,实体工厂等
@@ -95,13 +98,26 @@ public abstract class AbstractService<E extends Entity, PK> implements CreateEnt
         }
         }
     }
     }
 
 
-    protected void tryValidate(Object data) {
+    protected void tryValidate(Object data, String property, Class... groups) {
+        validate(() -> validator.validateProperty(data, property, groups));
+    }
+
+    protected void tryValidate(Class type, String property, Object value, Class... groups) {
+        validate(() -> validator.validateValue(type, property, value, groups));
+    }
+
+    protected void tryValidate(Object data, Class... groups) {
+        validate(() -> validator.validate(data, groups));
+    }
+
+    private <T> void validate(Supplier<Set<ConstraintViolation<T>>> validatorSetFunction) {
         if (validator == null) {
         if (validator == null) {
             logger.warn("validator is null!");
             logger.warn("validator is null!");
             return;
             return;
         }
         }
         SimpleValidateResults results = new SimpleValidateResults();
         SimpleValidateResults results = new SimpleValidateResults();
-        validator.validate(data).forEach(violation -> results.addResult(violation.getPropertyPath().toString(), violation.getMessage()));
+        validatorSetFunction.get()
+                .forEach(violation -> results.addResult(violation.getPropertyPath().toString(), violation.getMessage()));
         if (!results.isSuccess())
         if (!results.isSuccess())
             throw new ValidationException(results);
             throw new ValidationException(results);
     }
     }

+ 4 - 2
hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/main/java/org/hswebframework/web/service/GenericEntityService.java

@@ -23,6 +23,8 @@ import org.hswebframework.web.commons.entity.RecordCreationEntity;
 import org.hswebframework.web.dao.CrudDao;
 import org.hswebframework.web.dao.CrudDao;
 import org.hswebframework.web.id.IDGenerator;
 import org.hswebframework.web.id.IDGenerator;
 import org.hswebframework.utils.ClassUtils;
 import org.hswebframework.utils.ClassUtils;
+import org.hswebframework.web.validator.group.CreateGroup;
+import org.hswebframework.web.validator.group.UpdateGroup;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.transaction.annotation.Transactional;
 
 
 import java.util.ArrayList;
 import java.util.ArrayList;
@@ -64,7 +66,7 @@ public abstract class GenericEntityService<E extends GenericEntity<PK>, PK>
     @Override
     @Override
     public int updateByPk(PK pk, E entity) {
     public int updateByPk(PK pk, E entity) {
         entity.setId(pk);
         entity.setId(pk);
-        tryValidate(entity);
+        tryValidate(entity, UpdateGroup.class);
         return createUpdate(entity)
         return createUpdate(entity)
                 //如果是RecordCreationEntity则不修改creator_id和creator_time
                 //如果是RecordCreationEntity则不修改creator_id和creator_time
                 .when(ClassUtils.instanceOf(getEntityType(), RecordCreationEntity.class),
                 .when(ClassUtils.instanceOf(getEntityType(), RecordCreationEntity.class),
@@ -104,7 +106,7 @@ public abstract class GenericEntityService<E extends GenericEntity<PK>, PK>
             tryValidateProperty(selectByPk(entity.getId()) == null, "id", entity.getId() + "已存在");
             tryValidateProperty(selectByPk(entity.getId()) == null, "id", entity.getId() + "已存在");
         }
         }
         if (entity.getId() == null) entity.setId(getIDGenerator().generate());
         if (entity.getId() == null) entity.setId(getIDGenerator().generate());
-        tryValidate(entity);
+        tryValidate(entity, CreateGroup.class);
         getDao().insert(entity);
         getDao().insert(entity);
         return entity.getId();
         return entity.getId();
     }
     }