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

com.blazebit.persistence.criteria.impl.path.AbstractJoin Maven / Gradle / Ivy

There is a newer version: 1.6.12
Show newest version
/*
 * Copyright 2014 - 2022 Blazebit.
 *
 * 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
 *
 *      http://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 com.blazebit.persistence.criteria.impl.path;

import com.blazebit.persistence.criteria.BlazeFetch;
import com.blazebit.persistence.criteria.BlazeFrom;
import com.blazebit.persistence.criteria.BlazeJoin;
import com.blazebit.persistence.criteria.impl.BlazeCriteriaBuilderImpl;
import com.blazebit.persistence.criteria.impl.ParameterVisitor;
import com.blazebit.persistence.criteria.impl.expression.SubqueryExpression;
import com.blazebit.persistence.criteria.impl.support.JoinSupport;

import javax.persistence.criteria.Expression;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.EntityType;

/**
 * @author Christian Beikov
 * @since 1.2.0
 */
public abstract class AbstractJoin extends AbstractFrom implements BlazeJoin, BlazeFetch, JoinSupport {

    private static final long serialVersionUID = 1L;

    protected EntityType treatJoinType;

    private final Attribute joinAttribute;
    private final JoinType joinType;
    private boolean fetch;

    private Predicate suppliedJoinCondition;

    protected AbstractJoin(BlazeCriteriaBuilderImpl criteriaBuilder, AbstractJoin original, EntityType treatType) {
        super(criteriaBuilder, treatType.getJavaType(), original.getBasePath());
        this.joinAttribute = original.getAttribute();
        this.joinType = original.getJoinType();
    }

    public AbstractJoin(BlazeCriteriaBuilderImpl criteriaBuilder, Class javaType, AbstractPath pathSource, Attribute joinAttribute, JoinType joinType) {
        super(criteriaBuilder, javaType, pathSource);
        this.joinAttribute = joinAttribute;
        this.joinType = joinType;
    }

    @Override
    public void visit(ParameterVisitor visitor) {
        visitor.visit(suppliedJoinCondition);
        super.visit(visitor);
    }

    @Override
    @SuppressWarnings("unchecked")
    public AbstractPath getBasePath() {
        return (AbstractPath) super.getBasePath();
    }

    @Override
    public Attribute getAttribute() {
        return joinAttribute;
    }

    @Override
    public JoinType getJoinType() {
        return joinType;
    }

    @Override
    @SuppressWarnings({"unchecked"})
    public BlazeFrom getParent() {
        return (BlazeFrom) getBasePath();
    }

    @Override
    @SuppressWarnings({"unchecked"})
    public BlazeJoin fetch() {
        ((AbstractFrom) getBasePath()).getJoinScope().addFetch(this);
        return this;
    }

    @Override
    public boolean isFetch() {
        return fetch;
    }

    public void setFetch(boolean fetch) {
        this.fetch = fetch;
    }

    @Override
    public AbstractJoin correlateTo(SubqueryExpression subquery) {
        return (AbstractJoin) super.correlateTo(subquery);
    }

    public abstract  AbstractJoin treatJoin(Class treatType);

    protected final void setTreatType(Class treatType) {
        if (treatType.isAssignableFrom(getJavaType())) {
            return;
        }
        if (treatJoinType != null) {
            throw new IllegalArgumentException("Invalid multiple invocations of treat on join: " + getPathExpression());
        }

        treatJoinType = criteriaBuilder.getEntityMetamodel().entity(treatType);
        setJavaType(treatJoinType.getJavaType());
    }

    @Override
    public abstract  AbstractJoin treatAs(Class treatAsType);

    public EntityType getTreatJoinType() {
        return treatJoinType;
    }

    protected final void onPredicates(Predicate... restrictions) {
        if (restrictions != null && restrictions.length > 0) {
            this.suppliedJoinCondition = criteriaBuilder.and(restrictions);
        } else {
            this.suppliedJoinCondition = null;
        }
    }

    protected final void onExpression(Expression restriction) {
        this.suppliedJoinCondition = criteriaBuilder.wrap(restriction);
    }

    @Override
    public Predicate getOn() {
        return suppliedJoinCondition;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy