All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.micronaut.data.model.jpa.criteria.impl.AbstractPersistentEntityJoinSupport Maven / Gradle / Ivy

There is a newer version: 4.10.3
Show newest version
/*
 * Copyright 2017-2021 original authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.micronaut.data.model.jpa.criteria.impl;

import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.data.model.Association;
import io.micronaut.data.model.PersistentEntity;
import io.micronaut.data.model.PersistentProperty;
import io.micronaut.data.model.jpa.criteria.PersistentAssociationPath;
import io.micronaut.data.model.jpa.criteria.PersistentCollectionAssociationPath;
import io.micronaut.data.model.jpa.criteria.PersistentEntityCollectionJoin;
import io.micronaut.data.model.jpa.criteria.PersistentEntityFrom;
import io.micronaut.data.model.jpa.criteria.PersistentEntityJoin;
import io.micronaut.data.model.jpa.criteria.PersistentEntityListJoin;
import io.micronaut.data.model.jpa.criteria.PersistentEntitySetJoin;
import io.micronaut.data.model.jpa.criteria.PersistentListAssociationPath;
import io.micronaut.data.model.jpa.criteria.PersistentSetAssociationPath;
import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.Fetch;
import jakarta.persistence.criteria.From;
import jakarta.persistence.criteria.Join;
import jakarta.persistence.criteria.JoinType;
import jakarta.persistence.criteria.MapJoin;
import jakarta.persistence.criteria.Path;
import jakarta.persistence.metamodel.CollectionAttribute;
import jakarta.persistence.metamodel.ListAttribute;
import jakarta.persistence.metamodel.MapAttribute;
import jakarta.persistence.metamodel.PluralAttribute;
import jakarta.persistence.metamodel.SetAttribute;
import jakarta.persistence.metamodel.SingularAttribute;

import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import static io.micronaut.data.model.jpa.criteria.impl.CriteriaUtils.notSupportedOperation;

/**
 * The abstract implementation of {@link PersistentEntityFrom}.
 *
 * @param  The associated entity owner type
 * @param  The association entity type
 * @author Denis Stepanov
 * @since 3.2
 */
@Internal
public abstract class AbstractPersistentEntityJoinSupport implements PersistentEntityFrom, SelectionVisitable {

    protected final Map joins = new LinkedHashMap<>();

    public abstract PersistentEntity getPersistentEntity();

    protected abstract  PersistentAssociationPath createJoinAssociation(@NonNull Association association,
                                                                                    @Nullable io.micronaut.data.annotation.Join.Type type,
                                                                                    @Nullable String alias);

    private  PersistentAssociationPath getJoin(String attributeName, io.micronaut.data.annotation.Join.Type type, String alias) {
        PersistentProperty persistentProperty = getPersistentEntity().getPropertyByName(attributeName);
        if (!(persistentProperty instanceof Association)) {
            throw new IllegalStateException("Expected an association for attribute name: " + attributeName);
        }

        PersistentAssociationPath path = joins.computeIfAbsent(attributeName, a -> createJoinAssociation((Association) persistentProperty, type, alias));

        if (type != null && type != io.micronaut.data.annotation.Join.Type.DEFAULT) {
            path.setAssociationJoinType(type);
        }
        if (alias != null) {
            path.setAlias(alias);
        }
        return (PersistentAssociationPath) path;
    }

    private  PersistentCollectionAssociationPath getCollectionJoin(String attributeName, io.micronaut.data.annotation.Join.Type type, String alias) {
        PersistentAssociationPath join = getJoin(attributeName, type, alias);
        if (!(join instanceof PersistentCollectionAssociationPath)) {
            throw new IllegalStateException("Join is not a Collection!");
        }
        return (PersistentCollectionAssociationPath) join;
    }

    private  PersistentSetAssociationPath getSetJoin(String attributeName, io.micronaut.data.annotation.Join.Type type, String alias) {
        PersistentAssociationPath join = getJoin(attributeName, type, alias);
        if (!(join instanceof PersistentSetAssociationPath)) {
            throw new IllegalStateException("Join is not a Set!");
        }
        return (PersistentSetAssociationPath) join;
    }

    private  PersistentListAssociationPath getListJoin(String attributeName, io.micronaut.data.annotation.Join.Type type, String alias) {
        PersistentAssociationPath join = getJoin(attributeName, type, alias);
        if (!(join instanceof PersistentListAssociationPath)) {
            throw new IllegalStateException("Join is not a List!");
        }
        return (PersistentListAssociationPath) join;
    }

    @Override
    public Path getParentPath() {
        throw notSupportedOperation();
    }

    @Override
    public > Expression get(MapAttribute map) {
        return get(map.getName());
    }

    @Override
    public > Expression get(PluralAttribute collection) {
        return get(collection.getName());
    }

    @Override
    public  PersistentEntityJoin join(String attributeName) {
        return getJoin(attributeName, null, null);
    }

    @Override
    public  PersistentEntityJoin join(String attributeName, JoinType jt) {
        return getJoin(attributeName, convert(Objects.requireNonNull(jt)), null);
    }

