org.hibernate.envers.internal.synchronization.work.PersistentCollectionChangeWorkUnit Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-envers Show documentation
Show all versions of hibernate-envers Show documentation
Hibernate's entity version (audit/history) support
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or .
*/
package org.hibernate.envers.internal.synchronization.work;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.Session;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.CollectionEntry;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.envers.RevisionType;
import org.hibernate.envers.boot.internal.EnversService;
import org.hibernate.envers.configuration.internal.AuditEntitiesConfiguration;
import org.hibernate.envers.internal.entities.mapper.PersistentCollectionChangeData;
/**
* @author Adam Warski (adam at warski dot org)
*/
public class PersistentCollectionChangeWorkUnit extends AbstractAuditWorkUnit implements AuditWorkUnit {
private final List collectionChanges;
private final String referencingPropertyName;
public PersistentCollectionChangeWorkUnit(
SessionImplementor sessionImplementor,
String entityName,
EnversService enversService,
PersistentCollection collection,
CollectionEntry collectionEntry,
Serializable snapshot,
Serializable id,
String referencingPropertyName) {
super(
sessionImplementor,
entityName,
enversService,
new PersistentCollectionChangeWorkUnitId( id, collectionEntry.getRole() ),
RevisionType.MOD
);
this.referencingPropertyName = referencingPropertyName;
collectionChanges = enversService.getEntitiesConfigurations().get( getEntityName() ).getPropertyMapper()
.mapCollectionChanges( sessionImplementor, referencingPropertyName, collection, snapshot, id );
}
public PersistentCollectionChangeWorkUnit(
SessionImplementor sessionImplementor,
String entityName,
EnversService enversService,
Serializable id,
List collectionChanges,
String referencingPropertyName) {
super( sessionImplementor, entityName, enversService, id, RevisionType.MOD );
this.collectionChanges = collectionChanges;
this.referencingPropertyName = referencingPropertyName;
}
@Override
public boolean containsWork() {
return collectionChanges != null && collectionChanges.size() != 0;
}
@Override
public Map generateData(Object revisionData) {
throw new UnsupportedOperationException( "Cannot generate data for a collection change work unit!" );
}
@Override
@SuppressWarnings({"unchecked"})
public void perform(Session session, Object revisionData) {
final AuditEntitiesConfiguration entitiesCfg = enversService.getAuditEntitiesConfiguration();
for ( PersistentCollectionChangeData persistentCollectionChangeData : collectionChanges ) {
// Setting the revision number
( (Map) persistentCollectionChangeData.getData().get( entitiesCfg.getOriginalIdPropName() ) )
.put( entitiesCfg.getRevisionFieldName(), revisionData );
auditStrategy.performCollectionChange(
session,
getEntityName(),
referencingPropertyName,
enversService,
persistentCollectionChangeData,
revisionData
);
}
}
public String getReferencingPropertyName() {
return referencingPropertyName;
}
public List getCollectionChanges() {
return collectionChanges;
}
@Override
public AuditWorkUnit merge(AddWorkUnit second) {
return null;
}
@Override
public AuditWorkUnit merge(ModWorkUnit second) {
return null;
}
@Override
public AuditWorkUnit merge(DelWorkUnit second) {
return null;
}
@Override
public AuditWorkUnit merge(CollectionChangeWorkUnit second) {
return null;
}
@Override
public AuditWorkUnit merge(FakeBidirectionalRelationWorkUnit second) {
return null;
}
@Override
public AuditWorkUnit dispatch(WorkUnitMergeVisitor first) {
if ( first instanceof PersistentCollectionChangeWorkUnit ) {
final PersistentCollectionChangeWorkUnit original = (PersistentCollectionChangeWorkUnit) first;
// Merging the collection changes in both work units.
// First building a map from the ids of the collection-entry-entities from the "second" collection changes,
// to the PCCD objects. That way, we will be later able to check if an "original" collection change
// should be added, or if it is overshadowed by a new one.
final Map