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

com.blazebit.persistence.criteria.impl.path.SingularAttributePath 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.impl.BlazeCriteriaBuilderImpl;
import com.blazebit.persistence.criteria.impl.RenderContext;

import javax.persistence.criteria.Selection;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.Bindable;
import javax.persistence.metamodel.EmbeddableType;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.IdentifiableType;
import javax.persistence.metamodel.ManagedType;
import javax.persistence.metamodel.SingularAttribute;

/**
 * @author Christian Beikov
 * @since 1.2.0
 */
public class SingularAttributePath extends AbstractPath {

    private static final long serialVersionUID = 1L;

    private final SingularAttribute attribute;
    private final ManagedType managedType;

    @SuppressWarnings("unchecked")
    private SingularAttributePath(BlazeCriteriaBuilderImpl criteriaBuilder, SingularAttributePath original, EntityType treatType) {
        super(criteriaBuilder, treatType.getJavaType(), original.getBasePath());
        this.attribute = (SingularAttribute) original.getAttribute();
        this.managedType = treatType;
    }

    public SingularAttributePath(BlazeCriteriaBuilderImpl criteriaBuilder, Class javaType, AbstractPath pathSource, SingularAttribute attribute) {
        super(criteriaBuilder, javaType, pathSource);
        this.attribute = attribute;
        this.managedType = getManagedType(attribute);
    }

    private SingularAttributePath(SingularAttributePath origin, String alias) {
        super(origin.criteriaBuilder, origin.getJavaType(), origin.getBasePath());
        this.attribute = origin.attribute;
        this.managedType = origin.managedType;
        this.setAlias(alias);
    }

    @Override
    public Selection alias(String alias) {
        return new SingularAttributePath(this, alias);
    }

    private ManagedType getManagedType(SingularAttribute attribute) {
        if (Attribute.PersistentAttributeType.BASIC == attribute.getPersistentAttributeType()) {
            return null;
        } else if (Attribute.PersistentAttributeType.EMBEDDED == attribute.getPersistentAttributeType()) {
            return (EmbeddableType) attribute.getType();
        } else {
            return (IdentifiableType) attribute.getType();
        }
    }

    @Override
    public SingularAttribute getAttribute() {
        return attribute;
    }

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

    @Override
    protected boolean isDereferencable() {
        return managedType != null;
    }

    @Override
    protected Attribute findAttribute(String attributeName) {
        final Attribute attribute = managedType.getAttribute(attributeName);
        // Some old hibernate versions don't throw an exception but return null
        if (attribute == null) {
            throw new IllegalArgumentException("Could not resolve attribute named: " + attributeName);
        }
        return attribute;
    }

    @Override
    public  SingularAttributePath treatAs(Class treatAsType) {
        return new TreatedSingularAttributePath(criteriaBuilder, this, getTreatType(treatAsType));
    }

    /**
     * @author Christian Beikov
     * @since 1.2.0
     */
    public static class TreatedSingularAttributePath extends SingularAttributePath implements TreatedPath {

        private static final long serialVersionUID = 1L;

        private final SingularAttributePath treatedPath;
        private final EntityType treatType;

        public TreatedSingularAttributePath(BlazeCriteriaBuilderImpl criteriaBuilder, SingularAttributePath treatedPath, EntityType treatType) {
            super(criteriaBuilder, treatedPath, treatType);
            this.treatedPath = treatedPath;
            this.treatType = treatType;
        }

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

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

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

        @Override
        public String getPathExpression() {
            return "TREAT(" + treatedPath.getPathExpression() + " AS " + getTreatType().getName() + ')';
        }

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy