001    package org.apache.ojb.broker;
002    
003    /* Copyright 2003-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.core.proxy.ProxyHelper;
019    import org.apache.ojb.broker.metadata.CollectionDescriptor;
020    
021    /**
022     * Helper class to handle single m:n relation entries (m:n indirection table entries).
023     * <br/>
024     * The "left / right" notation is only used to differ both sides of the relation.
025     *
026     * @author Leandro Rodrigo Saad Cruz
027     * @version $Id: MtoNImplementor.java,v 1.1 2007-08-24 22:17:36 ewestfal Exp $
028     */
029    public class MtoNImplementor
030    {
031        private Object leftObject;
032        private Object rightObject;
033        private Class leftClass;
034        private Class rightClass;
035        private CollectionDescriptor leftDescriptor;
036    
037        /**
038         * Creates a new instance.
039         * 
040         * @param pb             The currently used {@link PersistenceBroker} instance
041         * @param leftDescriptor The collection descriptor for the left side
042         * @param left           The left side object
043         * @param right          The right side object
044         * @deprecated
045         */
046        public MtoNImplementor(PersistenceBroker pb, CollectionDescriptor leftDescriptor, Object left, Object right)
047        {
048            init(leftDescriptor, left, right);
049        }
050    
051        /**
052         * Creates a new instance.
053         *
054         * @param pb            The currently used {@link PersistenceBroker} instance
055         * @param leftFieldName Field name of the left m:n reference
056         * @param left          The left side object
057         * @param right         The right side object
058         */
059        public MtoNImplementor(PersistenceBroker pb, String leftFieldName, Object left, Object right)
060        {
061            if(left == null || right == null)
062            {
063                throw new IllegalArgumentException("both objects must exist");
064            }
065            CollectionDescriptor cod = pb.getClassDescriptor(ProxyHelper.getRealClass(left)).getCollectionDescriptorByName(leftFieldName);
066            init(cod, left, right);
067        }
068    
069        /**
070         * Creates a new instance.
071         * 
072         * @param leftDescriptor The collection descriptor for the left side
073         * @param left           The left side object
074         * @param right          The right side object
075         * @deprecated
076         */
077        public MtoNImplementor(CollectionDescriptor leftDescriptor, Object left, Object right)
078        {
079            init(leftDescriptor, left, right);
080        }
081    
082        private void init(CollectionDescriptor leftDescriptor, Object left, Object right)
083        {
084            if(left == null || right == null)
085            {
086                throw new IllegalArgumentException("both objects must exist");
087            }
088            this.leftDescriptor = leftDescriptor;
089            leftObject = left;
090            rightObject = right;
091            leftClass = ProxyHelper.getRealClass(leftObject);
092            rightClass = ProxyHelper.getRealClass(rightObject);
093        }
094    
095        /**
096         * Returns the collection descriptor for the left side of the m:n collection.
097         * 
098         * @return The collection descriptor
099         */
100        public CollectionDescriptor getLeftDescriptor()
101        {
102            return leftDescriptor;
103        }
104    
105        /**
106         * Returns the class of the left side of the m:n collection.
107         * 
108         * @return The class of the left side
109         */
110        public Class getLeftClass()
111        {
112            return leftClass;
113        }
114    
115        /**
116         * Returns the class of the right side of the m:n collection.
117         * 
118         * @return The class of the right side
119         */
120        public Class getRightClass()
121        {
122            return rightClass;
123        }
124    
125        /**
126         * Returns the object for the left side of the m:n collection.
127         * 
128         * @return The object for the left side
129         */
130        public Object getLeftObject()
131        {
132            return leftObject;
133        }
134    
135        /**
136         * Returns the object for the right side of the m:n collection.
137         * 
138         * @return The object for the right side
139         */
140        public Object getRightObject()
141        {
142            return rightObject;
143        }
144    }