All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.jpa.graph.internal.AbstractGraphNode Maven / Gradle / Ivy

There is a newer version: 7.0.0.Alpha1
Show newest version
/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 * See the lgpl.txt file in the root directory or .
 */
package org.hibernate.jpa.graph.internal;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.AttributeNode;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.ManagedType;

import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.graph.spi.AttributeNodeImplementor;
import org.hibernate.graph.spi.GraphNodeImplementor;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.jpa.spi.HibernateEntityManagerFactoryAware;

import org.jboss.logging.Logger;

/**
 * Base class for EntityGraph and Subgraph implementations.
 *
 * @author Steve Ebersole
 */
public abstract class AbstractGraphNode implements GraphNodeImplementor, HibernateEntityManagerFactoryAware {
	private static final Logger log = Logger.getLogger( AbstractGraphNode.class );

	private final SessionFactoryImplementor sessionFactory;
	private final boolean mutable;

	private Map> attributeNodeMap;

	@SuppressWarnings("WeakerAccess")
	protected AbstractGraphNode(SessionFactoryImplementor sessionFactory, boolean mutable) {
		this.sessionFactory = sessionFactory;
		this.mutable = mutable;
	}

	@SuppressWarnings("WeakerAccess")
	protected AbstractGraphNode(AbstractGraphNode original, boolean mutable) {
		this.sessionFactory = original.sessionFactory;
		this.mutable = mutable;
		this.attributeNodeMap = makeSafeMapCopy( original.attributeNodeMap );
	}

	private static Map> makeSafeMapCopy(Map> attributeNodeMap) {
		if ( attributeNodeMap == null ) {
			return null;
		}

		final int properSize = CollectionHelper.determineProperSizing( attributeNodeMap );
		final HashMap> copy = new HashMap<>( properSize );
		for ( Map.Entry> attributeNodeEntry : attributeNodeMap.entrySet() ) {
			copy.put(
					attributeNodeEntry.getKey(),
					( ( AttributeNodeImpl ) attributeNodeEntry.getValue() ).makeImmutableCopy()
			);
		}
		return copy;
	}


	@Override
	public SessionFactoryImplementor getFactory() {
		return sessionFactory;
	}

	@Override
	public List> attributeImplementorNodes() {
		if ( attributeNodeMap == null ) {
			return Collections.emptyList();
		}
		else {
			return new ArrayList<>( attributeNodeMap.values() );
		}
	}

	@Override
	public List> attributeNodes() {
		if ( attributeNodeMap == null ) {
			return Collections.emptyList();
		}
		else {
			return new ArrayList<>( attributeNodeMap.values() );
		}
	}

	public void addAttributeNodes(String... attributeNames) {
		for ( String attributeName : attributeNames ) {
			addAttribute( attributeName );
		}
	}

	public AttributeNodeImpl addAttribute(String attributeName) {
		return addAttributeNode( buildAttributeNode( attributeName ) );
	}

	@SuppressWarnings("unchecked")
	private AttributeNodeImpl buildAttributeNode(String attributeName) {
		return buildAttributeNode( resolveAttribute( attributeName ) );
	}

	protected abstract Attribute resolveAttribute(String attributeName);

	@SuppressWarnings("WeakerAccess")
	protected  AttributeNodeImpl buildAttributeNode(Attribute attribute) {
		return new AttributeNodeImpl<>( sessionFactory, getManagedType(), attribute );
	}

	@SuppressWarnings("WeakerAccess")
	protected AttributeNodeImpl addAttributeNode(AttributeNodeImpl attributeNode) {
		if ( ! mutable ) {
			throw new IllegalStateException( "Entity/sub graph is not mutable" );
		}

		if ( attributeNodeMap == null ) {
			attributeNodeMap = new HashMap<>();
		}
		else {
			final AttributeNode old = attributeNodeMap.get( attributeNode.getRegistrationName() );
			if ( old != null ) {
				log.debugf(
						"Encountered request to add entity graph node [%s] using a name [%s] under which another " +
								"node is already registered [%s]",
						old.getClass().getName(),
						attributeNode.getRegistrationName(),
						attributeNode.getClass().getName()
				);
			}
		}
		attributeNodeMap.put( attributeNode.getRegistrationName(), attributeNode );

		return attributeNode;
	}

	@SuppressWarnings("unchecked")
	protected void addAttributeNodes(Attribute... attributes) {
		for ( Attribute attribute : attributes ) {
			addAttribute( attribute );
		}
	}

	@SuppressWarnings("unchecked")
	protected AttributeNodeImpl addAttribute(Attribute attribute) {
		return addAttributeNode( buildAttributeNode( attribute ) );
	}

	@SuppressWarnings("unchecked")
	public  SubgraphImpl addSubgraph(Attribute attribute) {
		return addAttribute( attribute ).makeSubgraph();
	}

	@SuppressWarnings("unchecked")
	public  SubgraphImpl addSubgraph(Attribute attribute, Class type) {
		return addAttribute( attribute ).makeSubgraph( type );
	}

	@SuppressWarnings("unchecked")
	public  SubgraphImpl addSubgraph(String attributeName) {
		return addAttribute( attributeName ).makeSubgraph();
	}

	@SuppressWarnings("unchecked")
	public  SubgraphImpl addSubgraph(String attributeName, Class type) {
		return addAttribute( attributeName ).makeSubgraph( type );
	}

	@SuppressWarnings("unchecked")
	public  SubgraphImpl addKeySubgraph(Attribute attribute) {
		return addAttribute( attribute ).makeKeySubgraph();
	}

	@SuppressWarnings("unchecked")
	public  SubgraphImpl addKeySubgraph(Attribute attribute, Class type) {
		return addAttribute( attribute ).makeKeySubgraph( type );
	}

	@SuppressWarnings("unchecked")
	public  SubgraphImpl addKeySubgraph(String attributeName) {
		return addAttribute( attributeName ).makeKeySubgraph();
	}

	@SuppressWarnings("unchecked")
	public  SubgraphImpl addKeySubgraph(String attributeName, Class type) {
		return addAttribute( attributeName ).makeKeySubgraph( type );
	}
	
	@Override
	public boolean containsAttribute(String name) {
		return attributeNodeMap != null && attributeNodeMap.containsKey( name );
	}

	abstract ManagedType getManagedType();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy