package org.hibernate.collection.internal;
import java.io.Serializable;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.type.Type;
import org.hibernate.type.XmlRepresentableType;
import org.dom4j.Element;
@SuppressWarnings({"UnusedDeclaration", "deprecation"})
@Deprecated
public class PersistentMapElementHolder extends PersistentIndexedElementHolder {
public PersistentMapElementHolder(SessionImplementor session, Element element) {
super( session, element );
}
public PersistentMapElementHolder(SessionImplementor session, CollectionPersister persister, Serializable key) {
super( session, persister, key );
}
@Override
@SuppressWarnings("unchecked")
public void initializeFromCache(CollectionPersister persister, Serializable disassembled, Object owner) {
final Type elementType = persister.getElementType();
final Type indexType = persister.getIndexType();
final String indexNodeName = getIndexAttributeName( persister );
final Serializable[] cached = (Serializable[]) disassembled;
int i = 0;
while ( i < cached.length ) {
final Object index = indexType.assemble( cached[i++], getSession(), owner );
final Object object = elementType.assemble( cached[i++], getSession(), owner );
final Element subElement = element.addElement( persister.getElementNodeName() );
elementType.setToXMLNode( subElement, object, persister.getFactory() );
final String indexString = ( (XmlRepresentableType) indexType ).toXMLString( index, persister.getFactory() );
setIndex( subElement, indexNodeName, indexString );
}
}
@Override
@SuppressWarnings("unchecked")
public Serializable disassemble(CollectionPersister persister) throws HibernateException {
final Type elementType = persister.getElementType();
final Type indexType = persister.getIndexType();
final String indexNodeName = getIndexAttributeName( persister );
final List elements = element.elements( persister.getElementNodeName() );
final int length = elements.size();
final Serializable[] result = new Serializable[length*2];
int i = 0;
while ( i < length*2 ) {
final Element elem = (Element) elements.get( i/2 );
final Object object = elementType.fromXMLNode( elem, persister.getFactory() );
final String indexString = getIndex( elem, indexNodeName, i );
final Object index = ( (XmlRepresentableType) indexType ).fromXMLString( indexString, persister.getFactory() );
result[i++] = indexType.disassemble( index, getSession(), null );
result[i++] = elementType.disassemble( object, getSession(), null );
}
return result;
}
}