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

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

There is a newer version: 1.6.12
Show newest version
/*
 * Copyright 2014 - 2021 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.BlazeRoot;
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.metamodel.EntityType;
import javax.persistence.metamodel.ManagedType;
import java.io.Serializable;

/**
 * @author Christian Beikov
 * @since 1.2.0
 */
public class RootImpl extends AbstractFrom implements BlazeRoot, Serializable {

    private static final long serialVersionUID = 1L;

    private final EntityType entityType;
    private final boolean joinsAllowed;

    private RootImpl(BlazeCriteriaBuilderImpl criteriaBuilder, EntityType entityType, boolean joinsAllowed) {
        super(criteriaBuilder, entityType.getJavaType());
        this.entityType = entityType;
        this.joinsAllowed = joinsAllowed;
    }

    public RootImpl(BlazeCriteriaBuilderImpl criteriaBuilder, EntityType entityType, String alias, boolean joinsAllowed) {
        super(criteriaBuilder, entityType.getJavaType());
        this.entityType = entityType;
        this.setAlias(alias);
        this.joinsAllowed = joinsAllowed;
    }

    public EntityType getEntityType() {
        return entityType;
    }

    public EntityType getModel() {
        return getEntityType();
    }

    @Override
    protected AbstractFrom createCorrelationDelegate() {
        return new RootImpl(criteriaBuilder, getEntityType(), getAlias(), true);
    }

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

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

    @Override
    protected void checkJoinAllowed() {
        if (!joinsAllowed) {
            throw new IllegalArgumentException("Update and delete criteria queries cannot have joins");
        }
        super.checkJoinAllowed();
    }

    @Override
    protected void checkFetchAllowed() {
        if (!joinsAllowed) {
            throw new IllegalArgumentException("Update and delete criteria queries cannot have join fetches");
        }
        super.checkFetchAllowed();
    }

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

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

    @Override
    public void renderPathExpression(RenderContext context) {
        prepareAlias(context);
        context.getBuffer().append(getAlias());
    }

    @Override
    public void render(RenderContext context) {
        prepareAlias(context);
        context.getBuffer().append(getAlias());
    }

    /**
     * @author Christian Beikov
     * @since 1.2.0
     */
    public static class TreatedRoot extends RootImpl implements TreatedPath {

        private static final long serialVersionUID = 1L;

        private final RootImpl treatedRoot;
        private final EntityType treatType;

        public TreatedRoot(BlazeCriteriaBuilderImpl criteriaBuilder, RootImpl treatedRoot, EntityType treatType) {
            super(criteriaBuilder, treatType, treatedRoot.isJoinAllowed());
            this.treatedRoot = treatedRoot;
            this.treatType = treatType;
        }

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

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

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

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

        @Override
        protected AbstractFrom createCorrelationDelegate() {
            return new TreatedRoot(criteriaBuilder, treatedRoot, treatType);
        }

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

        @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