提交 f56f42cd 编写于 作者: ibizdev's avatar ibizdev

ibizdev提交

上级 c7abe26b
package cn.ibizlab.util.aspect; package cn.ibizlab.util.aspect;
import cn.ibizlab.util.annotation.DEField; import cn.ibizlab.util.annotation.DEField;
import cn.ibizlab.util.domain.EntityBase;
import cn.ibizlab.util.enums.DEFieldDefaultValueType; import cn.ibizlab.util.enums.DEFieldDefaultValueType;
import cn.ibizlab.util.enums.DEPredefinedFieldType; import cn.ibizlab.util.enums.DEPredefinedFieldType;
import cn.ibizlab.util.helper.DEFieldCacheMap; import cn.ibizlab.util.helper.DEFieldCacheMap;
...@@ -8,18 +9,13 @@ import cn.ibizlab.util.security.AuthenticationUser; ...@@ -8,18 +9,13 @@ import cn.ibizlab.util.security.AuthenticationUser;
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Before;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.AlternativeJdkIdGenerator; import org.springframework.util.AlternativeJdkIdGenerator;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils; 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.sql.Timestamp;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -36,80 +32,41 @@ public class DEFieldDefaultValueAspect ...@@ -36,80 +32,41 @@ public class DEFieldDefaultValueAspect
* @param point * @param point
* @throws Exception * @throws Exception
*/ */
@Before(value = "execution(* cn.ibizlab.core.*.service.*.create(..))") @Before(value = "execution(* com.cntmtech.support.core.*.service.*.create(..))")
public void BeforeCreate(JoinPoint point) throws Exception { public void BeforeCreate(JoinPoint point) throws Exception {
fillDEFieldDefaultValue(point); fillDEFieldDefaultValue(point);
} }
@Before(value = "execution(* com.cntmtech.support.core.*.service.*.createBatch(..))")
public void BeforeCreateBatch(JoinPoint point) throws Exception {
fillDEFieldDefaultValue(point);
}
/** /**
* 更新数据切入点 * 更新数据切入点
* @param point * @param point
* @throws Exception * @throws Exception
*/ */
@Before(value = "execution(* cn.ibizlab.core.*.service.*.update(..))") @Before(value = "execution(* com.cntmtech.support.core.*.service.*.update(..))")
public void BeforeUpdate(JoinPoint point) throws Exception { public void BeforeUpdate(JoinPoint point) throws Exception {
fillDEFieldDefaultValue(point); fillDEFieldDefaultValue(point);
} }
@Before(value = "execution(* com.cntmtech.support.core.*.service.*.updateBatch(..))")
public void BeforeUpdateBatch(JoinPoint point) throws Exception {
fillDEFieldDefaultValue(point);
}
/** /**
* 保存数据切入点 * 保存数据切入点
* @param point * @param point
* @throws Exception * @throws Exception
*/ */
@Before(value = "execution(* cn.ibizlab.core.*.service.*.save(..))") @Before(value = "execution(* com.cntmtech.support.core.*.service.*.save(..))")
public void BeforeSave(JoinPoint point) throws Exception { public void BeforeSave(JoinPoint point) throws Exception {
checkAction(point); fillDEFieldDefaultValue(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);
}
}
} }
@Before(value = "execution(* com.cntmtech.support.core.*.service.*.saveBatch(..))")
/** public void BeforeSaveBatch(JoinPoint point) throws Exception {
* 获取主键属性 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;
} }
/** /**
...@@ -119,53 +76,56 @@ public class DEFieldDefaultValueAspect ...@@ -119,53 +76,56 @@ public class DEFieldDefaultValueAspect
* @throws Exception * @throws Exception
*/ */
public Object fillDEFieldDefaultValue(JoinPoint joinPoint) throws Exception { public Object fillDEFieldDefaultValue(JoinPoint joinPoint) throws Exception {
Object[] args = joinPoint.getArgs(); Object[] args = joinPoint.getArgs();
if (args.length > 0) { if (args.length > 0) {
Object obj = args[0]; Object obj = args[0];
String className=obj.getClass().getName(); String actionName=joinPoint.getSignature().getName();
//获取当前po对象中的属性 if(obj instanceof EntityBase) {
DEFieldCacheMap.getFieldMap(className); Map<String, DEField> deFields = DEFieldCacheMap.getDEFields(obj.getClass());
//从属性列表中过滤出预置属性 AuthenticationUser curUser = AuthenticationUser.getAuthenticationUser();
Map<Field, DEField> deFields = this.SearchDEField(className); String keyField=DEFieldCacheMap.getDEKeyField(obj.getClass());
//填充预置属性 if(StringUtils.isEmpty(keyField))
fillDEField(obj, deFields,joinPoint); return true;
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; 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 当前实体对象 * @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) if(deFields.size()==0)
return ; 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(); for (Map.Entry<String, DEField> entry : deFields.entrySet()) {
String fileName=deField.getName(); String fieldname=entry.getKey();
//获取注解 //获取注解
DEField fieldAnnotation=entry.getValue(); DEField fieldAnnotation=entry.getValue();
//获取默认值类型 //获取默认值类型
...@@ -176,43 +136,35 @@ public class DEFieldDefaultValueAspect ...@@ -176,43 +136,35 @@ public class DEFieldDefaultValueAspect
DEPredefinedFieldType predefinedFieldType = fieldAnnotation.preType(); DEPredefinedFieldType predefinedFieldType = fieldAnnotation.preType();
//填充系统默认值 //填充系统默认值
if( deFieldType!= DEFieldDefaultValueType.NONE || (!StringUtils.isEmpty(deFieldDefaultValue)) ){ if(actionName.toLowerCase().startsWith("create") && ( deFieldType!= DEFieldDefaultValueType.NONE || (!StringUtils.isEmpty(deFieldDefaultValue)) )){
fillFieldDefaultValue(fileName, deFieldType, deFieldDefaultValue, et , fieldAnnotation) ; fillFieldDefaultValue(fieldname, deFieldType, deFieldDefaultValue, et , curUser) ;
} }
//填充系统预置属性 //填充系统预置属性
if(predefinedFieldType != DEPredefinedFieldType.NONE){ 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 deFieldType 默认值类型
* @param deFieldDefaultValue 默认值 * @param deFieldDefaultValue 默认值
* @param et 当前实体对象 * @param et 当前实体对象
* @throws Exception * @throws Exception
*/ */
private void fillFieldDefaultValue(String fileName , DEFieldDefaultValueType deFieldType,String deFieldDefaultValue,Object et ,DEField fieldAnnotation) throws Exception { private void fillFieldDefaultValue(String fieldname , DEFieldDefaultValueType deFieldType,String deFieldDefaultValue,EntityBase et ,AuthenticationUser curUser) throws Exception {
Object fieldValue = et.get(fieldname);
AuthenticationUser curUser=AuthenticationUser.getAuthenticationUser(); if(org.springframework.util.ObjectUtils.isEmpty(fieldValue)){
BeanMap beanMap = BeanMap.create(et);
Object fieldValue = beanMap.get(fileName);
//获取当前所需填充属性的get、set方法及字段值
PropertyDescriptor field = new PropertyDescriptor(fileName, et.getClass());
if(org.springframework.util.StringUtils.isEmpty(fieldValue)){
//填充直接值及其余默认值类型 //填充直接值及其余默认值类型
if( (deFieldType== DEFieldDefaultValueType.NONE && !StringUtils.isEmpty(deFieldDefaultValue)) || (deFieldType != DEFieldDefaultValueType.NONE) ){ if( (deFieldType== DEFieldDefaultValueType.NONE && !StringUtils.isEmpty(deFieldDefaultValue)) || (deFieldType != DEFieldDefaultValueType.NONE) ){
switch(deFieldType){ switch(deFieldType){
case SESSION: case SESSION:
String sessionField=fieldAnnotation.defaultValue(); if(!StringUtils.isEmpty(deFieldDefaultValue)){
if(!StringUtils.isEmpty(sessionField)){ Object sessionFieldValue = curUser.getSessionParams().get(deFieldDefaultValue.toLowerCase());
Object sessionFieldValue = curUser.getSessionParams().get(sessionField);
if(!ObjectUtils.isEmpty(sessionFieldValue)){ if(!ObjectUtils.isEmpty(sessionFieldValue)){
beanMap.put(fileName,sessionFieldValue); et.set(fieldname,sessionFieldValue);
} }
} }
break; break;
...@@ -220,146 +172,94 @@ public class DEFieldDefaultValueAspect ...@@ -220,146 +172,94 @@ public class DEFieldDefaultValueAspect
//暂未实现 //暂未实现
break; break;
case UNIQUEID: case UNIQUEID:
String uuid=(new AlternativeJdkIdGenerator()).generateId().toString(); et.set(fieldname,(new AlternativeJdkIdGenerator()).generateId().toString().replace("-", ""));
Object objUuId=fieldValueConvert(uuid,field);
beanMap.put(fileName,objUuId);
break; break;
case CONTEXT: case CONTEXT:
//暂未实现 if(!StringUtils.isEmpty(deFieldDefaultValue)){
Object paramFieldValue=et.get(deFieldDefaultValue);
if(!ObjectUtils.isEmpty(paramFieldValue)){
et.set(fieldname,paramFieldValue);
}
}
break; break;
case PARAM: case PARAM:
String paramField=fieldAnnotation.defaultValue(); if(!StringUtils.isEmpty(deFieldDefaultValue)){
if(!StringUtils.isEmpty(paramField)){ Object paramFieldValue=et.get(deFieldDefaultValue);
Object paramFieldValue=beanMap.get(paramField);
if(!ObjectUtils.isEmpty(paramFieldValue)){ if(!ObjectUtils.isEmpty(paramFieldValue)){
beanMap.put(fileName,paramFieldValue); et.set(fieldname,paramFieldValue);
} }
} }
break; break;
case OPERATOR: case OPERATOR:
beanMap.put(fileName,curUser.getUserid()); et.set(fieldname,curUser.getUserid());
break; break;
case OPERATORNAME: case OPERATORNAME:
beanMap.put(fileName,curUser.getPersonname()); et.set(fieldname,curUser.getPersonname());
break; break;
case CURTIME: case CURTIME:
beanMap.put(fileName,new Timestamp(new Date().getTime())); et.set(fieldname,new Timestamp(new Date().getTime()));
break; break;
case APPDATA: case APPDATA:
//暂未实现 //暂未实现
break; break;
case NONE: case NONE:
Object deFieldDefaultValueObj=fieldValueConvert(deFieldDefaultValue,field); et.set(fieldname,deFieldDefaultValue);
beanMap.put(fileName,deFieldDefaultValueObj);
break; break;
} }
} }
} }
} }
/** private void fillPreFieldValue(String fieldname , DEPredefinedFieldType preFieldType ,EntityBase et , String actionName,String logicValue ,AuthenticationUser curUser) throws Exception {
* 填充系统预置属性 Object fieldValue = et.get(fieldname);
* @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);
//获取当前所需填充属性的get、set方法及字段值
PropertyDescriptor field = new PropertyDescriptor(fileName, et.getClass());
//为预置属性进行赋值 //为预置属性进行赋值
if( (actionName.equalsIgnoreCase("create") && org.springframework.util.StringUtils.isEmpty(fieldValue) )|| if( actionName.equalsIgnoreCase("create")||
preFieldType== DEPredefinedFieldType.UPDATEDATE|| preFieldType== DEPredefinedFieldType.UPDATEMAN|| preFieldType== DEPredefinedFieldType.UPDATEDATE|| preFieldType== DEPredefinedFieldType.UPDATEMAN||
preFieldType== DEPredefinedFieldType.UPDATEMANNAME){ preFieldType== DEPredefinedFieldType.UPDATEMANNAME){
switch(preFieldType){//根据注解给预置属性填充值 switch(preFieldType){//根据注解给预置属性填充值
case CREATEMAN: case CREATEMAN:
beanMap.put(fileName,curUser.getUserid()); et.set(fieldname,curUser.getUserid());
break; break;
case CREATEMANNAME: case CREATEMANNAME:
beanMap.put(fileName,curUser.getPersonname()); et.set(fieldname,curUser.getPersonname());
break; break;
case UPDATEMAN: case UPDATEMAN:
beanMap.put(fileName,curUser.getUserid()); et.set(fieldname,curUser.getUserid());
break; break;
case UPDATEMANNAME: case UPDATEMANNAME:
beanMap.put(fileName,curUser.getPersonname()); et.set(fieldname,curUser.getPersonname());
break; break;
case CREATEDATE: case CREATEDATE:
beanMap.put(fileName,new Timestamp(new Date().getTime())); et.set(fieldname,new Timestamp(new Date().getTime()));
break; break;
case UPDATEDATE: case UPDATEDATE:
beanMap.put(fileName,new Timestamp(new Date().getTime())); et.set(fieldname,new Timestamp(new Date().getTime()));
break; break;
case ORGID: case ORGID:
beanMap.put(fileName,curUser.getOrgid()); if(org.springframework.util.StringUtils.isEmpty(fieldValue))
break; et.set(fieldname,curUser.getOrgid());
case ORGNAME: break;
beanMap.put(fileName,curUser.getOrgname()); case ORGNAME:
break; if(org.springframework.util.StringUtils.isEmpty(fieldValue))
case ORGSECTORID: et.set(fieldname,curUser.getOrgname());
beanMap.put(fileName,curUser.getMdeptid()); break;
break; case ORGSECTORID:
case ORGSECTORNAME: if(org.springframework.util.StringUtils.isEmpty(fieldValue))
beanMap.put(fileName,curUser.getMdeptname()); et.set(fieldname,curUser.getMdeptid());
break; break;
case LOGICVALID: case ORGSECTORNAME:
String logicValue=fieldAnnotation.logicval(); if(org.springframework.util.StringUtils.isEmpty(fieldValue))
Object objLogicValue=fieldValueConvert(logicValue,field); et.set(fieldname,curUser.getMdeptname());
if(!StringUtils.isEmpty(objLogicValue)){ break;
beanMap.put(fileName,objLogicValue); case LOGICVALID:
} if(!StringUtils.isEmpty(logicValue)){
break; 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;
}
} }
...@@ -2,8 +2,8 @@ package cn.ibizlab.util.helper; ...@@ -2,8 +2,8 @@ package cn.ibizlab.util.helper;
import cn.ibizlab.util.annotation.DEField; import cn.ibizlab.util.annotation.DEField;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Hashtable; import java.util.Hashtable;
...@@ -20,6 +20,10 @@ public class DEFieldCacheMap { ...@@ -20,6 +20,10 @@ public class DEFieldCacheMap {
private static Hashtable<String, Hashtable<String,String>> cacheKey = new Hashtable<>(); 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(); private static Object objLock1=new Object();
/** /**
...@@ -39,15 +43,24 @@ public class DEFieldCacheMap { ...@@ -39,15 +43,24 @@ public class DEFieldCacheMap {
Hashtable<String,Field> result = new Hashtable<String,Field>(); Hashtable<String,Field> result = new Hashtable<String,Field>();
List<Field> list=new ArrayList<Field>(); List<Field> list=new ArrayList<Field>();
Hashtable<String,String> keys=new Hashtable<String,String>(); Hashtable<String,String> keys=new Hashtable<String,String>();
Hashtable<String,DEField> defields=new Hashtable<>();
Hashtable<String,String> dekeyfields=new Hashtable<>();
Field[] fields=clazz.getDeclaredFields(); Field[] fields=clazz.getDeclaredFields();
for(Field field:fields){ for(Field field:fields){
result.put(field.getName(),field); result.put(field.getName(),field);
list.add(field); list.add(field);
keys.put(field.getName().toLowerCase(),field.getName()); 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); cacheMap.put(className, result);
cacheList.put(className,list); cacheList.put(className,list);
cacheKey.put(className,keys); cacheKey.put(className,keys);
cacheDEField.put(className,defields);
return result; return result;
} }
} }
...@@ -67,6 +80,41 @@ public class DEFieldCacheMap { ...@@ -67,6 +80,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 * @param
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册