com.lushapp.core.HibernateAspectInterceptor.java Source code

Java tutorial

Introduction

Here is the source code for com.lushapp.core.HibernateAspectInterceptor.java

Source

/**
 *  Copyright (c) 2014 http://www.lushapp.wang
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.lushapp.core;

import com.lushapp.core.security.SecurityUtils;
import com.lushapp.core.security.SessionInfo;

import org.apache.log4j.Logger;
import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;

import java.io.Serializable;
import java.util.Date;

/**
 * Hibernate .
 * 
 * @author honey.zhao@aliyun.com  
 * @date 2014-10-21 ?12:30:54
 * 
 */
public class HibernateAspectInterceptor extends EmptyInterceptor {
    private static final Logger logger = Logger.getLogger(HibernateAspectInterceptor.class);
    private static final long serialVersionUID = 1L;

    @Override
    public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
        logger.debug("onSave");
        SessionInfo sessionInfo = null;
        try {
            sessionInfo = SecurityUtils.getCurrentSessionInfo();
            if (sessionInfo == null) {
                logger.warn("session?.");
                return true;
            }
        } catch (Exception e) {
            return true;
        }
        try {
            // ?
            for (int index = 0; index < propertyNames.length; index++) {
                if ("createUser".equals(propertyNames[index])) {
                    /* "??" */
                    if (state[index] == null) {
                        state[index] = sessionInfo.getLoginName();
                    }
                    continue;
                }
                if ("createTime".equals(propertyNames[index])) {
                    /* "" */
                    if (state[index] == null) {
                        state[index] = new Date();
                    }
                    continue;
                }
            }
        } catch (Exception e) {
            return false;
        }

        return true;
    }

    @Override
    public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState,
            String[] propertyNames, Type[] types) {
        logger.debug("onFlushDirty");
        SessionInfo sessionInfo = null;
        try {
            sessionInfo = SecurityUtils.getCurrentSessionInfo();
            if (sessionInfo == null) {
                logger.warn("session?.");
                return true;
            }
        } catch (Exception e) {
            return true;
        }
        try {
            // ?
            for (int index = 0; index < propertyNames.length; index++) {
                if ("updateUser".equals(propertyNames[index])) {
                    /* "??" */
                    currentState[index] = sessionInfo.getLoginName();
                    continue;
                }
                if ("updateTime".equals(propertyNames[index])) {
                    /* "" */
                    currentState[index] = new Date();
                    continue;
                }
            }
        } catch (Exception e) {
            return false;
        }

        return true;
    }
}