Resolving HHH-5588: Improve support for entityNames in envers.

Adding methods for knowing if an entityName is marked to be resolved by core (@Audited(targetAuditMode='NOT_AUDITED')
	isEntityNameNotAudited()

Adding method to resolve the entityName for an entity retrieved previously by envers.
	getEntityName()
This commit is contained in:
Hernan 2010-11-15 00:11:49 -02:00 committed by adamw
parent f3feba66ce
commit c032502969
2 changed files with 92 additions and 7 deletions

View File

@ -28,13 +28,14 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.hibernate.envers.exception.EnversException;
import org.hibernate.envers.exception.NotAuditedException; import org.hibernate.envers.exception.NotAuditedException;
import org.hibernate.envers.exception.RevisionDoesNotExistException; import org.hibernate.envers.exception.RevisionDoesNotExistException;
import org.hibernate.envers.query.AuditQueryCreator; import org.hibernate.envers.query.AuditQueryCreator;
/** /**
* @author Adam Warski (adam at warski dot org) * @author Adam Warski (adam at warski dot org)
* @author Hern<EFBFBD>n Chanfreau * @author Hern&aacute;n Chanfreau
*/ */
public interface AuditReader { public interface AuditReader {
/** /**
@ -177,10 +178,58 @@ public interface AuditReader {
AuditQueryCreator createQuery(); AuditQueryCreator createQuery();
/** /**
* Checks if the entityClass was configured to be audited. * Checks if the entityClass was configured to be audited. Calling
* isEntityNameAudited() with the string of the class name will return the
* same value.
* *
* @param entityClass Class of the entity asking for audit support * @param entityClass
* Class of the entity asking for audit support
* @return true if the entityClass is audited. * @return true if the entityClass is audited.
*/ */
boolean isEntityClassAudited(Class<?> entityClass); boolean isEntityClassAudited(Class<?> entityClass);
/**
* Checks if the entityName was configured to be audited.
*
* @param entityClass
* EntityName of the entity asking for audit support.
* @return true if the entityName is audited.
*/
boolean isEntityNameAudited(String entityName);
/**
* Checks if the entityClass was configured to be a audited in a relation
* with the targetAuditMode as NOT_AUDITED . Calling
* isEntityNameNotAudited() with the string of the class name will return
* the same value.
*
* @param entityClass
* Class of the entity asking for
* @Audit(targetAuditMode=...NOT_AUDITED) support
* @return true if the entityClass is marked in a relation as NOT_AUDITED.
*/
boolean isEntityClassNotAudited(Class<?> entityClass);
/**
* Checks if the entityName was configured be a audited in a relation
* with the targetAuditMode as NOT_AUDITED .
*
* @param entityClass
* Class of the entity asking for
* @Audit(targetAuditMode=...NOT_AUDITED) support
* @return true if the entityClass is marked in a relation as NOT_AUDITED.
*/
boolean isEntityNameNotAudited(String entityName);
/**
*
* @param entity
* that was obtained previously from the same AuditReader.
*
* @return the entityName for the given entity, null in case the entity is
* not associated with this AuditReader instance.
*/
String getEntityName(Object primaryKey, Number revision, Object entity)
throws EnversException;
} }

View File

@ -42,16 +42,18 @@ import org.hibernate.Session;
import org.hibernate.engine.SessionImplementor; import org.hibernate.engine.SessionImplementor;
import org.hibernate.envers.configuration.AuditConfiguration; import org.hibernate.envers.configuration.AuditConfiguration;
import org.hibernate.envers.exception.AuditException; import org.hibernate.envers.exception.AuditException;
import org.hibernate.envers.exception.EnversException;
import org.hibernate.envers.exception.NotAuditedException; import org.hibernate.envers.exception.NotAuditedException;
import org.hibernate.envers.exception.RevisionDoesNotExistException; import org.hibernate.envers.exception.RevisionDoesNotExistException;
import org.hibernate.envers.query.AuditEntity; import org.hibernate.envers.query.AuditEntity;
import org.hibernate.envers.query.AuditQueryCreator; import org.hibernate.envers.query.AuditQueryCreator;
import org.hibernate.envers.synchronization.AuditProcess; import org.hibernate.envers.synchronization.AuditProcess;
import org.hibernate.event.EventSource; import org.hibernate.event.EventSource;
import org.hibernate.proxy.HibernateProxy;
/** /**
* @author Adam Warski (adam at warski dot org) * @author Adam Warski (adam at warski dot org)
* @author Hern<EFBFBD>n Chanfreau * @author Hern&aacute;n Chanfreau
*/ */
public class AuditReaderImpl implements AuditReaderImplementor { public class AuditReaderImpl implements AuditReaderImplementor {
private final AuditConfiguration verCfg; private final AuditConfiguration verCfg;
@ -256,10 +258,44 @@ public class AuditReaderImpl implements AuditReaderImplementor {
} }
public boolean isEntityClassAudited(Class<?> entityClass) { public boolean isEntityClassAudited(Class<?> entityClass) {
checkNotNull(entityClass, "Entity class"); return this.isEntityNameAudited(entityClass.getName());
checkSession(); }
String entityName = entityClass.getName();
public boolean isEntityNameAudited(String entityName) {
checkNotNull(entityName, "Entity name");
checkSession();
return (verCfg.getEntCfg().isVersioned(entityName)); return (verCfg.getEntCfg().isVersioned(entityName));
} }
public boolean isEntityClassNotAudited(Class<?> entityClass) {
return this.isEntityNameNotAudited(entityClass.getName());
}
public boolean isEntityNameNotAudited(String entityName) {
checkNotNull(entityName, "Entity name");
checkSession();
return (verCfg.getEntCfg().isNotAudited(entityName));
}
public String getEntityName(Object primaryKey, Number revision ,Object entity) throws EnversException{
checkNotNull(primaryKey, "Primary key");
checkNotNull(revision, "Entity revision");
checkPositive(revision, "Entity revision");
checkNotNull(entity, "Entity");
checkSession();
// Unwrap if necessary
if(entity instanceof HibernateProxy) {
entity = ((HibernateProxy)entity).getHibernateLazyInitializer().getImplementation();
}
if(firstLevelCache.containsEntityName(primaryKey, revision, entity)) {
// it´s on envers FLC!
return firstLevelCache.getFromEntityNameCache(primaryKey, revision, entity);
} else {
throw new EnversException(
"Can´t resolve entityName for historic entity. The id, revision and entity is not on envers first level cache.");
}
}
} }