Package org.hibernate.tuple.entity

Source Code of org.hibernate.tuple.entity.Dom4jEntityTuplizer

/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors.  All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA  02110-1301  USA
*
*/
package org.hibernate.tuple.entity;

import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
import org.hibernate.proxy.ProxyFactory;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.dom4j.Dom4jProxyFactory;
import org.hibernate.property.PropertyAccessor;
import org.hibernate.property.PropertyAccessorFactory;
import org.hibernate.property.Getter;
import org.hibernate.property.Setter;
import org.hibernate.EntityMode;
import org.hibernate.HibernateException;
import org.hibernate.tuple.Instantiator;
import org.hibernate.tuple.Dom4jInstantiator;
import org.hibernate.type.AbstractComponentType;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import java.util.Iterator;

/**
* An {@link EntityTuplizer} specific to the dom4j entity mode.
*
* @author Steve Ebersole
* @author Gavin King
*/
public class Dom4jEntityTuplizer extends AbstractEntityTuplizer {

  static final Logger log = LoggerFactory.getLogger( Dom4jEntityTuplizer.class );

  private Set subclassNodeNames = new HashSet();

  Dom4jEntityTuplizer(EntityMetamodel entityMetamodel, PersistentClass mappedEntity) {
    super(entityMetamodel, mappedEntity);
    Iterator itr = mappedEntity.getSubclassClosureIterator();
    while( itr.hasNext() ) {
      final PersistentClass mapping = ( PersistentClass ) itr.next();
      subclassNodeNames.add( mapping.getNodeName() );
    }
  }
 
  public EntityMode getEntityMode() {
    return EntityMode.DOM4J;
  }

  private PropertyAccessor buildPropertyAccessor(Property mappedProperty) {
    if ( mappedProperty.isBackRef() ) {
      return mappedProperty.getPropertyAccessor(null);
    }
    else {
      return PropertyAccessorFactory.getDom4jPropertyAccessor(
          mappedProperty.getNodeName(),
          mappedProperty.getType(),
          getEntityMetamodel().getSessionFactory()
        );
    }
  }

  protected Getter buildPropertyGetter(Property mappedProperty, PersistentClass mappedEntity) {
    return buildPropertyAccessor(mappedProperty).getGetter( null, mappedProperty.getName() );
  }

  protected Setter buildPropertySetter(Property mappedProperty, PersistentClass mappedEntity) {
    return buildPropertyAccessor(mappedProperty).getSetter( null, mappedProperty.getName() );
  }

  protected Instantiator buildInstantiator(PersistentClass persistentClass) {
    return new Dom4jInstantiator( persistentClass );
  }

  public Serializable getIdentifier(Object entityOrId) throws HibernateException {
    if (entityOrId instanceof Element) {
      return super.getIdentifier(entityOrId);
    }
    else {
      //it was not embedded, so the argument is just an id
      return (Serializable) entityOrId;
    }
  }
 
  protected ProxyFactory buildProxyFactory(PersistentClass mappingInfo, Getter idGetter, Setter idSetter) {
    HashSet proxyInterfaces = new HashSet();
    proxyInterfaces.add( HibernateProxy.class );
    proxyInterfaces.add( Element.class );

    ProxyFactory pf = new Dom4jProxyFactory();
    try {
      pf.postInstantiate(
          getEntityName(),
          Element.class,
          proxyInterfaces,
          null,
          null,
          mappingInfo.hasEmbeddedIdentifier() ?
                      (AbstractComponentType) mappingInfo.getIdentifier().getType() :
                      null
      );
    }
    catch ( HibernateException he ) {
      log.warn( "could not create proxy factory for:" + getEntityName(), he );
      pf = null;
    }
    return pf;
  }

  public Class getMappedClass() {
    return Element.class;
  }

  public Class getConcreteProxyClass() {
    return Element.class;
  }

  public boolean isInstrumented() {
    return false;
  }
}
TOP

Related Classes of org.hibernate.tuple.entity.Dom4jEntityTuplizer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.