HHH-6605 - Removed some duplication in mappers code

This commit is contained in:
Michal Skowronek 2011-10-02 23:37:36 +02:00
parent 726571b270
commit 43a602b100
6 changed files with 23 additions and 13 deletions

View File

@ -25,6 +25,8 @@ package org.hibernate.envers.entities;
import org.hibernate.envers.ModificationStore;
import org.hibernate.envers.configuration.metadata.MetadataTools;
import java.util.Map;
/**
* Holds information on a property that is audited.
* @author Adam Warski (adam at warski dot org)
@ -96,7 +98,11 @@ public class PropertyData {
return usingModifiedFlag;
}
public String getModifiedFlagPropertyName() {
public void addModifiedFlag(Map<String, Object> data, boolean flagValue) {
data.put(getModifiedFlagPropertyName(), flagValue);
}
private String getModifiedFlagPropertyName() {
return MetadataTools.getModifiedFlagPropertyName(name);
}

View File

@ -72,7 +72,7 @@ public class ComponentPropertyMapper implements PropertyMapper, CompositeMapperB
@Override
public void mapModifiedFlagsToMapFromEntity(SessionImplementor session, Map<String, Object> data, Object newObj, Object oldObj) {
if (propertyData.isUsingModifiedFlag()) {
data.put(propertyData.getModifiedFlagPropertyName(),
propertyData.addModifiedFlag(data,
delegate.mapToMapFromEntity(session, new HashMap<String, Object>(), newObj, oldObj));
}
}
@ -87,7 +87,7 @@ public class ComponentPropertyMapper implements PropertyMapper, CompositeMapperB
break;
}
}
data.put(propertyData.getModifiedFlagPropertyName(), hasModifiedCollection);
propertyData.addModifiedFlag(data, hasModifiedCollection);
}
}

View File

@ -69,7 +69,7 @@ public class SinglePropertyMapper implements PropertyMapper, SimpleMapperBuilder
@Override
public void mapModifiedFlagsToMapFromEntity(SessionImplementor session, Map<String, Object> data, Object newObj, Object oldObj) {
if (propertyData.isUsingModifiedFlag()) {
data.put(propertyData.getModifiedFlagPropertyName(), !Tools.objectsEqual(newObj, oldObj));
propertyData.addModifiedFlag(data, !Tools.objectsEqual(newObj, oldObj));
}
}

View File

@ -22,7 +22,6 @@
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.entities.mapper.relation;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.envers.RevisionType;
@ -39,7 +38,13 @@ import org.hibernate.property.Setter;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author Adam Warski (adam at warski dot org)
@ -135,11 +140,11 @@ public abstract class AbstractCollectionMapper<T> implements PropertyMapper {
PropertyData propertyData = commonCollectionMapperData.getCollectionReferencingPropertyData();
if (propertyData.isUsingModifiedFlag()) {
if(isFromNullToEmptyOrFromEmptyToNull((PersistentCollection) newObj, (Serializable) oldObj)){
data.put(propertyData.getModifiedFlagPropertyName(), true);
propertyData.addModifiedFlag(data, true);
} else {
List<PersistentCollectionChangeData> changes = mapCollectionChanges(commonCollectionMapperData.getCollectionReferencingPropertyData().getName(),
(PersistentCollection) newObj, (Serializable) oldObj, null);
data.put(propertyData.getModifiedFlagPropertyName(), !changes.isEmpty());
propertyData.addModifiedFlag(data, !changes.isEmpty());
}
}
}
@ -157,7 +162,7 @@ public abstract class AbstractCollectionMapper<T> implements PropertyMapper {
public void mapModifiedFlagsToMapForCollectionChange(String collectionPropertyName, Map<String, Object> data) {
PropertyData propertyData = commonCollectionMapperData.getCollectionReferencingPropertyData();
if (propertyData.isUsingModifiedFlag()) {
data.put(propertyData.getModifiedFlagPropertyName(), propertyData.getName().equals(collectionPropertyName));
propertyData.addModifiedFlag(data, propertyData.getName().equals(collectionPropertyName));
}
}

View File

@ -69,8 +69,7 @@ public class OneToOneNotOwningMapper implements PropertyMapper {
@Override
public void mapModifiedFlagsToMapForCollectionChange(String collectionPropertyName, Map<String, Object> data) {
if (propertyData.isUsingModifiedFlag()) {
data.put(propertyData.getModifiedFlagPropertyName(),
collectionPropertyName.equals(propertyData.getName()));
propertyData.addModifiedFlag(data, collectionPropertyName.equals(propertyData.getName()));
}
}

View File

@ -78,14 +78,14 @@ public class ToOneIdMapper implements PropertyMapper {
@Override
public void mapModifiedFlagsToMapFromEntity(SessionImplementor session, Map<String, Object> data, Object newObj, Object oldObj) {
if (propertyData.isUsingModifiedFlag()) {
data.put(propertyData.getModifiedFlagPropertyName(), checkModified(session, newObj, oldObj));
propertyData.addModifiedFlag(data, checkModified(session, newObj, oldObj));
}
}
@Override
public void mapModifiedFlagsToMapForCollectionChange(String collectionPropertyName, Map<String, Object> data) {
if (propertyData.isUsingModifiedFlag()) {
data.put(propertyData.getModifiedFlagPropertyName(), collectionPropertyName.equals(propertyData.getName()));
propertyData.addModifiedFlag(data, collectionPropertyName.equals(propertyData.getName()));
}
}