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();
|
}
|
}
|