FileDocCategorySizeDatePackage
PersistentElementHolder.javaAPI DocHibernate 3.2.56974Wed Jul 05 13:17:28 BST 2006org.hibernate.collection

PersistentElementHolder

public class PersistentElementHolder extends AbstractPersistentCollection
A persistent wrapper for an XML element
author
Gavin King

Fields Summary
protected org.dom4j.Element
element
Constructors Summary
public PersistentElementHolder(org.hibernate.engine.SessionImplementor session, org.dom4j.Element element)

		super(session);
		this.element = element;
		setInitialized();
	
public PersistentElementHolder(org.hibernate.engine.SessionImplementor session, org.hibernate.persister.collection.CollectionPersister persister, Serializable key)

		super(session);
		Element owner = (Element) session.getPersistenceContext().getCollectionOwner(key, persister);
		if (owner==null) throw new AssertionFailure("null owner");
		//element = XMLHelper.generateDom4jElement( persister.getNodeName() );
		final String nodeName = persister.getNodeName();
		if ( ".".equals(nodeName) ) {
			element = owner;
		}
		else {
			element = owner.element( nodeName );
			if (element==null) element = owner.addElement( nodeName );
		}
	
Methods Summary
public voidbeforeInitialize(org.hibernate.persister.collection.CollectionPersister persister, int anticipatedSize)

public java.io.Serializabledisassemble(org.hibernate.persister.collection.CollectionPersister persister)

		
		Type elementType = persister.getElementType();
		List elements =  element.elements( persister.getElementNodeName() );
		int length = elements.size();
		Serializable[] result = new Serializable[length];
		for ( int i=0; i<length; i++ ) {
			Element elem = (Element) elements.get(i);
			Object object = elementType.fromXMLNode( elem, persister.getFactory() );
			result[i] = elementType.disassemble( object, getSession(), null );
		}
		return result;
	
public booleanempty()

		return !element.elementIterator().hasNext();
	
public java.util.Iteratorentries(org.hibernate.persister.collection.CollectionPersister persister)

		
		final Type elementType = persister.getElementType();
		List elements =  element.elements( persister.getElementNodeName() );
		int length = elements.size();
		List result = new ArrayList(length);
		for ( int i=0; i<length; i++ ) {
			Element elem = (Element) elements.get(i);
			Object object = elementType.fromXMLNode( elem, persister.getFactory() );
			result.add(object);
		}
		return result.iterator();
	
public booleanentryExists(java.lang.Object entry, int i)

		return entry!=null;
	
public booleanequalsSnapshot(org.hibernate.persister.collection.CollectionPersister persister)

		Type elementType = persister.getElementType();
		
		ArrayList snapshot = (ArrayList) getSnapshot();
		List elements = element.elements( persister.getElementNodeName() );
		if ( snapshot.size()!= elements.size() ) return false;
		for ( int i=0; i<snapshot.size(); i++ ) {
			Object old = snapshot.get(i);
			Element elem = (Element) elements.get(i);
			Object current = elementType.fromXMLNode( elem, persister.getFactory() );
			if ( elementType.isDirty( old, current, getSession() ) ) return false;
		}
		return true;
	
public java.util.IteratorgetDeletes(org.hibernate.persister.collection.CollectionPersister persister, boolean indexIsFormula)

		
		Type elementType = persister.getElementType();
		ArrayList snapshot = (ArrayList) getSnapshot();
		List elements = element.elements( persister.getElementNodeName() );
		ArrayList result = new ArrayList();
		for ( int i=0; i<snapshot.size(); i++ ) {
			Object old = snapshot.get(i);
			if ( i >= elements.size() ) {
				result.add(old);
			}
			else {
				Element elem = (Element) elements.get(i);
				Object object = elementType.fromXMLNode( elem, persister.getFactory() );
				if ( elementType.isDirty( old, object, getSession() ) ) result.add(old);
			}
		}
		return result.iterator();
		
	
public java.lang.ObjectgetElement(java.lang.Object entry)

		return entry;
	
public java.lang.ObjectgetIndex(java.lang.Object entry, int i, org.hibernate.persister.collection.CollectionPersister persister)

		throw new UnsupportedOperationException();
	
public java.util.CollectiongetOrphans(java.io.Serializable snapshot, java.lang.String entityName)

		//orphan delete not supported for EntityMode.DOM4J
		return CollectionHelper.EMPTY_COLLECTION; 
	
public java.io.SerializablegetSnapshot(org.hibernate.persister.collection.CollectionPersister persister)

		
		final Type elementType = persister.getElementType();		
		List elements = element.elements( persister.getElementNodeName() );
		ArrayList snapshot = new ArrayList( elements.size() );
		for ( int i=0; i<elements.size(); i++ ) {
			Element elem = (Element) elements.get(i);
			Object value = elementType.fromXMLNode( elem, persister.getFactory() );
			Object copy = elementType.deepCopy(value , getSession().getEntityMode(), persister.getFactory() );
			snapshot.add(copy);
		}
		return snapshot;
		
	
public java.lang.ObjectgetSnapshotElement(java.lang.Object entry, int i)

		throw new UnsupportedOperationException();
	
public java.lang.ObjectgetValue()

		return element;
	
public voidinitializeFromCache(org.hibernate.persister.collection.CollectionPersister persister, java.io.Serializable disassembled, java.lang.Object owner)

		
		Type elementType = persister.getElementType();
		Serializable[] cached = (Serializable[]) disassembled;
		for ( int i=0; i<cached.length; i++ ) {
			Object object = elementType.assemble( cached[i], getSession(), owner );
			Element subelement = element.addElement( persister.getElementNodeName() );
			elementType.setToXMLNode( subelement, object, persister.getFactory() );
		}
		
	
public booleanisDirectlyAccessible()

		return true;
	
public booleanisSnapshotEmpty(java.io.Serializable snapshot)

		return ( (Collection) snapshot ).isEmpty();
	
public booleanisWrapper(java.lang.Object collection)

		return element==collection;
	
public booleanneedsInserting(java.lang.Object entry, int i, org.hibernate.type.Type elementType)

		ArrayList snapshot = (ArrayList) getSnapshot();
		return i>=snapshot.size() || elementType.isDirty( snapshot.get(i), entry, getSession() );
	
public booleanneedsUpdating(java.lang.Object entry, int i, org.hibernate.type.Type elementType)

		return false;
	
public java.lang.ObjectreadFrom(java.sql.ResultSet rs, org.hibernate.persister.collection.CollectionPersister persister, org.hibernate.loader.CollectionAliases descriptor, java.lang.Object owner)

		Object object = persister.readElement( rs, owner, descriptor.getSuffixedElementAliases(), getSession() );
		final Type elementType = persister.getElementType();
		Element subelement = element.addElement( persister.getElementNodeName() );
		elementType.setToXMLNode( subelement, object, persister.getFactory() ); 
		return object;