Search in sources :

Example 6 with DataType

use of io.micronaut.data.model.DataType in project micronaut-data by micronaut-projects.

the class MappedEntityVisitor method getDataTypeFromConverter.

private DataType getDataTypeFromConverter(ClassElement type, String converter, Map<String, DataType> dataTypes, VisitorContext context) {
    ClassElement classElement = context.getClassElement(converter).orElseThrow(IllegalStateException::new);
    ClassElement genericType = classElement.getGenericType();
    Map<String, ClassElement> typeArguments = genericType.getTypeArguments(AttributeConverter.class.getName());
    if (typeArguments.isEmpty()) {
        typeArguments = genericType.getTypeArguments("javax.persistence.AttributeConverter");
    }
    if (typeArguments.isEmpty()) {
        typeArguments = genericType.getTypeArguments("jakarta.persistence.AttributeConverter");
    }
    ClassElement entityElement = typeArguments.get("X");
    if (entityElement != null) {
        Optional<DataType> explicitType = entityElement.getValue(TypeDef.class, "type", DataType.class);
        if (explicitType.isPresent()) {
            return explicitType.get();
        }
    }
    Optional<DataType> explicitType = type.getValue(TypeDef.class, "type", DataType.class);
    if (explicitType.isPresent()) {
        return explicitType.get();
    }
    ClassElement dataTypeClassElement = typeArguments.get("Y");
    if (dataTypeClassElement != null) {
        DataType dataType = TypeUtils.resolveDataType(dataTypeClassElement, dataTypes);
        if (dataType == DataType.OBJECT) {
            dataType = null;
        }
        return dataType;
    }
    return null;
}
Also used : AttributeConverter(io.micronaut.data.model.runtime.convert.AttributeConverter) DataType(io.micronaut.data.model.DataType) ClassElement(io.micronaut.inject.ast.ClassElement)

Example 7 with DataType

use of io.micronaut.data.model.DataType in project micronaut-data by micronaut-projects.

the class MappedEntityVisitor method visitClass.

@Override
public void visitClass(ClassElement element, VisitorContext context) {
    NamingStrategy namingStrategy = resolveNamingStrategy(element);
    Optional<String> targetName = element.stringValue(MappedEntity.class);
    SourcePersistentEntity entity = entityResolver.apply(element);
    if (isMappedEntity() && !targetName.isPresent()) {
        element.annotate(MappedEntity.class, builder -> {
            String mappedName = namingStrategy.mappedName(entity);
            builder.value(mappedName);
        });
    }
    Map<String, DataType> dataTypes = getConfiguredDataTypes(element);
    Map<String, String> dataConverters = getConfiguredDataConverters(element);
    List<SourcePersistentProperty> properties = entity.getPersistentProperties();
    final List<AnnotationValue<Index>> indexes = properties.stream().filter(x -> ((PersistentProperty) x).findAnnotation(Indexes.class).isPresent()).map(prop -> prop.findAnnotation(Index.class)).flatMap(o -> o.map(Stream::of).orElseGet(Stream::empty)).collect(Collectors.toList());
    if (!indexes.isEmpty()) {
        element.annotate(Indexes.class, builder -> builder.values(indexes.toArray(new AnnotationValue[] {})));
    }
    for (PersistentProperty property : properties) {
        computeMappingDefaults(namingStrategy, property, dataTypes, dataConverters, context);
    }
    SourcePersistentProperty identity = entity.getIdentity();
    if (identity != null) {
        computeMappingDefaults(namingStrategy, identity, dataTypes, dataConverters, context);
    }
    SourcePersistentProperty[] compositeIdentities = entity.getCompositeIdentity();
    if (compositeIdentities != null) {
        for (SourcePersistentProperty compositeIdentity : compositeIdentities) {
            computeMappingDefaults(namingStrategy, compositeIdentity, dataTypes, dataConverters, context);
        }
    }
    SourcePersistentProperty version = entity.getVersion();
    if (version != null) {
        computeMappingDefaults(namingStrategy, version, dataTypes, dataConverters, context);
    }
}
Also used : DataType(io.micronaut.data.model.DataType) MappedEntity(io.micronaut.data.annotation.MappedEntity) SourcePersistentProperty(io.micronaut.data.processor.model.SourcePersistentProperty) ClassElement(io.micronaut.inject.ast.ClassElement) HashMap(java.util.HashMap) TypeDef(io.micronaut.data.annotation.TypeDef) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Utils.getConfiguredDataTypes(io.micronaut.data.processor.visitors.Utils.getConfiguredDataTypes) Utils.getConfiguredDataConverters(io.micronaut.data.processor.visitors.Utils.getConfiguredDataConverters) TypeElementVisitor(io.micronaut.inject.visitor.TypeElementVisitor) MappedProperty(io.micronaut.data.annotation.MappedProperty) PropertyElement(io.micronaut.inject.ast.PropertyElement) InstantiationUtils(io.micronaut.core.reflect.InstantiationUtils) Map(java.util.Map) TypeUtils(io.micronaut.data.processor.visitors.finders.TypeUtils) AnnotationClassValue(io.micronaut.core.annotation.AnnotationClassValue) NamingStrategies(io.micronaut.data.model.naming.NamingStrategies) PersistentProperty(io.micronaut.data.model.PersistentProperty) Property(io.micronaut.context.annotation.Property) Index(io.micronaut.data.annotation.Index) NamingStrategy(io.micronaut.data.model.naming.NamingStrategy) SourcePersistentEntity(io.micronaut.data.processor.model.SourcePersistentEntity) Collectors(java.util.stream.Collectors) Indexes(io.micronaut.data.annotation.Indexes) Objects(java.util.Objects) NonNull(io.micronaut.core.annotation.NonNull) VisitorContext(io.micronaut.inject.visitor.VisitorContext) List(java.util.List) Association(io.micronaut.data.model.Association) Stream(java.util.stream.Stream) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) Optional(java.util.Optional) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) AttributeConverter(io.micronaut.data.model.runtime.convert.AttributeConverter) Relation(io.micronaut.data.annotation.Relation) SourcePersistentEntity(io.micronaut.data.processor.model.SourcePersistentEntity) Index(io.micronaut.data.annotation.Index) SourcePersistentProperty(io.micronaut.data.processor.model.SourcePersistentProperty) PersistentProperty(io.micronaut.data.model.PersistentProperty) Indexes(io.micronaut.data.annotation.Indexes) NamingStrategy(io.micronaut.data.model.naming.NamingStrategy) SourcePersistentProperty(io.micronaut.data.processor.model.SourcePersistentProperty) DataType(io.micronaut.data.model.DataType) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) Stream(java.util.stream.Stream)

Example 8 with DataType

use of io.micronaut.data.model.DataType in project micronaut-data by micronaut-projects.

the class RepositoryTypeElementVisitor method bindAdditionalParameters.

private void bindAdditionalParameters(MatchContext matchContext, SourcePersistentEntity entity, List<QueryParameterBinding> parameterBinding, ParameterElement[] parameters, Map<String, String> params) {
    if (CollectionUtils.isNotEmpty(params)) {
        Map<String, DataType> configuredDataTypes = Utils.getConfiguredDataTypes(matchContext.getRepositoryClass());
        for (Map.Entry<String, String> param : params.entrySet()) {
            ParameterElement parameter = Arrays.stream(parameters).filter(p -> p.stringValue(Parameter.class).orElse(p.getName()).equals(param.getValue())).findFirst().orElse(null);
            if (parameter == null) {
                throw new MatchFailedException("A @Where(..) definition requires a parameter called [" + param.getValue() + "] which is not present in the method signature.");
            }
            PersistentPropertyPath propertyPath = entity.getPropertyPath(parameter.getName());
            BindingParameter.BindingContext bindingContext = BindingParameter.BindingContext.create().name(param.getKey()).incomingMethodParameterProperty(propertyPath).outgoingQueryParameterProperty(propertyPath);
            QueryParameterBinding binding = new SourceParameterExpressionImpl(configuredDataTypes, matchContext.parameters, parameter, false).bind(bindingContext);
            parameterBinding.add(binding);
        }
    }
}
Also used : QueryParameterBinding(io.micronaut.data.model.query.builder.QueryParameterBinding) SourceParameterExpressionImpl(io.micronaut.data.processor.model.criteria.impl.SourceParameterExpressionImpl) PersistentPropertyPath(io.micronaut.data.model.PersistentPropertyPath) BindingParameter(io.micronaut.data.model.query.BindingParameter) DataType(io.micronaut.data.model.DataType) Parameter(io.micronaut.context.annotation.Parameter) BindingParameter(io.micronaut.data.model.query.BindingParameter) DataMethodQueryParameter(io.micronaut.data.intercept.annotation.DataMethodQueryParameter) ParameterElement(io.micronaut.inject.ast.ParameterElement) Map(java.util.Map) HashMap(java.util.HashMap)

