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

io.github.perplexhub.rsql.JoinUtils Maven / Gradle / Ivy

There is a newer version: 6.0.23
Show newest version
package io.github.perplexhub.rsql;

import jakarta.persistence.criteria.Fetch;
import jakarta.persistence.criteria.From;
import jakarta.persistence.criteria.Join;
import jakarta.persistence.criteria.JoinType;

final class JoinUtils {

    private JoinUtils() {
    }

    public static  Join getOrCreateJoin(
            final From root, final String attribute, final JoinType joinType) {
        final Join join = getJoin(root, attribute, joinType);
        return join == null ? createJoin(root, attribute, joinType) : join;
    }

    private static  Join createJoin(final From root, final String attribute, final JoinType joinType) {
        return joinType == null ? root.join(attribute) : root.join(attribute, joinType);
    }

    private static  Join getJoin(
            final From root, final String attribute, final JoinType joinType) {
        final Join fetchJoin = getJoinFromFetches(root, attribute, joinType);
        if (fetchJoin != null) {
            return fetchJoin;
        }
        return getJoinFromJoins(root, attribute, joinType);
    }

    private static  Join getJoinFromFetches(
            final From root, final String attribute, final JoinType joinType) {
        for (final Fetch fetch : root.getFetches()) {
            if (Join.class.isAssignableFrom(fetch.getClass()) &&
                    fetch.getAttribute().getName().equals(attribute) &&
                    (joinType == null || fetch.getJoinType().equals(joinType))) {
                return (Join) fetch;
            }
        }
        return null;
    }

    private static  Join getJoinFromJoins(
            final From root, final String attribute, final JoinType joinType) {
        for (final Join join : root.getJoins()) {
            if (join.getAttribute().getName().equals(attribute) &&
                    (joinType == null || join.getJoinType().equals(joinType))) {
                return join;
            }
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy