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;
}
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);
}
}
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);
}
}
}
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;
}
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);
}
}
Aggregations