HHH-7038 - Define sources for filters, filter-defs, type-defs, fetch-profiles

This commit is contained in:
Steve Ebersole 2012-02-08 16:00:35 -06:00
parent b847b0598a
commit dc8afc67ce
12 changed files with 687 additions and 31 deletions

View File

@ -0,0 +1,101 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, Red Hat Inc. 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 Inc.
*
* 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.metamodel.internal.source.annotations;
import java.util.ArrayList;
import java.util.List;
import org.jboss.jandex.AnnotationInstance;
import org.hibernate.MappingException;
import org.hibernate.annotations.FetchMode;
import org.hibernate.metamodel.spi.binding.FetchProfile;
import org.hibernate.metamodel.spi.source.FetchProfileSource;
/**
* @author Steve Ebersole
*/
public class FetchProfileSourceImpl implements FetchProfileSource {
private final String name;
private final List<AssociationOverrideSource> associationOverrideSources;
public FetchProfileSourceImpl(AnnotationInstance fetchProfileAnnotation) {
this.name = JandexHelper.getValue( fetchProfileAnnotation, "name", String.class );
this.associationOverrideSources = buildAssociationOverrideSources( fetchProfileAnnotation );
}
@Override
public String getName() {
return name;
}
@Override
public Iterable<AssociationOverrideSource> getAssociationOverrides() {
return associationOverrideSources;
}
private static List<AssociationOverrideSource> buildAssociationOverrideSources(AnnotationInstance fetchProfileAnnotation) {
final List<AssociationOverrideSource> associationOverrideSources = new ArrayList<AssociationOverrideSource>();
AnnotationInstance[] overrideAnnotations = JandexHelper.getValue(
fetchProfileAnnotation,
"fetchOverrides",
AnnotationInstance[].class
);
for ( AnnotationInstance overrideAnnotation : overrideAnnotations ) {
associationOverrideSources.add( new AssociationOverrideSourceImpl( overrideAnnotation ) );
}
return associationOverrideSources;
}
private static class AssociationOverrideSourceImpl implements AssociationOverrideSource {
private final String entityName;
private final String attributeName;
private final String fetchMode;
private AssociationOverrideSourceImpl(AnnotationInstance overrideAnnotation) {
this.entityName = JandexHelper.getValue( overrideAnnotation, "entity", String.class );
this.attributeName = JandexHelper.getValue( overrideAnnotation, "association", String.class );
FetchMode fetchMode = JandexHelper.getEnumValue( overrideAnnotation, "mode", FetchMode.class );
if ( !fetchMode.equals( org.hibernate.annotations.FetchMode.JOIN ) ) {
throw new MappingException( "Only FetchMode.JOIN is currently supported" );
}
this.fetchMode = "join";
}
@Override
public String getEntityName() {
return entityName;
}
@Override
public String getAttributeName() {
return attributeName;
}
@Override
public String getFetchModeName() {
return fetchMode;
}
}
}

View File

@ -0,0 +1,90 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, Red Hat Inc. 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 Inc.
*
* 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.metamodel.internal.source.annotations;
import java.util.ArrayList;
import java.util.List;
import org.jboss.jandex.AnnotationInstance;
import org.hibernate.metamodel.spi.source.FilterDefSource;
import org.hibernate.metamodel.spi.source.FilterParameterSource;
/**
* @author Steve Ebersole
*/
public class FilterDefSourceImpl implements FilterDefSource {
private final String name;
private final String condition;
private List<FilterParameterSource> parameterSources;
public FilterDefSourceImpl(AnnotationInstance filerDefAnnotation) {
this.name = JandexHelper.getValue( filerDefAnnotation, "name", String.class );
this.condition = JandexHelper.getValue( filerDefAnnotation, "defaultCondition", String.class );
this.parameterSources = buildParameterSources( filerDefAnnotation );
}
private List<FilterParameterSource> buildParameterSources(AnnotationInstance filerDefAnnotation) {
final List<FilterParameterSource> parameterSources = new ArrayList<FilterParameterSource>();
for ( AnnotationInstance paramAnnotation : JandexHelper.getValue( filerDefAnnotation, "parameters", AnnotationInstance[].class ) ) {
parameterSources.add( new FilterParameterSourceImpl( paramAnnotation ) );
}
return parameterSources;
}
@Override
public String getName() {
return name;
}
@Override
public String getCondition() {
return condition;
}
@Override
public Iterable<FilterParameterSource> getParameterSources() {
return parameterSources;
}
private static class FilterParameterSourceImpl implements FilterParameterSource {
private final String name;
private final String type;
public FilterParameterSourceImpl(AnnotationInstance paramAnnotation) {
this.name = JandexHelper.getValue( paramAnnotation, "name", String.class );
this.type = JandexHelper.getValue( paramAnnotation, "type", String.class );
}
@Override
public String getParameterName() {
return name;
}
@Override
public String getParameterValueTyeName() {
return type;
}
}
}

