001: //$Id: ReattachVisitor.java 10949 2006-12-07 21:53:41Z steve.ebersole@jboss.com $
002: package org.hibernate.event.def;
003:
004: import java.io.Serializable;
005:
006: import org.apache.commons.logging.Log;
007: import org.apache.commons.logging.LogFactory;
008: import org.hibernate.HibernateException;
009: import org.hibernate.action.CollectionRemoveAction;
010: import org.hibernate.event.EventSource;
011: import org.hibernate.persister.collection.CollectionPersister;
012: import org.hibernate.pretty.MessageHelper;
013: import org.hibernate.type.AbstractComponentType;
014: import org.hibernate.type.Type;
015:
016: /**
017: * Abstract superclass of visitors that reattach collections.
018: *
019: * @author Gavin King
020: */
021: public abstract class ReattachVisitor extends ProxyVisitor {
022:
023: private static final Log log = LogFactory
024: .getLog(ReattachVisitor.class);
025:
026: private final Serializable ownerIdentifier;
027: private final Object owner;
028:
029: public ReattachVisitor(EventSource session,
030: Serializable ownerIdentifier, Object owner) {
031: super (session);
032: this .ownerIdentifier = ownerIdentifier;
033: this .owner = owner;
034: }
035:
036: /**
037: * Retrieve the identifier of the entity being visited.
038: *
039: * @return The entity's identifier.
040: */
041: final Serializable getOwnerIdentifier() {
042: return ownerIdentifier;
043: }
044:
045: /**
046: * Retrieve the entity being visited.
047: *
048: * @return The entity.
049: */
050: final Object getOwner() {
051: return owner;
052: }
053:
054: /**
055: * {@inheritDoc}
056: */
057: Object processComponent(Object component,
058: AbstractComponentType componentType)
059: throws HibernateException {
060: Type[] types = componentType.getSubtypes();
061: if (component == null) {
062: processValues(new Object[types.length], types);
063: } else {
064: super .processComponent(component, componentType);
065: }
066:
067: return null;
068: }
069:
070: /**
071: * Schedules a collection for deletion.
072: *
073: * @param role The persister representing the collection to be removed.
074: * @param collectionKey The collection key (differs from owner-id in the case of property-refs).
075: * @param source The session from which the request originated.
076: * @throws HibernateException
077: */
078: void removeCollection(CollectionPersister role,
079: Serializable collectionKey, EventSource source)
080: throws HibernateException {
081: if (log.isTraceEnabled()) {
082: log.trace("collection dereferenced while transient "
083: + MessageHelper.collectionInfoString(role,
084: ownerIdentifier, source.getFactory()));
085: }
086: source.getActionQueue().addAction(
087: new CollectionRemoveAction(null, role, collectionKey,
088: false, source));
089: }
090:
091: /**
092: * This version is slightly different in that here we need to assume that
093: * the owner is not yet associated with the session, and thus we cannot
094: * rely on the owner's EntityEntry snapshot...
095: *
096: * @param role The persister for the collection role being processed.
097: * @return
098: */
099: final Serializable extractCollectionKeyFromOwner(
100: CollectionPersister role) {
101: if (role.getCollectionType().useLHSPrimaryKey()) {
102: return ownerIdentifier;
103: } else {
104: return (Serializable) role.getOwnerEntityPersister()
105: .getPropertyValue(
106: owner,
107: role.getCollectionType()
108: .getLHSPropertyName(),
109: getSession().getEntityMode());
110: }
111:
112: }
113: }
|