Fix for HHH-4646 and HHH-5177

- Read audited properties from subclasses first, then check recursively on superclasses.
- Chech on superclasses only if a class is audited
- Calculate if the "current" class of the recursion is audited at class level.
- Created ComponentAuditedPropertiesReader subclass for AuditedPropertiesReader to use for reading component audited properties inside classes
- Created a protected method in AuditedPropertiesReader, letting the subclass do their job.
- Ordered the code to be more understandable.
This commit is contained in:
Hernán Chanfreau 2010-10-19 13:34:37 -03:00 committed by adamw
parent 873fe754e3
commit b919969497
2 changed files with 125 additions and 41 deletions

View File

@ -30,9 +30,10 @@ import org.hibernate.MappingException;
* filling all the auditing data. * filling all the auditing data.
* @author Adam Warski (adam at warski dot org) * @author Adam Warski (adam at warski dot org)
* @author Erik-Berndt Scheper * @author Erik-Berndt Scheper
* @author Hern&aacut;n Chanfreau
*/ */
public class AuditedPropertiesReader { public class AuditedPropertiesReader {
private final ModificationStore defaultStore; protected final ModificationStore defaultStore;
private final PersistentPropertiesSource persistentPropertiesSource; private final PersistentPropertiesSource persistentPropertiesSource;
private final AuditedPropertiesHolder auditedPropertiesHolder; private final AuditedPropertiesHolder auditedPropertiesHolder;
private final GlobalConfiguration globalCfg; private final GlobalConfiguration globalCfg;
@ -68,7 +69,7 @@ public class AuditedPropertiesReader {
} }
private void readPersistentPropertiesAccess() { private void readPersistentPropertiesAccess() {
Iterator propertyIter = persistentPropertiesSource.getPropertyIterator(); Iterator<Property> propertyIter = persistentPropertiesSource.getPropertyIterator();
while (propertyIter.hasNext()) { while (propertyIter.hasNext()) {
Property property = (Property) propertyIter.next(); Property property = (Property) propertyIter.next();
if ("field".equals(property.getPropertyAccessorName())) { if ("field".equals(property.getPropertyAccessorName())) {
@ -80,48 +81,83 @@ public class AuditedPropertiesReader {
} }
private void addPropertiesFromClass(XClass clazz) { private void addPropertiesFromClass(XClass clazz) {
Audited allClassAudited = clazz.getAnnotation(Audited.class);
//look in the class
addFromProperties(clazz.getDeclaredProperties("field"), "field", fieldAccessedPersistentProperties, allClassAudited);
addFromProperties(clazz.getDeclaredProperties("property"), "property", propertyAccessedPersistentProperties, allClassAudited);
if(allClassAudited != null || !auditedPropertiesHolder.isEmpty()) {
XClass superclazz = clazz.getSuperclass(); XClass superclazz = clazz.getSuperclass();
if (!clazz.isInterface() && !"java.lang.Object".equals(superclazz.getName())) { if (!clazz.isInterface() && !"java.lang.Object".equals(superclazz.getName())) {
addPropertiesFromClass(superclazz); addPropertiesFromClassRec(superclazz);
}
}
} }
addFromProperties(clazz.getDeclaredProperties("field"), "field", fieldAccessedPersistentProperties); private void addPropertiesFromClassRec(XClass clazz) {
addFromProperties(clazz.getDeclaredProperties("property"), "property", propertyAccessedPersistentProperties);
Audited allClassAudited = clazz.getAnnotation(Audited.class);
XClass superclazz = clazz.getSuperclass();
if (!clazz.isInterface() && !"java.lang.Object".equals(superclazz.getName())) {
addPropertiesFromClassRec(superclazz);
} }
private void addFromProperties(Iterable<XProperty> properties, String accessType, Set<String> persistentProperties) { addFromProperties(clazz.getDeclaredProperties("field"), "field", fieldAccessedPersistentProperties, allClassAudited);
addFromProperties(clazz.getDeclaredProperties("property"), "property", propertyAccessedPersistentProperties, allClassAudited);
}
private void addFromProperties(Iterable<XProperty> properties, String accessType, Set<String> persistentProperties, Audited allClassAudited) {
for (XProperty property : properties) { for (XProperty property : properties) {
// If this is not a persistent property, with the same access type as currently checked, // If this is not a persistent property, with the same access type as currently checked,
// it's not audited as well. // it's not audited as well.
if (persistentProperties.contains(property.getName())) { // If the property was already defined by the subclass, is ignored by superclasses
if ((persistentProperties.contains(property.getName()) && (!auditedPropertiesHolder
.contains(property.getName())))) {
Value propertyValue = persistentPropertiesSource.getProperty(property.getName()).getValue(); Value propertyValue = persistentPropertiesSource.getProperty(property.getName()).getValue();
PropertyAuditingData propertyData;
boolean isAudited;
if (propertyValue instanceof Component) { if (propertyValue instanceof Component) {
this.addFromComponentProperty(property, accessType, (Component)propertyValue, allClassAudited);
} else {
this.addFromNotComponentProperty(property, accessType, allClassAudited);
}
}
}
}
private void addFromComponentProperty(XProperty property,
String accessType, Component propertyValue, Audited allClassAudited) {
ComponentAuditingData componentData = new ComponentAuditingData(); ComponentAuditingData componentData = new ComponentAuditingData();
isAudited = fillPropertyData(property, componentData, accessType); boolean isAudited = fillPropertyData(property, componentData, accessType,
allClassAudited);
PersistentPropertiesSource componentPropertiesSource = new ComponentPropertiesSource( PersistentPropertiesSource componentPropertiesSource = new ComponentPropertiesSource(
(Component) propertyValue); (Component) propertyValue);
new AuditedPropertiesReader(ModificationStore.FULL, componentPropertiesSource, componentData,
globalCfg, reflectionManager,
propertyNamePrefix + MappingTools.createComponentPrefix(property.getName()))
.read();
propertyData = componentData; ComponentAuditedPropertiesReader audPropReader = new ComponentAuditedPropertiesReader(
} else { ModificationStore.FULL, componentPropertiesSource,
propertyData = new PropertyAuditingData(); componentData, globalCfg, reflectionManager, propertyNamePrefix
isAudited = fillPropertyData(property, propertyData, accessType); + MappingTools
.createComponentPrefix(property.getName()));
audPropReader.read();
if (isAudited) {
// Now we know that the property is audited
auditedPropertiesHolder.addPropertyAuditingData(property.getName(),
componentData);
} }
}
private void addFromNotComponentProperty(XProperty property, String accessType, Audited allClassAudited){
PropertyAuditingData propertyData = new PropertyAuditingData();
boolean isAudited = fillPropertyData(property, propertyData, accessType, allClassAudited);
if (isAudited) { if (isAudited) {
// Now we know that the property is audited // Now we know that the property is audited
auditedPropertiesHolder.addPropertyAuditingData(property.getName(), propertyData); auditedPropertiesHolder.addPropertyAuditingData(property.getName(), propertyData);
} }
} }
}
}
/** /**
* Checks if a property is audited and if yes, fills all of its data. * Checks if a property is audited and if yes, fills all of its data.
@ -131,7 +167,7 @@ public class AuditedPropertiesReader {
* @return False if this property is not audited. * @return False if this property is not audited.
*/ */
private boolean fillPropertyData(XProperty property, PropertyAuditingData propertyData, private boolean fillPropertyData(XProperty property, PropertyAuditingData propertyData,
String accessType) { String accessType, Audited allClassAudited) {
// check if a property is declared as not audited to exclude it // check if a property is declared as not audited to exclude it
// useful if a class is audited but some properties should be excluded // useful if a class is audited but some properties should be excluded
@ -149,18 +185,11 @@ public class AuditedPropertiesReader {
} }
} }
// Checking if this property is explicitly audited or if all properties are.
Audited aud = property.getAnnotation(Audited.class); if(!this.checkAudited(property, propertyData, allClassAudited)){
if (aud != null) {
propertyData.setStore(aud.modStore());
propertyData.setRelationTargetAuditMode(aud.targetAuditMode());
} else {
if (defaultStore != null) {
propertyData.setStore(defaultStore);
} else {
return false; return false;
} }
}
propertyData.setName(propertyNamePrefix + property.getName()); propertyData.setName(propertyNamePrefix + property.getName());
propertyData.setBeanName(property.getName()); propertyData.setBeanName(property.getName());
@ -177,6 +206,21 @@ public class AuditedPropertiesReader {
return true; return true;
} }
protected boolean checkAudited(XProperty property,
PropertyAuditingData propertyData, Audited allClassAudited) {
// Checking if this property is explicitly audited or if all properties are.
Audited aud = (property.isAnnotationPresent(Audited.class)) ? (property.getAnnotation(Audited.class)) : allClassAudited;
//Audited aud = property.getAnnotation(Audited.class);
if (aud != null) {
propertyData.setStore(aud.modStore());
propertyData.setRelationTargetAuditMode(aud.targetAuditMode());
return true;
} else {
return false;
}
}
private void setPropertyAuditMappedBy(XProperty property, PropertyAuditingData propertyData) { private void setPropertyAuditMappedBy(XProperty property, PropertyAuditingData propertyData) {
AuditMappedBy auditMappedBy = property.getAnnotation(AuditMappedBy.class); AuditMappedBy auditMappedBy = property.getAnnotation(AuditMappedBy.class);
if (auditMappedBy != null) { if (auditMappedBy != null) {

View File

@ -0,0 +1,40 @@
package org.hibernate.envers.configuration.metadata.reader;
import org.hibernate.annotations.common.reflection.ReflectionManager;
import org.hibernate.annotations.common.reflection.XProperty;
import org.hibernate.envers.Audited;
import org.hibernate.envers.ModificationStore;
import org.hibernate.envers.configuration.GlobalConfiguration;
/**
* Reads the audited properties for components.
*
* @author Hern&aacut;n Chanfreau
*
*/
public class ComponentAuditedPropertiesReader extends AuditedPropertiesReader {
public ComponentAuditedPropertiesReader(ModificationStore defaultStore,
PersistentPropertiesSource persistentPropertiesSource,
AuditedPropertiesHolder auditedPropertiesHolder,
GlobalConfiguration globalCfg, ReflectionManager reflectionManager,
String propertyNamePrefix) {
super(defaultStore, persistentPropertiesSource, auditedPropertiesHolder,
globalCfg, reflectionManager, propertyNamePrefix);
}
@Override
protected boolean checkAudited(XProperty property,
PropertyAuditingData propertyData, Audited allClassAudited) {
// Checking if this property is explicitly audited or if all properties are.
Audited aud = property.getAnnotation(Audited.class);
if (aud != null) {
propertyData.setStore(aud.modStore());
propertyData.setRelationTargetAuditMode(aud.targetAuditMode());
} else {
propertyData.setStore(ModificationStore.FULL);
}
return true;
}
}