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

PersistentIndexedElementHolder

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

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

		super(session);
		this.element = element;
		setInitialized();
	
public PersistentIndexedElementHolder(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 booleanempty()

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

		
		final Type elementType = persister.getElementType();
		String indexNode = getIndexAttributeName(persister);
		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( new IndexedValue( getIndex(elem, indexNode, i), 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();
		String indexNode = getIndexAttributeName(persister);		
		HashMap snapshot = (HashMap) getSnapshot();
		List elements = element.elements( persister.getElementNodeName() );
		if ( snapshot.size()!= elements.size() ) return false;
		for ( int i=0; i<snapshot.size(); i++ ) {
			Element elem = (Element) elements.get(i);
			Object old = snapshot.get( getIndex(elem, indexNode, 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)

		
		final Type indexType = persister.getIndexType();
		HashMap snapshot = (HashMap) getSnapshot();
		HashMap deletes = (HashMap) snapshot.clone();
		deletes.keySet().removeAll( ( (HashMap) getSnapshot(persister) ).keySet() );
		ArrayList deleteList = new ArrayList( deletes.size() );
		Iterator iter = deletes.entrySet().iterator();
		while ( iter.hasNext() ) {
			Map.Entry me = (Map.Entry) iter.next();
			final Object object = indexIsFormula ?
				me.getValue() :
				( (NullableType) indexType ).fromXMLString( (String) me.getKey(), persister.getFactory() );
			if (object!=null) deleteList.add(object);
		}
		
		return deleteList.iterator();
		
	
public java.lang.ObjectgetElement(java.lang.Object entry)

		return ( (IndexedValue) entry ).value;
	
protected static java.lang.StringgetIndex(org.dom4j.Element element, java.lang.String indexNodeName, int i)

		if (indexNodeName!=null) {
			return element.attributeValue(indexNodeName);
		}
		else {
			return Integer.toString(i);
		}
	
public java.lang.ObjectgetIndex(java.lang.Object entry, int i, org.hibernate.persister.collection.CollectionPersister persister)

		String index = ( (IndexedValue) entry ).index;
		final Type indexType = persister.getIndexType();
		return ( (NullableType) indexType ).fromXMLString( index, persister.getFactory() );
	
protected static java.lang.StringgetIndexAttributeName(org.hibernate.persister.collection.CollectionPersister persister)

		String node = persister.getIndexNodeName();
		return node==null ? null : node.substring(1);
	
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();
		String indexNode = getIndexAttributeName(persister);		
		List elements = element.elements( persister.getElementNodeName() );
		HashMap snapshot = new HashMap( 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.put( getIndex(elem, indexNode, i), copy );
		}
		return snapshot;
		
	
public java.lang.ObjectgetSnapshotElement(java.lang.Object entry, int i)

		return ( (HashMap) getSnapshot() ).get( ( (IndexedValue) entry ).index );
	
public java.lang.ObjectgetValue()

		return element;
	
public booleanisDirectlyAccessible()

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

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

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

		HashMap snapshot = (HashMap) getSnapshot();
		IndexedValue iv = (IndexedValue) entry;
		return iv.value!=null && snapshot.get( iv.index )==null;
	
public booleanneedsUpdating(java.lang.Object entry, int i, org.hibernate.type.Type elementType)

		HashMap snapshot = (HashMap) getSnapshot();
		IndexedValue iv = (IndexedValue) entry;
		Object old = snapshot.get( iv.index );
		return old!=null && elementType.isDirty( old, iv.value, getSession() );
	
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();
		final SessionFactoryImplementor factory = persister.getFactory();
		String indexNode = getIndexAttributeName(persister);

		Element elem = element.addElement( persister.getElementNodeName() );
		elementType.setToXMLNode( elem, object, factory ); 
		
		final Type indexType = persister.getIndexType();
		final Object indexValue = persister.readIndex( rs, descriptor.getSuffixedIndexAliases(), getSession() );
		final String index = ( (NullableType) indexType ).toXMLString( indexValue, factory );
		setIndex(elem, indexNode, index);
		return object;
	
protected static voidsetIndex(org.dom4j.Element element, java.lang.String indexNodeName, java.lang.String index)

		if (indexNodeName!=null) element.addAttribute(indexNodeName, index);