KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > action > CollectionAction


1 //$Id: CollectionAction.java,v 1.16 2005/06/15 13:19:57 oneovthafew Exp $
2
package org.hibernate.action;
3
4 import org.hibernate.cache.CacheConcurrencyStrategy.SoftLock;
5 import org.hibernate.cache.CacheException;
6 import org.hibernate.cache.CacheKey;
7 import org.hibernate.collection.PersistentCollection;
8 import org.hibernate.engine.SessionImplementor;
9 import org.hibernate.persister.collection.CollectionPersister;
10 import org.hibernate.pretty.MessageHelper;
11 import org.hibernate.util.StringHelper;
12
13 import java.io.IOException JavaDoc;
14 import java.io.ObjectInputStream JavaDoc;
15 import java.io.Serializable JavaDoc;
16
17 /**
18  * Any action relating to insert/update/delete of a collection
19  * @author Gavin King
20  */

21 public abstract class CollectionAction implements Executable, Serializable JavaDoc, Comparable JavaDoc {
22
23     private transient CollectionPersister persister;
24     private final Serializable JavaDoc key;
25     private final SessionImplementor session;
26     private SoftLock lock;
27     private final String JavaDoc collectionRole;
28     private final PersistentCollection collection;
29
30     public CollectionAction(
31             final CollectionPersister persister,
32             final PersistentCollection collection,
33             final Serializable JavaDoc key,
34             final SessionImplementor session)
35     throws CacheException {
36         this.persister = persister;
37         this.session = session;
38         this.key = key;
39         this.collectionRole = persister.getRole();
40         this.collection = collection;
41     }
42     
43     protected PersistentCollection getCollection() {
44         return collection;
45     }
46
47     private void readObject(ObjectInputStream JavaDoc ois) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
48         ois.defaultReadObject();
49         persister = session.getFactory().getCollectionPersister( collectionRole );
50     }
51
52     public void afterTransactionCompletion(boolean success) throws CacheException {
53         if ( persister.hasCache() ) {
54             final CacheKey ck = new CacheKey(
55                     key,
56                     persister.getKeyType(),
57                     persister.getRole(),
58                     session.getEntityMode(),
59                     session.getFactory()
60                 );
61             persister.getCache().release(ck, lock);
62         }
63     }
64
65     public boolean hasAfterTransactionCompletion() {
66         return persister.hasCache();
67     }
68
69     public Serializable JavaDoc[] getPropertySpaces() {
70         return persister.getCollectionSpaces();
71     }
72
73     protected final CollectionPersister getPersister() {
74         return persister;
75     }
76
77     protected final Serializable JavaDoc getKey() {
78         return key;
79     }
80
81     protected final SessionImplementor getSession() {
82         return session;
83     }
84
85     public final void beforeExecutions() throws CacheException {
86         // we need to obtain the lock before any actions are
87
// executed, since this may be an inverse="true"
88
// bidirectional association and it is one of the
89
// earlier entity actions which actually updates
90
// the database (this action is resposible for
91
// second-level cache invalidation only)
92
if ( persister.hasCache() ) {
93             final CacheKey ck = new CacheKey(
94                     key,
95                     persister.getKeyType(),
96                     persister.getRole(),
97                     session.getEntityMode(),
98                     session.getFactory()
99                 );
100             lock = persister.getCache().lock(ck, null);
101         }
102     }
103
104     protected final void evict() throws CacheException {
105         if ( persister.hasCache() ) {
106             CacheKey ck = new CacheKey(
107                     key,
108                     persister.getKeyType(),
109                     persister.getRole(),
110                     session.getEntityMode(),
111                     session.getFactory()
112                 );
113             persister.getCache().evict(ck);
114         }
115     }
116
117     public String JavaDoc toString() {
118         return StringHelper.unqualify( getClass().getName() ) +
119                 MessageHelper.infoString( collectionRole, key );
120     }
121
122     public int compareTo(Object JavaDoc other) {
123         CollectionAction action = ( CollectionAction ) other;
124         //sort first by role name
125
int roleComparison = collectionRole.compareTo( action.collectionRole );
126         if ( roleComparison != 0 ) {
127             return roleComparison;
128         }
129         else {
130             //then by fk
131
return persister.getKeyType()
132                     .compare( key, action.key, session.getEntityMode() );
133         }
134     }
135 }
136
137
138
139
140
141
142
Popular Tags