Example 9 with DataType

use of io.micronaut.data.model.DataType in project micronaut-data by micronaut-projects.

the class SourceParameterExpressionImpl method getDataType.

private DataType getDataType(PersistentPropertyPath propertyPath, ParameterElement parameterElement) {
    if (propertyPath != null) {
        PersistentProperty property = propertyPath.getProperty();
        if (!(property instanceof Association)) {
            return property.getDataType();
        }
    }
    if (parameterElement != null) {
        DataType dataType = TypeUtils.resolveDataType(parameterElement).orElse(null);
        if (dataType != null) {
            return dataType;
        }
        ClassElement type = parameterElement.getType();
        if (TypeUtils.isContainerType(type)) {
            type = type.getFirstTypeArgument().orElse(type);
        }
        return TypeUtils.resolveDataType(type, dataTypes);
    }
    return DataType.OBJECT;
}
Also used : Association(io.micronaut.data.model.Association) DataType(io.micronaut.data.model.DataType) ClassElement(io.micronaut.inject.ast.ClassElement) PersistentProperty(io.micronaut.data.model.PersistentProperty) SourcePersistentProperty(io.micronaut.data.processor.model.SourcePersistentProperty)

Example 10 with DataType

use of io.micronaut.data.model.DataType in project micronaut-data by micronaut-projects.

the class DTOMapper method read.

/**
 * Read the given property.
 * @param resultSet The result set
 * @param property THe property
 * @return The result
 */
@Nullable
public Object read(@NonNull S resultSet, @NonNull RuntimePersistentProperty<T> property) {
    String propertyName = property.getPersistedName();
    DataType dataType = property.getDataType();
    if (dataType == DataType.JSON && jsonCodec != null) {
        String data = resultReader.readString(resultSet, propertyName);
        return jsonCodec.decode(property.getArgument(), data);
    } else {
        return read(resultSet, propertyName, dataType);
    }
}
Also used : DataType(io.micronaut.data.model.DataType) Nullable(io.micronaut.core.annotation.Nullable)

Aggregations

DataType (io.micronaut.data.model.DataType)15 PersistentPropertyPath (io.micronaut.data.model.PersistentPropertyPath)7 Association (io.micronaut.data.model.Association)6 PersistentProperty (io.micronaut.data.model.PersistentProperty)6 Map (java.util.Map)6 ClassElement (io.micronaut.inject.ast.ClassElement)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Objects (java.util.Objects)5 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)4 AnnotationValue (io.micronaut.core.annotation.AnnotationValue)4 NonNull (io.micronaut.core.annotation.NonNull)4 Index (io.micronaut.data.annotation.Index)4 Indexes (io.micronaut.data.annotation.Indexes)4 MappedEntity (io.micronaut.data.annotation.MappedEntity)4 MappedProperty (io.micronaut.data.annotation.MappedProperty)4 Relation (io.micronaut.data.annotation.Relation)4 NamingStrategy (io.micronaut.data.model.naming.NamingStrategy)4 QueryParameterBinding (io.micronaut.data.model.query.builder.QueryParameterBinding)4 HashMap (java.util.HashMap)4