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

org.apache.openjpa.persistence.criteria.FetchPathImpl Maven / Gradle / Ivy

There is a newer version: 4.0.1
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.openjpa.persistence.criteria;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.criteria.Fetch;
import javax.persistence.criteria.FetchParent;
import javax.persistence.criteria.JoinType;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.ManagedType;
import javax.persistence.metamodel.PluralAttribute;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.openjpa.persistence.meta.Members;

/**
 *
 * @author Pinaki Poddar
 *
 * @param  type of parent
 * @param  type of this
 */
class FetchPathImpl extends PathImpl implements Fetch {
    protected Set> _fetches;
    protected JoinType joinType;


    FetchPathImpl(FetchParent parent, Members.Member member) {
        this(parent, member, JoinType.INNER);
    }

    FetchPathImpl(FetchParent parent, Members.Member member, JoinType type) {
        super((PathImpl)parent, member, member.getJavaType());
        this.joinType = type;
    }

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

    /**
     * Return the metamodel attribute corresponding to the fetch join.
     * @return metamodel attribute for the join
     */
    @Override
    public Attribute getAttribute() {
        return (Attribute)_member;
    }

    @Override
    public FetchParent getParent() {
        return (FetchParent)_parent;
    }

    @Override
    public  Fetch fetch(SingularAttribute assoc) {
        return addFetch((Members.Member)assoc, JoinType.INNER);
    }

    @Override
    public  Fetch fetch(PluralAttribute assoc) {
        return addFetch((Members.Member)assoc, JoinType.INNER);
    }

    @Override
    public  Fetch fetch(String assocName) {
        return fetch(assocName, JoinType.INNER);
    }

    @Override
    public  Fetch fetch(SingularAttribute assoc, JoinType jt) {
        return addFetch((Members.Member)assoc, jt);
    }

    @Override
    public  Fetch fetch(PluralAttribute assoc, JoinType jt) {
        return addFetch((Members.Member)assoc, jt);
    }

    @Override
    public  Fetch fetch(String assocName, JoinType jt) {
        Attribute assoc = ((ManagedType)_member.getType()).getAttribute(assocName);
        return addFetch((Members.Member)assoc, jt);
    }

    @Override
    public Set> getFetches() {
        Set> result = new HashSet<>();
        for (Fetch f : _fetches) {
            result.add(f);
        }
        return result;
    }

    private  Fetch addFetch(Members.Member member, JoinType jt) {
        Fetch fetch = new FetchPathImpl(this, member, jt);
        if (_fetches == null)
            _fetches = new HashSet<>();
        _fetches.add(fetch);
        return fetch;
    }

    @Override
    public StringBuilder asValue(AliasContext q) {
        return super.asValue(q).insert(0, " " + joinType + " JOIN FETCH ");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy