Search in sources :

Example 1 with RuntimePersistentProperty

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

the class PersistentPropertyPath method getPropertyValue.

/**
 * Gets property path value.
 * (Only possible for runtime properties)
 *
 * @param bean The root bean
 * @return The value
 */
public Object getPropertyValue(Object bean) {
    if (!(property instanceof RuntimePersistentProperty)) {
        throw new IllegalStateException("Expected runtime property!");
    }
    Object value = bean;
    for (Association association : associations) {
        RuntimePersistentProperty<?> property = (RuntimePersistentProperty) association;
        BeanProperty beanProperty = property.getProperty();
        value = beanProperty.get(value);
        if (value == null) {
            return null;
        }
    }
    RuntimePersistentProperty<?> p = (RuntimePersistentProperty<?>) property;
    if (value != null) {
        BeanProperty beanProperty = p.getProperty();
        value = beanProperty.get(value);
    }
    return value;
}
Also used : RuntimePersistentProperty(io.micronaut.data.model.runtime.RuntimePersistentProperty) BeanProperty(io.micronaut.core.beans.BeanProperty)

Example 2 with RuntimePersistentProperty

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

the class PersistentPropertyPath method setProperty.

private Object setProperty(List<Association> associations, RuntimePersistentProperty property, Object bean, Object value) {
    if (associations.isEmpty()) {
        BeanProperty beanProperty = property.getProperty();
        return setProperty(beanProperty, bean, value);
    }
    Association association = associations.iterator().next();
    RuntimePersistentProperty<?> p = (RuntimePersistentProperty) association;
    BeanProperty beanProperty = p.getProperty();
    Object prevBean = beanProperty.get(bean);
    Object newBean = setProperty(associations.subList(1, associations.size()), property, prevBean, value);
    if (prevBean != newBean) {
        return setProperty(beanProperty, bean, newBean);
    }
    return bean;
}
Also used : BeanProperty(io.micronaut.core.beans.BeanProperty) RuntimePersistentProperty(io.micronaut.data.model.runtime.RuntimePersistentProperty)

Example 3 with RuntimePersistentProperty

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

the class AbstractSqlRepositoryOperations method idPropertiesWithValues.

private Stream<Map.Entry<PersistentProperty, Object>> idPropertiesWithValues(PersistentProperty property, Object value) {
    Object propertyValue = ((RuntimePersistentProperty) property).getProperty().get(value);
    if (property instanceof Embedded) {
        Embedded embedded = (Embedded) property;
        PersistentEntity embeddedEntity = embedded.getAssociatedEntity();
        return embeddedEntity.getPersistentProperties().stream().flatMap(prop -> idPropertiesWithValues(prop, propertyValue));
    } else if (property instanceof Association) {
        Association association = (Association) property;
        if (association.isForeignKey()) {
            return Stream.empty();
        }
        PersistentEntity associatedEntity = association.getAssociatedEntity();
        PersistentProperty identity = associatedEntity.getIdentity();
        if (identity == null) {
            throw new IllegalStateException("Identity cannot be missing for: " + associatedEntity);
        }
        return idPropertiesWithValues(identity, propertyValue);
    }
    return Stream.of(new AbstractMap.SimpleEntry<>(property, propertyValue));
}
Also used : AbstractMap(java.util.AbstractMap) RuntimeAssociation(io.micronaut.data.model.runtime.RuntimeAssociation) Association(io.micronaut.data.model.Association) PersistentEntity(io.micronaut.data.model.PersistentEntity) RuntimePersistentEntity(io.micronaut.data.model.runtime.RuntimePersistentEntity) Embedded(io.micronaut.data.model.Embedded) PersistentProperty(io.micronaut.data.model.PersistentProperty) RuntimePersistentProperty(io.micronaut.data.model.runtime.RuntimePersistentProperty)

Example 4 with RuntimePersistentProperty

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

the class AbstractRepositoryOperations method getIdReader.

/**
 * Obtain an ID reader for the given object.
 *
 * @param o The object
 * @return The ID reader
 */
@NonNull
protected final RuntimePersistentProperty<Object> getIdReader(@NonNull Object o) {
    Class<Object> type = (Class<Object>) o.getClass();
    RuntimePersistentProperty beanProperty = idReaders.get(type);
    if (beanProperty == null) {
        RuntimePersistentEntity<Object> entity = getEntity(type);
        RuntimePersistentProperty<Object> identity = entity.getIdentity();
        if (identity == null) {
            throw new DataAccessException("Entity has no ID: " + entity.getName());
        }
        beanProperty = identity;
        idReaders.put(type, beanProperty);
    }
    return beanProperty;
}
Also used : DataAccessException(io.micronaut.data.exceptions.DataAccessException) RuntimePersistentProperty(io.micronaut.data.model.runtime.RuntimePersistentProperty) NonNull(io.micronaut.core.annotation.NonNull)

Example 5 with RuntimePersistentProperty

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

