Esempio n. 1
0
        private bool shouldGenerateRevision(AbstractCollectionEvent evt)
        {
            var entityName = evt.GetAffectedOwnerEntityName();

            return(VerCfg.GlobalCfg.GenerateRevisionsForCollections &&
                   VerCfg.EntCfg.IsVersioned(entityName));
        }
Esempio n. 2
0
        private void onCollectionAction(AbstractCollectionEvent evt,
                                        IPersistentCollection newColl,
                                        object oldColl,
                                        CollectionEntry collectionEntry)
        {
            if (!VerCfg.GlobalCfg.GenerateRevisionsForCollections)
            {
                return;
            }
            var entityName = evt.GetAffectedOwnerEntityName();

            if (!VerCfg.EntCfg.IsVersioned(entityName))
            {
                return;
            }
            checkIfTransactionInProgress(evt.Session);

            var verSync                 = VerCfg.AuditProcessManager.Get(evt.Session);
            var ownerEntityName         = ((AbstractCollectionPersister)collectionEntry.LoadedPersister).OwnerEntityName;
            var referencingPropertyName = collectionEntry.Role.Substring(ownerEntityName.Length + 1);

            // Checking if this is not a "fake" many-to-one bidirectional relation. The relation description may be
            // null in case of collections of non-entities.
            var rd = getRelationDescriptionWithInheritedRelations(entityName, referencingPropertyName);

            if (rd?.MappedByPropertyName != null)
            {
                generateFakeBidirecationalRelationWorkUnits(verSync, newColl, oldColl, entityName,
                                                            referencingPropertyName, evt, rd);
            }
            else
            {
                var workUnit = new PersistentCollectionChangeWorkUnit(evt.Session, entityName, VerCfg, newColl,
                                                                      collectionEntry, oldColl, evt.AffectedOwnerIdOrNull,
                                                                      referencingPropertyName);
                verSync.AddWorkUnit(workUnit);

                if (workUnit.ContainsWork())
                {
                    // There are some changes: a revision needs also be generated for the collection owner
                    verSync.AddWorkUnit(new CollectionChangeWorkUnit(evt.Session, evt.GetAffectedOwnerEntityName(), referencingPropertyName,
                                                                     VerCfg, evt.AffectedOwnerIdOrNull, evt.AffectedOwnerOrNull));

                    generateBidirectionalCollectionChangeWorkUnits(verSync, evt, workUnit, rd);
                }
            }
        }
Esempio n. 3
0
        private void OnCollectionAction(AbstractCollectionEvent evt, IPersistentCollection newColl, object oldColl,
                                        CollectionEntry collectionEntry)
        {
            String entityName = evt.GetAffectedOwnerEntityName();

            if (verCfg.EntCfg.IsVersioned(entityName))
            {
                AuditSync verSync = verCfg.AuditSyncManager.get(evt.Session);

                String ownerEntityName         = ((AbstractCollectionPersister)collectionEntry.LoadedPersister).OwnerEntityName;
                String referencingPropertyName = collectionEntry.Role.Substring(ownerEntityName.Length + 1);

                // Checking if this is not a "fake" many-to-one bidirectional relation. The relation description may be
                // null in case of collections of non-entities.
                RelationDescription rd = verCfg.EntCfg[entityName].GetRelationDescription(referencingPropertyName);
                if (rd != null && rd.MappedByPropertyName != null)
                {
                    GenerateFakeBidirecationalRelationWorkUnits(verSync, newColl, oldColl, entityName,
                                                                referencingPropertyName, evt, rd);
                }
                else
                {
                    PersistentCollectionChangeWorkUnit workUnit = new PersistentCollectionChangeWorkUnit(evt.Session,
                                                                                                         entityName, verCfg, newColl, collectionEntry, oldColl, evt.AffectedOwnerIdOrNull,
                                                                                                         referencingPropertyName);
                    verSync.AddWorkUnit(workUnit);

                    if (workUnit.ContainsWork())
                    {
                        // There are some changes: a revision needs also be generated for the collection owner
                        verSync.AddWorkUnit(new CollectionChangeWorkUnit(evt.Session, evt.GetAffectedOwnerEntityName(),
                                                                         verCfg, evt.AffectedOwnerIdOrNull, evt.AffectedOwnerOrNull));

                        GenerateBidirectionalCollectionChangeWorkUnits(verSync, evt, workUnit, rd);
                    }
                }
            }
        }
Esempio n. 4
0
        private void generateBidirectionalCollectionChangeWorkUnits(AuditProcess auditProcess,
                                                                    AbstractCollectionEvent evt,
                                                                    PersistentCollectionChangeWorkUnit workUnit,
                                                                    RelationDescription rd)
        {
            // Checking if this is enabled in configuration ...
            if (!VerCfg.GlobalCfg.GenerateRevisionsForCollections)
            {
                return;
            }

            // Checking if this is not a bidirectional relation - then, a revision needs also be generated for
            // the other side of the relation.
            // relDesc can be null if this is a collection of simple values (not a relation).
            if (rd != null && rd.Bidirectional)
            {
                var relatedEntityName = rd.ToEntityName;
                var relatedIdMapper   = VerCfg.EntCfg[relatedEntityName].IdMapper;

                foreach (var changeData in workUnit.CollectionChanges)
                {
                    var relatedObj = changeData.GetChangedElement();
                    var relatedId  = relatedIdMapper.MapToIdFromEntity(relatedObj);

                    var toPropertyNames = VerCfg.EntCfg.ToPropertyNames(evt.GetAffectedOwnerEntityName(), rd.FromPropertyName, relatedEntityName);
                    var toPropertyName  = toPropertyNames.First();

                    auditProcess.AddWorkUnit(new CollectionChangeWorkUnit(evt.Session,
                                                                          evt.Session.BestGuessEntityName(relatedObj),
                                                                          toPropertyName,
                                                                          VerCfg,
                                                                          relatedId,
                                                                          relatedObj));
                }
            }
        }
Esempio n. 5
0
        private void ProcessCollectionEvent(AbstractCollectionEvent @event)
        {
            object entity = @event.AffectedOwnerOrNull;

            if (entity == null)
            {
                // Hibernate cannot determine every single time the owner especially incase detached objects are involved
                // or property-ref is used
                // Should log really but we don't know if we're interested in this collection for indexing
                return;
            }

            if (used && EntityIsIndexed(entity))
            {
                object id = GetId(entity, @event);
                if (id == null)
                {
                    log.Warn("Unable to reindex entity on collection change, id cannot be extracted: " + @event.GetAffectedOwnerEntityName());
                    return;
                }

                ProcessWork(entity, id, WorkType.Collection, @event);
            }
        }