View File

@ -21,25 +21,31 @@
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.metamodel.spi.source;
package org.hibernate.metamodel.internal.source.annotations;
import org.jboss.jandex.AnnotationInstance;
import org.hibernate.metamodel.spi.source.FilterSource;
/**
* Defines a source of configuration value.
*
* @author Steve Ebersole
*/
public interface ConfigurationValueSource {
/**
* Get the configuration name.
*
* @return The configuration name
*/
public String getName();
public class FilterSourceImpl implements FilterSource {
private final String name;
private final String condition;
/**
* Get the configuration value
*
* @return The configuration value.
*/
public String getValue();
public FilterSourceImpl(AnnotationInstance filerDefAnnotation) {
this.name = JandexHelper.getValue( filerDefAnnotation, "name", String.class );
this.condition = JandexHelper.getValue( filerDefAnnotation, "condition", String.class );
}
@Override
public String getName() {
return name;
}
@Override
public String getCondition() {
return condition;
}
}

View File

@ -0,0 +1,107 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, Red Hat Inc. 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 Inc.
*
* 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.metamodel.internal.source.annotations;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.jboss.jandex.AnnotationInstance;
import org.hibernate.AnnotationException;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.spi.source.TypeDescriptorSource;
/**
* @author Steve Ebersole
*/
public class TypeDescriptorSourceImpl implements TypeDescriptorSource {
private final String name;
private final String implementationClassName;
private final String registrationKey;
private Map<String, String> parameterValueMap;
public TypeDescriptorSourceImpl(AnnotationInstance typeDefAnnotation) {
this.name = JandexHelper.getValue( typeDefAnnotation, "name", String.class );
this.implementationClassName = JandexHelper.getValue( typeDefAnnotation, "typeClass", String.class );
String defaultForType = JandexHelper.getValue( typeDefAnnotation, "defaultForType", String.class );
if ( defaultForType != null ) {
if ( void.class.getName().equals( defaultForType ) ) {
defaultForType = null;
}
}
registrationKey = defaultForType;
if ( StringHelper.isEmpty( name ) && registrationKey == null ) {
throw new AnnotationException(
String.format(
"Either name or defaultForType (or both) must be set on TypeDef [%s]",
implementationClassName
)
);
}
this.parameterValueMap = extractParameterValues( typeDefAnnotation );
}
private Map<String, String> extractParameterValues(AnnotationInstance typeDefAnnotation) {
Map<String, String> parameterMaps = new HashMap<String, String>();
AnnotationInstance[] parameterAnnotations = JandexHelper.getValue(
typeDefAnnotation,
"parameters",
AnnotationInstance[].class
);
for ( AnnotationInstance parameterAnnotation : parameterAnnotations ) {
parameterMaps.put(
JandexHelper.getValue( parameterAnnotation, "name", String.class ),
JandexHelper.getValue( parameterAnnotation, "value", String.class )
);
}
return parameterMaps;
}
@Override
public String getName() {
return name;
}
@Override
public String getTypeImplementationClassName() {
return implementationClassName;
}
@Override
public Iterable<String> getRegistrationKeys() {
return registrationKey == null
? Collections.<String>emptyList()
: Collections.singletonList( registrationKey );
}
@Override
public Map<String, String> getParameters() {
return parameterValueMap;
}
}

View File

@ -0,0 +1,88 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, Red Hat Inc. 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 Inc.
*
* 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.metamodel.internal.source.hbm;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbFetchProfileElement;
import org.hibernate.metamodel.spi.source.FetchProfileSource;
/**
* @author Steve Ebersole
*/
public class FetchProfileSourceImpl implements FetchProfileSource {
private final String name;
private final List<AssociationOverrideSource> associationOverrideSources;
public FetchProfileSourceImpl(JaxbFetchProfileElement fetchProfileElement) {
this.name = fetchProfileElement.getName();
this.associationOverrideSources = buildAssociationOverrideSources( fetchProfileElement );
}
@Override
public String getName() {
return name;
}
@Override
public Iterable<AssociationOverrideSource> getAssociationOverrides() {
return associationOverrideSources;
}
private static List<AssociationOverrideSource> buildAssociationOverrideSources(JaxbFetchProfileElement fetchProfileElement) {
final List<AssociationOverrideSource> associationOverrideSources = new ArrayList<AssociationOverrideSource>();
for ( JaxbFetchProfileElement.JaxbFetch fetch : fetchProfileElement.getFetch() ) {
associationOverrideSources.add( new AssociationOverrideSourceImpl( fetch ) );
}
return associationOverrideSources;
}
private static class AssociationOverrideSourceImpl implements AssociationOverrideSource {
private final String entityName;
private final String attributeName;
private final String fetchMode;
private AssociationOverrideSourceImpl(JaxbFetchProfileElement.JaxbFetch fetchElement) {
this.entityName = fetchElement.getEntity();
this.attributeName = fetchElement.getAssociation();
this.fetchMode = fetchElement.getStyle();
}
@Override
public String getEntityName() {
return entityName;
}
@Override
public String getAttributeName() {
return attributeName;
}
@Override
public String getFetchModeName() {
return fetchMode;
}
}
}

View File

@ -0,0 +1,97 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, Red Hat Inc. 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 Inc.
*
* 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.metamodel.internal.source.hbm;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbHibernateMapping;
import org.hibernate.metamodel.spi.source.FilterDefSource;
import org.hibernate.metamodel.spi.source.FilterParameterSource;
/**
* @author Steve Ebersole
*/
public class FilterDefSourceImpl implements FilterDefSource {
private final String name;
private final String condition;
private List<FilterParameterSource> parameterSources;
public FilterDefSourceImpl(JaxbHibernateMapping.JaxbFilterDef filterDefElement) {
this.name = filterDefElement.getName();
String conditionAttribute = filterDefElement.getCondition();
String conditionContent = null;
final List<FilterParameterSource> parameterSources = new ArrayList<FilterParameterSource>();
for ( Object content : filterDefElement.getContent() ) {
if ( String.class.isInstance( content ) ){
conditionContent = (String) content;
}
else {
parameterSources.add(
new FilterParameterSourceImpl( (JaxbHibernateMapping.JaxbFilterDef.JaxbFilterParam) content )
);
}
}
this.condition = Helper.coalesce( conditionContent, conditionAttribute );
this.parameterSources = parameterSources;
}
@Override
public String getName() {
return name;
}
@Override
public String getCondition() {
return condition;
}
@Override
public Iterable<FilterParameterSource> getParameterSources() {
return parameterSources;
}
private static class FilterParameterSourceImpl implements FilterParameterSource {
private final String name;
private final String type;
public FilterParameterSourceImpl(JaxbHibernateMapping.JaxbFilterDef.JaxbFilterParam filterParamElement) {
this.name = filterParamElement.getName();
this.type = filterParamElement.getType();
}
@Override
public String getParameterName() {
return name;
}
@Override
public String getParameterValueTyeName() {
return type;
}
}
}

View File

@ -0,0 +1,53 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, Red Hat Inc. 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 Inc.
*
* 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.metamodel.internal.source.hbm;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbFilterElement;
import org.hibernate.metamodel.spi.source.FilterSource;
/**
* @author Steve Ebersole
*/
public class FilterSourceImpl implements FilterSource {
private final String name;
private final String condition;
public FilterSourceImpl(JaxbFilterElement filterElement) {
this.name = filterElement.getName();
String conditionAttribute = filterElement.getCondition();
String conditionContent = filterElement.getValue();
this.condition = Helper.coalesce( conditionContent, conditionAttribute );
}
@Override
public String getName() {
return name;
}
@Override
public String getCondition() {
return condition;
}
}

View File

@ -259,6 +259,33 @@ public class Helper {
return Identifier.toIdentifier( name );
}
/**
* Operates like SQL coalesce expression, except empty strings are treated as null. Return the first non-empty value
*
* @param values The list of values.
* @param <T>
*
* @return The first non-empty value, or null if all values were empty
*/
public static <T> T coalesce(T... values) {
if ( values == null ) {
return null;
}
for ( T value : values ) {
if ( value != null ) {
if ( String.class.isInstance( value ) ) {
if ( StringHelper.isNotEmpty( (String) value ) ) {
return value;
}
}
else {
return value;
}
}
}
return null;
}
/**
* For things that define one or more "value sources" there is a lot of variance in terms of how they
* look in the XML. As an example, consider {@code <property/>} which might have:<ul>

View File

@ -0,0 +1,65 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, Red Hat Inc. 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 Inc.
*
* 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.metamodel.internal.source.hbm;
import java.util.Collections;
import java.util.Map;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbHibernateMapping;
import org.hibernate.metamodel.spi.source.TypeDescriptorSource;
/**
* @author Steve Ebersole
*/
public class TypeDescriptorSourceImpl implements TypeDescriptorSource {
private final String name;
private final String implementationClassName;
private final Map<String,String> params;
public TypeDescriptorSourceImpl(JaxbHibernateMapping.JaxbTypedef typeDefElement) {
this.name = typeDefElement.getName();
this.implementationClassName = typeDefElement.getClazz();
this.params = Helper.extractParameters( typeDefElement.getParam() );
}
@Override
public String getName() {
return name;
}
@Override
public String getTypeImplementationClassName() {
return implementationClassName;
}
@Override
public Iterable<String> getRegistrationKeys() {
return Collections.emptyList();
}
@Override
public Map<String, String> getParameters() {
return params;
}
}

View File

@ -50,11 +50,11 @@ public interface FetchProfileSource {
public String getAttributeName();
/**
* Retrieve the fetch mode to be applied to the association as part of this profile.
* Retrieve the name of the fetch mode to be applied to the association as part of this profile.
*
* @return the fetch mode.
* @return the fetch mode name.
*/
public FetchMode getFetchMode();
public String getFetchModeName();
}
/**

View File

@ -46,15 +46,4 @@ public interface FilterSource {
* @see {@link org.hibernate.metamodel.spi.source.FilterDefSource#getCondition()}
*/
public String getCondition();
/**
* Retrieves the defined sources of parameter information pertaining to this filer.
* <p/>
* Will be <b>merged</b> with parameter sources associated with matching "filter def"
*
* @return The parameter sources defined on the filter.
*
* @see {@link org.hibernate.metamodel.spi.source.FilterDefSource#getParameterSources()}
*/
public Iterable<FilterParameterSource> getParameterSources();
}

