forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractCollectionEvent.cs
111 lines (100 loc) · 4.21 KB
/
AbstractCollectionEvent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using NHibernate.Collection;
using NHibernate.Engine;
using NHibernate.Persister.Collection;
namespace NHibernate.Event
{
/// <summary> Defines a base class for events involving collections. </summary>
[Serializable]
public abstract class AbstractCollectionEvent : AbstractEvent
{
private readonly object affectedOwner;
private readonly string affectedOwnerEntityName;
private readonly object affectedOwnerId;
private readonly IPersistentCollection collection;
/// <summary> Constructs an AbstractCollectionEvent object. </summary>
/// <param name="collectionPersister">The collection persister.</param>
/// <param name="collection">The collection </param>
/// <param name="source">The Session source </param>
/// <param name="affectedOwner">The owner that is affected by this event; can be null if unavailable </param>
/// <param name="affectedOwnerId">
/// The ID for the owner that is affected by this event; can be null if unavailable
/// that is affected by this event; can be null if unavailable
/// </param>
protected AbstractCollectionEvent(ICollectionPersister collectionPersister, IPersistentCollection collection,
IEventSource source, object affectedOwner, object affectedOwnerId) : base(source)
{
this.collection = collection;
this.affectedOwner = affectedOwner;
this.affectedOwnerId = affectedOwnerId;
affectedOwnerEntityName = GetAffectedOwnerEntityName(collectionPersister, affectedOwner, source);
}
public IPersistentCollection Collection
{
get { return collection; }
}
/// <summary> The collection owner entity that is affected by this event. </summary>
/// <value>
/// Returns null if the entity is not in the persistence context
/// (e.g., because the collection from a detached entity was moved to a new owner)
/// </value>
public object AffectedOwnerOrNull
{
get { return affectedOwner; }
}
/// <summary> Get the ID for the collection owner entity that is affected by this event. </summary>
/// <value>
/// Returns null if the ID cannot be obtained
/// from the collection's loaded key (e.g., a property-ref is used for the
/// collection and does not include the entity's ID)
/// </value>
public object AffectedOwnerIdOrNull
{
get { return affectedOwnerId; }
}
protected static ICollectionPersister GetLoadedCollectionPersister(IPersistentCollection collection,
IEventSource source)
{
CollectionEntry ce = source.PersistenceContext.GetCollectionEntry(collection);
return (ce == null ? null : ce.LoadedPersister);
}
protected static object GetLoadedOwnerOrNull(IPersistentCollection collection, IEventSource source)
{
return source.PersistenceContext.GetLoadedCollectionOwnerOrNull(collection);
}
protected static object GetLoadedOwnerIdOrNull(IPersistentCollection collection, IEventSource source)
{
return source.PersistenceContext.GetLoadedCollectionOwnerIdOrNull(collection);
}
protected static object GetOwnerIdOrNull(object owner, IEventSource source)
{
EntityEntry ownerEntry = source.PersistenceContext.GetEntry(owner);
return (ownerEntry == null ? null : ownerEntry.Id);
}
protected static string GetAffectedOwnerEntityName(ICollectionPersister collectionPersister, object affectedOwner,
IEventSource source)
{
// collectionPersister should not be null, but we don't want to throw
// an exception if it is null
string entityName = (collectionPersister == null ? null : collectionPersister.OwnerEntityPersister.EntityName);
if (affectedOwner != null)
{
EntityEntry ee = source.PersistenceContext.GetEntry(affectedOwner);
if (ee != null && ee.EntityName != null)
{
entityName = ee.EntityName;
}
}
return entityName;
}
/// <summary> Get the entity name for the collection owner entity that is affected by this event. </summary>
/// <returns>
/// The entity name; if the owner is not in the PersistenceContext, the
/// returned value may be a superclass name, instead of the actual class name
/// </returns>
public virtual string GetAffectedOwnerEntityName()
{
return affectedOwnerEntityName;
}
}
}