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

org.apache.openjpa.persistence.criteria.FromImpl 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 javax.persistence.criteria.CollectionJoin;
import javax.persistence.criteria.Fetch;
import javax.persistence.criteria.From;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.ListJoin;
import javax.persistence.criteria.MapJoin;
import javax.persistence.criteria.SetJoin;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.CollectionAttribute;
import javax.persistence.metamodel.ListAttribute;
import javax.persistence.metamodel.ManagedType;
import javax.persistence.metamodel.MapAttribute;
import javax.persistence.metamodel.PluralAttribute;
import javax.persistence.metamodel.SetAttribute;
import javax.persistence.metamodel.SingularAttribute;
import javax.persistence.metamodel.Type;
import javax.persistence.metamodel.Type.PersistenceType;

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

/**
 * Represents a bound type, usually an entity that appears in the from clause, 
 * but may also be an embeddable belonging to an entity in the from clause. 
 * Serves as a factory for Joins of associations, embeddables and collections 
 * belonging to the type, and for Paths of attributes belonging to the type.
 * 
 * @param  the parent type of this receiver
 * @param  the type represented by this receiver 
 */

class FromImpl extends PathImpl implements From {
    private java.util.Set> _joins;
    private java.util.Set> _fetches;
    private Type type;
    
    /**
     * Supply the non-null managed type.
     */
    protected FromImpl(AbstractManagedType type) {
        super(type.getJavaType());
        this.type = type;
    }
    
    protected FromImpl(PathImpl parent, Members.Member m, Class x) {
        super(parent, m, x);
        this.type = (Type)m.getType();
    }
    
    @Override
    public Type getType() {
        return type;
    }
    
    /**
     *  Return the joins that have been made from this receiver.
     */
    public java.util.Set> getJoins() {
        return Expressions.returnCopy(_joins);
    }

    /**
     *  Join to the given attribute using an inner join.
     */
    public  Join join(SingularAttribute attribute) {
        return join(attribute, JoinType.INNER);
    }

    /**
     *  Join to the given attribute using the given join type.
     */
    public  Join join(SingularAttribute attribute, JoinType jt) {
        Join join = new Joins.SingularJoin(this, 
                (Members.SingularAttributeImpl) attribute, jt);
        addJoin(join);
        
        return join;
    }

    /**
     *  Join to the given Collection-valued attribute using an inner join.
     */
    public  CollectionJoin join(CollectionAttribute collection) {
        return join(collection, JoinType.INNER);
    }

    /**
     *  Join to the given Collection-valued attribute using the given 
     *  join type.
     */
    public  CollectionJoin join(CollectionAttribute collection, 
        JoinType jt) {
        CollectionJoin join = new Joins.Collection(this, 
             (Members.CollectionAttributeImpl)collection, jt);
        addJoin(join);
         
         return join;
    }

    /**
     *  Join to the given Set-valued attribute using an inner join.
     */
    public  SetJoin join(SetAttribute set) {
        return join(set, JoinType.INNER);
    }
    
    /**
     *  Join to the given Set-valued attribute using the given join type.
     */
    public  SetJoin join(SetAttribute set, JoinType jt) {
        SetJoin join = new Joins.Set(this, (Members.SetAttributeImpl)set, jt);
        addJoin(join);    
        return join;
    }

    /**
     *  Join to the given List-valued attribute using an inner join.
     */
    public  ListJoin join(ListAttribute list) {
        return join(list, JoinType.INNER);
    }

    /**
     *  Join to the given List-valued attribute using the given join type.
     */
    public  ListJoin join(ListAttribute list, JoinType jt) {
        ListJoin join = new Joins.List(this, (Members.ListAttributeImpl)list, jt);
        addJoin(join);    
        return join;
    }
    
    /**
     *  Join to the given Map-valued attribute using an inner join.
     */
    public  MapJoin join(MapAttribute map) {
        return join(map, JoinType.INNER);
    }

