package org.hibernate.engine.spi;
import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.persistence.EntityGraph;
import org.hibernate.Filter;
import org.hibernate.UnknownProfileException;
import org.hibernate.internal.FilterImpl;
import org.hibernate.type.Type;
public class LoadQueryInfluencers implements Serializable {
public static final LoadQueryInfluencers NONE = new LoadQueryInfluencers();
private final SessionFactoryImplementor sessionFactory;
private String internalFetchProfile;
private final Map<String,Filter> enabledFilters;
private final Set<String> enabledFetchProfileNames;
private EntityGraph fetchGraph;
private EntityGraph loadGraph;
public LoadQueryInfluencers() {
this( null );
}
public LoadQueryInfluencers(SessionFactoryImplementor sessionFactory) {
this( sessionFactory, new HashMap<String,Filter>(), new HashSet<String>() );
}
private LoadQueryInfluencers(SessionFactoryImplementor sessionFactory, Map<String,Filter> enabledFilters, Set<String> enabledFetchProfileNames) {
this.sessionFactory = sessionFactory;
this.enabledFilters = enabledFilters;
this.enabledFetchProfileNames = enabledFetchProfileNames;
}
public SessionFactoryImplementor getSessionFactory() {
return sessionFactory;
}
public String getInternalFetchProfile() {
return internalFetchProfile;
}
public void setInternalFetchProfile(String internalFetchProfile) {
if ( sessionFactory == null ) {
throw new IllegalStateException( "Cannot modify context-less LoadQueryInfluencers" );
}
this.internalFetchProfile = internalFetchProfile;
}
public boolean hasEnabledFilters() {
return !enabledFilters.isEmpty();
}
public Map<String,Filter> getEnabledFilters() {
for ( Filter filter : enabledFilters.values() ) {
filter.validate();
}
return enabledFilters;
}
public Set<String> getEnabledFilterNames() {
return java.util.Collections.unmodifiableSet( enabledFilters.keySet() );
}
public Filter getEnabledFilter(String filterName) {
return enabledFilters.get( filterName );
}
public Filter enableFilter(String filterName) {
FilterImpl filter = new FilterImpl( sessionFactory.getFilterDefinition( filterName ) );
enabledFilters.put( filterName, filter );
return filter;
}
public void disableFilter(String filterName) {
enabledFilters.remove( filterName );
}
public Object getFilterParameterValue(String filterParameterName) {
final String[] parsed = parseFilterParameterName( filterParameterName );
final FilterImpl filter = (FilterImpl) enabledFilters.get( parsed[0] );
if ( filter == null ) {
throw new IllegalArgumentException( "Filter [" + parsed[0] + "] currently not enabled" );
}
return filter.getParameter( parsed[1] );
}
public Type getFilterParameterType(String filterParameterName) {
final String[] parsed = parseFilterParameterName( filterParameterName );
final FilterDefinition filterDef = sessionFactory.getFilterDefinition( parsed[0] );
if ( filterDef == null ) {
throw new IllegalArgumentException( "Filter [" + parsed[0] + "] not defined" );
}
final Type type = filterDef.getParameterType( parsed[1] );
if ( type == null ) {
throw new InternalError( "Unable to locate type for filter parameter" );
}
return type;
}
public static String[] parseFilterParameterName(String filterParameterName) {
int dot = filterParameterName.indexOf( '.' );
if ( dot <= 0 ) {
throw new IllegalArgumentException( "Invalid filter-parameter name format" );
}
final String filterName = filterParameterName.substring( 0, dot );
final String parameterName = filterParameterName.substring( dot + 1 );
return new String[] { filterName, parameterName };
}
public boolean hasEnabledFetchProfiles() {
return !enabledFetchProfileNames.isEmpty();
}
public Set<String> getEnabledFetchProfileNames() {
return enabledFetchProfileNames;
}
private void checkFetchProfileName(String name) {
if ( !sessionFactory.containsFetchProfileDefinition( name ) ) {
throw new UnknownProfileException( name );
}
}
public boolean isFetchProfileEnabled(String name) throws UnknownProfileException {
checkFetchProfileName( name );
return enabledFetchProfileNames.contains( name );
}
public void enableFetchProfile(String name) throws UnknownProfileException {
checkFetchProfileName( name );
enabledFetchProfileNames.add( name );
}
public void disableFetchProfile(String name) throws UnknownProfileException {
checkFetchProfileName( name );
enabledFetchProfileNames.remove( name );
}
public EntityGraph getFetchGraph() {
return fetchGraph;
}
public void setFetchGraph(final EntityGraph fetchGraph) {
this.fetchGraph = fetchGraph;
}
public EntityGraph getLoadGraph() {
return loadGraph;
}
public void setLoadGraph(final EntityGraph loadGraph) {
this.loadGraph = loadGraph;
}
}