View File

@ -23,6 +23,8 @@
*/
package org.hibernate.metamodel.spi.source;
import java.util.Map;
/**
* Describes the source of a custom type description. For example, {@code <type-def/>} or
* {@link org.hibernate.annotations.TypeDef @TypeDef}
@ -30,8 +32,39 @@ package org.hibernate.metamodel.spi.source;
* @author Steve Ebersole
*/
public interface TypeDescriptorSource {
/**
* Retrieve the name of the type def.
*
* @return The name.
*/
public String getName();
/**
* Retrieve the name of the class implementing {@link org.hibernate.type.Type},
* {@link org.hibernate.usertype.UserType}, etc.
*
* @return The implementation class name.
*/
public String getTypeImplementationClassName();
/**
* For what are termed "basic types" there is a registry that contain the type keyed by various
* keys. This is the mechanism that allows a "string type" to reference to by "string", "java.lang.String",
* etc in the mapping. This method returns the keys under which this type should be registered in
* that registry.
* <p/>
* Note that if the type def contains registration keys, it should be considered illegal for its
* corresponding {@link ExplicitHibernateTypeSource} to define parameters.
*
* @return The registration keys for the type built from this type def.
*/
public Iterable<String> getRegistrationKeys();
public Iterable<ConfigurationValueSource> getConfigurationValueSources();
/**
* Types accept configuration. The values here represent the user supplied values that will be given
* to the type instance after instantiation
*
* @return The configuration parameters from the underlying source.
*/
public Map<String,String> getParameters();
}