    /**
     *  Join to the given Map-valued attribute using the given join type.
     */
    public  MapJoin join(MapAttribute map, JoinType jt) {
        MapJoin join = new Joins.Map(this, (Members.MapAttributeImpl)map, jt);
        addJoin(join);    
        return join;
    }
    
    // String based counterparts

    public  Join join(String attr) {
        return join(attr, JoinType.INNER);
    }

    public  Join join(String name, JoinType jt) {
        assertJoinable(type);
        ManagedType mType = (ManagedType)type;
        Attribute attr = mType.getAttribute(name);
        assertJoinable(attr.getDeclaringType());
        if (attr instanceof SingularAttribute) {
            return join((SingularAttribute)attr, jt);
        } else if (attr instanceof ListAttribute) {
            return join((ListAttribute)attr, jt);
        } else if (attr instanceof SetAttribute) {
            return join((SetAttribute)attr, jt);
        } else if (attr instanceof CollectionAttribute) {
            return join((CollectionAttribute)attr, jt);
        } else if (attr instanceof MapAttribute) {
            return join((MapAttribute)attr, jt);
        } else {
            throw new IllegalArgumentException(name);
        }
    }


    public  CollectionJoin joinCollection(String attr) {
        assertJoinable(type);
        return (CollectionJoin)join(((ManagedType)type).getCollection(attr), JoinType.INNER);
    }

    public  CollectionJoin joinCollection(String attr, JoinType jt) {
        assertJoinable(type);
        return (CollectionJoin)join(((ManagedType)type).getCollection(attr), jt);
    }

    public  ListJoin joinList(String attr) {
        assertJoinable(type);
        return (ListJoin)join(((ManagedType)type).getList(attr), JoinType.INNER);
    }

    public  ListJoin joinList(String attr, JoinType jt) {
        assertJoinable(type);
        return (ListJoin)join(((ManagedType)type).getList(attr), jt);
    }

    public  MapJoin joinMap(String attr) {
        assertJoinable(type);
        return (MapJoin)join(((ManagedType)type).getMap(attr));
    }

    public  MapJoin  joinMap(String attr, JoinType jt) {
        assertJoinable(type);
        return (MapJoin)join(((ManagedType)type).getMap(attr));
    }

    public  SetJoin  joinSet(String attr) {
        assertJoinable(type);
        return (SetJoin)join(((ManagedType)type).getSet(attr));
    }

    public  SetJoin  joinSet(String attr, JoinType jt) {
        assertJoinable(type);
        return (SetJoin)join(((ManagedType)type).getSet(attr), jt);
    }
    
    void assertJoinable(Type type) {
        if (type.getPersistenceType() == PersistenceType.BASIC) {
            throw new IllegalArgumentException(this + " is a basic path and can not be navigated to ");
        }
    }

    private void addJoin(Join join) {
        if (_joins == null)
            _joins = new HashSet>();
         _joins.add(join);
    }
    
    
    public  Fetch fetch(SingularAttribute assoc, JoinType jt) {
        return addFetch((Members.Member)assoc, jt);
    }

    public  Fetch fetch(SingularAttribute assoc) {
        return fetch(assoc, JoinType.INNER);
    }

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

    //String-based:

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

    public  Fetch fetch(String name, JoinType jt) {
        assertJoinable(type);
        Attribute attr = ((ManagedType)type).getAttribute(name);
        if (attr.isCollection()) {
            return fetch((PluralAttribute)attr, jt);
        } else {
            return fetch(((SingularAttribute)attr), jt);
        }
    }

    public java.util.Set> getFetches() {
        return Expressions.returnCopy(_fetches);
    }
    
    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;
    }
    
    public void acceptVisit(CriteriaExpressionVisitor visitor) {
        Expressions.acceptVisit(visitor, this, 
            _joins == null ? null : _joins.toArray(new ExpressionImpl[_joins.size()]));
    }
    
    public From getCorrelationParent() {
        return (From)getCorrelatedPath();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy