OWL ITS + 탐지시스템(인터넷 진흥원)
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
82
package kr.wisestone.owl.domain.interceptor;
 
import kr.wisestone.owl.domain.BaseEntity;
import kr.wisestone.owl.util.ApplicationContextUtil;
import kr.wisestone.owl.util.WebAppUtil;
import org.hibernate.CallbackException;
import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;
import java.io.Serializable;
import java.util.Date;
 
public class AuditLogInterceptor extends EmptyInterceptor {
    private static final long serialVersionUID = 1L;
 
    @Override
    public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState,
            Object[] previousState, String[] propertyNames, Type[] types) throws CallbackException {
        this.setModifyInfoToEntity(entity, currentState, propertyNames);
 
        return true;
    }
    
    @Override
    public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames,
            Type[] types) throws CallbackException {
 
        this.setRegisterInfo(entity, state, propertyNames);
 
        return true;
    }
    
    private void setModifyInfoToEntity(Object entity, Object[] state, String[] propertyNames) {
        if (entity instanceof BaseEntity) {
            Long loginId = this.getUserId();
            if (loginId == null) {
                loginId = Long.valueOf(1);
            }
 
            for (int i = 0; i < propertyNames.length; i++) {
                if ("modifyId".equals(propertyNames[i])) {
                    state[i] = loginId;
                } else if ("modifyDate".equals(propertyNames[i])) {
                    state[i] = new Date();
                }
            }
        }
    }
    
    private void setRegisterInfo(Object entity, Object[] state, String[] propertyNames) {
        if (entity instanceof BaseEntity) {
            Long loginId = this.getUserId();
            if (loginId == null) {
                loginId = Long.valueOf(1);
            }
 
            Date currentDate = new Date();
 
            for (int i = 0; i < propertyNames.length; i++) {
                if ("registerId".equals(propertyNames[i])) {
                    if (state[i] == null) {     // 입력 값이 없을때만 자동 입력
                        state[i] = loginId;
                    }
                } else if ("modifyId".equals(propertyNames[i])) {
                    state[i] = loginId;
                } else if ("registerDate".equals(propertyNames[i])) {
                    state[i] = currentDate;
                } else if ("modifyDate".equals(propertyNames[i])) {
                    state[i] = currentDate;
                }
            }
        }
    }
    
    private Long getUserId() {
        WebAppUtil webAppUtil = ApplicationContextUtil.getBean("webAppUtil", WebAppUtil.class);
        if (webAppUtil == null) {
            return null;
        }
 
        return webAppUtil.getLoginId();
    }
}