001    package org.apache.ojb.broker.query;
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 java.io.Serializable;
019    import java.util.ArrayList;
020    import java.util.HashMap;
021    import java.util.Iterator;
022    import java.util.Map;
023    
024    import org.apache.ojb.broker.util.logging.Logger;
025    import org.apache.ojb.broker.util.logging.LoggerFactory;
026    
027    /**
028     * This class is used to specify the path segments of a Criteria
029     * that should have associated table aliases.  Previously, the default
030     * behaviour was that all path segments participated in the alias
031     * 
032     * @author <a href="mailto:philip.warrick@mcgill.ca">Phil Warrick</a> 
033     */
034    public class UserAlias implements Serializable
035    {
036        private static final long serialVersionUID = 3257282552220627249L;
037        
038        private Map m_mapping = new HashMap();
039        private String m_name = null;
040        private String m_attributePath = null;
041        private boolean m_allPathsAliased = false;
042        private Logger m_logger = LoggerFactory.getLogger(UserAlias.class);
043    
044        /**
045         * Constructor declaration
046         *
047         * @param name the name of the alias
048         */
049        public UserAlias(String name)
050        {
051            m_name = name;
052        }
053    
054        /**
055         * Constructor declaration
056         *
057         * @param name the name of the alias
058         * @param attributePath the full path of the SelectionCriteria attribute
059         * @param aliasPath the portion of the attributePath which should be aliased.
060         * This should be unambiguous.  If ambiguous portions need aliasing (e.g.
061         * B.C in allAs.B.C.B.C), use add() instead
062         */
063        public UserAlias(String name, String attributePath, String aliasPath)
064        {
065            m_name = name;
066            m_attributePath = attributePath;
067            if (attributePath.lastIndexOf(aliasPath) == -1)
068            {
069                m_logger.warn("aliasPath should be a substring of attributePath");
070            }
071            initMapping(attributePath, aliasPath);
072        }
073    
074        /**
075         * Constructor declaration
076         *
077         * @param name the name of the alias
078         * @param attributePath the full path of the SelectionCriteria attribute
079         * @param allPathsAliased indicates that all path portions of attributePath
080         * should be aliased (previously was the default)
081         */
082        public UserAlias(String name, String attributePath, boolean allPathsAliased)
083        {
084            m_name = name;
085            m_attributePath = attributePath;
086            m_allPathsAliased = allPathsAliased;
087        }
088    
089        /**
090         * generates the mapping from the aliasPath
091         * @param aliasPath the portion of attributePath which should be aliased
092         *
093         */
094        private void initMapping(String attributePath, String aliasPath)
095        {
096            Iterator aliasSegmentItr = pathToSegments(aliasPath).iterator();
097            String currPath = "";
098            String separator = "";
099            while (aliasSegmentItr.hasNext())
100            {
101                currPath = currPath + separator + (String) aliasSegmentItr.next();
102                int beginIndex = attributePath.indexOf(currPath);
103                if (beginIndex == -1)
104                {
105                    break;
106                }
107                int endIndex = beginIndex + currPath.length();
108                m_mapping.put(attributePath.substring(0, endIndex), m_name);
109                separator = ".";
110            }
111        }
112    
113        private ArrayList pathToSegments(String path)
114        {
115            ArrayList segments = new ArrayList();
116            int sp = path.indexOf('.');
117            while (sp != -1)
118            {
119                segments.add(path.substring(0, sp));
120                path = path.substring(sp + 1);
121                sp = path.indexOf('.');
122            }
123            segments.add(path);
124            return segments;
125        }
126    
127        /**
128         * Returns the name of this alias
129         */
130        public String getName()
131        {
132            return m_name;
133        }
134    
135        /**
136         * Returns the name of this alias if path has been added
137         * to the aliased portions of attributePath
138         *
139         * @param path the path to test for inclusion in the alias
140         */
141        public String getAlias(String path)
142        {
143            if (m_allPathsAliased && m_attributePath.lastIndexOf(path) != -1)
144            {
145                return m_name;
146            }
147            Object retObj = m_mapping.get(path);
148            if (retObj != null)
149            {
150                return (String) retObj;
151            }
152            return null;
153        }
154    
155        /**
156         * Adds a path to the aliased paths
157         *
158         * @param path the path to add to the aliased paths
159         */
160        public void add(String path)
161        {
162            m_mapping.put(path, m_name);
163        }
164    }