|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.tool.language.internal; |
| 6 | + |
| 7 | +import org.hibernate.metamodel.model.domain.ManagedDomainType; |
| 8 | +import org.hibernate.tool.language.spi.MetamodelSerializer; |
| 9 | +import org.hibernate.type.format.StringJsonDocumentWriter; |
| 10 | + |
| 11 | +import jakarta.persistence.metamodel.Attribute; |
| 12 | +import jakarta.persistence.metamodel.EmbeddableType; |
| 13 | +import jakarta.persistence.metamodel.EntityType; |
| 14 | +import jakarta.persistence.metamodel.IdentifiableType; |
| 15 | +import jakarta.persistence.metamodel.ManagedType; |
| 16 | +import jakarta.persistence.metamodel.MapAttribute; |
| 17 | +import jakarta.persistence.metamodel.MappedSuperclassType; |
| 18 | +import jakarta.persistence.metamodel.Metamodel; |
| 19 | +import jakarta.persistence.metamodel.PluralAttribute; |
| 20 | +import jakarta.persistence.metamodel.Type; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Collection; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Set; |
| 27 | + |
| 28 | +/** |
| 29 | + * Implementation of {@link MetamodelSerializer} that represents the {@link Metamodel} as a JSON array of mapped objects. |
| 30 | + */ |
| 31 | +public class MetamodelJsonSerializerImpl implements MetamodelSerializer { |
| 32 | + public static MetamodelJsonSerializerImpl INSTANCE = new MetamodelJsonSerializerImpl(); |
| 33 | + |
| 34 | + /** |
| 35 | + * Utility method that generates a JSON string representation of the mapping information |
| 36 | + * contained in the provided {@link Metamodel metamodel} instance. The representation |
| 37 | + * does not follow a strict scheme, and is more akin to natural language, as it's |
| 38 | + * mainly meant for consumption by a LLM. |
| 39 | + * |
| 40 | + * @param metamodel the metamodel instance containing information on the persistence structures |
| 41 | + * |
| 42 | + * @return the JSON representation of the provided {@link Metamodel metamodel} |
| 43 | + */ |
| 44 | + @Override |
| 45 | + public String toString(Metamodel metamodel) { |
| 46 | + final List<Map<String, Object>> entities = new ArrayList<>(); |
| 47 | + final List<Map<String, Object>> embeddables = new ArrayList<>(); |
| 48 | + final List<Map<String, Object>> mappedSupers = new ArrayList<>(); |
| 49 | + for ( ManagedType<?> managedType : metamodel.getManagedTypes() ) { |
| 50 | + switch ( managedType.getPersistenceType() ) { |
| 51 | + case ENTITY -> entities.add( getEntityTypeDescription( (EntityType<?>) managedType ) ); |
| 52 | + case EMBEDDABLE -> embeddables.add( getEmbeddableTypeDescription( (EmbeddableType<?>) managedType ) ); |
| 53 | + case MAPPED_SUPERCLASS -> mappedSupers.add( getMappedSuperclassTypeDescription( (MappedSuperclassType<?>) managedType ) ); |
| 54 | + default -> |
| 55 | + throw new IllegalStateException( "Unexpected persistence type for managed type [" + managedType + "]" ); |
| 56 | + } |
| 57 | + } |
| 58 | + return toJson( Map.of( |
| 59 | + "entities", entities, |
| 60 | + "mappedSuperclasses", mappedSupers, |
| 61 | + "embeddables", embeddables |
| 62 | + ) ); |
| 63 | + } |
| 64 | + |
| 65 | + private static String toJson(Map<String, Object> map) { |
| 66 | + if ( map.isEmpty() ) { |
| 67 | + return "{}"; |
| 68 | + } |
| 69 | + |
| 70 | + final StringJsonDocumentWriter writer = new StringJsonDocumentWriter( new StringBuilder() ); |
| 71 | + toJson( map, writer ); |
| 72 | + return writer.toString(); |
| 73 | + } |
| 74 | + |
| 75 | + private static void toJson(Object value, StringJsonDocumentWriter writer) { |
| 76 | + if ( value instanceof String strValue ) { |
| 77 | + writer.stringValue( strValue ); |
| 78 | + } |
| 79 | + else if ( value instanceof Boolean boolValue ) { |
| 80 | + writer.booleanValue( boolValue ); |
| 81 | + } |
| 82 | + else if ( value instanceof Number numValue ) { |
| 83 | + writer.numericValue( numValue ); |
| 84 | + } |
| 85 | + else if ( value instanceof Map<?, ?> map ) { |
| 86 | + writer.startObject(); |
| 87 | + for ( final var entry : map.entrySet() ) { |
| 88 | + writer.objectKey( entry.getKey().toString() ); |
| 89 | + toJson( entry.getValue(), writer ); |
| 90 | + } |
| 91 | + writer.endObject(); |
| 92 | + } |
| 93 | + else if ( value instanceof Collection<?> collection ) { |
| 94 | + writer.startArray(); |
| 95 | + for ( final var item : collection ) { |
| 96 | + toJson( item, writer ); |
| 97 | + } |
| 98 | + writer.endArray(); |
| 99 | + } |
| 100 | + else if ( value == null ) { |
| 101 | + writer.nullValue(); |
| 102 | + } |
| 103 | + else { |
| 104 | + throw new IllegalArgumentException( "Unsupported value type: " + value.getClass().getName() ); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private static void putIfNotNull(Map<String, Object> map, String key, Object value) { |
| 109 | + if ( value != null ) { |
| 110 | + map.put( key, value ); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private static <T> Map<String, Object> getEntityTypeDescription(EntityType<T> entityType) { |
| 115 | + final Map<String, Object> map = new HashMap<>( 5 ); |
| 116 | + map.put( "name", entityType.getName() ); |
| 117 | + map.put( "class", entityType.getJavaType().getTypeName() ); |
| 118 | + putIfNotNull( map, "superType", superTypeDescriptor( (ManagedDomainType<?>) entityType ) ); |
| 119 | + putIfNotNull( map, "identifierAttribute", identifierDescriptor( entityType ) ); |
| 120 | + map.put( "attributes", attributeArray( entityType.getAttributes() ) ); |
| 121 | + return map; |
| 122 | + } |
| 123 | + |
| 124 | + private static String superTypeDescriptor(ManagedDomainType<?> managedType) { |
| 125 | + final var superType = managedType.getSuperType(); |
| 126 | + return superType != null ? superType.getJavaType().getTypeName() : null; |
| 127 | + } |
| 128 | + |
| 129 | + private static <T> Map<String, Object> getMappedSuperclassTypeDescription(MappedSuperclassType<T> mappedSuperclass) { |
| 130 | + final Class<T> javaType = mappedSuperclass.getJavaType(); |
| 131 | + final Map<String, Object> map = new HashMap<>( 5 ); |
| 132 | + map.put( "name", javaType.getSimpleName() ); |
| 133 | + map.put( "class", javaType.getTypeName() ); |
| 134 | + putIfNotNull( map, "superType", superTypeDescriptor( (ManagedDomainType<?>) mappedSuperclass ) ); |
| 135 | + putIfNotNull( map, "identifierAttribute", identifierDescriptor( mappedSuperclass ) ); |
| 136 | + map.put( "attributes", attributeArray( mappedSuperclass.getAttributes() ) ); |
| 137 | + return map; |
| 138 | + } |
| 139 | + |
| 140 | + private static <T> String identifierDescriptor(IdentifiableType<T> identifiableType) { |
| 141 | + final Type<?> idType = identifiableType.getIdType(); |
| 142 | + if ( idType != null ) { |
| 143 | + final var id = identifiableType.getId( idType.getJavaType() ); |
| 144 | + return id.getName(); |
| 145 | + } |
| 146 | + else { |
| 147 | + return null; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private static <T> Map<String, Object> getEmbeddableTypeDescription(EmbeddableType<T> embeddableType) { |
| 152 | + final Class<T> javaType = embeddableType.getJavaType(); |
| 153 | + final Map<String, Object> map = new HashMap<>( 4 ); |
| 154 | + map.put( "name", javaType.getSimpleName() ); |
| 155 | + map.put( "class", javaType.getTypeName() ); |
| 156 | + putIfNotNull( map, "superType", superTypeDescriptor( (ManagedDomainType<?>) embeddableType ) ); |
| 157 | + map.put( "attributes", attributeArray( embeddableType.getAttributes() ) ); |
| 158 | + return map; |
| 159 | + } |
| 160 | + |
| 161 | + private static <T> List<Map<String, String>> attributeArray(Set<Attribute<? super T, ?>> attributes) { |
| 162 | + if ( attributes.isEmpty() ) { |
| 163 | + return List.of(); |
| 164 | + } |
| 165 | + |
| 166 | + return attributes.stream().map( attribute -> { |
| 167 | + final String name = attribute.getName(); |
| 168 | + String type = attribute.getJavaType().getTypeName(); |
| 169 | + // add key and element types for plural attributes |
| 170 | + if ( attribute instanceof PluralAttribute<?, ?, ?> pluralAttribute ) { |
| 171 | + type += "<"; |
| 172 | + final var collectionType = pluralAttribute.getCollectionType(); |
| 173 | + if ( collectionType == PluralAttribute.CollectionType.MAP ) { |
| 174 | + type += ( (MapAttribute<?, ?, ?>) pluralAttribute ).getKeyJavaType().getTypeName() + ","; |
| 175 | + } |
| 176 | + type += pluralAttribute.getElementType().getJavaType().getTypeName() + ">"; |
| 177 | + } |
| 178 | + return Map.of( |
| 179 | + "type", type, |
| 180 | + "name", name |
| 181 | + ); |
| 182 | + } ).toList(); |
| 183 | + } |
| 184 | +} |
0 commit comments