KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > collection > PersistentArrayHolder


1 //$Id: PersistentArrayHolder.java,v 1.19 2005/06/28 08:59:06 maxcsaucdk Exp $
2
package org.hibernate.collection;
3
4 import java.io.Serializable JavaDoc;
5 import java.lang.reflect.Array JavaDoc;
6 import java.sql.ResultSet JavaDoc;
7 import java.sql.SQLException JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.Collection JavaDoc;
10 import java.util.Iterator JavaDoc;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.hibernate.EntityMode;
15 import org.hibernate.HibernateException;
16 import org.hibernate.loader.CollectionAliases;
17 import org.hibernate.engine.SessionImplementor;
18 import org.hibernate.persister.collection.CollectionPersister;
19 import org.hibernate.type.Type;
20
21 /**
22  * A persistent wrapper for an array. Lazy initialization
23  * is NOT supported. Use of Hibernate arrays is not really
24  * recommended.
25  *
26  * @author Gavin King
27  */

28 public class PersistentArrayHolder extends AbstractPersistentCollection {
29     protected Object JavaDoc array;
30
31     private static final Log log = LogFactory.getLog(PersistentArrayHolder.class);
32
33     //just to help out during the load (ugly, i know)
34
private transient Class JavaDoc elementClass;
35     private transient java.util.List JavaDoc tempList;
36
37     public PersistentArrayHolder(SessionImplementor session, Object JavaDoc array) {
38         super(session);
39         this.array = array;
40         setInitialized();
41     }
42
43     public Serializable JavaDoc getSnapshot(CollectionPersister persister) throws HibernateException {
44         EntityMode entityMode = getSession().getEntityMode();
45         int length = /*(array==null) ? tempList.size() :*/ Array.getLength(array);
46         Serializable JavaDoc result = (Serializable JavaDoc) Array.newInstance( persister.getElementClass(), length );
47         for ( int i=0; i<length; i++ ) {
48             Object JavaDoc elt = /*(array==null) ? tempList.get(i) :*/ Array.get(array, i);
49             try {
50                 Array.set( result, i, persister.getElementType().deepCopy(elt, entityMode, persister.getFactory()) );
51             }
52             catch (IllegalArgumentException JavaDoc iae) {
53                 log.error("Array element type error", iae);
54                 throw new HibernateException( "Array element type error", iae );
55             }
56         }
57         return result;
58     }
59
60     public boolean isSnapshotEmpty(Serializable JavaDoc snapshot) {
61         return Array.getLength( snapshot ) == 0;
62     }
63
64     public Collection JavaDoc getOrphans(Serializable JavaDoc snapshot, String JavaDoc entityName) throws HibernateException {
65         Object JavaDoc[] sn = (Object JavaDoc[]) snapshot;
66         Object JavaDoc[] arr = (Object JavaDoc[]) array;
67         ArrayList JavaDoc result = new ArrayList JavaDoc();
68         for (int i=0; i<sn.length; i++) result.add( sn[i] );
69         for (int i=0; i<sn.length; i++) identityRemove( result, arr[i], entityName, getSession() );
70         return result;
71     }
72
73     public PersistentArrayHolder(SessionImplementor session, CollectionPersister persister) throws HibernateException {
74         super(session);
75         elementClass = persister.getElementClass();
76     }
77
78     public Object JavaDoc getArray() {
79         return array;
80     }
81
82     public boolean isWrapper(Object JavaDoc collection) {
83         return array==collection;
84     }
85
86     public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException {
87         Type elementType = persister.getElementType();
88         Serializable JavaDoc snapshot = getSnapshot();
89         int xlen = Array.getLength(snapshot);
90         if ( xlen!= Array.getLength(array) ) return false;
91         for ( int i=0; i<xlen; i++) {
92             if ( elementType.isDirty( Array.get(snapshot, i), Array.get(array, i), getSession() ) ) return false;
93         }
94         return true;
95     }
96
97     public Iterator JavaDoc elements() {
98         //if (array==null) return tempList.iterator();
99
int length = Array.getLength(array);
100         java.util.List JavaDoc list = new ArrayList JavaDoc(length);
101         for (int i=0; i<length; i++) {
102             list.add( Array.get(array, i) );
103         }
104         return list.iterator();
105     }
106     public boolean empty() {
107         return false;
108     }
109
110     public Object JavaDoc readFrom(ResultSet JavaDoc rs, CollectionPersister persister, CollectionAliases descriptor, Object JavaDoc owner)
111     throws HibernateException, SQLException JavaDoc {
112
113         Object JavaDoc element = persister.readElement( rs, owner, descriptor.getSuffixedElementAliases(), getSession() );
114         int index = ( (Integer JavaDoc) persister.readIndex( rs, descriptor.getSuffixedIndexAliases(), getSession() ) ).intValue();
115         for ( int i = tempList.size(); i<=index; i++) {
116             tempList.add(i, null);
117         }
118         tempList.set(index, element);
119         return element;
120     }
121
122     public Iterator JavaDoc entries(CollectionPersister persister) {
123         return elements();
124     }
125
126     public void beginRead() {
127         super.beginRead();
128         tempList = new ArrayList JavaDoc();
129     }
130     public boolean endRead() {
131         setInitialized();
132         array = Array.newInstance( elementClass, tempList.size() );
133         for ( int i=0; i<tempList.size(); i++) {
134             Array.set(array, i, tempList.get(i) );
135         }
136         tempList=null;
137         return true;
138     }
139
140     public void beforeInitialize(CollectionPersister persister) {
141         //if (tempList==null) throw new UnsupportedOperationException("Can't lazily initialize arrays");
142
}
143
144     public boolean isDirectlyAccessible() {
145         return true;
146     }
147
148     public void initializeFromCache(CollectionPersister persister, Serializable JavaDoc disassembled, Object JavaDoc owner)
149     throws HibernateException {
150         Serializable JavaDoc[] cached = (Serializable JavaDoc[]) disassembled;
151
152         array = Array.newInstance( persister.getElementClass(), cached.length );
153
154         for ( int i=0; i<cached.length; i++ ) {
155             Array.set( array, i, persister.getElementType().assemble( cached[i], getSession(), owner ) );
156         }
157     }
158
159     public Serializable JavaDoc disassemble(CollectionPersister persister) throws HibernateException {
160         int length = Array.getLength(array);
161         Serializable JavaDoc[] result = new Serializable JavaDoc[length];
162         for ( int i=0; i<length; i++ ) {
163             result[i] = persister.getElementType().disassemble( Array.get(array,i), getSession(), null );
164         }
165
166         /*int length = tempList.size();
167         Serializable[] result = new Serializable[length];
168         for ( int i=0; i<length; i++ ) {
169             result[i] = persister.getElementType().disassemble( tempList.get(i), session );
170         }*/

171
172         return result;
173
174     }
175
176     public Object JavaDoc getValue() {
177         return array;
178     }
179
180     public Iterator JavaDoc getDeletes(CollectionPersister persister, boolean indexIsFormula) throws HibernateException {
181         java.util.List JavaDoc deletes = new ArrayList JavaDoc();
182         Serializable JavaDoc sn = getSnapshot();
183         int snSize = Array.getLength(sn);
184         int arraySize = Array.getLength(array);
185         int end;
186         if ( snSize > arraySize ) {
187             for ( int i=arraySize; i<snSize; i++ ) deletes.add( new Integer JavaDoc(i) );
188             end = arraySize;
189         }
190         else {
191             end = snSize;
192         }
193         for ( int i=0; i<end; i++ ) {
194             if ( Array.get(array, i)==null && Array.get(sn, i)!=null ) deletes.add( new Integer JavaDoc(i) );
195         }
196         return deletes.iterator();
197     }
198
199     public boolean needsInserting(Object JavaDoc entry, int i, Type elemType) throws HibernateException {
200         Serializable JavaDoc sn = getSnapshot();
201         return Array.get(array, i)!=null && ( i >= Array.getLength(sn) || Array.get(sn, i)==null );
202     }
203
204     public boolean needsUpdating(Object JavaDoc entry, int i, Type elemType) throws HibernateException {
205         Serializable JavaDoc sn = getSnapshot();
206         return i<Array.getLength(sn) &&
207             Array.get(sn, i)!=null &&
208             Array.get(array, i)!=null &&
209             elemType.isDirty( Array.get(array, i), Array.get(sn, i), getSession() );
210     }
211
212     public Object JavaDoc getIndex(Object JavaDoc entry, int i, CollectionPersister persister) {
213         return new Integer JavaDoc(i);
214     }
215
216     public Object JavaDoc getElement(Object JavaDoc entry) {
217         return entry;
218     }
219
220     public Object JavaDoc getSnapshotElement(Object JavaDoc entry, int i) {
221         Serializable JavaDoc sn = getSnapshot();
222         return Array.get(sn, i);
223     }
224
225     public boolean entryExists(Object JavaDoc entry, int i) {
226         return entry!=null;
227     }
228
229 }
230
Popular Tags