Skip to content

Commit

Permalink
sebersole#31 - Flesh out AnnotationUsage for element-collection attri…
Browse files Browse the repository at this point in the history
…butes
  • Loading branch information
mbladel committed Nov 21, 2023
1 parent aa87600 commit 80bc53b
Show file tree
Hide file tree
Showing 3 changed files with 259 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.hibernate.AnnotationException;
import org.hibernate.annotations.AttributeAccessor;
import org.hibernate.annotations.CollectionId;
import org.hibernate.annotations.CollectionType;
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterJoinTable;
Expand All @@ -42,6 +43,7 @@
import org.hibernate.boot.jaxb.mapping.spi.JaxbBasicMapping;
import org.hibernate.boot.jaxb.mapping.spi.JaxbCachingImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbCheckConstraintImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbCollectionIdImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbCollectionUserTypeImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbColumnImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbConfigurationParameterImpl;
Expand Down Expand Up @@ -122,7 +124,6 @@

import static java.util.Collections.emptyList;
import static org.hibernate.internal.util.NullnessHelper.coalesce;
import static org.hibernate.boot.models.categorize.xml.internal.XmlProcessingHelper.getOrMakeNamedAnnotation;

/**
* Helper for creating annotation from equivalent JAXB
Expand Down Expand Up @@ -335,6 +336,33 @@ public static void applyCollectionUserType(
typeAnn.setAttributeValue( "parameters", collectParameters( jaxbType.getParameters(), memberDetails, xmlDocumentContext ) );
}

public static void applyCollectionId(
JaxbCollectionIdImpl jaxbCollectionId,
MutableMemberDetails memberDetails,
XmlDocumentContext xmlDocumentContext) {
if ( jaxbCollectionId == null ) {
return;
}

final MutableAnnotationUsage<CollectionId> collectionIdAnn = XmlProcessingHelper.getOrMakeAnnotation( CollectionId.class, memberDetails, xmlDocumentContext );
final AnnotationDescriptor<CollectionId> collectionIdDescriptor = xmlDocumentContext
.getModelBuildingContext()
.getAnnotationDescriptorRegistry()
.getDescriptor( CollectionId.class );

final JaxbColumnImpl jaxbColumn = jaxbCollectionId.getColumn();
if ( jaxbColumn != null ) {
collectionIdAnn.setAttributeValue( "column", createColumnAnnotation(
jaxbColumn,
memberDetails,
xmlDocumentContext
) );
}

applyOr( jaxbCollectionId, JaxbCollectionIdImpl::getGenerator, "generator", collectionIdAnn, collectionIdDescriptor );
}


public static void applyTargetClass(
String name,
MutableMemberDetails memberDetails,
Expand Down Expand Up @@ -468,6 +496,14 @@ public static void applyAttributeOverrides(
List<JaxbAttributeOverrideImpl> jaxbOverrides,
MutableMemberDetails memberDetails,
XmlDocumentContext xmlDocumentContext) {
applyAttributeOverrides( jaxbOverrides, memberDetails, null, xmlDocumentContext );
}

public static void applyAttributeOverrides(
List<JaxbAttributeOverrideImpl> jaxbOverrides,
MutableMemberDetails memberDetails,
String namePrefix,
XmlDocumentContext xmlDocumentContext) {
if ( CollectionHelper.isEmpty( jaxbOverrides ) ) {
return;
}
Expand All @@ -479,7 +515,7 @@ public static void applyAttributeOverrides(
xmlDocumentContext
);
memberDetails.addAnnotationUsage( annotationUsage );
annotationUsage.setAttributeValue( "name", jaxbOverride.getName() );
annotationUsage.setAttributeValue( "name", prefixIfNotAlready( jaxbOverride.getName(), namePrefix ) );
annotationUsage.setAttributeValue( "column", createColumnAnnotation( jaxbOverride.getColumn(), memberDetails, xmlDocumentContext ) );
} );
}
Expand All @@ -488,6 +524,14 @@ public static void applyAssociationOverrides(
List<JaxbAssociationOverrideImpl> jaxbOverrides,
MutableMemberDetails memberDetails,
XmlDocumentContext xmlDocumentContext) {
applyAssociationOverrides( jaxbOverrides, memberDetails, null, xmlDocumentContext );
}

public static void applyAssociationOverrides(
List<JaxbAssociationOverrideImpl> jaxbOverrides,
MutableMemberDetails memberDetails,
String namePrefix,
XmlDocumentContext xmlDocumentContext) {
if ( CollectionHelper.isEmpty( jaxbOverrides ) ) {
return;
}
Expand All @@ -499,7 +543,7 @@ public static void applyAssociationOverrides(
xmlDocumentContext
);
memberDetails.addAnnotationUsage( annotationUsage );
annotationUsage.setAttributeValue( "name", jaxbOverride.getName() );
annotationUsage.setAttributeValue( "name", prefixIfNotAlready( jaxbOverride.getName(), namePrefix ) );
// todo : join columns
// todo : join table
// todo : foreign key
Expand All @@ -523,6 +567,14 @@ public static void applyConvert(
JaxbConvertImpl jaxbConvert,
MutableMemberDetails memberDetails,
XmlDocumentContext xmlDocumentContext) {
applyConvert( jaxbConvert, memberDetails, null, xmlDocumentContext );
}

public static void applyConvert(
JaxbConvertImpl jaxbConvert,
MutableMemberDetails memberDetails,
String namePrefix,
XmlDocumentContext xmlDocumentContext) {
if ( jaxbConvert == null ) {
return;
}
Expand All @@ -537,7 +589,7 @@ public static void applyConvert(
final ClassDetailsRegistry classDetailsRegistry = xmlDocumentContext.getModelBuildingContext().getClassDetailsRegistry();
final ClassDetails converter = classDetailsRegistry.resolveClassDetails( jaxbConvert.getConverter() );
annotationUsage.setAttributeValue( "converter", converter );
annotationUsage.setAttributeValue( "attributeName", jaxbConvert.getAttributeName() );
annotationUsage.setAttributeValue( "attributeName", prefixIfNotAlready( jaxbConvert.getAttributeName(), namePrefix ) );
annotationUsage.setAttributeValue( "disableConversion", jaxbConvert.isDisableConversion() );
}

Expand Down Expand Up @@ -1037,4 +1089,14 @@ static void applyRowId(
applyAttributeIfSpecified( rowIdAnn, "value", rowId );
}
}

private static String prefixIfNotAlready(String value, String prefix) {
if ( StringHelper.isNotEmpty( prefix ) ) {
final String previous = StringHelper.unqualify( value );
if ( !previous.equalsIgnoreCase( prefix ) ) {
return StringHelper.qualify( prefix, value );
}
}
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* SPDX-License-Identifier: Apache-2.0
* Copyright: Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.boot.models.categorize.xml.internal.attr;

import org.hibernate.annotations.Bag;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLDeleteAll;
import org.hibernate.annotations.SQLInsert;
import org.hibernate.annotations.SQLUpdate;
import org.hibernate.annotations.SortComparator;
import org.hibernate.annotations.SortNatural;
import org.hibernate.boot.internal.CollectionClassification;
import org.hibernate.boot.internal.LimitedCollectionClassification;
import org.hibernate.boot.jaxb.mapping.spi.JaxbOrderColumnImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbPluralAttribute;
import org.hibernate.boot.models.categorize.xml.internal.XmlAnnotationHelper;
import org.hibernate.boot.models.categorize.xml.spi.XmlDocumentContext;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.models.internal.MutableAnnotationUsage;
import org.hibernate.models.internal.MutableMemberDetails;
import org.hibernate.models.spi.ClassDetails;
import org.hibernate.models.spi.ClassDetailsRegistry;
import org.hibernate.models.spi.SourceModelBuildingContext;

import jakarta.persistence.MapKey;
import jakarta.persistence.MapKeyClass;
import jakarta.persistence.MapKeyEnumerated;
import jakarta.persistence.MapKeyTemporal;
import jakarta.persistence.OrderBy;
import jakarta.persistence.OrderColumn;

import static org.hibernate.boot.models.categorize.xml.internal.XmlProcessingHelper.getOrMakeAnnotation;
import static org.hibernate.boot.models.categorize.xml.internal.XmlProcessingHelper.setIf;

/**
* @author Marco Belladelli
*/
public class CommonPluralAttributeProcessing {
public static void applyPluralAttributeStructure(
JaxbPluralAttribute jaxbPluralAttribute,
MutableMemberDetails memberDetails,
XmlDocumentContext documentContext) {
final SourceModelBuildingContext buildingContext = documentContext.getModelBuildingContext();
final ClassDetailsRegistry classDetailsRegistry = buildingContext.getClassDetailsRegistry();

if ( jaxbPluralAttribute.getFetchMode() != null ) {
final MutableAnnotationUsage<Fetch> fetchAnn = getOrMakeAnnotation( Fetch.class, memberDetails, documentContext );
fetchAnn.setAttributeValue( "value", jaxbPluralAttribute.getFetchMode() );
}

if ( jaxbPluralAttribute.getClassification() != null ) {
final MutableAnnotationUsage<CollectionClassification> collectionClassificationAnn = getOrMakeAnnotation(
CollectionClassification.class,
memberDetails,
documentContext
);
setIf( jaxbPluralAttribute.getClassification(), "value", collectionClassificationAnn );
if ( jaxbPluralAttribute.getClassification() == LimitedCollectionClassification.BAG ) {
getOrMakeAnnotation( Bag.class, memberDetails, documentContext );
}
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// collection-structure

XmlAnnotationHelper.applyCollectionUserType( jaxbPluralAttribute.getCollectionType(), memberDetails, documentContext );

XmlAnnotationHelper.applyCollectionId( jaxbPluralAttribute.getCollectionId(), memberDetails, documentContext );

if ( StringHelper.isNotEmpty( jaxbPluralAttribute.getOrderBy() ) ) {
final MutableAnnotationUsage<OrderBy> orderByAnn = getOrMakeAnnotation(
OrderBy.class,
memberDetails,
documentContext
);
orderByAnn.setAttributeValue( "value", jaxbPluralAttribute.getOrderBy() );
}

final JaxbOrderColumnImpl orderColumn = jaxbPluralAttribute.getOrderColumn();
if ( orderColumn != null ) {
final MutableAnnotationUsage<OrderColumn> orderByAnn = getOrMakeAnnotation(
OrderColumn.class,
memberDetails,
documentContext
);
setIf( orderColumn.getName(), "name", orderByAnn );
setIf( orderColumn.isNullable(), "nullable", orderByAnn );
setIf( orderColumn.isInsertable(), "insertable", orderByAnn );
setIf( orderColumn.isUpdatable(), "updatable", orderByAnn );
setIf( orderColumn.getColumnDefinition(), "columnDefinition", orderByAnn );
}

if ( StringHelper.isNotEmpty( jaxbPluralAttribute.getSort() ) ) {
final MutableAnnotationUsage<SortComparator> sortAnn = getOrMakeAnnotation(
SortComparator.class,
memberDetails,
documentContext
);
final ClassDetails comparatorClassDetails = classDetailsRegistry.resolveClassDetails( jaxbPluralAttribute.getSort() );
sortAnn.setAttributeValue( "value", comparatorClassDetails );
}

if ( jaxbPluralAttribute.getSortNatural() != null ) {
getOrMakeAnnotation( SortNatural.class, memberDetails, documentContext );
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// map-key

if ( jaxbPluralAttribute.getMapKey() != null ) {
final MutableAnnotationUsage<MapKey> mapKeyAnn = getOrMakeAnnotation( MapKey.class, memberDetails, documentContext );
setIf( jaxbPluralAttribute.getMapKey().getName(), "name", mapKeyAnn );
}

if ( jaxbPluralAttribute.getMapKeyClass() != null ) {
final ClassDetails mapKeyClass = classDetailsRegistry.resolveClassDetails( jaxbPluralAttribute.getMapKeyClass().getClazz() );
getOrMakeAnnotation( MapKeyClass.class, memberDetails, documentContext ).setAttributeValue( "value", mapKeyClass );
}

if ( jaxbPluralAttribute.getMapKeyTemporal() != null ) {
getOrMakeAnnotation( MapKeyTemporal.class, memberDetails, documentContext ).setAttributeValue(
"value",
jaxbPluralAttribute.getMapKeyTemporal()
);
}

if ( jaxbPluralAttribute.getMapKeyEnumerated() != null ) {
getOrMakeAnnotation( MapKeyEnumerated.class, memberDetails, documentContext ).setAttributeValue(
"value",
jaxbPluralAttribute.getMapKeyEnumerated()
);
}

XmlAnnotationHelper.applyAttributeOverrides(
jaxbPluralAttribute.getMapKeyAttributeOverrides(),
memberDetails,
"key",
documentContext
);

jaxbPluralAttribute.getMapKeyConverts().forEach( (jaxbConvert) -> {
XmlAnnotationHelper.applyConvert( jaxbConvert, memberDetails, "key", documentContext );
} );


// todo : map-key-column, map-key-join-column, map-key-foreign-key
// XmlAnnotationHelper.applyMapKeyColumn( jaxbPluralAttribute.getMapKeyColumn(), memberDetails, buildingContext );
//
// jaxbPluralAttribute.getMapKeyJoinColumns().forEach( jaxbMapKeyJoinColumn -> {
// XmlAnnotationHelper.applyMapKeyJoinColumn( jaxbMapKeyJoinColumn, memberDetails, buildingContext );
// } );
//
// XmlAnnotationHelper.applyForeignKey( jaxbPluralAttribute.getMapKeyForeignKey(), memberDetails, buildingContext );

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// filters and custom sql

jaxbPluralAttribute.getFilters().forEach( (jaxbFilter) -> {
XmlAnnotationHelper.applyFilter( jaxbFilter, memberDetails, documentContext );
} );

XmlAnnotationHelper.applySqlRestriction( jaxbPluralAttribute.getSqlRestriction(), memberDetails, documentContext );

XmlAnnotationHelper.applyCustomSql( jaxbPluralAttribute.getSqlInsert(), memberDetails, SQLInsert.class, documentContext );
XmlAnnotationHelper.applyCustomSql( jaxbPluralAttribute.getSqlUpdate(), memberDetails, SQLUpdate.class, documentContext );
XmlAnnotationHelper.applyCustomSql( jaxbPluralAttribute.getSqlDelete(), memberDetails, SQLDelete.class, documentContext );
XmlAnnotationHelper.applyCustomSql( jaxbPluralAttribute.getSqlDeleteAll(), memberDetails, SQLDeleteAll.class, documentContext );
}
}
Loading

0 comments on commit 80bc53b

Please sign in to comment.