org.hibernate.envers.internal.entities.mapper.relation.BasicCollectionMapper 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.Collection;
import java.util.Map;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.envers.boot.internal.EnversService;
import org.hibernate.envers.internal.entities.mapper.PropertyMapper;
import org.hibernate.envers.internal.entities.mapper.relation.lazy.initializor.BasicCollectionInitializor;
import org.hibernate.envers.internal.entities.mapper.relation.lazy.initializor.Initializor;
import org.hibernate.envers.internal.reader.AuditReaderImplementor;
/**
* @author Adam Warski (adam at warski dot org)
*/
public class BasicCollectionMapper extends AbstractCollectionMapper implements PropertyMapper {
protected final MiddleComponentData elementComponentData;
public BasicCollectionMapper(
CommonCollectionMapperData commonCollectionMapperData,
Class collectionClass,
Class proxyClass,
MiddleComponentData elementComponentData,
boolean ordinalInId,
boolean revisionTypeInId) {
super( commonCollectionMapperData, collectionClass, proxyClass, ordinalInId, revisionTypeInId );
this.elementComponentData = elementComponentData;
}
@Override
protected Initializor getInitializor(
EnversService enversService,
AuditReaderImplementor versionsReader,
Object primaryKey,
Number revision,
boolean removed) {
return new BasicCollectionInitializor<>(
enversService,
versionsReader,
commonCollectionMapperData.getQueryGenerator(),
primaryKey,
revision,
removed,
collectionClass,
elementComponentData
);
}
@Override
protected Collection getNewCollectionContent(PersistentCollection newCollection) {
return (Collection) newCollection;
}
@Override
protected Collection getOldCollectionContent(Serializable oldCollection) {
if ( oldCollection == null ) {
return null;
}
else if ( oldCollection instanceof Map ) {
return ( (Map) oldCollection ).keySet();
}
else {
return (Collection) oldCollection;
}
}
@Override
protected void mapToMapFromObject(
SessionImplementor session,
Map idData,
Map data,
Object changed) {
elementComponentData.getComponentMapper().mapToMapFromObject( session, idData, data, changed );
}
}