com.webbfontaine.valuewebb.model.envers.AttDocsBytesLoader.java Source code

Java tutorial

Introduction

Here is the source code for com.webbfontaine.valuewebb.model.envers.AttDocsBytesLoader.java

Source

package com.webbfontaine.valuewebb.model.envers;

import com.webbfontaine.valuewebb.model.TtDoc;
import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;

import java.io.Serializable;
import java.util.Arrays;

/**
 * Copyrights 2002-2014 Webb Fontaine
 * This software is the proprietary information of Webb Fontaine.
 * Its use is subject to License terms.
 * Developer: nigiyan
 * Date: 16/05/2014
 */

// Loads attached file before Audited entity initialized. ELL-1559.
// relates to https://hibernate.atlassian.net/browse/HHH-5255 but connected here with envers
public class AttDocsBytesLoader extends EmptyInterceptor {
    @Override
    public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState,
            String[] propertyNames, Type[] types) {
        if (TtDoc.class == entity.getClass()) {
            TtDoc ttDoc = (TtDoc) entity;
            ttDoc.getBytes(); // force lazy property to be loaded
            int bytesIndex = Arrays.asList(propertyNames).indexOf("bytes");
            currentState[bytesIndex] = previousState[bytesIndex]; // prev. val. and ttDoc.getBytes() are same since bytes field is not updated once added
        }
        return super.onFlushDirty(entity, id, currentState, previousState, propertyNames, types);
    }
}