Search in sources :

Example 1 with RuntimeAssociation

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

the class AbstractCascadeOperations method afterCascadedOne.

/**
 * Process after a child element has been cascaded.
 *
 * @param entity       The parent entity.
 * @param associations The association leading to the child
 * @param prevChild    The previous child value
 * @param newChild     The new child value
 * @param <T>          The entity type
 * @return The entity instance
 */
protected <T> T afterCascadedOne(T entity, List<Association> associations, Object prevChild, Object newChild) {
    RuntimeAssociation<Object> association = (RuntimeAssociation<Object>) associations.iterator().next();
    if (associations.size() == 1) {
        if (association.isForeignKey()) {
            PersistentAssociationPath inverse = association.getInversePathSide().orElse(null);
            if (inverse != null) {
                // don't cast to BeanProperty<T..> here because its the inverse, so we want to set the entity onto the newChild
                newChild = inverse.setPropertyValue(newChild, entity);
            }
        }
        if (prevChild != newChild) {
            entity = setProperty((BeanProperty<T, Object>) association.getProperty(), entity, newChild);
        }
        return entity;
    } else {
        BeanProperty<T, Object> property = (BeanProperty<T, Object>) association.getProperty();
        Object innerEntity = property.get(entity);
        Object newInnerEntity = afterCascadedOne(innerEntity, associations.subList(1, associations.size()), prevChild, newChild);
        if (newInnerEntity != innerEntity) {
            innerEntity = convertAndSetWithValue(property, entity, newInnerEntity);
        }
        return (T) innerEntity;
    }
}
Also used : RuntimeAssociation(io.micronaut.data.model.runtime.RuntimeAssociation) PersistentAssociationPath(io.micronaut.data.model.PersistentAssociationPath) BeanProperty(io.micronaut.core.beans.BeanProperty)

Example 2 with RuntimeAssociation

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

the class DefaultMongoRepositoryOperations method triggerPostLoad.

private <K> K triggerPostLoad(AnnotationMetadata annotationMetadata, RuntimePersistentEntity<K> persistentEntity, K entity) {
    if (persistentEntity.hasPostLoadEventListeners()) {
        entity = triggerPostLoad(entity, persistentEntity, annotationMetadata);
    }
    for (PersistentProperty pp : persistentEntity.getPersistentProperties()) {
        if (pp instanceof RuntimeAssociation) {
            RuntimeAssociation runtimeAssociation = (RuntimeAssociation) pp;
            Object o = runtimeAssociation.getProperty().get(entity);
            if (o == null) {
                continue;
            }
            RuntimePersistentEntity associatedEntity = runtimeAssociation.getAssociatedEntity();
            switch(runtimeAssociation.getKind()) {
                case MANY_TO_MANY:
                case ONE_TO_MANY:
                    if (o instanceof Iterable) {
                        for (Object value : ((Iterable) o)) {
                            triggerPostLoad(value, associatedEntity, annotationMetadata);
                        }
                    }
                    continue;
                case MANY_TO_ONE:
                case ONE_TO_ONE:
                case EMBEDDED:
                    triggerPostLoad(o, associatedEntity, annotationMetadata);
                    continue;
                default:
                    throw new IllegalStateException("Unknown kind: " + runtimeAssociation.getKind());
            }
        }
    }
    return entity;
}
Also used : RuntimePersistentEntity(io.micronaut.data.model.runtime.RuntimePersistentEntity) AggregateIterable(com.mongodb.client.AggregateIterable) FindIterable(com.mongodb.client.FindIterable) MongoIterable(com.mongodb.client.MongoIterable) RuntimeAssociation(io.micronaut.data.model.runtime.RuntimeAssociation) PersistentProperty(io.micronaut.data.model.PersistentProperty) RuntimePersistentProperty(io.micronaut.data.model.runtime.RuntimePersistentProperty)

Example 3 with RuntimeAssociation

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

the class DefaultReactiveMongoRepositoryOperations method triggerPostLoad.

private <K> K triggerPostLoad(AnnotationMetadata annotationMetadata, RuntimePersistentEntity<K> persistentEntity, K entity) {
    if (persistentEntity.hasPostLoadEventListeners()) {
        entity = triggerPostLoad(entity, persistentEntity, annotationMetadata);
    }
    for (PersistentProperty pp : persistentEntity.getPersistentProperties()) {
        if (pp instanceof RuntimeAssociation) {
            RuntimeAssociation runtimeAssociation = (RuntimeAssociation) pp;
            Object o = runtimeAssociation.getProperty().get(entity);
            if (o == null) {
                continue;
            }
            RuntimePersistentEntity associatedEntity = runtimeAssociation.getAssociatedEntity();
            switch(runtimeAssociation.getKind()) {
                case MANY_TO_MANY:
                case ONE_TO_MANY:
                    if (o instanceof Iterable) {
                        for (Object value : ((Iterable) o)) {
                            triggerPostLoad(value, associatedEntity, annotationMetadata);
                        }
                    }
                    continue;
                case MANY_TO_ONE:
                case ONE_TO_ONE:
                case EMBEDDED:
                    triggerPostLoad(o, associatedEntity, annotationMetadata);
                    continue;
                default:
                    throw new IllegalStateException("Unknown kind: " + runtimeAssociation.getKind());
            }
        }
    }
    return entity;
}
Also used : RuntimePersistentEntity(io.micronaut.data.model.runtime.RuntimePersistentEntity) RuntimeAssociation(io.micronaut.data.model.runtime.RuntimeAssociation) PersistentProperty(io.micronaut.data.model.PersistentProperty) RuntimePersistentProperty(io.micronaut.data.model.runtime.RuntimePersistentProperty)

