use of io.micronaut.data.model.Association in project micronaut-data by micronaut-projects.
the class AbstractCriteriaMethodMatch method applyJoinSpecs.
protected final void applyJoinSpecs(PersistentEntityRoot<?> root, @NonNull List<AnnotationValue<Join>> joinSpecs) {
for (AnnotationValue<Join> joinSpec : joinSpecs) {
String path = joinSpec.stringValue().orElse(null);
Join.Type type = joinSpec.enumValue("type", Join.Type.class).orElse(Join.Type.FETCH);
String alias = joinSpec.stringValue("alias").orElse(null);
if (path != null) {
PersistentPropertyPath propertyPath = root.getPersistentEntity().getPropertyPath(path);
if (propertyPath == null || !(propertyPath.getProperty() instanceof Association)) {
throw new MatchFailedException("Invalid join spec [" + path + "]. Property is not an association!");
} else {
PersistentEntityFrom<?, ?> p = root;
for (Association association : propertyPath.getAssociations()) {
p = p.join(association.getName(), type);
}
if (alias != null) {
p.join(propertyPath.getProperty().getName(), type, alias);
} else {
p.join(propertyPath.getProperty().getName(), type);
}
}
}
}
}
use of io.micronaut.data.model.Association in project micronaut-data by micronaut-projects.
the class AbstractCriteriaMethodMatch method findProperty.
@Nullable
protected final <T> io.micronaut.data.model.jpa.criteria.PersistentPropertyPath<Object> findProperty(PersistentEntityRoot<T> root, String propertyName) {
propertyName = NameUtils.decapitalize(propertyName);
PersistentEntity entity = root.getPersistentEntity();
PersistentProperty prop = entity.getPropertyByName(propertyName);
PersistentPropertyPath pp;
if (prop == null) {
Optional<String> propertyPath = entity.getPath(propertyName);
if (propertyPath.isPresent()) {
String path = propertyPath.get();
pp = entity.getPropertyPath(path);
if (pp == null) {
return null;
}
} else {
return null;
}
} else {
pp = PersistentPropertyPath.of(Collections.emptyList(), prop, propertyName);
}
PersistentEntityFrom<?, ?> path = root;
for (Association association : pp.getAssociations()) {
path = path.join(association.getName());
}
Path<Object> exp;
if (pp.getProperty() instanceof Association && ((Association) pp.getProperty()).getKind() != Relation.Kind.EMBEDDED) {
exp = path.join(pp.getProperty().getName());
} else {
exp = path.get(pp.getProperty().getName());
}
return CriteriaUtils.requireProperty(exp);
}
use of io.micronaut.data.model.Association in project micronaut-data by micronaut-projects.
the class MongoUtils method idValue.
public static BsonValue idValue(ConversionService<?> conversionService, RuntimePersistentEntity<?> persistentEntity, Object idValue, CodecRegistry codecRegistry) {
RuntimePersistentProperty<?> identity = persistentEntity.getIdentity();
if (identity != null) {
if (identity instanceof Association) {
return toBsonValue(conversionService, idValue, codecRegistry);
}
AnnotationValue<BsonRepresentation> bsonRepresentation = identity.getAnnotationMetadata().getAnnotation(BsonRepresentation.class);
if (bsonRepresentation != null) {
BsonType bsonType = bsonRepresentation.getRequiredValue(BsonType.class);
return toBsonValue(conversionService, bsonType, idValue);
} else {
BeanProperty property = identity.getProperty();
Class<?> type = property.getType();
if (type == String.class && idValue != null) {
return new BsonObjectId(new ObjectId(idValue.toString()));
}
return toBsonValue(conversionService, idValue, codecRegistry);
}
}
throw new IllegalStateException("Cannot determine id!");
}
use of io.micronaut.data.model.Association in project micronaut-data by micronaut-projects.
the class SourcePersistentEntityPath method get.
@Override
default <Y> PersistentPropertyPath<Y> get(String attributeName) {
SourcePersistentProperty property = getPersistentEntity().getPropertyByName(attributeName);
if (property == null) {
throw new IllegalStateException("Cannot query entity [" + getPersistentEntity().getSimpleName() + "] on non-existent property: " + attributeName);
}
if (this instanceof PersistentAssociationPath) {
PersistentAssociationPath<?, ?> associationPath = (PersistentAssociationPath) this;
List<Association> associations = associationPath.getAssociations();
List<Association> newAssociations = new ArrayList<>(associations.size() + 1);
newAssociations.addAll(associations);
newAssociations.add(associationPath.getAssociation());
return new SourcePersistentPropertyPathImpl<>(this, newAssociations, property);
}
return new SourcePersistentPropertyPathImpl<>(this, Collections.emptyList(), property);
}
use of io.micronaut.data.model.Association 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;
}
Aggregations