KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: PersistentListElementHolder.java,v 1.5 2005/05/20 19:50:07 oneovthafew Exp $
2
package org.hibernate.collection;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.List JavaDoc;
6
7 import org.dom4j.Element;
8 import org.hibernate.Hibernate;
9 import org.hibernate.HibernateException;
10 import org.hibernate.engine.SessionImplementor;
11 import org.hibernate.persister.collection.CollectionPersister;
12 import org.hibernate.type.Type;
13
14 /**
15  * @author Gavin King
16  */

17 public class PersistentListElementHolder extends PersistentIndexedElementHolder {
18
19     public PersistentListElementHolder(SessionImplementor session, Element element) {
20         super( session, element );
21     }
22
23     public PersistentListElementHolder(SessionImplementor session, CollectionPersister persister,
24             Serializable JavaDoc key) throws HibernateException {
25         super( session, persister, key );
26     }
27
28     public void initializeFromCache(CollectionPersister persister, Serializable JavaDoc disassembled, Object JavaDoc owner)
29     throws HibernateException {
30         
31         Type elementType = persister.getElementType();
32         final String JavaDoc indexNodeName = getIndexAttributeName(persister);
33         Serializable JavaDoc[] cached = (Serializable JavaDoc[]) disassembled;
34         for ( int i=0; i<cached.length; i++ ) {
35             Object JavaDoc object = elementType.assemble( cached[i], getSession(), owner );
36             Element subelement = element.addElement( persister.getElementNodeName() );
37             elementType.setToXMLNode( subelement, object, persister.getFactory() );
38             setIndex( subelement, indexNodeName, Integer.toString(i) );
39         }
40         
41     }
42
43     public Serializable JavaDoc disassemble(CollectionPersister persister) throws HibernateException {
44                 
45         Type elementType = persister.getElementType();
46         final String JavaDoc indexNodeName = getIndexAttributeName(persister);
47         List JavaDoc elements = element.elements( persister.getElementNodeName() );
48         int length = elements.size();
49         Serializable JavaDoc[] result = new Serializable JavaDoc[length];
50         for ( int i=0; i<length; i++ ) {
51             Element elem = (Element) elements.get(i);
52             Object JavaDoc object = elementType.fromXMLNode( elem, persister.getFactory() );
53             Integer JavaDoc index = (Integer JavaDoc) Hibernate.INTEGER.fromStringValue( getIndex(elem, indexNodeName, i) );
54             result[ index.intValue() ] = elementType.disassemble( object, getSession(), null );
55         }
56         return result;
57     }
58
59
60 }
61
Popular Tags