From 2596e456fc54dc01a95c8374d0ffe7fba442f218 Mon Sep 17 00:00:00 2001 From: zhouhao Date: Wed, 21 Jun 2017 15:55:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=AA=8C=E8=AF=81=E5=99=A8?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/service/AbstractService.java | 22 ++++++++++++++++--- .../web/service/GenericEntityService.java | 6 +++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/main/java/org/hswebframework/web/service/AbstractService.java b/hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/main/java/org/hswebframework/web/service/AbstractService.java index 4d7839419..d69eb52d4 100644 --- a/hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/main/java/org/hswebframework/web/service/AbstractService.java +++ b/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; +import org.hswebframework.utils.ClassUtils; import org.hswebframework.web.NotFoundException; import org.hswebframework.web.commons.entity.Entity; import org.hswebframework.web.commons.entity.factory.EntityFactory; import org.hswebframework.web.validate.SimpleValidateResults; import org.hswebframework.web.validate.ValidationException; -import org.hswebframework.utils.ClassUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import javax.validation.ConstraintViolation; import javax.validation.Validator; +import java.util.Set; +import java.util.function.Supplier; /** * 抽象服务类,提供通用模板方法、类,如验证器,实体工厂等 @@ -95,13 +98,26 @@ public abstract class AbstractService 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 void validate(Supplier>> validatorSetFunction) { if (validator == null) { logger.warn("validator is null!"); return; } 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()) throw new ValidationException(results); } diff --git a/hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/main/java/org/hswebframework/web/service/GenericEntityService.java b/hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/main/java/org/hswebframework/web/service/GenericEntityService.java index 653383830..fb7b49249 100644 --- a/hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/main/java/org/hswebframework/web/service/GenericEntityService.java +++ b/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.id.IDGenerator; 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 java.util.ArrayList; @@ -64,7 +66,7 @@ public abstract class GenericEntityService, PK> @Override public int updateByPk(PK pk, E entity) { entity.setId(pk); - tryValidate(entity); + tryValidate(entity, UpdateGroup.class); return createUpdate(entity) //如果是RecordCreationEntity则不修改creator_id和creator_time .when(ClassUtils.instanceOf(getEntityType(), RecordCreationEntity.class), @@ -104,7 +106,7 @@ public abstract class GenericEntityService, PK> tryValidateProperty(selectByPk(entity.getId()) == null, "id", entity.getId() + "已存在"); } if (entity.getId() == null) entity.setId(getIDGenerator().generate()); - tryValidate(entity); + tryValidate(entity, CreateGroup.class); getDao().insert(entity); return entity.getId(); }