KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > query > UserAlias


1 package org.apache.ojb.broker.query;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.io.Serializable JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.ojb.broker.util.logging.Logger;
25 import org.apache.ojb.broker.util.logging.LoggerFactory;
26
27 /**
28  * This class is used to specify the path segments of a Criteria
29  * that should have associated table aliases. Previously, the default
30  * behaviour was that all path segments participated in the alias
31  *
32  * @author <a HREF="mailto:philip.warrick@mcgill.ca">Phil Warrick</a>
33  */

34 public class UserAlias implements Serializable JavaDoc
35 {
36     private static final long serialVersionUID = 3257282552220627249L;
37     
38     private Map JavaDoc m_mapping = new HashMap JavaDoc();
39     private String JavaDoc m_name = null;
40     private String JavaDoc m_attributePath = null;
41     private boolean m_allPathsAliased = false;
42     private Logger m_logger = LoggerFactory.getLogger(UserAlias.class);
43
44     /**
45      * Constructor declaration
46      *
47      * @param name the name of the alias
48      */

49     public UserAlias(String JavaDoc name)
50     {
51         m_name = name;
52     }
53
54     /**
55      * Constructor declaration
56      *
57      * @param name the name of the alias
58      * @param attributePath the full path of the SelectionCriteria attribute
59      * @param aliasPath the portion of the attributePath which should be aliased.
60      * This should be unambiguous. If ambiguous portions need aliasing (e.g.
61      * B.C in allAs.B.C.B.C), use add() instead
62      */

63     public UserAlias(String JavaDoc name, String JavaDoc attributePath, String JavaDoc aliasPath)
64     {
65         m_name = name;
66         m_attributePath = attributePath;
67         if (attributePath.lastIndexOf(aliasPath) == -1)
68         {
69             m_logger.warn("aliasPath should be a substring of attributePath");
70         }
71         initMapping(attributePath, aliasPath);
72     }
73
74     /**
75      * Constructor declaration
76      *
77      * @param name the name of the alias
78      * @param attributePath the full path of the SelectionCriteria attribute
79      * @param allPathsAliased indicates that all path portions of attributePath
80      * should be aliased (previously was the default)
81      */

82     public UserAlias(String JavaDoc name, String JavaDoc attributePath, boolean allPathsAliased)
83     {
84         m_name = name;
85         m_attributePath = attributePath;
86         m_allPathsAliased = allPathsAliased;
87     }
88
89     /**
90      * generates the mapping from the aliasPath
91      * @param aliasPath the portion of attributePath which should be aliased
92      *
93      */

94     private void initMapping(String JavaDoc attributePath, String JavaDoc aliasPath)
95     {
96         Iterator JavaDoc aliasSegmentItr = pathToSegments(aliasPath).iterator();
97         String JavaDoc currPath = "";
98         String JavaDoc separator = "";
99         while (aliasSegmentItr.hasNext())
100         {
101             currPath = currPath + separator + (String JavaDoc) 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 JavaDoc pathToSegments(String JavaDoc path)
114     {
115         ArrayList JavaDoc segments = new ArrayList JavaDoc();
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 JavaDoc 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 JavaDoc getAlias(String JavaDoc path)
142     {
143         if (m_allPathsAliased && m_attributePath.lastIndexOf(path) != -1)
144         {
145             return m_name;
146         }
147         Object JavaDoc retObj = m_mapping.get(path);
148         if (retObj != null)
149         {
150             return (String JavaDoc) 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 JavaDoc path)
161     {
162         m_mapping.put(path, m_name);
163     }
164 }
Popular Tags