use of io.micronaut.data.processor.model.SourcePersistentProperty 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.processor.model.SourcePersistentProperty in project micronaut-data by micronaut-projects.
the class UpdateMethodMatcher method batchUpdate.
private UpdateCriteriaMethodMatch batchUpdate(java.util.regex.Matcher matcher, ParameterElement idParameter) {
return new UpdateCriteriaMethodMatch(matcher) {
@Override
protected <T> void applyPredicates(String querySequence, ParameterElement[] parameters, PersistentEntityRoot<T> root, PersistentEntityCriteriaUpdate<T> query, SourcePersistentEntityCriteriaBuilder cb) {
super.applyPredicates(querySequence, parameters, root, query, cb);
ParameterElement versionParameter = Arrays.stream(parameters).filter(p -> p.hasAnnotation(Version.class)).findFirst().orElse(null);
Predicate predicate;
if (versionParameter != null) {
predicate = cb.and(cb.equal(root.id(), cb.parameter(idParameter)), cb.equal(root.version(), cb.parameter(versionParameter)));
} else {
predicate = cb.equal(root.id(), cb.parameter(idParameter));
}
query.where(predicate);
}
@Override
protected <T> void applyPredicates(List<ParameterElement> parameters, PersistentEntityRoot<T> root, PersistentEntityCriteriaUpdate<T> query, SourcePersistentEntityCriteriaBuilder cb) {
ParameterElement versionParameter = parameters.stream().filter(p -> p.hasAnnotation(Version.class)).findFirst().orElse(null);
Predicate predicate;
if (versionParameter != null) {
predicate = cb.and(cb.equal(root.id(), cb.parameter(idParameter)), cb.equal(root.version(), cb.parameter(versionParameter)));
} else {
predicate = cb.equal(root.id(), cb.parameter(idParameter));
}
query.where(predicate);
}
@Override
protected <T> void addPropertiesToUpdate(MethodMatchContext matchContext, PersistentEntityRoot<T> root, PersistentEntityCriteriaUpdate<T> query, SourcePersistentEntityCriteriaBuilder cb) {
List<ParameterElement> parameters = matchContext.getParametersNotInRole();
List<ParameterElement> remainingParameters = parameters.stream().filter(p -> !p.hasAnnotation(Id.class) && !p.hasAnnotation(Version.class)).collect(Collectors.toList());
ParameterElement idParameter = parameters.stream().filter(p -> p.hasAnnotation(Id.class)).findFirst().orElse(null);
if (idParameter == null) {
throw new MatchFailedException("ID required for update method, but not specified");
}
SourcePersistentEntity entity = (SourcePersistentEntity) root.getPersistentEntity();
// Validate @IdClass for composite entity
if (entity.hasIdentity()) {
SourcePersistentProperty identity = entity.getIdentity();
String idType = TypeUtils.getTypeName(identity.getType());
String idParameterType = TypeUtils.getTypeName(idParameter.getType());
if (!idType.equals(idParameterType)) {
throw new MatchFailedException("ID type of method [" + idParameterType + "] does not match ID type of entity: " + idType);
}
} else {
throw new MatchFailedException("Cannot update by ID for entity that has no ID");
}
for (ParameterElement parameter : remainingParameters) {
String name = getParameterName(parameter);
SourcePersistentProperty prop = entity.getPropertyByName(name);
if (prop == null) {
throw new MatchFailedException("Cannot update non-existent property: " + name);
} else {
if (prop.isGenerated()) {
throw new MatchFailedException("Cannot update a generated property: " + name);
} else {
query.set(name, cb.parameter(parameter));
}
}
}
}
};
}
use of io.micronaut.data.processor.model.SourcePersistentProperty in project micronaut-data by micronaut-projects.
the class AbstractCriteriaMethodMatch method provideParams.
private <T> List<ParameterExpression<T>> provideParams(Iterator<ParameterElement> parameters, int requiredParameters, String restrictionName, SourcePersistentEntityCriteriaBuilder cb, @Nullable Expression<?> expression) {
if (requiredParameters == 0) {
return Collections.emptyList();
}
List<ParameterExpression<T>> params = new ArrayList<>(requiredParameters);
for (int i = 0; i < requiredParameters; i++) {
if (!parameters.hasNext()) {
throw new MatchFailedException("Insufficient arguments to method criteria: " + restrictionName);
}
ParameterElement parameter = parameters.next();
ClassElement genericType = parameter.getGenericType();
if (TypeUtils.isContainerType(genericType)) {
genericType = genericType.getFirstTypeArgument().orElse(genericType);
}
if (expression instanceof io.micronaut.data.model.jpa.criteria.PersistentPropertyPath) {
io.micronaut.data.model.jpa.criteria.PersistentPropertyPath<?> pp = (io.micronaut.data.model.jpa.criteria.PersistentPropertyPath<?>) expression;
PersistentPropertyPath propertyPath = PersistentPropertyPath.of(pp.getAssociations(), pp.getProperty());
if (!isValidType(genericType, (SourcePersistentProperty) propertyPath.getProperty())) {
SourcePersistentProperty property = (SourcePersistentProperty) propertyPath.getProperty();
throw new IllegalArgumentException("Parameter [" + genericType.getType().getName() + " " + parameter.getName() + "] is not compatible with property [" + property.getType().getName() + " " + property.getName() + "] of entity: " + property.getOwner().getName());
}
}
ParameterExpression p = cb.parameter(parameter);
params.add(p);
}
return params;
}
use of io.micronaut.data.processor.model.SourcePersistentProperty in project micronaut-data by micronaut-projects.
the class SourcePersistentEntityCriteriaQueryImpl method getQueryResultTypeName.
@Override
public String getQueryResultTypeName() {
if (selection instanceof ISelection) {
String[] result = new String[1];
((SelectionVisitable) selection).accept(new SelectionVisitor() {
@Override
public void visit(Predicate predicate) {
}
@Override
public void visit(PersistentPropertyPath<?> persistentPropertyPath) {
result[0] = ((SourcePersistentPropertyPath) persistentPropertyPath).getProperty().getType().getName();
}
@Override
public void visit(AliasedSelection<?> aliasedSelection) {
((SelectionVisitable) aliasedSelection.getSelection()).accept(this);
}
@Override
public void visit(PersistentEntityRoot<?> entityRoot) {
result[0] = ((SourcePersistentEntity) entityRoot.getPersistentEntity()).getType().getName();
}
@Override
public void visit(AggregateExpression<?, ?> aggregateExpression) {
switch(aggregateExpression.getType()) {
case COUNT:
case COUNT_DISTINCT:
result[0] = Long.class.getName();
break;
case MAX:
case MIN:
result[0] = requireProperty(aggregateExpression.getExpression()).getProperty().getTypeName();
break;
case SUM:
case AVG:
ClassElement type = ((SourcePersistentProperty) requireProperty(aggregateExpression.getExpression()).getProperty()).getType();
if (aggregateExpression.getExpressionType() != null) {
result[0] = aggregateExpression.getExpressionType().getName();
}
if (TypeUtils.isNumber(type)) {
result[0] = Number.class.getName();
} else {
result[0] = type.getName();
}
break;
default:
return;
}
}
@Override
public void visit(CompoundSelection<?> compoundSelection) {
if (compoundSelection.getCompoundSelectionItems().size() == 1) {
// Multiple selection shouldn't result in one type
compoundSelection.getCompoundSelectionItems().forEach(s -> ((SelectionVisitable) s).accept(this));
}
}
@Override
public void visit(LiteralExpression<?> literalExpression) {
result[0] = literalExpression.getValue().getClass().getName();
}
@Override
public void visit(IdExpression<?, ?> idExpression) {
SourcePersistentEntity persistentEntity = (SourcePersistentEntity) idExpression.getRoot().getPersistentEntity();
if (persistentEntity.hasCompositeIdentity()) {
throw new IllegalStateException("IdClass is unknown!");
}
result[0] = persistentEntity.getIdentity().getType().getName();
}
});
return result[0];
}
return null;
}
use of io.micronaut.data.processor.model.SourcePersistentProperty 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);
}
Aggregations