Search in sources :

Example 1 with AuditWorkUnit

use of org.hibernate.envers.internal.synchronization.work.AuditWorkUnit in project hibernate-orm by hibernate.

the class EnversPostInsertEventListenerImpl method onPostInsert.

@Override
public void onPostInsert(PostInsertEvent event) {
    final String entityName = event.getPersister().getEntityName();
    if (getEnversService().getEntitiesConfigurations().isVersioned(entityName)) {
        checkIfTransactionInProgress(event.getSession());
        final AuditProcess auditProcess = getEnversService().getAuditProcessManager().get(event.getSession());
        final AuditWorkUnit workUnit = new AddWorkUnit(event.getSession(), event.getPersister().getEntityName(), getEnversService(), event.getId(), event.getPersister(), event.getState());
        auditProcess.addWorkUnit(workUnit);
        if (workUnit.containsWork()) {
            generateBidirectionalCollectionChangeWorkUnits(auditProcess, event.getPersister(), entityName, event.getState(), null, event.getSession());
        }
    }
}
Also used : AuditProcess(org.hibernate.envers.internal.synchronization.AuditProcess) AuditWorkUnit(org.hibernate.envers.internal.synchronization.work.AuditWorkUnit) AddWorkUnit(org.hibernate.envers.internal.synchronization.work.AddWorkUnit)

Example 2 with AuditWorkUnit

use of org.hibernate.envers.internal.synchronization.work.AuditWorkUnit in project hibernate-orm by hibernate.

the class EnversPostUpdateEventListenerImpl method onPostUpdate.

@Override
public void onPostUpdate(PostUpdateEvent event) {
    final String entityName = event.getPersister().getEntityName();
    if (getEnversService().getEntitiesConfigurations().isVersioned(entityName)) {
        checkIfTransactionInProgress(event.getSession());
        final AuditProcess auditProcess = getEnversService().getAuditProcessManager().get(event.getSession());
        Object[] oldState = getOldDBState(auditProcess, entityName, event);
        final Object[] newDbState = postUpdateDBState(event);
        final AuditWorkUnit workUnit = new ModWorkUnit(event.getSession(), event.getPersister().getEntityName(), getEnversService(), event.getId(), event.getPersister(), newDbState, oldState);
        auditProcess.addWorkUnit(workUnit);
        if (workUnit.containsWork()) {
            generateBidirectionalCollectionChangeWorkUnits(auditProcess, event.getPersister(), entityName, newDbState, oldState, event.getSession());
        }
    }
}
Also used : AuditProcess(org.hibernate.envers.internal.synchronization.AuditProcess) AuditWorkUnit(org.hibernate.envers.internal.synchronization.work.AuditWorkUnit) ModWorkUnit(org.hibernate.envers.internal.synchronization.work.ModWorkUnit)

Example 3 with AuditWorkUnit

use of org.hibernate.envers.internal.synchronization.work.AuditWorkUnit in project hibernate-orm by hibernate.

the class AuditProcess method executeInSession.

private void executeInSession(Session session) {
    // Making sure the revision data is persisted.
    final Object currentRevisionData = getCurrentRevisionData(session, true);
    AuditWorkUnit vwu;
    // First undoing any performed work units
    while ((vwu = undoQueue.poll()) != null) {
        vwu.undo(session);
    }
    while ((vwu = workUnits.poll()) != null) {
        vwu.perform(session, revisionData);
        entityChangeNotifier.entityChanged(session, currentRevisionData, vwu);
    }
}
Also used : AuditWorkUnit(org.hibernate.envers.internal.synchronization.work.AuditWorkUnit)

Example 4 with AuditWorkUnit

use of org.hibernate.envers.internal.synchronization.work.AuditWorkUnit in project hibernate-orm by hibernate.

the class BaseEnversCollectionEventListener method generateFakeBidirecationalRelationWorkUnits.

private void generateFakeBidirecationalRelationWorkUnits(AuditProcess auditProcess, PersistentCollection newColl, Serializable oldColl, String collectionEntityName, String referencingPropertyName, AbstractCollectionEvent event, RelationDescription rd) {
    // First computing the relation changes
    final List<PersistentCollectionChangeData> collectionChanges = getEnversService().getEntitiesConfigurations().get(collectionEntityName).getPropertyMapper().mapCollectionChanges(event.getSession(), referencingPropertyName, newColl, oldColl, event.getAffectedOwnerIdOrNull());
    // Getting the id mapper for the related entity, as the work units generated will correspond to the related
    // entities.
    final String relatedEntityName = rd.getToEntityName();
    final IdMapper relatedIdMapper = getEnversService().getEntitiesConfigurations().get(relatedEntityName).getIdMapper();
    // For each collection change, generating the bidirectional work unit.
    for (PersistentCollectionChangeData changeData : collectionChanges) {
        final Object relatedObj = changeData.getChangedElement();
        final Serializable relatedId = (Serializable) relatedIdMapper.mapToIdFromEntity(relatedObj);
        final RevisionType revType = (RevisionType) changeData.getData().get(getEnversService().getAuditEntitiesConfiguration().getRevisionTypePropName());
        // This can be different from relatedEntityName, in case of inheritance (the real entity may be a subclass
        // of relatedEntityName).
        final String realRelatedEntityName = event.getSession().bestGuessEntityName(relatedObj);
        // By default, the nested work unit is a collection change work unit.
        final AuditWorkUnit nestedWorkUnit = new CollectionChangeWorkUnit(event.getSession(), realRelatedEntityName, rd.getMappedByPropertyName(), getEnversService(), relatedId, relatedObj);
        auditProcess.addWorkUnit(new FakeBidirectionalRelationWorkUnit(event.getSession(), realRelatedEntityName, getEnversService(), relatedId, referencingPropertyName, event.getAffectedOwnerOrNull(), rd, revType, changeData.getChangedElementIndex(), nestedWorkUnit));
    }
    // We also have to generate a collection change work unit for the owning entity.
    auditProcess.addWorkUnit(new CollectionChangeWorkUnit(event.getSession(), collectionEntityName, referencingPropertyName, getEnversService(), event.getAffectedOwnerIdOrNull(), event.getAffectedOwnerOrNull()));
}
Also used : Serializable(java.io.Serializable) RevisionType(org.hibernate.envers.RevisionType) IdMapper(org.hibernate.envers.internal.entities.mapper.id.IdMapper) AuditWorkUnit(org.hibernate.envers.internal.synchronization.work.AuditWorkUnit) FakeBidirectionalRelationWorkUnit(org.hibernate.envers.internal.synchronization.work.FakeBidirectionalRelationWorkUnit) PersistentCollectionChangeData(org.hibernate.envers.internal.entities.mapper.PersistentCollectionChangeData) CollectionChangeWorkUnit(org.hibernate.envers.internal.synchronization.work.CollectionChangeWorkUnit) PersistentCollectionChangeWorkUnit(org.hibernate.envers.internal.synchronization.work.PersistentCollectionChangeWorkUnit)

Example 5 with AuditWorkUnit

use of org.hibernate.envers.internal.synchronization.work.AuditWorkUnit in project hibernate-orm by hibernate.

the class EnversPostDeleteEventListenerImpl method onPostDelete.

@Override
public void onPostDelete(PostDeleteEvent event) {
    final String entityName = event.getPersister().getEntityName();
    if (getEnversService().getEntitiesConfigurations().isVersioned(entityName)) {
        checkIfTransactionInProgress(event.getSession());
        final AuditProcess auditProcess = getEnversService().getAuditProcessManager().get(event.getSession());
        final AuditWorkUnit workUnit = new DelWorkUnit(event.getSession(), event.getPersister().getEntityName(), getEnversService(), event.getId(), event.getPersister(), event.getDeletedState());
        auditProcess.addWorkUnit(workUnit);
        if (workUnit.containsWork()) {
            generateBidirectionalCollectionChangeWorkUnits(auditProcess, event.getPersister(), entityName, null, event.getDeletedState(), event.getSession());
        }
    }
}
Also used : AuditProcess(org.hibernate.envers.internal.synchronization.AuditProcess) DelWorkUnit(org.hibernate.envers.internal.synchronization.work.DelWorkUnit) AuditWorkUnit(org.hibernate.envers.internal.synchronization.work.AuditWorkUnit)

Aggregations

AuditWorkUnit (org.hibernate.envers.internal.synchronization.work.AuditWorkUnit)6 AuditProcess (org.hibernate.envers.internal.synchronization.AuditProcess)3 Serializable (java.io.Serializable)1 RevisionType (org.hibernate.envers.RevisionType)1 PersistentCollectionChangeData (org.hibernate.envers.internal.entities.mapper.PersistentCollectionChangeData)1 IdMapper (org.hibernate.envers.internal.entities.mapper.id.IdMapper)1 AddWorkUnit (org.hibernate.envers.internal.synchronization.work.AddWorkUnit)1 CollectionChangeWorkUnit (org.hibernate.envers.internal.synchronization.work.CollectionChangeWorkUnit)1 DelWorkUnit (org.hibernate.envers.internal.synchronization.work.DelWorkUnit)1 FakeBidirectionalRelationWorkUnit (org.hibernate.envers.internal.synchronization.work.FakeBidirectionalRelationWorkUnit)1 ModWorkUnit (org.hibernate.envers.internal.synchronization.work.ModWorkUnit)1 PersistentCollectionChangeWorkUnit (org.hibernate.envers.internal.synchronization.work.PersistentCollectionChangeWorkUnit)1