KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > dbPort > Join


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.dbPort;
15
16 import org.compiere.util.*;
17
18 /**
19  * Join Clause.
20  * <pre>
21  * f.AD_Column_ID = c.AD_Column_ID(+)
22  * </pre>
23  * @author Jorg Janke
24  * @version $Id: Join.java,v 1.4 2002/08/12 01:55:15 danb Exp $
25  */

26 public class Join
27 {
28     /**
29      * Constructor
30      * @param joinClause
31      */

32     public Join (String JavaDoc joinClause)
33     {
34         if (joinClause == null)
35             throw new IllegalArgumentException JavaDoc("Join - clause cannot be null");
36         evaluate (joinClause);
37     } // Join
38

39     private String JavaDoc m_joinClause;
40     private String JavaDoc m_mainTable;
41     private String JavaDoc m_mainAlias;
42     private String JavaDoc m_joinTable;
43     private String JavaDoc m_joinAlias;
44     private boolean m_left;
45     private String JavaDoc m_condition;
46
47     /**
48      * Evaluate the clause.
49      * e.g. tb.AD_User_ID(+)=?
50      * f.AD_Column_ID = c.AD_Column_ID(+)
51      * @param joinClause
52      */

53     private void evaluate (String JavaDoc joinClause)
54     {
55         m_joinClause = joinClause;
56         int indexEqual = joinClause.indexOf('=');
57         m_left = indexEqual < joinClause.indexOf("(+)"); // converts to LEFT if true
58
// get table alias of it
59
if (m_left) // f.AD_Column_ID = c.AD_Column_ID(+) => f / c
60
{
61             m_mainAlias = joinClause.substring
62                 (0, Util.findIndexOf(joinClause, '.','=')).trim(); // f
63
int end = joinClause.indexOf('.', indexEqual);
64             if (end == -1) // no alias
65
end = joinClause.indexOf('(', indexEqual);
66             m_joinAlias = joinClause.substring(indexEqual+1, end).trim(); // c
67
}
68         else // f.AD_Column_ID(+) = c.AD_Column_ID => c / f
69
{
70             int end = joinClause.indexOf('.', indexEqual);
71             if (end == -1) // no alias
72
end = joinClause.length();
73             m_mainAlias = joinClause.substring(indexEqual+1, end).trim(); // c
74
m_joinAlias = joinClause.substring
75                 (0, Util.findIndexOf(joinClause, '.','(')).trim(); // f
76
}
77         m_condition = Util.replace(joinClause, "(+)", "").trim();
78     } // evaluate
79

80     /**
81      * Get origial Join Clause.
82      * e.g. f.AD_Column_ID = c.AD_Column_ID(+)
83      * @return Join cluase
84      */

85     public String JavaDoc getJoinClause()
86     {
87         return m_joinClause;
88     } // getJoinClause
89

90     /**
91      * Get Main Table Alias
92      * @return Main Table Alias
93      */

94     public String JavaDoc getMainAlias()
95     {
96         return m_mainAlias;
97     } // getMainAlias
98

99     /**
100      * Get Join Table Alias
101      * @return Join Table Alias
102      */

103     public String JavaDoc getJoinAlias()
104     {
105         return m_joinAlias;
106     } // getJoinAlias
107

108     /**
109      * Is Left Aouter Join
110      * @return true if left outer join
111      */

112     public boolean isLeft()
113     {
114         return m_left;
115     } // isLeft
116

117     /**
118      * Get Join condition.
119      * e.g. f.AD_Column_ID = c.AD_Column_ID
120      * @return join condition
121      */

122     public String JavaDoc getCondition()
123     {
124         return m_condition;
125     } // getCondition
126

127     /*************************************************************************/
128
129     /**
130      * Set Main Table Name.
131      * If table name equals alias, the alias is set to ""
132      * @param mainTable
133      */

134     public void setMainTable(String JavaDoc mainTable)
135     {
136         if (mainTable == null || mainTable.length() == 0)
137             return;
138         m_mainTable = mainTable;
139         if (m_mainAlias.equals(mainTable))
140             m_mainAlias = "";
141     } // setMainTable
142

143     /**
144      * Get Main Table Name
145      * @return Main Table Name
146      */

147     public String JavaDoc getMainTable()
148     {
149         return m_mainTable;
150     } // getMainTable
151

152     /**
153      * Set Main Table Name.
154      * If table name equals alias, the alias is set to ""
155      * @param joinTable
156      */

157     public void setJoinTable(String JavaDoc joinTable)
158     {
159         if (joinTable == null || joinTable.length() == 0)
160             return;
161         m_joinTable = joinTable;
162         if (m_joinAlias.equals(joinTable))
163             m_joinAlias = "";
164     } // setJoinTable
165

166     /**
167      * Get Join Table Name
168      * @return Join Table Name
169      */

170     public String JavaDoc getJoinTable()
171     {
172         return m_joinTable;
173     } // getJoinTable
174

175     /*************************************************************************/
176
177     /**
178      * This Join is a condition of the first Join.
179      * e.g. tb.AD_User_ID(+)=? or tb.AD_User_ID(+)='123'
180      * @param first
181      * @return true if condition
182      */

183     public boolean isConditionOf (Join first)
184     {
185         if (m_mainTable == null // did not find Table from "Alias"
186
&& (first.getJoinTable().equals(m_joinTable) // same join table
187
|| first.getMainAlias().equals(m_joinTable))) // same main table
188
return true;
189         return false;
190     } // isConditionOf
191

192     /**
193      * String representation
194      * @return info
195      */

196     public String JavaDoc toString()
197     {
198         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ("Join[");
199         sb.append(m_joinClause)
200             .append(" - Main=").append(m_mainTable).append("/").append(m_mainAlias)
201             .append(", Join=").append(m_joinTable).append("/").append(m_joinAlias)
202             .append(", Left=").append(m_left)
203             .append(", Condition=").append(m_condition)
204             .append("]");
205         return sb.toString();
206     } // toString
207

208 } // Join
209
Popular Tags