Example 4 with RuntimeAssociation

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

the class SqlResultEntityTypeMapper method readChildren.

private void readChildren(RS rs, Object instance, Object parent, MappingContext<R> ctx) {
    if (ctx.manyAssociations != null) {
        Object id = readEntityId(rs, ctx);
        MappingContext associatedCtx = ctx.manyAssociations.get(id);
        if (associatedCtx == null) {
            associatedCtx = ctx.copy();
            R entity = (R) readEntity(rs, associatedCtx, parent, id);
            Objects.requireNonNull(id);
            ctx.associate(associatedCtx, id, entity);
        } else {
            readChildren(rs, instance, parent, associatedCtx);
        }
        return;
    }
    if (ctx.associations != null) {
        for (Map.Entry<Association, MappingContext> e : ctx.associations.entrySet()) {
            MappingContext associationCtx = e.getValue();
            RuntimeAssociation runtimeAssociation = (RuntimeAssociation) e.getKey();
            Object in = instance == null || !runtimeAssociation.getKind().isSingleEnded() ? null : runtimeAssociation.getProperty().get(instance);
            readChildren(rs, in, instance, associationCtx);
        }
    }
}
Also used : RuntimeAssociation(io.micronaut.data.model.runtime.RuntimeAssociation) Association(io.micronaut.data.model.Association) RuntimeAssociation(io.micronaut.data.model.runtime.RuntimeAssociation) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 5 with RuntimeAssociation

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

the class SqlResultEntityTypeMapper method setChildrenAndTriggerPostLoad.

private Object setChildrenAndTriggerPostLoad(Object instance, MappingContext<?> ctx, Object parent) {
    if (ctx.manyAssociations != null) {
        List<Object> values = new ArrayList<>(ctx.manyAssociations.size());
        for (MappingContext associationCtx : ctx.manyAssociations.values()) {
            values.add(setChildrenAndTriggerPostLoad(associationCtx.entity, associationCtx, parent));
        }
        return values;
    } else if (ctx.associations != null) {
        for (Map.Entry<Association, MappingContext> e : ctx.associations.entrySet()) {
            MappingContext associationCtx = e.getValue();
            RuntimeAssociation runtimeAssociation = (RuntimeAssociation) e.getKey();
            BeanProperty beanProperty = runtimeAssociation.getProperty();
            if (runtimeAssociation.getKind().isSingleEnded() && (associationCtx.manyAssociations == null || associationCtx.manyAssociations.isEmpty())) {
                Object value = beanProperty.get(instance);
                Object newValue = setChildrenAndTriggerPostLoad(value, associationCtx, instance);
                if (newValue != value) {
                    instance = setProperty(beanProperty, instance, newValue);
                }
            } else {
                Object newValue = setChildrenAndTriggerPostLoad(null, associationCtx, instance);
                newValue = resultReader.convertRequired(newValue == null ? new ArrayList<>() : newValue, beanProperty.getType());
                instance = setProperty(beanProperty, instance, newValue);
            }
        }
    }
    if (instance != null && (ctx.association == null || ctx.jp != null)) {
        if (parent != null && ctx.association != null && ctx.association.isBidirectional()) {
            PersistentAssociationPath inverse = ctx.association.getInversePathSide().orElseThrow(IllegalStateException::new);
            Association association = inverse.getAssociation();
            if (association.getKind().isSingleEnded()) {
                Object inverseInstance = inverse.getPropertyValue(instance);
                if (inverseInstance != parent) {
                    instance = inverse.setPropertyValue(instance, parent);
                }
            }
        }
        triggerPostLoad(ctx.persistentEntity, instance);
    }
    return instance;
}
Also used : RuntimeAssociation(io.micronaut.data.model.runtime.RuntimeAssociation) Association(io.micronaut.data.model.Association) ArrayList(java.util.ArrayList) RuntimeAssociation(io.micronaut.data.model.runtime.RuntimeAssociation) PersistentAssociationPath(io.micronaut.data.model.PersistentAssociationPath) BeanProperty(io.micronaut.core.beans.BeanProperty)

Aggregations

RuntimeAssociation (io.micronaut.data.model.runtime.RuntimeAssociation)9 BeanProperty (io.micronaut.core.beans.BeanProperty)5 Association (io.micronaut.data.model.Association)4 PersistentAssociationPath (io.micronaut.data.model.PersistentAssociationPath)4 RuntimePersistentProperty (io.micronaut.data.model.runtime.RuntimePersistentProperty)4 RuntimePersistentEntity (io.micronaut.data.model.runtime.RuntimePersistentEntity)3 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)2 Relation (io.micronaut.data.annotation.Relation)2 Embedded (io.micronaut.data.model.Embedded)2 PersistentProperty (io.micronaut.data.model.PersistentProperty)2 ArrayList (java.util.ArrayList)2 AggregateIterable (com.mongodb.client.AggregateIterable)1 FindIterable (com.mongodb.client.FindIterable)1 MongoIterable (com.mongodb.client.MongoIterable)1 Internal (io.micronaut.core.annotation.Internal)1 Nullable (io.micronaut.core.annotation.Nullable)1 ArgumentConversionContext (io.micronaut.core.convert.ArgumentConversionContext)1 ConversionContext (io.micronaut.core.convert.ConversionContext)1 ConversionService (io.micronaut.core.convert.ConversionService)1 ConversionErrorException (io.micronaut.core.convert.exceptions.ConversionErrorException)1