提交 874d7ea6 编写于 作者: zhouweidong's avatar zhouweidong

补充值规则异常信息

上级 98677386
...@@ -185,11 +185,11 @@ public class ${item.getCodeName()}DTO extends ${dtoBase} implements Serializable ...@@ -185,11 +185,11 @@ public class ${item.getCodeName()}DTO extends ${dtoBase} implements Serializable
<#list de.getAllPSDEFValueRules() as deRule> <#list de.getAllPSDEFValueRules() as deRule>
<#if deRule.codeName!='Default'> <#if deRule.codeName!='Default'>
<#if deRule.getPSDEFVRGroupCondition()??> <#if deRule.getPSDEFVRGroupCondition()??>
<#assign ruleName=de.codeName+"_"+deRule.getPSDEField().codeName+"_"+deRule.codeName> <#assign ruleName=(de.codeName+"_"+deRule.getPSDEField().codeName+"_"+deRule.codeName)?lower_case>
<#assign ruleField=deRule.getPSDEField().codeName> <#assign ruleField=(deRule.getPSDEField().codeName)?lower_case>
<#assign group=deRule.getPSDEFVRGroupCondition()> <#assign group=deRule.getPSDEFVRGroupCondition()>
<#assign ruleMsg=getGroupCond(group)> <#assign ruleMsg=getGroupCond(group)>
@ValueRuleCheck(field = "${ruleField}", rule = "${ruleName}", message = "${ruleMsg}"), @ValueRuleCheck(field = "${ruleField}", rule = "${ruleName}"),
</#if> </#if>
</#if> </#if>
</#list> </#list>
...@@ -222,7 +222,7 @@ public class ${item.getCodeName()}DTO extends ${dtoBase} implements Serializable ...@@ -222,7 +222,7 @@ public class ${item.getCodeName()}DTO extends ${dtoBase} implements Serializable
<#function getFieldCond fieldRule> <#function getFieldCond fieldRule>
<#assign fieldCond=""> <#assign fieldCond="">
<#if fieldRule.getRuleInfo?? && fieldRule.getRuleInfo()??> <#if fieldRule.getRuleInfo?? && fieldRule.getRuleInfo()??>
<#assign fieldCond="("+fieldRule.getRuleInfo()+")"> <#assign fieldCond=fieldRule.getRuleInfo()>
</#if> </#if>
<#return fieldCond/> <#return fieldCond/>
</#function> </#function>
\ No newline at end of file
...@@ -3,16 +3,20 @@ TARGET=PSSYSTEM ...@@ -3,16 +3,20 @@ TARGET=PSSYSTEM
</#ibiztemplate> </#ibiztemplate>
package ${pub.getPKGCodeName()}.util.domain; package ${pub.getPKGCodeName()}.util.domain;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonAnyGetter; import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter; import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.alibaba.fastjson.annotation.JSONField; import lombok.Data;
import ${pub.getPKGCodeName()}.util.helper.DEFieldCacheMap;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.data.annotation.Transient;
import org.springframework.util.StringUtils;
import java.io.Serializable; import java.io.Serializable;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import lombok.Data;
@Data @Data
public class DTOBase implements Serializable { public class DTOBase implements Serializable {
...@@ -76,9 +80,44 @@ public class DTOBase implements Serializable { ...@@ -76,9 +80,44 @@ public class DTOBase implements Serializable {
return extensionparams; return extensionparams;
} }
@JsonIgnore
@JSONField(serialize = false)
@Transient
private BeanMap map;
private BeanMap getMap()
{
if(map==null) {
map=BeanMap.create(this);
}
return map;
}
public Object get(String field) {
String fieldRealName= DEFieldCacheMap.getFieldRealName(this.getClass(),field);
if(!StringUtils.isEmpty(fieldRealName)) {
return getMap().get(fieldRealName);
}
else {
return this.extensionparams.get(field.toLowerCase());
}
}
@JsonAnySetter @JsonAnySetter
public void set(String name, Object value) { public void set(String field, Object value) {
extensionparams.put(name.toLowerCase(), value); field=field.toLowerCase();
String fieldRealName=DEFieldCacheMap.getFieldRealName(this.getClass(),field);
if(!StringUtils.isEmpty(fieldRealName)) {
if (value == null) {
getMap().put(fieldRealName, null);
}
else {
getMap().put(fieldRealName, DEFieldCacheMap.fieldValueOf(this.getClass(), fieldRealName, value));
}
}
else {
this.extensionparams.put(field.toLowerCase(),value);
}
} }
} }
<#ibiztemplate>
TARGET=PSSYSTEM
</#ibiztemplate>
<#if sys.codeName == 'Sample'>
package ${pub.getPKGCodeName()}.util.domain;
import lombok.Data;
/**
* 属性值规则实体对象
*/
@Data
public class ValueRule {
public String cond;
public String message;
public ValueRule(String cond, String message) {
this.cond = cond;
this.message = message;
}
}
</#if>
\ No newline at end of file
...@@ -6,13 +6,13 @@ package ${pub.getPKGCodeName()}.util.service; ...@@ -6,13 +6,13 @@ package ${pub.getPKGCodeName()}.util.service;
import ${pub.getPKGCodeName()}.util.annotation.ValueRuleCheck; import ${pub.getPKGCodeName()}.util.annotation.ValueRuleCheck;
import ${pub.getPKGCodeName()}.util.domain.DTOBase; import ${pub.getPKGCodeName()}.util.domain.DTOBase;
import ${pub.getPKGCodeName()}.util.domain.ValueRule;
import org.springframework.expression.EvaluationContext; import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression; import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser; import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext; import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext; import javax.validation.ConstraintValidatorContext;
import java.util.HashMap; import java.util.HashMap;
...@@ -20,29 +20,38 @@ import java.util.Map; ...@@ -20,29 +20,38 @@ import java.util.Map;
public class ValueRuleValidator implements ConstraintValidator<ValueRuleCheck, DTOBase> { public class ValueRuleValidator implements ConstraintValidator<ValueRuleCheck, DTOBase> {
private static Map<String, String> sysRule = new HashMap<>(); private static Map<String, ValueRule> sysRule = new HashMap<>();
private final ExpressionParser parser = new SpelExpressionParser(); private final ExpressionParser parser = new SpelExpressionParser();
private String field; private String field;
private String rule; private String ruleName;
@Override @Override
public void initialize(ValueRuleCheck constraintAnnotation) { public void initialize(ValueRuleCheck constraintAnnotation) {
this.field = constraintAnnotation.field(); this.field = constraintAnnotation.field();
this.rule = constraintAnnotation.rule(); this.ruleName = constraintAnnotation.rule();
} }
@Override @Override
public boolean isValid(DTOBase dto, ConstraintValidatorContext constraintContext) { public boolean isValid(DTOBase dto, ConstraintValidatorContext context) {
if (ObjectUtils.isEmpty(dto) || ObjectUtils.isEmpty(rule) || ObjectUtils.isEmpty(sysRule.get(rule))) { if (!ObjectUtils.isEmpty(dto) && !ObjectUtils.isEmpty(ruleName) && !ObjectUtils.isEmpty(sysRule.get(ruleName)) && !ObjectUtils.isEmpty(dto.get(field))) {
return true; ValueRule rule = sysRule.get(ruleName);
String ruleCond = rule.getCond();
String ruleMessage = rule.getMessage();
EvaluationContext searchContextCtx = new StandardEvaluationContext();
searchContextCtx.setVariable("dto", dto);
Expression searchContextExp = parser.parseExpression(ruleCond);
boolean isValid = searchContextExp.getValue(searchContextCtx, Boolean.class);
if (!isValid) {
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(String.format("执行[%s]属性[%s]值规则发生异常,%s", field, ruleName, ruleMessage))
.addConstraintViolation();
return false;
}
} }
EvaluationContext searchContextCtx = new StandardEvaluationContext(); return true;
searchContextCtx.setVariable("dto", dto);
Expression searchContextExp = parser.parseExpression(sysRule.get(rule));
return searchContextExp.getValue(searchContextCtx, Boolean.class);
} }
static { static {
...@@ -53,7 +62,7 @@ public class ValueRuleValidator implements ConstraintValidator<ValueRuleCheck, D ...@@ -53,7 +62,7 @@ public class ValueRuleValidator implements ConstraintValidator<ValueRuleCheck, D
<#if deRule.codeName!='Default'> <#if deRule.codeName!='Default'>
<#if deRule.getPSDEFVRGroupCondition()??> <#if deRule.getPSDEFVRGroupCondition()??>
<#assign group=deRule.getPSDEFVRGroupCondition()> <#assign group=deRule.getPSDEFVRGroupCondition()>
<#assign ruleName=de.codeName+"_"+deRule.getPSDEField().codeName+"_"+deRule.codeName> <#assign ruleName=(de.codeName+"_"+deRule.getPSDEField().codeName+"_"+deRule.codeName)?lower_case>
<#assign ruleCond=getGroupCond(group)> <#assign ruleCond=getGroupCond(group)>
sysRule.put("${ruleName}", "${ruleCond}"); sysRule.put("${ruleName}", "${ruleCond}");
</#if> </#if>
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册