Search in sources :

Example 1 with BasicUserType

use of com.blazebit.persistence.view.spi.type.BasicUserType in project blaze-persistence by Blazebit.

the class TypeDescriptor method forType.

public static TypeDescriptor forType(EntityViewManagerImpl evm, Map<Object, EntityViewUpdaterImpl> localCache, EntityViewUpdaterImpl updater, AbstractMethodAttribute<?, ?> attribute, Type<?> type, EntityViewUpdaterImpl owner, String ownerMapping) {
    EntityMetamodel entityMetamodel = evm.getMetamodel().getEntityMetamodel();
    String attributeLocation = attribute.getLocation();
    boolean cascadePersist = attribute.isPersistCascaded();
    boolean cascadeUpdate = attribute.isUpdateCascaded();
    Set<Type<?>> readOnlyAllowedSubtypes = attribute.getReadOnlyAllowedSubtypes();
    Set<Type<?>> persistAllowedSubtypes = attribute.getPersistCascadeAllowedSubtypes();
    Set<Type<?>> updateAllowedSubtypes = attribute.getUpdateCascadeAllowedSubtypes();
    EntityToEntityMapper entityToEntityMapper = null;
    ViewToEntityMapper viewToEntityMapper = null;
    ViewToEntityMapper loadOnlyViewToEntityMapper = null;
    final ManagedType<?> managedType = entityMetamodel.getManagedType(type.getJavaType());
    final boolean jpaEntity = managedType instanceof EntityType<?>;
    final boolean jpaManaged = managedType != null;
    final boolean mutable;
    final boolean identifiable;
    TypeConverter<Object, Object> converter = (TypeConverter<Object, Object>) type.getConverter();
    BasicUserType<Object> basicUserType;
    ManagedType<?> ownerEntityType = owner == null ? attribute.getDeclaringType().getJpaManagedType() : owner.getManagedViewType().getJpaManagedType();
    String attributeIdAttributeName = null;
    String entityIdAttributeName = null;
    Class<?> jpaType;
    // TODO: currently we only check if the declared type is mutable, but we have to let the collection flusher which types are considered updatable/creatable
    if (type instanceof BasicType<?>) {
        jpaType = type.getJavaType();
        basicUserType = (BasicUserType<Object>) ((BasicType<?>) type).getUserType();
        mutable = basicUserType.isMutable();
        identifiable = jpaEntity || !jpaManaged;
        if (jpaEntity) {
            Map<String, Map<?, ?>> fetchGraph = null;
            UnmappedAttributeCascadeDeleter deleter = null;
            // We only need to know the fetch graph when we actually do updates
            if (cascadeUpdate) {
                fetchGraph = getFetchGraph(attribute.getFetches(), attribute.getMapping(), managedType);
            }
            if (ownerEntityType instanceof EntityType<?>) {
                ExtendedManagedType<?> extendedManagedType = evm.getMetamodel().getEntityMetamodel().getManagedType(ExtendedManagedType.class, type.getJavaType());
                entityIdAttributeName = extendedManagedType.getIdAttribute().getName();
                attributeIdAttributeName = getAttributeElementIdentifier(evm, (EntityType<?>) ownerEntityType, attribute.getName(), ownerMapping, attribute.getMapping(), extendedManagedType.getType());
                // Only construct when orphanRemoval or delete cascading is enabled, orphanRemoval implies delete cascading
                if (attribute.isDeleteCascaded()) {
                    String mapping = attribute.getMapping();
                    ExtendedManagedType elementManagedType = entityMetamodel.getManagedType(ExtendedManagedType.class, attribute.getDeclaringType().getJpaManagedType());
                    deleter = new UnmappedBasicAttributeCascadeDeleter(evm, mapping, elementManagedType.getAttribute(mapping), mapping + "." + attributeIdAttributeName, false);
                }
            }
            // TODO: EntityToEntityMapper should also use the attributeIdAttributeName
            entityToEntityMapper = new DefaultEntityToEntityMapper(cascadePersist, cascadeUpdate, jpaType, basicUserType, new DefaultEntityLoaderFetchGraphNode(evm, attribute.getName(), (EntityType<?>) managedType, fetchGraph), deleter);
        }
    } else {
        ManagedViewTypeImplementor<?> elementType = (ManagedViewTypeImplementor<?>) type;
        jpaType = elementType.getEntityClass();
        basicUserType = null;
        mutable = elementType.isUpdatable() || elementType.isCreatable() || !persistAllowedSubtypes.isEmpty() || !updateAllowedSubtypes.isEmpty();
        Set<ManagedViewType<?>> viewTypes = attribute.getViewTypes();
        if (elementType.getJpaManagedType() instanceof EntityType<?> && ownerEntityType instanceof EntityType<?>) {
            ExtendedManagedType<?> extendedManagedType = evm.getMetamodel().getEntityMetamodel().getManagedType(ExtendedManagedType.class, elementType.getEntityClass());
            entityIdAttributeName = extendedManagedType.getIdAttribute().getName();
            attributeIdAttributeName = getAttributeElementIdentifier(evm, (EntityType<?>) ownerEntityType, attribute.getName(), ownerMapping, attribute.getMapping(), extendedManagedType.getType());
        }
        viewToEntityMapper = createViewToEntityMapper(evm, localCache, updater, attributeIdAttributeName, attribute.getMapping(), attributeLocation, elementType, cascadePersist, cascadeUpdate, readOnlyAllowedSubtypes, persistAllowedSubtypes, updateAllowedSubtypes, EntityLoaders.referenceLoaderForAttribute(evm, localCache, elementType, viewTypes, attributeIdAttributeName), owner, ownerMapping);
        loadOnlyViewToEntityMapper = createLoadOnlyViewToEntityMapper(attributeLocation, evm, localCache, attributeIdAttributeName, attribute.getMapping(), elementType, cascadePersist, cascadeUpdate, readOnlyAllowedSubtypes, persistAllowedSubtypes, updateAllowedSubtypes, EntityLoaders.referenceLoaderForAttribute(evm, localCache, elementType, viewTypes, attributeIdAttributeName), owner, ownerMapping);
        identifiable = viewToEntityMapper.getViewIdAccessor() != null;
    }
    final boolean shouldJpaMerge = jpaEntity && mutable && cascadeUpdate;
    final boolean shouldJpaPersist = jpaEntity && mutable && cascadePersist;
    final boolean shouldFlushUpdates = cascadeUpdate && !updateAllowedSubtypes.isEmpty();
    final boolean shouldFlushPersists = cascadePersist && !persistAllowedSubtypes.isEmpty();
    return new TypeDescriptor(mutable, identifiable, jpaManaged, jpaEntity, shouldJpaMerge, shouldJpaPersist, shouldFlushPersists, shouldFlushUpdates, entityIdAttributeName, attributeIdAttributeName, jpaType, converter, basicUserType, entityToEntityMapper, viewToEntityMapper, loadOnlyViewToEntityMapper);
}
Also used : BasicType(com.blazebit.persistence.view.metamodel.BasicType) EntityToEntityMapper(com.blazebit.persistence.view.impl.entity.EntityToEntityMapper) DefaultEntityToEntityMapper(com.blazebit.persistence.view.impl.entity.DefaultEntityToEntityMapper) DefaultEntityLoaderFetchGraphNode(com.blazebit.persistence.view.impl.entity.DefaultEntityLoaderFetchGraphNode) LoadOrPersistViewToEntityMapper(com.blazebit.persistence.view.impl.entity.LoadOrPersistViewToEntityMapper) EmbeddableUpdaterBasedViewToEntityMapper(com.blazebit.persistence.view.impl.entity.EmbeddableUpdaterBasedViewToEntityMapper) UpdaterBasedViewToEntityMapper(com.blazebit.persistence.view.impl.entity.UpdaterBasedViewToEntityMapper) ViewToEntityMapper(com.blazebit.persistence.view.impl.entity.ViewToEntityMapper) ManagedViewType(com.blazebit.persistence.view.metamodel.ManagedViewType) DefaultEntityToEntityMapper(com.blazebit.persistence.view.impl.entity.DefaultEntityToEntityMapper) ManagedViewTypeImplementor(com.blazebit.persistence.view.impl.metamodel.ManagedViewTypeImplementor) ExtendedManagedType(com.blazebit.persistence.spi.ExtendedManagedType) EntityType(javax.persistence.metamodel.EntityType) TypeConverter(com.blazebit.persistence.view.spi.type.TypeConverter) BasicType(com.blazebit.persistence.view.metamodel.BasicType) EntityType(javax.persistence.metamodel.EntityType) Type(com.blazebit.persistence.view.metamodel.Type) BasicUserType(com.blazebit.persistence.view.spi.type.BasicUserType) ExtendedManagedType(com.blazebit.persistence.spi.ExtendedManagedType) MutableBasicUserType(com.blazebit.persistence.view.spi.type.MutableBasicUserType) ManagedType(javax.persistence.metamodel.ManagedType) ManagedViewType(com.blazebit.persistence.view.metamodel.ManagedViewType) ViewType(com.blazebit.persistence.view.metamodel.ViewType) EntityMetamodel(com.blazebit.persistence.parser.EntityMetamodel) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

EntityMetamodel (com.blazebit.persistence.parser.EntityMetamodel)1 ExtendedManagedType (com.blazebit.persistence.spi.ExtendedManagedType)1 DefaultEntityLoaderFetchGraphNode (com.blazebit.persistence.view.impl.entity.DefaultEntityLoaderFetchGraphNode)1 DefaultEntityToEntityMapper (com.blazebit.persistence.view.impl.entity.DefaultEntityToEntityMapper)1 EmbeddableUpdaterBasedViewToEntityMapper (com.blazebit.persistence.view.impl.entity.EmbeddableUpdaterBasedViewToEntityMapper)1 EntityToEntityMapper (com.blazebit.persistence.view.impl.entity.EntityToEntityMapper)1 LoadOrPersistViewToEntityMapper (com.blazebit.persistence.view.impl.entity.LoadOrPersistViewToEntityMapper)1 UpdaterBasedViewToEntityMapper (com.blazebit.persistence.view.impl.entity.UpdaterBasedViewToEntityMapper)1 ViewToEntityMapper (com.blazebit.persistence.view.impl.entity.ViewToEntityMapper)1 ManagedViewTypeImplementor (com.blazebit.persistence.view.impl.metamodel.ManagedViewTypeImplementor)1 BasicType (com.blazebit.persistence.view.metamodel.BasicType)1 ManagedViewType (com.blazebit.persistence.view.metamodel.ManagedViewType)1 Type (com.blazebit.persistence.view.metamodel.Type)1 ViewType (com.blazebit.persistence.view.metamodel.ViewType)1 BasicUserType (com.blazebit.persistence.view.spi.type.BasicUserType)1 MutableBasicUserType (com.blazebit.persistence.view.spi.type.MutableBasicUserType)1 TypeConverter (com.blazebit.persistence.view.spi.type.TypeConverter)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 EntityType (javax.persistence.metamodel.EntityType)1