    @Override
    public  PersistentEntityJoin join(String attributeName, io.micronaut.data.annotation.Join.Type type) {
        return getJoin(attributeName, Objects.requireNonNull(type), null);
    }

    @Override
    public  PersistentEntityJoin join(String attributeName, io.micronaut.data.annotation.Join.Type type, String alias) {
        return getJoin(attributeName, Objects.requireNonNull(type), Objects.requireNonNull(alias));
    }

    @Nullable
    private io.micronaut.data.annotation.Join.Type convert(@Nullable JoinType joinType) {
        if (joinType == null) {
            return null;
        }
        switch (joinType) {
            case LEFT:
                return io.micronaut.data.annotation.Join.Type.LEFT;
            case RIGHT:
                return io.micronaut.data.annotation.Join.Type.RIGHT;
            case INNER:
                return io.micronaut.data.annotation.Join.Type.INNER;
            default:
                return null;
        }
    }

    @Override
    public  PersistentEntityJoin join(SingularAttribute attribute) {
        return getJoin(attribute.getName(), null, null);
    }

    @Override
    public  PersistentEntityJoin join(SingularAttribute attribute, JoinType jt) {
        return getJoin(attribute.getName(), convert(Objects.requireNonNull(jt)), null);
    }

    @Override
    public  PersistentEntityCollectionJoin join(CollectionAttribute collection, JoinType jt) {
        return getCollectionJoin(collection.getName(), convert(jt), null);
    }

    @Override
    public  PersistentEntityCollectionJoin join(CollectionAttribute collection) {
        return getCollectionJoin(collection.getName(), null, null);
    }

    @Override
    public  PersistentEntitySetJoin join(SetAttribute set) {
        return getSetJoin(set.getName(), null, null);
    }

    @Override
    public  PersistentEntityListJoin join(ListAttribute list) {
        return getListJoin(list.getName(), null, null);
    }

    @Override
    public  MapJoin join(MapAttribute map) {
        throw notSupportedOperation();
    }

    @Override
    public  PersistentEntitySetJoin join(SetAttribute set, JoinType jt) {
        return getSetJoin(set.getName(), convert(Objects.requireNonNull(jt)), null);
    }

    @Override
    public  PersistentEntityListJoin join(ListAttribute list, JoinType jt) {
        return getListJoin(list.getName(), convert(Objects.requireNonNull(jt)), null);
    }

    @Override
    public  MapJoin join(MapAttribute map, JoinType jt) {
        throw notSupportedOperation();
    }

    @Override
    public  PersistentEntityCollectionJoin joinCollection(String attributeName) {
        return getCollectionJoin(attributeName, null, null);
    }

    @Override
    public  PersistentEntitySetJoin joinSet(String attributeName) {
        return getSetJoin(attributeName, null, null);
    }

    @Override
    public  PersistentEntityListJoin joinList(String attributeName) {
        return getListJoin(attributeName, null, null);
    }

    @Override
    public  MapJoin joinMap(String attributeName) {
        throw notSupportedOperation();
    }

    @Override
    public  PersistentEntityCollectionJoin joinCollection(String attributeName, JoinType jt) {
        return getCollectionJoin(attributeName, null, null);
    }

    @Override
    public  PersistentEntitySetJoin joinSet(String attributeName, JoinType jt) {
        return getSetJoin(attributeName, convert(Objects.requireNonNull(jt)), null);
    }

    @Override
    public  PersistentEntityListJoin joinList(String attributeName, JoinType jt) {
        return getListJoin(attributeName, convert(Objects.requireNonNull(jt)), null);
    }

    @Override
    public  MapJoin joinMap(String attributeName, JoinType jt) {
        throw notSupportedOperation();
    }

    @Override
    public Set> getJoins() {
        return new HashSet(joins.values());
    }

    @Override
    public boolean isCorrelated() {
        throw notSupportedOperation();
    }

    @Override
    public From getCorrelationParent() {
        throw notSupportedOperation();
    }

    @Override
    public Set> getFetches() {
        throw notSupportedOperation();
    }

    @Override
    public  Fetch fetch(SingularAttribute attribute) {
        throw notSupportedOperation();
    }

    @Override
    public  Fetch fetch(SingularAttribute attribute, JoinType jt) {
        throw notSupportedOperation();
    }

    @Override
    public  Fetch fetch(PluralAttribute attribute) {
        throw notSupportedOperation();
    }

    @Override
    public  Fetch fetch(PluralAttribute attribute, JoinType jt) {
        throw notSupportedOperation();
    }

    @Override
    public  Fetch fetch(String attributeName) {
        throw notSupportedOperation();
    }

    @Override
    public  Fetch fetch(String attributeName, JoinType jt) {
        throw notSupportedOperation();
    }

    @Override
    public  Path get(SingularAttribute attribute) {
        return get(attribute.getName());
    }

    @Override
    public Expression> type() {
        throw notSupportedOperation();
    }

    @Override
    public Class getJavaType() {
        throw notSupportedOperation();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy