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

com.blazebit.persistence.criteria.impl.path.MapAttributeJoin 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.BlazeMapJoin;
import com.blazebit.persistence.criteria.impl.BlazeCriteriaBuilderImpl;
import com.blazebit.persistence.criteria.impl.RenderContext;
import com.blazebit.persistence.criteria.impl.expression.SubqueryExpression;
import com.blazebit.persistence.criteria.impl.expression.function.EntryFunction;
import com.blazebit.persistence.criteria.impl.support.MapJoinSupport;

import javax.persistence.criteria.Expression;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.ManagedType;
import javax.persistence.metamodel.MapAttribute;
import java.util.Map;

/**
 * @author Christian Beikov
 * @since 1.2.0
 */
public class MapAttributeJoin extends AbstractPluralAttributeJoin, V> implements BlazeMapJoin, MapJoinSupport {

    private static final long serialVersionUID = 1L;

    private MapAttributeJoin(BlazeCriteriaBuilderImpl criteriaBuilder, MapAttributeJoin original, EntityType treatType) {
        super(criteriaBuilder, original, treatType);
    }

    public MapAttributeJoin(BlazeCriteriaBuilderImpl criteriaBuilder, Class javaType, AbstractPath pathSource, MapAttribute joinAttribute, JoinType joinType) {
        super(criteriaBuilder, javaType, pathSource, joinAttribute, joinType);
    }

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

    @Override
    public MapAttribute getModel() {
        return getAttribute();
    }

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

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

    @Override
    public Path value() {
        return this;
    }

    @Override
    @SuppressWarnings({"unchecked"})
    public Expression> entry() {
        return new EntryFunction(criteriaBuilder, Map.Entry.class, this);
    }

    @Override
    @SuppressWarnings({"unchecked"})
    public Path key() {
        final MapKeyBasePath mapKeyBasePath = new MapKeyBasePath(criteriaBuilder, getAttribute().getJavaType(), this, getAttribute());
        final MapKeyAttribute mapKeyAttribute = new MapKeyAttribute(criteriaBuilder, getAttribute());
        return new MapKeyPath(criteriaBuilder, mapKeyBasePath, mapKeyAttribute);
    }

    /* JPA 2.1 support */

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

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

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

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

    /**
     * @author Christian Beikov
     * @since 1.2.0
     */
    public static class TreatedMapAttributeJoin extends MapAttributeJoin implements TreatedPath {

        private static final long serialVersionUID = 1L;

        private final MapAttributeJoin treatedJoin;
        private final EntityType treatType;

        public TreatedMapAttributeJoin(BlazeCriteriaBuilderImpl criteriaBuilder, MapAttributeJoin 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) {
            final StringBuilder buffer = context.getBuffer();
            buffer.append("TREAT(")
                    .append(resolveAlias(context))
                    .append(" AS ")
                    .append(getTreatType().getName())
                    .append(')');
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy