DEFieldDefaultValueAspect.java 11.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
package cn.ibizlab.util.aspect;

import cn.ibizlab.util.annotation.DEField;
import cn.ibizlab.util.domain.EntityBase;
import cn.ibizlab.util.enums.DEFieldDefaultValueType;
import cn.ibizlab.util.enums.DEPredefinedFieldType;
import cn.ibizlab.util.helper.DEFieldCacheMap;
import cn.ibizlab.util.security.AuthenticationUser;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
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.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 实体属性默认值切面,只有新建(Create)时才会填充默认值
 */
@Aspect
@Order(50)
@Component
public class DEFieldDefaultValueAspect
{
    /**
     * 新建数据切入点
     * @param point
     * @throws Exception
     */
    @Before(value = "execution(* cn.ibizlab.core.*.service.*.create(..))")
    public void BeforeCreate(JoinPoint point) throws Exception {
        fillDEFieldDefaultValue(point);
    }
    @Before(value = "execution(* cn.ibizlab.core.*.service.*.createBatch(..))")
    public void BeforeCreateBatch(JoinPoint point) throws Exception {
        fillDEFieldDefaultValue(point);
    }

    /**
     * 更新数据切入点
     * @param point
     * @throws Exception
     */
    @Before(value = "execution(* cn.ibizlab.core.*.service.*.update(..))")
    public void BeforeUpdate(JoinPoint point) throws Exception {
        fillDEFieldDefaultValue(point);
    }
    @Before(value = "execution(* cn.ibizlab.core.*.service.*.updateBatch(..))")
    public void BeforeUpdateBatch(JoinPoint point) throws Exception {
        fillDEFieldDefaultValue(point);
    }

    /**
     * 保存数据切入点
     * @param point
     * @throws Exception
     */
    @Before(value = "execution(* cn.ibizlab.core.*.service.*.save(..))")
    public void BeforeSave(JoinPoint point) throws Exception {
        fillDEFieldDefaultValue(point);
    }
    @Before(value = "execution(* cn.ibizlab.core.*.service.*.saveBatch(..))")
    public void BeforeSaveBatch(JoinPoint point) throws Exception {
        fillDEFieldDefaultValue(point);
    }

    /**
     * 填充属性默认值
     * @param joinPoint
     * @return
     * @throws Exception
     */
    public Object fillDEFieldDefaultValue(JoinPoint joinPoint) throws Exception {
        Object[] args = joinPoint.getArgs();
        if (args.length > 0) {
            Object obj = args[0];
82
            String actionName = joinPoint.getSignature().getName();
83 84 85
            if(obj instanceof EntityBase) {
                Map<String, DEField> deFields = DEFieldCacheMap.getDEFields(obj.getClass());
                AuthenticationUser curUser = AuthenticationUser.getAuthenticationUser();
86
                String keyField = DEFieldCacheMap.getDEKeyField(obj.getClass());
87 88 89
                if(StringUtils.isEmpty(keyField)) {
                    return true;
                }
90
                fillDEField((EntityBase)obj, deFields, actionName, curUser, keyField);
91 92 93 94 95
            }
            else if (obj instanceof List) {
                Map<String, DEField> deFields = null;
                AuthenticationUser curUser = null;
                String keyField = "";
96
                for(Object item : (List)obj) {
97
                    if(item instanceof EntityBase) {
98
                        if(deFields == null) {
99 100
                            deFields = DEFieldCacheMap.getDEFields(item.getClass());
                            curUser = AuthenticationUser.getAuthenticationUser();
101
                            keyField = DEFieldCacheMap.getDEKeyField(item.getClass());
102 103 104 105
                            if(StringUtils.isEmpty(keyField)) {
                                return true;
                            }
                        }
106
                        fillDEField((EntityBase)item, deFields, actionName, curUser, keyField);
107 108 109 110 111 112 113 114 115 116 117 118 119
                    }
                }
            }
        }
        return true;
    }



    /**
     * 填充系统预置属性
     * @param et   当前实体对象
     */
120
    private void fillDEField(EntityBase et, Map<String, DEField> deFields, String actionName, AuthenticationUser curUser, String keyField) throws Exception {
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
        if(deFields.size()==0) {
            return;
        }
        if(actionName.toLowerCase().startsWith("save")) {
            if(ObjectUtils.isEmpty(et.get(keyField))) {
                actionName="create";
            }
        }
        for (Map.Entry<String, DEField> entry : deFields.entrySet()) {
            String fieldname=entry.getKey();
            //获取注解
            DEField fieldAnnotation=entry.getValue();
            //获取默认值类型
            DEFieldDefaultValueType deFieldType=fieldAnnotation.defaultValueType();
            //获取属性默认值
            String deFieldDefaultValue = fieldAnnotation.defaultValue();
            //获取预置属性类型
            DEPredefinedFieldType predefinedFieldType = fieldAnnotation.preType();

            //填充系统默认值
141
            if(actionName.toLowerCase().startsWith("create") && (deFieldType!= DEFieldDefaultValueType.NONE  ||  (!StringUtils.isEmpty(deFieldDefaultValue)))) {
142 143 144
                fillFieldDefaultValue(fieldname,  deFieldType,  deFieldDefaultValue,  et , curUser) ;
            }
            //填充系统预置属性
145 146
            if(predefinedFieldType != DEPredefinedFieldType.NONE) {
                fillPreFieldValue(fieldname, predefinedFieldType , et ,actionName ,fieldAnnotation.logicval(),curUser);
147 148 149 150 151 152 153 154 155 156 157 158
            }
        }
    }

    /**
     * 填充属性默认值
     * @param fieldname 实体属性名
     * @param deFieldType 默认值类型
     * @param deFieldDefaultValue 默认值
     * @param et 当前实体对象
     * @throws Exception
     */
159
    private void fillFieldDefaultValue(String fieldname, DEFieldDefaultValueType deFieldType, String deFieldDefaultValue, EntityBase et , AuthenticationUser curUser) throws Exception {
160
        Object fieldValue = et.get(fieldname);
161
        if(org.springframework.util.ObjectUtils.isEmpty(fieldValue)) {
162
            //填充直接值及其余默认值类型
163 164
            if( (deFieldType== DEFieldDefaultValueType.NONE && !StringUtils.isEmpty(deFieldDefaultValue)) || (deFieldType != DEFieldDefaultValueType.NONE)) {
                switch(deFieldType) {
165
                    case SESSION:
166
                        if(!StringUtils.isEmpty(deFieldDefaultValue)) {
167
                            Object sessionFieldValue = curUser.getSessionParams().get(deFieldDefaultValue.toLowerCase());
168 169
                            if(!ObjectUtils.isEmpty(sessionFieldValue)) {
                                et.set(fieldname, sessionFieldValue);
170 171 172 173 174 175 176
                            }
                        }
                        break;
                    case APPLICATION:
                        //暂未实现
                        break;
                    case UNIQUEID:
177
                        et.set(fieldname, (new AlternativeJdkIdGenerator()).generateId().toString().replace("-", ""));
178 179
                        break;
                    case CONTEXT:
180
                        if(!StringUtils.isEmpty(deFieldDefaultValue)) {
181
                            Object paramFieldValue=et.get(deFieldDefaultValue);
182 183
                            if(!ObjectUtils.isEmpty(paramFieldValue)) {
                                et.set(fieldname, paramFieldValue);
184 185 186 187
                            }
                        }
                        break;
                    case PARAM:
188
                        if(!StringUtils.isEmpty(deFieldDefaultValue)) {
189
                            Object paramFieldValue=et.get(deFieldDefaultValue);
190 191
                            if(!ObjectUtils.isEmpty(paramFieldValue)) {
                                et.set(fieldname, paramFieldValue);
192 193 194 195
                            }
                        }
                        break;
                    case OPERATOR:
196
                        et.set(fieldname, curUser.getUserid());
197 198
                        break;
                    case OPERATORNAME:
199
                        et.set(fieldname, curUser.getPersonname());
200 201
                        break;
                    case CURTIME:
202
                        et.set(fieldname, new Timestamp(new Date().getTime()));
203 204 205 206 207
                        break;
                    case APPDATA:
                        //暂未实现
                        break;
                    case NONE:
208
                        et.set(fieldname, deFieldDefaultValue);
209 210 211 212 213 214
                        break;
                }
            }
        }
    }

215
    private void fillPreFieldValue(String fieldname, DEPredefinedFieldType preFieldType, EntityBase et, String actionName, String logicValue, AuthenticationUser curUser) throws Exception {
216 217
        Object fieldValue = et.get(fieldname);
        //为预置属性进行赋值
218
        if(actionName.toLowerCase().startsWith("create") ||
219
                preFieldType== DEPredefinedFieldType.UPDATEDATE|| preFieldType== DEPredefinedFieldType.UPDATEMAN||
220
                preFieldType== DEPredefinedFieldType.UPDATEMANNAME) {
221

222
            switch(preFieldType) {
223
                case CREATEMAN:
224
                    et.set(fieldname, curUser.getUserid());
225 226
                    break;
                case CREATEMANNAME:
227
                    et.set(fieldname, curUser.getPersonname());
228 229
                    break;
                case UPDATEMAN:
230
                    et.set(fieldname, curUser.getUserid());
231 232
                    break;
                case UPDATEMANNAME:
233
                    et.set(fieldname, curUser.getPersonname());
234 235
                    break;
                case CREATEDATE:
236
                    et.set(fieldname, new Timestamp(new Date().getTime()));
237 238
                    break;
                case UPDATEDATE:
239
                    et.set(fieldname, new Timestamp(new Date().getTime()));
240 241
                    break;
                case ORGID:
242 243 244
                    if(org.springframework.util.StringUtils.isEmpty(fieldValue)) {
                        et.set(fieldname, curUser.getOrgid());
                    }
245 246
                    break;
                case ORGNAME:
247 248 249
                    if(org.springframework.util.StringUtils.isEmpty(fieldValue)) {
                        et.set(fieldname, curUser.getOrgname());
                    }
250 251
                    break;
                case ORGSECTORID:
252 253 254
                    if(org.springframework.util.StringUtils.isEmpty(fieldValue)) {
                        et.set(fieldname, curUser.getMdeptid());
                    }
255 256
                    break;
                case ORGSECTORNAME:
257 258 259
                    if(org.springframework.util.StringUtils.isEmpty(fieldValue)) {
                        et.set(fieldname, curUser.getMdeptname());
                    }
260 261
                    break;
                case LOGICVALID:
262
                    if(StringUtils.isEmpty(logicValue)) {
263 264
                        logicValue="1";
                    }
265
                    et.set(fieldname, logicValue);
266 267 268 269 270 271
                    break;
            }
        }
    }

}