001    package org.apache.ojb.broker.accesslayer;
002    
003    /* Copyright 2002-2005 The Apache Software Foundation
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    import org.apache.ojb.broker.PersistenceBrokerException;
019    import org.apache.ojb.broker.core.PersistenceBrokerImpl;
020    import org.apache.ojb.broker.metadata.ClassDescriptor;
021    import org.apache.ojb.broker.metadata.CollectionDescriptor;
022    import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
023    
024    /**
025     * Factory for Relationship Prefetchers
026     * 
027     * @author <a href="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
028     * @version $Id: RelationshipPrefetcherFactory.java,v 1.1 2007-08-24 22:17:30 ewestfal Exp $
029     */
030    public class RelationshipPrefetcherFactory
031    {
032        private PersistenceBrokerImpl broker;
033    
034        public RelationshipPrefetcherFactory(final PersistenceBrokerImpl broker)
035        {
036            this.broker = broker;
037        }
038    
039        /**
040         * create either a CollectionPrefetcher or a ReferencePrefetcher
041         */ 
042        public RelationshipPrefetcher createRelationshipPrefetcher(ObjectReferenceDescriptor ord)
043        {
044            if (ord instanceof CollectionDescriptor)
045            {
046                CollectionDescriptor cds = (CollectionDescriptor)ord;
047                if (cds.isMtoNRelation())
048                {
049                    return new MtoNCollectionPrefetcher(broker, cds);
050                }
051                else
052                {
053                    return new CollectionPrefetcher(broker, cds);
054                }
055            }
056            else
057            {    
058                return new ReferencePrefetcher(broker, ord);
059            }
060        }   
061        
062        /**
063         * create either a CollectionPrefetcher or a ReferencePrefetcher
064         */ 
065        public RelationshipPrefetcher createRelationshipPrefetcher(ClassDescriptor anOwnerCld, String aRelationshipName)
066        {
067            ObjectReferenceDescriptor ord;
068            
069            ord = anOwnerCld.getCollectionDescriptorByName(aRelationshipName);
070            if (ord == null)
071            {
072                ord = anOwnerCld.getObjectReferenceDescriptorByName(aRelationshipName);
073                if (ord == null)
074                {
075                    throw new PersistenceBrokerException("Relationship named '" + aRelationshipName
076                            + "' not found in owner class " + (anOwnerCld != null ? anOwnerCld.getClassNameOfObject() : null));
077                }
078            }
079            return createRelationshipPrefetcher(ord);
080        }   
081    }