KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: PersistentMapElementHolder.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.HibernateException;
9 import org.hibernate.engine.SessionImplementor;
10 import org.hibernate.persister.collection.CollectionPersister;
11 import org.hibernate.type.NullableType;
12 import org.hibernate.type.Type;
13
14 /**
15  * @author Gavin King
16  */

17 public class PersistentMapElementHolder extends PersistentIndexedElementHolder {
18
19     public PersistentMapElementHolder(SessionImplementor session, Element element) {
20         super( session, element );
21     }
22
23     public PersistentMapElementHolder(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         Type indexType = persister.getIndexType();
33         final String JavaDoc indexNodeName = getIndexAttributeName(persister);
34
35         Serializable JavaDoc[] cached = (Serializable JavaDoc[]) disassembled;
36
37         for ( int i=0; i<cached.length; ) {
38             Object JavaDoc index = indexType.assemble( cached[i++], getSession(), owner );
39             Object JavaDoc object = elementType.assemble( cached[i++], getSession(), owner );
40             
41             Element subelement = element.addElement( persister.getElementNodeName() );
42             elementType.setToXMLNode( subelement, object, persister.getFactory() );
43             
44             String JavaDoc indexString = ( (NullableType) indexType ).toXMLString( index, persister.getFactory() );
45             setIndex( subelement, indexNodeName, indexString );
46         }
47         
48     }
49
50     public Serializable JavaDoc disassemble(CollectionPersister persister) throws HibernateException {
51         
52         Type elementType = persister.getElementType();
53         Type indexType = persister.getIndexType();
54         final String JavaDoc indexNodeName = getIndexAttributeName(persister);
55
56         List JavaDoc elements = element.elements( persister.getElementNodeName() );
57         int length = elements.size();
58         Serializable JavaDoc[] result = new Serializable JavaDoc[length*2];
59         for ( int i=0; i<length*2; ) {
60             Element elem = (Element) elements.get(i/2);
61             Object JavaDoc object = elementType.fromXMLNode( elem, persister.getFactory() );
62             final String JavaDoc indexString = getIndex(elem, indexNodeName, i);
63             Object JavaDoc index = ( (NullableType) indexType ).fromXMLString( indexString, persister.getFactory() );
64             result[i++] = indexType.disassemble( index, getSession(), null );
65             result[i++] = elementType.disassemble( object, getSession(), null );
66         }
67         return result;
68     }
69
70
71 }
72
Popular Tags