the class PreparedQueryDBOperation method setParameters.

@Override
public <K, Cnt, PS> void setParameters(OpContext<Cnt, PS> context, Cnt connection, PS stmt, RuntimePersistentEntity<K> persistentEntity, K entity, Map<QueryParameterBinding, Object> previousValues) {
    int index = context.shiftIndex(0);
    Object[] parameterArray = preparedQuery.getParameterArray();
    Argument[] parameterArguments = preparedQuery.getArguments();
    for (QueryParameterBinding queryParameterBinding : preparedQuery.getQueryBindings()) {
        Class<?> parameterConverter = queryParameterBinding.getParameterConverterClass();
        Object value;
        if (queryParameterBinding.getParameterIndex() != -1) {
            value = resolveParameterValue(queryParameterBinding, parameterArray);
        } else if (queryParameterBinding.isAutoPopulated()) {
            String[] propertyPath = queryParameterBinding.getRequiredPropertyPath();
            PersistentPropertyPath pp = persistentEntity.getPropertyPath(propertyPath);
            if (pp == null) {
                throw new IllegalStateException("Cannot find auto populated property: " + String.join(".", propertyPath));
            }
            RuntimePersistentProperty<?> persistentProperty = (RuntimePersistentProperty) pp.getProperty();
            Object previousValue = null;
            QueryParameterBinding previousPopulatedValueParameter = queryParameterBinding.getPreviousPopulatedValueParameter();
            if (previousPopulatedValueParameter != null) {
                if (previousPopulatedValueParameter.getParameterIndex() == -1) {
                    throw new IllegalStateException("Previous value parameter cannot be bind!");
                }
                previousValue = resolveParameterValue(previousPopulatedValueParameter, parameterArray);
            }
            value = context.getRuntimeEntityRegistry().autoPopulateRuntimeProperty(persistentProperty, previousValue);
            value = context.convert(connection, value, persistentProperty);
            parameterConverter = null;
        } else {
            throw new IllegalStateException("Invalid query [" + query + "]. Unable to establish parameter value for parameter at position: " + (index + 1));
        }
        DataType dataType = queryParameterBinding.getDataType();
        List<Object> values = queryParameterBinding.isExpandable() ? expandValue(value, dataType) : Collections.singletonList(value);
        if (values != null && values.isEmpty()) {
            // Empty collections / array should always set at least one value
            value = null;
            values = null;
        }
        if (values == null) {
            if (parameterConverter != null) {
                int parameterIndex = queryParameterBinding.getParameterIndex();
                Argument<?> argument = parameterIndex > -1 ? parameterArguments[parameterIndex] : null;
                value = context.convert(parameterConverter, connection, value, argument);
            }
            context.setStatementParameter(stmt, index++, dataType, value, dialect);
        } else {
            for (Object v : values) {
                if (parameterConverter != null) {
                    int parameterIndex = queryParameterBinding.getParameterIndex();
                    Argument<?> argument = parameterIndex > -1 ? parameterArguments[parameterIndex] : null;
                    v = context.convert(parameterConverter, connection, v, argument);
                }
                context.setStatementParameter(stmt, index++, dataType, v, dialect);
            }
        }
    }
}
Also used : QueryParameterBinding(io.micronaut.data.model.runtime.QueryParameterBinding) Argument(io.micronaut.core.type.Argument) PersistentPropertyPath(io.micronaut.data.model.PersistentPropertyPath) DataType(io.micronaut.data.model.DataType) RuntimePersistentProperty(io.micronaut.data.model.runtime.RuntimePersistentProperty)

Aggregations

RuntimePersistentProperty (io.micronaut.data.model.runtime.RuntimePersistentProperty)11 DataType (io.micronaut.data.model.DataType)5 BeanProperty (io.micronaut.core.beans.BeanProperty)4 RuntimeAssociation (io.micronaut.data.model.runtime.RuntimeAssociation)4 DataAccessException (io.micronaut.data.exceptions.DataAccessException)3 PersistentPropertyPath (io.micronaut.data.model.PersistentPropertyPath)3 QueryParameterBinding (io.micronaut.data.model.runtime.QueryParameterBinding)3 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)2 NonNull (io.micronaut.core.annotation.NonNull)2 Nullable (io.micronaut.core.annotation.Nullable)2 Association (io.micronaut.data.model.Association)2 Embedded (io.micronaut.data.model.Embedded)2 PersistentProperty (io.micronaut.data.model.PersistentProperty)2 RuntimePersistentEntity (io.micronaut.data.model.runtime.RuntimePersistentEntity)2 CursorType (com.mongodb.CursorType)1 BulkWriteResult (com.mongodb.bulk.BulkWriteResult)1 AggregateIterable (com.mongodb.client.AggregateIterable)1 ClientSession (com.mongodb.client.ClientSession)1 FindIterable (com.mongodb.client.FindIterable)1 MongoClient (com.mongodb.client.MongoClient)1