Search in sources :

Example 1 with AutoPopulated

use of io.micronaut.data.annotation.AutoPopulated in project micronaut-data by micronaut-projects.

the class SourceParameterExpressionImpl method bind.

@Override
public QueryParameterBinding bind(BindingContext bindingContext) {
    String bindName;
    if (bindingContext.getName() == null) {
        bindName = String.valueOf(bindingContext.getIndex());
    } else {
        bindName = bindingContext.getName();
    }
    PersistentPropertyPath incomingMethodParameterProperty = bindingContext.getIncomingMethodParameterProperty();
    PersistentPropertyPath outgoingQueryParameterProperty = bindingContext.getOutgoingQueryParameterProperty();
    PersistentPropertyPath propertyPath = outgoingQueryParameterProperty == null ? incomingMethodParameterProperty : outgoingQueryParameterProperty;
    if (propertyPath == null) {
        Objects.requireNonNull(parameterElement);
        int index = Arrays.asList(parameters).indexOf(parameterElement);
        DataType dataType = getDataType(null, parameterElement);
        String converter = parameterElement.stringValue(TypeDef.class, "converter").orElse(null);
        boolean isExpandable = isExpandable(bindingContext, dataType);
        return new QueryParameterBinding() {

            @Override
            public String getKey() {
                return bindName;
            }

            @Override
            public int getParameterIndex() {
                return index;
            }

            @Override
            public DataType getDataType() {
                return dataType;
            }

            @Override
            public String getConverterClassName() {
                return converter;
            }

            @Override
            public boolean isExpandable() {
                return isExpandable;
            }
        };
    }
    if (outgoingQueryParameterProperty == null) {
        throw new IllegalStateException("Outgoing query parameter property is required!");
    }
    boolean autopopulated = propertyPath.getProperty().findAnnotation(AutoPopulated.class).map(ap -> ap.getRequiredValue(AutoPopulated.UPDATEABLE, Boolean.class)).orElse(false);
    DataType dataType = getDataType(propertyPath, parameterElement);
    String converterClassName = ((SourcePersistentProperty) propertyPath.getProperty()).getConverterClassName();
    int index = parameterElement == null || isEntityParameter ? -1 : Arrays.asList(parameters).indexOf(parameterElement);
    String[] path = asStringPath(outgoingQueryParameterProperty.getAssociations(), outgoingQueryParameterProperty.getProperty());
    String[] parameterBindingPath = index != -1 ? getBindingPath(incomingMethodParameterProperty, outgoingQueryParameterProperty) : null;
    boolean requiresPrevValue = index == -1 && autopopulated && !isUpdate;
    boolean isExpandable = isExpandable(bindingContext, dataType);
    return new QueryParameterBinding() {

        @Override
        public String getKey() {
            return bindName;
        }

        @Override
        public DataType getDataType() {
            return dataType;
        }

        @Override
        public String getConverterClassName() {
            return converterClassName;
        }

        @Override
        public int getParameterIndex() {
            return index;
        }

        @Override
        public String[] getParameterBindingPath() {
            return parameterBindingPath;
        }

        @Override
        public String[] getPropertyPath() {
            return path;
        }

        @Override
        public boolean isAutoPopulated() {
            return autopopulated;
        }

        @Override
        public boolean isRequiresPreviousPopulatedValue() {
            return requiresPrevValue;
        }

        @Override
        public boolean isExpandable() {
            return isExpandable;
        }
    };
}
Also used : DataType(io.micronaut.data.model.DataType) PersistentProperty(io.micronaut.data.model.PersistentProperty) QueryParameterBinding(io.micronaut.data.model.query.builder.QueryParameterBinding) Arrays(java.util.Arrays) Expandable(io.micronaut.data.annotation.Expandable) AutoPopulated(io.micronaut.data.annotation.AutoPopulated) SourcePersistentProperty(io.micronaut.data.processor.model.SourcePersistentProperty) ClassElement(io.micronaut.inject.ast.ClassElement) ParameterExpressionImpl(io.micronaut.data.model.jpa.criteria.impl.ParameterExpressionImpl) CriteriaUtils.notSupportedOperation(io.micronaut.data.model.jpa.criteria.impl.CriteriaUtils.notSupportedOperation) Internal(io.micronaut.core.annotation.Internal) TypeDef(io.micronaut.data.annotation.TypeDef) ParameterElement(io.micronaut.inject.ast.ParameterElement) ArrayList(java.util.ArrayList) Objects(java.util.Objects) BindingParameter(io.micronaut.data.model.query.BindingParameter) List(java.util.List) Association(io.micronaut.data.model.Association) Map(java.util.Map) TypeUtils(io.micronaut.data.processor.visitors.finders.TypeUtils) LinkedList(java.util.LinkedList) PersistentPropertyPath(io.micronaut.data.model.PersistentPropertyPath) QueryParameterBinding(io.micronaut.data.model.query.builder.QueryParameterBinding) TypeDef(io.micronaut.data.annotation.TypeDef) SourcePersistentProperty(io.micronaut.data.processor.model.SourcePersistentProperty) DataType(io.micronaut.data.model.DataType) PersistentPropertyPath(io.micronaut.data.model.PersistentPropertyPath)

Aggregations

Internal (io.micronaut.core.annotation.Internal)1 AutoPopulated (io.micronaut.data.annotation.AutoPopulated)1 Expandable (io.micronaut.data.annotation.Expandable)1 TypeDef (io.micronaut.data.annotation.TypeDef)1 Association (io.micronaut.data.model.Association)1 DataType (io.micronaut.data.model.DataType)1 PersistentProperty (io.micronaut.data.model.PersistentProperty)1 PersistentPropertyPath (io.micronaut.data.model.PersistentPropertyPath)1 CriteriaUtils.notSupportedOperation (io.micronaut.data.model.jpa.criteria.impl.CriteriaUtils.notSupportedOperation)1 ParameterExpressionImpl (io.micronaut.data.model.jpa.criteria.impl.ParameterExpressionImpl)1 BindingParameter (io.micronaut.data.model.query.BindingParameter)1 QueryParameterBinding (io.micronaut.data.model.query.builder.QueryParameterBinding)1 SourcePersistentProperty (io.micronaut.data.processor.model.SourcePersistentProperty)1 TypeUtils (io.micronaut.data.processor.visitors.finders.TypeUtils)1 ClassElement (io.micronaut.inject.ast.ClassElement)1 ParameterElement (io.micronaut.inject.ast.ParameterElement)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1