1 package org.hibernate.collection; 3 4 import java.io.Serializable ; 5 import java.sql.ResultSet ; 6 import java.sql.SQLException ; 7 import java.util.ArrayList ; 8 import java.util.Collection ; 9 import java.util.Iterator ; 10 import java.util.List ; 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 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 getSnapshot(CollectionPersister persister) 36 throws HibernateException { 37 38 final Type elementType = persister.getElementType(); 39 List elements = element.elements( persister.getElementNodeName() ); 40 ArrayList snapshot = new ArrayList ( elements.size() ); 41 for ( int i=0; i<elements.size(); i++ ) { 42 Element elem = (Element) elements.get(i); 43 Object value = elementType.fromXMLNode( elem, persister.getFactory() ); 44 Object copy = elementType.deepCopy(value , getSession().getEntityMode(), persister.getFactory() ); 45 snapshot.add(copy); 46 } 47 return snapshot; 48 49 } 50 51 public Collection getOrphans(Serializable snapshot, String entityName) 52 throws HibernateException { 53 return CollectionHelper.EMPTY_COLLECTION; 55 } 56 57 public PersistentElementHolder(SessionImplementor session, CollectionPersister persister, Serializable 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 final String 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 collection) { 74 return element==collection; 75 } 76 77 public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException { 78 Type elementType = persister.getElementType(); 79 80 ArrayList snapshot = (ArrayList ) getSnapshot(); 81 List elements = element.elements( persister.getElementNodeName() ); 82 if ( snapshot.size()!= elements.size() ) return false; 83 for ( int i=0; i<snapshot.size(); i++ ) { 84 Object old = snapshot.get(i); 85 Element elem = (Element) elements.get(i); 86 Object 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 snapshot) { 93 return ( (Collection ) snapshot ).isEmpty(); 94 } 95 96 public boolean empty() { 97 return !element.elementIterator().hasNext(); 98 } 99 100 public Object readFrom(ResultSet rs, CollectionPersister persister, CollectionAliases descriptor, Object owner) 101 throws HibernateException, SQLException { 102 Object 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 entries(CollectionPersister persister) { 110 111 final Type elementType = persister.getElementType(); 112 List elements = element.elements( persister.getElementNodeName() ); 113 int length = elements.size(); 114 List result = new ArrayList (length); 115 for ( int i=0; i<length; i++ ) { 116 Element elem = (Element) elements.get(i); 117 Object 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 disassembled, Object owner) 130 throws HibernateException { 131 132 Type elementType = persister.getElementType(); 133 Serializable [] cached = (Serializable []) disassembled; 134 for ( int i=0; i<cached.length; i++ ) { 135 Object 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 disassemble(CollectionPersister persister) throws HibernateException { 143 144 Type elementType = persister.getElementType(); 145 List elements = element.elements( persister.getElementNodeName() ); 146 int length = elements.size(); 147 Serializable [] result = new Serializable [length]; 148 for ( int i=0; i<length; i++ ) { 149 Element elem = (Element) elements.get(i); 150 Object object = elementType.fromXMLNode( elem, persister.getFactory() ); 151 result[i] = elementType.disassemble( object, getSession(), null ); 152 } 153 return result; 154 } 155 156 public Object getValue() { 157 return element; 158 } 159 160 public Iterator getDeletes(CollectionPersister persister, boolean indexIsFormula) 161 throws HibernateException { 162 163 Type elementType = persister.getElementType(); 164 ArrayList snapshot = (ArrayList ) getSnapshot(); 165 List elements = element.elements( persister.getElementNodeName() ); 166 ArrayList result = new ArrayList (); 167 for ( int i=0; i<snapshot.size(); i++ ) { 168 Object 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 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 entry, int i, Type elementType) 183 throws HibernateException { 184 ArrayList snapshot = (ArrayList ) getSnapshot(); 185 return i>=snapshot.size() || elementType.isDirty( snapshot.get(i), entry, getSession() ); 186 } 187 188 public boolean needsUpdating(Object entry, int i, Type elementType) 189 throws HibernateException { 190 return false; 191 } 192 193 public Object getIndex(Object entry, int i, CollectionPersister persister) { 194 throw new UnsupportedOperationException (); 195 } 196 197 public Object getElement(Object entry) { 198 return entry; 199 } 200 201 public Object getSnapshotElement(Object entry, int i) { 202 throw new UnsupportedOperationException (); 203 } 204 205 public boolean entryExists(Object entry, int i) { 206 return entry!=null; 207 } 208 209 } 210 | Popular Tags |