提交 99c555fc 编写于 作者: sq3536's avatar sq3536

默认值

上级 1891cb2e
......@@ -4,6 +4,7 @@ TARGET=PSSYSTEM
package ${pub.getPKGCodeName()}.util.aspect;
import ${pub.getPKGCodeName()}.util.annotation.DEField;
import ${pub.getPKGCodeName()}.util.domain.EntityBase;
import ${pub.getPKGCodeName()}.util.enums.DEFieldDefaultValueType;
import ${pub.getPKGCodeName()}.util.enums.DEPredefinedFieldType;
import ${pub.getPKGCodeName()}.util.helper.DEFieldCacheMap;
......@@ -11,18 +12,13 @@ import ${pub.getPKGCodeName()}.util.security.AuthenticationUser;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.AlternativeJdkIdGenerator;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -39,80 +35,41 @@ public class DEFieldDefaultValueAspect
* @param point
* @throws Exception
*/
@Before(value = "execution(* ${pub.getPKGCodeName()}.core.*.service.*.create(..))")
@Before(value = "execution(* com.cntmtech.support.core.*.service.*.create(..))")
public void BeforeCreate(JoinPoint point) throws Exception {
fillDEFieldDefaultValue(point);
}
@Before(value = "execution(* com.cntmtech.support.core.*.service.*.createBatch(..))")
public void BeforeCreateBatch(JoinPoint point) throws Exception {
fillDEFieldDefaultValue(point);
}
/**
* 更新数据切入点
* @param point
* @throws Exception
*/
@Before(value = "execution(* ${pub.getPKGCodeName()}.core.*.service.*.update(..))")
@Before(value = "execution(* com.cntmtech.support.core.*.service.*.update(..))")
public void BeforeUpdate(JoinPoint point) throws Exception {
fillDEFieldDefaultValue(point);
}
@Before(value = "execution(* com.cntmtech.support.core.*.service.*.updateBatch(..))")
public void BeforeUpdateBatch(JoinPoint point) throws Exception {
fillDEFieldDefaultValue(point);
}
/**
* 保存数据切入点
* @param point
* @throws Exception
*/
@Before(value = "execution(* ${pub.getPKGCodeName()}.core.*.service.*.save(..))")
@Before(value = "execution(* com.cntmtech.support.core.*.service.*.save(..))")
public void BeforeSave(JoinPoint point) throws Exception {
checkAction(point);
}
/**
* 判断当前是否为新建操作,若为新建,则填充属性默认值
* @param joinPoint
* @throws Exception
*/
private void checkAction(JoinPoint joinPoint) throws Exception {
Object[] args = joinPoint.getArgs();
if (args.length > 0) {
Object obj = args[0];
String className=obj.getClass().getName();
//获取当前do对象中的属性
DEFieldCacheMap.getFieldMap(className);
//从属性列表中过滤出主键属性
Field keyPSDEField = this.getKeyPSDEField(className);
if(ObjectUtils.isEmpty(keyPSDEField))
return ;
String filename = keyPSDEField.getName();
PropertyDescriptor field = new PropertyDescriptor(filename, obj.getClass());
Method fieldGetMethod = field.getReadMethod();
//获取主键值
Object fieldValue = fieldGetMethod.invoke(obj);
//实体数据主键为空,则为新建操作,填充属性默认值
if(ObjectUtils.isEmpty(fieldValue)){
fillDEFieldDefaultValue(joinPoint);
}
}
fillDEFieldDefaultValue(point);
}
/**
* 获取主键属性
* @param className 实体类名
* @return 主键属性
*/
private Field getKeyPSDEField(String className){
Field keyField =null;
List<Field> fields = DEFieldCacheMap.getFields(className);
for(Field field:fields){
DEField deField=field.getAnnotation(DEField.class);
if(!ObjectUtils.isEmpty(deField) && deField.isKeyField()) {
return field;
}
}
return keyField;
@Before(value = "execution(* com.cntmtech.support.core.*.service.*.saveBatch(..))")
public void BeforeSaveBatch(JoinPoint point) throws Exception {
fillDEFieldDefaultValue(point);
}
/**
......@@ -122,53 +79,56 @@ public class DEFieldDefaultValueAspect
* @throws Exception
*/
public Object fillDEFieldDefaultValue(JoinPoint joinPoint) throws Exception {
Object[] args = joinPoint.getArgs();
if (args.length > 0) {
Object obj = args[0];
String className=obj.getClass().getName();
//获取当前po对象中的属性
DEFieldCacheMap.getFieldMap(className);
//从属性列表中过滤出预置属性
Map<Field, DEField> deFields = this.SearchDEField(className);
//填充预置属性
fillDEField(obj, deFields,joinPoint);
return true;
String actionName=joinPoint.getSignature().getName();
if(obj instanceof EntityBase) {
Map<String, DEField> deFields = DEFieldCacheMap.getDEFields(obj.getClass());
AuthenticationUser curUser = AuthenticationUser.getAuthenticationUser();
String keyField=DEFieldCacheMap.getDEKeyField(obj.getClass());
if(StringUtils.isEmpty(keyField))
return true;
fillDEField((EntityBase)obj, deFields,actionName,curUser,keyField);
}
else if (obj instanceof List) {
Map<String, DEField> deFields = null;
AuthenticationUser curUser = null;
String keyField = "";
for(Object item:(List)obj) {
if(item instanceof EntityBase) {
if(deFields==null) {
deFields = DEFieldCacheMap.getDEFields(obj.getClass());
curUser = AuthenticationUser.getAuthenticationUser();
keyField=DEFieldCacheMap.getDEKeyField(obj.getClass());
if(StringUtils.isEmpty(keyField))
return true;
}
fillDEField((EntityBase)item, deFields,actionName,curUser,keyField);
}
}
}
}
return true;
}
/**
*获取含有@DEField注解的实体属性
* @param className do对象类名
* @return
*/
private Map <Field, DEField> SearchDEField(String className){
List<Field> fields = DEFieldCacheMap.getFields(className);
Map <Field, DEField> deFieldMap =new HashMap<>();
for(Field field:fields){
DEField deField=field.getAnnotation(DEField.class);
if(!ObjectUtils.isEmpty(deField)) {
deFieldMap.put(field,deField);
}
}
return deFieldMap;
}
/**
* 填充系统预置属性
* @param et 当前实体对象
*/
private void fillDEField(Object et, Map<Field, DEField> deFields, JoinPoint joinPoint) throws Exception {
private void fillDEField(EntityBase et, Map<String, DEField> deFields, String actionName,AuthenticationUser curUser,String keyField) throws Exception {
if(deFields.size()==0)
return ;
for (Map.Entry<Field, DEField> entry : deFields.entrySet()) {
if(actionName.toLowerCase().startsWith("save")) {
if(ObjectUtils.isEmpty(et.get(keyField)))
actionName="create";
}
Field deField=entry.getKey();
String fileName=deField.getName();
for (Map.Entry<String, DEField> entry : deFields.entrySet()) {
String fieldname=entry.getKey();
//获取注解
DEField fieldAnnotation=entry.getValue();
//获取默认值类型
......@@ -179,43 +139,35 @@ public class DEFieldDefaultValueAspect
DEPredefinedFieldType predefinedFieldType = fieldAnnotation.preType();
//填充系统默认值
if( deFieldType!= DEFieldDefaultValueType.NONE || (!StringUtils.isEmpty(deFieldDefaultValue)) ){
fillFieldDefaultValue(fileName, deFieldType, deFieldDefaultValue, et , fieldAnnotation) ;
if(actionName.toLowerCase().startsWith("create") && ( deFieldType!= DEFieldDefaultValueType.NONE || (!StringUtils.isEmpty(deFieldDefaultValue)) )){
fillFieldDefaultValue(fieldname, deFieldType, deFieldDefaultValue, et , curUser) ;
}
//填充系统预置属性
if(predefinedFieldType != DEPredefinedFieldType.NONE){
fillPreFieldValue( fileName, predefinedFieldType , et ,joinPoint ,fieldAnnotation);
fillPreFieldValue( fieldname, predefinedFieldType , et ,actionName ,fieldAnnotation.logicval(),curUser);
}
}
}
/**
* 填充属性默认值
* @param fileName 实体属性名
* @param fieldname 实体属性名
* @param deFieldType 默认值类型
* @param deFieldDefaultValue 默认值
* @param et 当前实体对象
* @throws Exception
*/
private void fillFieldDefaultValue(String fileName , DEFieldDefaultValueType deFieldType,String deFieldDefaultValue,Object et ,DEField fieldAnnotation) throws Exception {
AuthenticationUser curUser=AuthenticationUser.getAuthenticationUser();
BeanMap beanMap = BeanMap.create(et);
Object fieldValue = beanMap.get(fileName);
//获取当前所需填充属性的getset方法及字段值
PropertyDescriptor field = new PropertyDescriptor(fileName, et.getClass());
if(org.springframework.util.StringUtils.isEmpty(fieldValue)){
private void fillFieldDefaultValue(String fieldname , DEFieldDefaultValueType deFieldType,String deFieldDefaultValue,EntityBase et ,AuthenticationUser curUser) throws Exception {
Object fieldValue = et.get(fieldname);
if(org.springframework.util.ObjectUtils.isEmpty(fieldValue)){
//填充直接值及其余默认值类型
if( (deFieldType== DEFieldDefaultValueType.NONE && !StringUtils.isEmpty(deFieldDefaultValue)) || (deFieldType != DEFieldDefaultValueType.NONE) ){
switch(deFieldType){
case SESSION:
String sessionField=fieldAnnotation.defaultValue();
if(!StringUtils.isEmpty(sessionField)){
Object sessionFieldValue = curUser.getSessionParams().get(sessionField);
if(!StringUtils.isEmpty(deFieldDefaultValue)){
Object sessionFieldValue = curUser.getSessionParams().get(deFieldDefaultValue.toLowerCase());
if(!ObjectUtils.isEmpty(sessionFieldValue)){
beanMap.put(fileName,sessionFieldValue);
et.set(fieldname,sessionFieldValue);
}
}
break;
......@@ -223,146 +175,94 @@ public class DEFieldDefaultValueAspect
//暂未实现
break;
case UNIQUEID:
String uuid=(new AlternativeJdkIdGenerator()).generateId().toString();
Object objUuId=fieldValueConvert(uuid,field);
beanMap.put(fileName,objUuId);
et.set(fieldname,(new AlternativeJdkIdGenerator()).generateId().toString().replace("-", ""));
break;
case CONTEXT:
//暂未实现
if(!StringUtils.isEmpty(deFieldDefaultValue)){
Object paramFieldValue=et.get(deFieldDefaultValue);
if(!ObjectUtils.isEmpty(paramFieldValue)){
et.set(fieldname,paramFieldValue);
}
}
break;
case PARAM:
String paramField=fieldAnnotation.defaultValue();
if(!StringUtils.isEmpty(paramField)){
Object paramFieldValue=beanMap.get(paramField);
if(!StringUtils.isEmpty(deFieldDefaultValue)){
Object paramFieldValue=et.get(deFieldDefaultValue);
if(!ObjectUtils.isEmpty(paramFieldValue)){
beanMap.put(fileName,paramFieldValue);
et.set(fieldname,paramFieldValue);
}
}
break;
case OPERATOR:
beanMap.put(fileName,curUser.getUserid());
et.set(fieldname,curUser.getUserid());
break;
case OPERATORNAME:
beanMap.put(fileName,curUser.getPersonname());
et.set(fieldname,curUser.getPersonname());
break;
case CURTIME:
beanMap.put(fileName,new Timestamp(new Date().getTime()));
et.set(fieldname,new Timestamp(new Date().getTime()));
break;
case APPDATA:
//暂未实现
break;
case NONE:
Object deFieldDefaultValueObj=fieldValueConvert(deFieldDefaultValue,field);
beanMap.put(fileName,deFieldDefaultValueObj);
et.set(fieldname,deFieldDefaultValue);
break;
}
}
}
}
/**
* 填充系统预置属性
* @param fileName 实体属性名
* @param preFieldType 预置类型
* @param et 当前实体对象
* @param joinPoint 切点
* @param fieldAnnotation 属性注解
* @throws Exception
*/
private void fillPreFieldValue(String fileName , DEPredefinedFieldType preFieldType ,Object et , JoinPoint joinPoint ,DEField fieldAnnotation) throws Exception {
AuthenticationUser curUser=AuthenticationUser.getAuthenticationUser();
BeanMap beanMap = BeanMap.create(et);
//当前操作行为
String actionName=joinPoint.getSignature().getName();
Object fieldValue = beanMap.get(fileName);
//获取当前所需填充属性的getset方法及字段值
PropertyDescriptor field = new PropertyDescriptor(fileName, et.getClass());
private void fillPreFieldValue(String fieldname , DEPredefinedFieldType preFieldType ,EntityBase et , String actionName,String logicValue ,AuthenticationUser curUser) throws Exception {
Object fieldValue = et.get(fieldname);
//为预置属性进行赋值
if( (actionName.equalsIgnoreCase("create") && org.springframework.util.StringUtils.isEmpty(fieldValue) )||
if( actionName.equalsIgnoreCase("create")||
preFieldType== DEPredefinedFieldType.UPDATEDATE|| preFieldType== DEPredefinedFieldType.UPDATEMAN||
preFieldType== DEPredefinedFieldType.UPDATEMANNAME){
switch(preFieldType){//根据注解给预置属性填充值
case CREATEMAN:
beanMap.put(fileName,curUser.getUserid());
break;
case CREATEMANNAME:
beanMap.put(fileName,curUser.getPersonname());
break;
case UPDATEMAN:
beanMap.put(fileName,curUser.getUserid());
break;
case UPDATEMANNAME:
beanMap.put(fileName,curUser.getPersonname());
break;
case CREATEDATE:
beanMap.put(fileName,new Timestamp(new Date().getTime()));
break;
case UPDATEDATE:
beanMap.put(fileName,new Timestamp(new Date().getTime()));
break;
case ORGID:
beanMap.put(fileName,curUser.getOrgid());
break;
case ORGNAME:
beanMap.put(fileName,curUser.getOrgname());
break;
case ORGSECTORID:
beanMap.put(fileName,curUser.getMdeptid());
break;
case ORGSECTORNAME:
beanMap.put(fileName,curUser.getMdeptname());
break;
case LOGICVALID:
String logicValue=fieldAnnotation.logicval();
Object objLogicValue=fieldValueConvert(logicValue,field);
if(!StringUtils.isEmpty(objLogicValue)){
beanMap.put(fileName,objLogicValue);
}
break;
}
switch(preFieldType){//根据注解给预置属性填充值
case CREATEMAN:
et.set(fieldname,curUser.getUserid());
break;
case CREATEMANNAME:
et.set(fieldname,curUser.getPersonname());
break;
case UPDATEMAN:
et.set(fieldname,curUser.getUserid());
break;
case UPDATEMANNAME:
et.set(fieldname,curUser.getPersonname());
break;
case CREATEDATE:
et.set(fieldname,new Timestamp(new Date().getTime()));
break;
case UPDATEDATE:
et.set(fieldname,new Timestamp(new Date().getTime()));
break;
case ORGID:
if(org.springframework.util.StringUtils.isEmpty(fieldValue))
et.set(fieldname,curUser.getOrgid());
break;
case ORGNAME:
if(org.springframework.util.StringUtils.isEmpty(fieldValue))
et.set(fieldname,curUser.getOrgname());
break;
case ORGSECTORID:
if(org.springframework.util.StringUtils.isEmpty(fieldValue))
et.set(fieldname,curUser.getMdeptid());
break;
case ORGSECTORNAME:
if(org.springframework.util.StringUtils.isEmpty(fieldValue))
et.set(fieldname,curUser.getMdeptname());
break;
case LOGICVALID:
if(!StringUtils.isEmpty(logicValue)){
logicValue="1";
}
et.set(fieldname,logicValue);
break;
}
}
}
/**
* 值类型转换
* @param fieldValue
* @param field
*/
private Object fieldValueConvert(String fieldValue,PropertyDescriptor field){
Object resultValue=fieldValue;
String targetType=field.getPropertyType().getSimpleName();
if(targetType.equals("Boolean")){
resultValue=Boolean.valueOf(fieldValue);
}
else if(targetType.equals("Character")){
resultValue=fieldValue.toCharArray();
}
else if(targetType.equals("Byte")){
resultValue=Byte.valueOf(fieldValue);
}
else if(targetType.equals("Short")){
resultValue=Short.valueOf(fieldValue);
}
else if(targetType.equals("Integer")){
resultValue= Integer.valueOf(fieldValue);
}
else if(targetType.equals("Long")){
resultValue=Long.valueOf(fieldValue);
}
else if(targetType.equals("Float")){
resultValue= Float.valueOf(fieldValue);
}
else if(targetType.equals("Double")){
resultValue= Double.valueOf(fieldValue);
}
return resultValue;
}
}
......@@ -5,8 +5,8 @@ package ${pub.getPKGCodeName()}.util.helper;
import ${pub.getPKGCodeName()}.util.annotation.DEField;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Hashtable;
......@@ -23,6 +23,10 @@ public class DEFieldCacheMap {
private static Hashtable<String, Hashtable<String,String>> cacheKey = new Hashtable<>();
private static Hashtable<String, Hashtable<String,DEField>> cacheDEField = new Hashtable<>();
private static Hashtable<String, String> cacheDEKeyField = new Hashtable<>();
private static Object objLock1=new Object();
/**
......@@ -42,15 +46,24 @@ public class DEFieldCacheMap {
Hashtable<String,Field> result = new Hashtable<String,Field>();
List<Field> list=new ArrayList<Field>();
Hashtable<String,String> keys=new Hashtable<String,String>();
Hashtable<String,DEField> defields=new Hashtable<>();
Hashtable<String,String> dekeyfields=new Hashtable<>();
Field[] fields=clazz.getDeclaredFields();
for(Field field:fields){
result.put(field.getName(),field);
list.add(field);
keys.put(field.getName().toLowerCase(),field.getName());
DEField deField=field.getAnnotation(DEField.class);
if(!ObjectUtils.isEmpty(deField)) {
defields.put(field.getName(),deField);
if(deField.isKeyField())
cacheDEKeyField.put(className,field.getName());
}
}
cacheMap.put(className, result);
cacheList.put(className,list);
cacheKey.put(className,keys);
cacheDEField.put(className,defields);
return result;
}
}
......@@ -70,6 +83,41 @@ public class DEFieldCacheMap {
}
}
/**
* 从缓存中查询实体对象属性集合
* @param
* @return
*/
public static <T> Hashtable<String,DEField> getDEFields(Class<T> clazz) {
String className=clazz.getName();
if(className.indexOf("_$")>0)
className=className.substring(0, className.lastIndexOf("_$"));
if(cacheDEField.containsKey(className))
return cacheDEField.get(className);
else{
DEFieldCacheMap.getFieldMap(clazz);
return cacheDEField.get(className);
}
}
/**
* 从缓存中查询实体对象主键
* @param
* @return
*/
public static <T> String getDEKeyField(Class<T> clazz) {
String className=clazz.getName();
if(className.indexOf("_$")>0)
className=className.substring(0, className.lastIndexOf("_$"));
if(cacheDEKeyField.containsKey(className))
return cacheDEKeyField.get(className);
else{
DEFieldCacheMap.getFieldMap(clazz);
return cacheDEKeyField.get(className);
}
}
/**
* 从缓存中查询实体对象属性列表
* @param
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册