KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: PersistentElementHolder.java,v 1.21 2005/06/28 08:59:06 maxcsaucdk Exp $
2
package org.hibernate.collection;
3
4 import java.io.Serializable JavaDoc;
5 import java.sql.ResultSet JavaDoc;
6 import java.sql.SQLException JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.Collection JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.List JavaDoc;
11
12 import org.dom4j.Element;
13 import org.hibernate.AssertionFailure;
14 import org.hibernate.HibernateException;
15 import org.hibernate.loader.CollectionAliases;
16 import org.hibernate.engine.SessionImplementor;
17 import org.hibernate.persister.collection.CollectionPersister;
18 import org.hibernate.type.Type;
19 import org.hibernate.util.CollectionHelper;
20
21 /**
22  * A persistent wrapper for an XML element
23  *
24  * @author Gavin King
25  */

26 public class PersistentElementHolder extends AbstractPersistentCollection {
27     protected Element element;
28     
29     public PersistentElementHolder(SessionImplementor session, Element element) {
30         super(session);
31         this.element = element;
32         setInitialized();
33     }
34
35     public Serializable JavaDoc getSnapshot(CollectionPersister persister)
36     throws HibernateException {
37         
38         final Type elementType = persister.getElementType();
39         List JavaDoc elements = element.elements( persister.getElementNodeName() );
40         ArrayList JavaDoc snapshot = new ArrayList JavaDoc( elements.size() );
41         for ( int i=0; i<elements.size(); i++ ) {
42             Element elem = (Element) elements.get(i);
43             Object JavaDoc value = elementType.fromXMLNode( elem, persister.getFactory() );
44             Object JavaDoc copy = elementType.deepCopy(value , getSession().getEntityMode(), persister.getFactory() );
45             snapshot.add(copy);
46         }
47         return snapshot;
48         
49     }
50
51     public Collection JavaDoc getOrphans(Serializable JavaDoc snapshot, String JavaDoc entityName)
52     throws HibernateException {
53         //orphan delete not supported for EntityMode.DOM4J
54
return CollectionHelper.EMPTY_COLLECTION;
55     }
56
57     public PersistentElementHolder(SessionImplementor session, CollectionPersister persister, Serializable JavaDoc key)
58     throws HibernateException {
59         super(session);
60         Element owner = (Element) session.getPersistenceContext().getCollectionOwner(key, persister);
61         if (owner==null) throw new AssertionFailure("null owner");
62         //element = XMLHelper.generateDom4jElement( persister.getNodeName() );
63
final String JavaDoc nodeName = persister.getNodeName();
64         if ( ".".equals(nodeName) ) {
65             element = owner;
66         }
67         else {
68             element = owner.element( nodeName );
69             if (element==null) element = owner.addElement( nodeName );
70         }
71     }
72
73     public boolean isWrapper(Object JavaDoc collection) {
74         return element==collection;
75     }
76
77     public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException {
78         Type elementType = persister.getElementType();
79         
80         ArrayList JavaDoc snapshot = (ArrayList JavaDoc) getSnapshot();
81         List JavaDoc elements = element.elements( persister.getElementNodeName() );
82         if ( snapshot.size()!= elements.size() ) return false;
83         for ( int i=0; i<snapshot.size(); i++ ) {
84             Object JavaDoc old = snapshot.get(i);
85             Element elem = (Element) elements.get(i);
86             Object JavaDoc current = elementType.fromXMLNode( elem, persister.getFactory() );
87             if ( elementType.isDirty( old, current, getSession() ) ) return false;
88         }
89         return true;
90     }
91
92     public boolean isSnapshotEmpty(Serializable JavaDoc snapshot) {
93         return ( (Collection JavaDoc) snapshot ).isEmpty();
94     }
95     
96     public boolean empty() {
97         return !element.elementIterator().hasNext();
98     }
99
100     public Object JavaDoc readFrom(ResultSet JavaDoc rs, CollectionPersister persister, CollectionAliases descriptor, Object JavaDoc owner)
101     throws HibernateException, SQLException JavaDoc {
102         Object JavaDoc object = persister.readElement( rs, owner, descriptor.getSuffixedElementAliases(), getSession() );
103         final Type elementType = persister.getElementType();
104         Element subelement = element.addElement( persister.getElementNodeName() );
105         elementType.setToXMLNode( subelement, object, persister.getFactory() );
106         return object;
107     }
108
109     public Iterator JavaDoc entries(CollectionPersister persister) {
110         
111         final Type elementType = persister.getElementType();
112         List JavaDoc elements = element.elements( persister.getElementNodeName() );
113         int length = elements.size();
114         List JavaDoc result = new ArrayList JavaDoc(length);
115         for ( int i=0; i<length; i++ ) {
116             Element elem = (Element) elements.get(i);
117             Object JavaDoc object = elementType.fromXMLNode( elem, persister.getFactory() );
118             result.add(object);
119         }
120         return result.iterator();
121     }
122
123     public void beforeInitialize(CollectionPersister persister) {}
124
125     public boolean isDirectlyAccessible() {
126         return true;
127     }
128
129     public void initializeFromCache(CollectionPersister persister, Serializable JavaDoc disassembled, Object JavaDoc owner)
130     throws HibernateException {
131         
132         Type elementType = persister.getElementType();
133         Serializable JavaDoc[] cached = (Serializable JavaDoc[]) disassembled;
134         for ( int i=0; i<cached.length; i++ ) {
135             Object JavaDoc object = elementType.assemble( cached[i], getSession(), owner );
136             Element subelement = element.addElement( persister.getElementNodeName() );
137             elementType.setToXMLNode( subelement, object, persister.getFactory() );
138         }
139         
140     }
141
142     public Serializable JavaDoc disassemble(CollectionPersister persister) throws HibernateException {
143         
144         Type elementType = persister.getElementType();
145         List JavaDoc elements = element.elements( persister.getElementNodeName() );
146         int length = elements.size();
147         Serializable JavaDoc[] result = new Serializable JavaDoc[length];
148         for ( int i=0; i<length; i++ ) {
149             Element elem = (Element) elements.get(i);
150             Object JavaDoc object = elementType.fromXMLNode( elem, persister.getFactory() );
151             result[i] = elementType.disassemble( object, getSession(), null );
152         }
153         return result;
154     }
155
156     public Object JavaDoc getValue() {
157         return element;
158     }
159
160     public Iterator JavaDoc getDeletes(CollectionPersister persister, boolean indexIsFormula)
161     throws HibernateException {
162         
163         Type elementType = persister.getElementType();
164         ArrayList JavaDoc snapshot = (ArrayList JavaDoc) getSnapshot();
165         List JavaDoc elements = element.elements( persister.getElementNodeName() );
166         ArrayList JavaDoc result = new ArrayList JavaDoc();
167         for ( int i=0; i<snapshot.size(); i++ ) {
168             Object JavaDoc old = snapshot.get(i);
169             if ( i >= elements.size() ) {
170                 result.add(old);
171             }
172             else {
173                 Element elem = (Element) elements.get(i);
174                 Object JavaDoc object = elementType.fromXMLNode( elem, persister.getFactory() );
175                 if ( elementType.isDirty( old, object, getSession() ) ) result.add(old);
176             }
177         }
178         return result.iterator();
179         
180     }
181
182     public boolean needsInserting(Object JavaDoc entry, int i, Type elementType)
183     throws HibernateException {
184         ArrayList JavaDoc snapshot = (ArrayList JavaDoc) getSnapshot();
185         return i>=snapshot.size() || elementType.isDirty( snapshot.get(i), entry, getSession() );
186     }
187
188     public boolean needsUpdating(Object JavaDoc entry, int i, Type elementType)
189     throws HibernateException {
190         return false;
191     }
192
193     public Object JavaDoc getIndex(Object JavaDoc entry, int i, CollectionPersister persister) {
194         throw new UnsupportedOperationException JavaDoc();
195     }
196
197     public Object JavaDoc getElement(Object JavaDoc entry) {
198         return entry;
199     }
200
201     public Object JavaDoc getSnapshotElement(Object JavaDoc entry, int i) {
202         throw new UnsupportedOperationException JavaDoc();
203     }
204
205     public boolean entryExists(Object JavaDoc entry, int i) {
206         return entry!=null;
207     }
208
209 }
210
Popular Tags