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

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

There is a newer version: 1.6.12
Show newest version
/*
 * Copyright 2014 - 2020 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.impl.BlazeCriteriaBuilderImpl;
import com.blazebit.persistence.criteria.impl.RenderContext;
import com.blazebit.persistence.criteria.impl.expression.SubqueryExpression;

import javax.persistence.criteria.Expression;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.Bindable;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.ManagedType;
import javax.persistence.metamodel.PluralAttribute;
import javax.persistence.metamodel.SingularAttribute;
import javax.persistence.metamodel.Type;

/**
 * @author Christian Beikov
 * @since 1.2.0
 */
public class SingularAttributeJoin extends AbstractJoin {

    private static final long serialVersionUID = 1L;

    private final Bindable model;

    private SingularAttributeJoin(BlazeCriteriaBuilderImpl criteriaBuilder, SingularAttributeJoin original, EntityType treatType) {
        super(criteriaBuilder, original, treatType);
        this.model = treatType;
    }

    @SuppressWarnings({"unchecked"})
    public SingularAttributeJoin(BlazeCriteriaBuilderImpl criteriaBuilder, Class javaType, AbstractPath pathSource, SingularAttribute joinAttribute, JoinType joinType) {
        super(criteriaBuilder, javaType, pathSource, joinAttribute, joinType);
        this.model = (Bindable) (Attribute.PersistentAttributeType.EMBEDDED == joinAttribute
                .getPersistentAttributeType() ? joinAttribute : criteriaBuilder.getEntityMetamodel().managedType(javaType));
    }

    @Override
    @SuppressWarnings({"unchecked"})
    public SingularAttribute getAttribute() {
        return (SingularAttribute) super.getAttribute();
    }

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

    @Override
    @SuppressWarnings({"unchecked"})
    protected AbstractFrom createCorrelationDelegate() {
        return new SingularAttributeJoin(criteriaBuilder, getJavaType(), (AbstractPath) getBasePath(), getAttribute(), getJoinType());
    }

    @Override
    protected boolean isJoinAllowed() {
        return true;
    }

    @Override
    @SuppressWarnings({"unchecked"})
    protected ManagedType getManagedType() {
        Bindable m = getModel();
        Bindable.BindableType t = m.getBindableType();

        switch (t) {
            case ENTITY_TYPE:
                return (ManagedType) m;
            case SINGULAR_ATTRIBUTE:
                final Type joinedAttributeType = ((SingularAttribute) getAttribute()).getType();
                if (!(joinedAttributeType instanceof ManagedType)) {
                    throw new IllegalArgumentException("Joins on '" + getPathExpression() + "' are not allowed");
                }
                return (ManagedType) joinedAttributeType;
            case PLURAL_ATTRIBUTE:
                final Type elementType = ((PluralAttribute) getAttribute()).getElementType();
                if (!(elementType instanceof ManagedType)) {
                    throw new IllegalArgumentException("Joins on '" + getPathExpression() + "' are not allowed");
                }
                return (ManagedType) elementType;
            default:
                break;
        }

        return super.getManagedType();
    }

    @SuppressWarnings("unchecked")
    public Bindable getModel() {
        if (treatJoinType != null) {
            return (Bindable) treatJoinType;
        }
        return model;
    }

    /* JPA 2.1 support */

    @Override
    public SingularAttributeJoin on(Expression restriction) {
        super.onExpression(restriction);
        return this;
    }

    @Override
    public SingularAttributeJoin on(Predicate... restrictions) {
        super.onPredicates(restrictions);
        return this;
    }

    @Override
    @SuppressWarnings("unchecked")
    public  SingularAttributeJoin treatJoin(Class treatType) {
        setTreatType(treatType);
        return (SingularAttributeJoin) this;
    }

    @Override
    @SuppressWarnings("unchecked")
    public  SingularAttributeJoin treatAs(Class treatAsType) {
        // No need to treat if it is already of the proper subtype
        if (treatAsType.isAssignableFrom(getJavaType())) {
            return (SingularAttributeJoin) this;
        }
        return addTreatedPath(new TreatedSingularAttributeJoin(criteriaBuilder, this, getTreatType(treatAsType)));
    }

    /**
     * @author Christian Beikov
     * @since 1.2.0
     */
    public static class TreatedSingularAttributeJoin extends SingularAttributeJoin implements TreatedPath {

        private static final long serialVersionUID = 1L;

        private final SingularAttributeJoin treatedJoin;
        private final EntityType treatType;

        public TreatedSingularAttributeJoin(BlazeCriteriaBuilderImpl criteriaBuilder, SingularAttributeJoin treatedJoin, EntityType treatType) {
            super(criteriaBuilder, treatedJoin, treatType);
            this.treatedJoin = treatedJoin;
            this.treatType = treatType;
        }

        @Override
        protected ManagedType getManagedType() {
            return treatType;
        }

        @Override
        public EntityType getTreatType() {
            return treatType;
        }

        @Override
        public AbstractPath getTreatedPath() {
            return treatedJoin;
        }

        @Override
        public String getAlias() {
            return treatedJoin.getAlias();
        }

        @Override
        public String getPathExpression() {
            return getAlias();
        }

        @Override
        public void renderPathExpression(RenderContext context) {
            render(context);
        }

        @Override
        public void render(RenderContext context) {
            prepareAlias(context);
            final StringBuilder buffer = context.getBuffer();
            buffer.append("TREAT(")
                    .append(getAlias())
                    .append(" AS ")
                    .append(getTreatType().getName())
                    .append(')');
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy