All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.hibernate.envers.internal.entities.mapper.relation.ListCollectionMapper Maven / Gradle / Ivy
/*
* 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.entities.mapper.relation;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.envers.RevisionType;
import org.hibernate.envers.boot.internal.EnversService;
import org.hibernate.envers.internal.entities.mapper.PersistentCollectionChangeData;
import org.hibernate.envers.internal.entities.mapper.PropertyMapper;
import org.hibernate.envers.internal.entities.mapper.relation.lazy.initializor.Initializor;
import org.hibernate.envers.internal.entities.mapper.relation.lazy.initializor.ListCollectionInitializor;
import org.hibernate.envers.internal.entities.mapper.relation.lazy.proxy.ListProxy;
import org.hibernate.envers.internal.reader.AuditReaderImplementor;
import org.hibernate.envers.internal.tools.Tools;
import org.hibernate.envers.tools.Pair;
import org.hibernate.persister.collection.CollectionPersister;
/**
* @author Adam Warski (adam at warski dot org)
* @author Chris Cranford
*/
public final class ListCollectionMapper extends AbstractCollectionMapper implements PropertyMapper {
private final MiddleComponentData elementComponentData;
private final MiddleComponentData indexComponentData;
public ListCollectionMapper(
CommonCollectionMapperData commonCollectionMapperData,
MiddleComponentData elementComponentData, MiddleComponentData indexComponentData,
boolean revisionTypeInId) {
super( commonCollectionMapperData, List.class, ListProxy.class, false, revisionTypeInId );
this.elementComponentData = elementComponentData;
this.indexComponentData = indexComponentData;
}
@Override
protected Initializor getInitializor(
EnversService enversService,
AuditReaderImplementor versionsReader,
Object primaryKey,
Number revision,
boolean removed) {
return new ListCollectionInitializor(
enversService,
versionsReader,
commonCollectionMapperData.getQueryGenerator(),
primaryKey,
revision,
removed,
elementComponentData,
indexComponentData
);
}
@Override
@SuppressWarnings({"unchecked"})
protected Collection getNewCollectionContent(PersistentCollection newCollection) {
if ( newCollection == null ) {
return null;
}
else {
return Tools.listToIndexElementPairList( (List) newCollection );
}
}
@Override
@SuppressWarnings({"unchecked"})
protected Collection getOldCollectionContent(Serializable oldCollection) {
if ( oldCollection == null ) {
return null;
}
else {
return Tools.listToIndexElementPairList( (List) oldCollection );
}
}
@Override
@SuppressWarnings({"unchecked"})
protected void mapToMapFromObject(
SessionImplementor session,
Map idData,
Map data,
Object changed) {
final Pair indexValuePair = (Pair) changed;
elementComponentData.getComponentMapper().mapToMapFromObject(
session,
idData,
data,
indexValuePair.getSecond()
);
indexComponentData.getComponentMapper().mapToMapFromObject( session, idData, data, indexValuePair.getFirst() );
}
@Override
protected Set buildCollectionChangeSet(Object eventCollection, Collection collection) {
final Set changeSet = new HashSet<>();
if ( eventCollection != null ) {
for ( Object entry : collection ) {
if ( entry != null ) {
final Pair pair = Pair.class.cast( entry );
if ( pair.getSecond() == null ) {
continue;
}
changeSet.add( entry );
}
}
}
return changeSet;
}
@Override
protected List mapCollectionChanges(
SessionImplementor session,
PersistentCollection newColl,
Serializable oldColl,
Serializable id) {
final List collectionChanges = new ArrayList<>();
final CollectionPersister collectionPersister = resolveCollectionPersister( session, newColl );
// Comparing new and old collection content.
final Collection newCollection = getNewCollectionContent( newColl );
final Collection oldCollection = getOldCollectionContent( oldColl );
final Set addedElements = buildCollectionChangeSet( newColl, newCollection );
if ( oldColl != null ) {
for ( int i = 0; i < oldCollection.size(); ++i ) {
Pair oldEntry = ((List>) oldCollection).get( i );
for ( Iterator itor = addedElements.iterator(); itor.hasNext(); ) {
Pair addedEntry = (Pair) itor.next();
if ( oldEntry.getFirst().equals( addedEntry.getFirst() ) ) {
if ( isSame( collectionPersister, oldEntry.getSecond(), addedEntry.getSecond() ) ) {
itor.remove();
break;
}
}
}
}
}
final Set deleteElements = buildCollectionChangeSet( oldColl, oldCollection );
if ( newColl != null ) {
for ( int i = 0; i < newCollection.size(); ++i ) {
Pair newEntry = ((List>) newCollection).get( i );
for ( Iterator itor = deleteElements.iterator(); itor.hasNext(); ) {
Pair deletedEntry = (Pair) itor.next();
if ( newEntry.getFirst().equals( deletedEntry.getFirst() ) ) {
if ( isSame( collectionPersister, deletedEntry.getSecond(), newEntry.getSecond() ) ) {
itor.remove();
break;
}
}
}
}
}
addCollectionChanges( session, collectionChanges, addedElements, RevisionType.ADD, id );
addCollectionChanges( session, collectionChanges, deleteElements, RevisionType.DEL, id );
return collectionChanges;
}
}