KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > model > MRecordAccess


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-2003 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.model;
15
16 import java.sql.*;
17 import java.util.*;
18
19 import org.compiere.util.*;
20
21 /**
22  * Record Access Model
23  *
24  * @author Jorg Janke
25  * @version $Id: MRecordAccess.java,v 1.5 2003/11/06 07:09:09 jjanke Exp $
26  */

27 public class MRecordAccess extends X_AD_Record_Access
28 {
29     /**
30      * Load Constructor
31      * @param ctx context
32      * @param rs result set
33      */

34     public MRecordAccess (Properties ctx, ResultSet rs)
35     {
36         super(ctx, rs);
37     } // MRecordAccess
38

39     /**
40      * Full New Constructor
41      * @param ctx context
42      * @param AD_Role_ID role
43      * @param AD_Table_ID table
44      * @param Record_ID record
45      */

46     public MRecordAccess (Properties ctx, int AD_Role_ID, int AD_Table_ID, int Record_ID)
47     {
48         super (ctx,0);
49         setAD_Role_ID(AD_Role_ID);
50         setAD_Table_ID(AD_Table_ID);
51         setRecord_ID(Record_ID);
52         //
53
setIsExclude (true);
54         setIsReadOnly (false);
55         setIsDependentEntities(false);
56     } // MRecordAccess
57

58     // Key Column Name */
59
private String JavaDoc m_keyColumnName = null;
60     
61     /**
62      * Get Key Column Name
63      * @return Key Column Name
64      */

65     public String JavaDoc getKeyColumnName()
66     {
67         if (m_keyColumnName != null)
68             return m_keyColumnName;
69         //
70
String JavaDoc sql = "SELECT ColumnName "
71             + "FROM AD_Column "
72             + "WHERE AD_Table_ID=? AND IsKey='Y' AND IsActive='Y'";
73         PreparedStatement pstmt = null;
74         try
75         {
76             pstmt = DB.prepareCall(sql);
77             pstmt.setInt(1, getAD_Table_ID());
78             ResultSet rs = pstmt.executeQuery();
79             while (rs.next())
80             {
81                 String JavaDoc s = rs.getString(1);
82                 if (m_keyColumnName == null)
83                     m_keyColumnName = s;
84                 else
85                     log.error("getKeyColumnName - more than one key = " + s);
86             }
87             rs.close();
88             pstmt.close();
89             pstmt = null;
90         }
91         catch (Exception JavaDoc e)
92         {
93             log.error("getKeyColumnName", e);
94         }
95         try
96         {
97             if (pstmt != null)
98                 pstmt.close();
99             pstmt = null;
100         }
101         catch (Exception JavaDoc e)
102         {
103             pstmt = null;
104         }
105         if (m_keyColumnName == null)
106             log.error("Record Access requires Table with one key column");
107         //
108
return m_keyColumnName;
109     } // getKeyColumnName
110

111     /**
112      * Get Synonym of Column
113      * @return Synonym Column Name
114      */

115     public String JavaDoc getSynonym()
116     {
117         if ("AD_User_ID".equals(getKeyColumnName()))
118             return "SalesRep_ID";
119         else if ("C_ElementValue_ID".equals(getKeyColumnName()))
120             return "Account_ID";
121         //
122
return null;
123     } // getSynonym
124

125     /**
126      * Key Column has a Synonym
127      * @return true if Key Column has Synonym
128      */

129     public boolean isSynonym()
130     {
131         return getSynonym() == null;
132     } // isSynonym
133

134     /**
135      * Get Key Column Name with consideration of Synonym
136      * @param tableInfo
137      * @return
138      */

139     public String JavaDoc getKeyColumnName (AccessSqlParser.TableInfo[] tableInfo)
140     {
141         String JavaDoc columnSyn = getSynonym();
142         if (columnSyn == null)
143             return m_keyColumnName;
144         // We have a synonym - ignore it if base table inquired
145
for (int i = 0; i < tableInfo.length; i++)
146         {
147             if (m_keyColumnName.equals("AD_User_ID"))
148             {
149                 // List of tables where not to use SalesRep_ID
150
if (tableInfo[i].getTableName().equals("AD_User"))
151                     return m_keyColumnName;
152             }
153             else if (m_keyColumnName.equals("AD_ElementValue_ID"))
154             {
155                 // List of tables where not to use Account_ID
156
if (tableInfo[i].getTableName().equals("AD_ElementValue"))
157                     return m_keyColumnName;
158             }
159         } // tables to be ignored
160
return columnSyn;
161     } // getKeyColumnInfo
162

163     /**
164      * String Representation
165      * @return info
166      */

167     public String JavaDoc toString()
168     {
169         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("MRecordAccess[AD_Role_ID=")
170             .append(getAD_Role_ID())
171             .append(",AD_Table_ID=").append(getAD_Table_ID())
172             .append(",Record_ID=").append(getRecord_ID())
173             .append(",Active=").append(isActive())
174             .append(",Exclude=").append(isExclude())
175             .append(",ReadOnly=").append(isReadOnly())
176             .append(",Dependent=").append(isDependentEntities())
177             .append("]");
178         return sb.toString();
179     } // toString
180

181     /**
182      * Extended String Representation
183      * @return extended info
184      */

185     public String JavaDoc toStringX (Properties ctx)
186     {
187         String JavaDoc in = Msg.getMsg(ctx, "Include");
188         String JavaDoc ex = Msg.getMsg(ctx, "Exclude");
189         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
190         sb.append(Msg.translate(ctx, "AD_Table_ID"))
191                 .append("=").append(getTableName(ctx)).append(", ")
192             .append(Msg.translate(ctx, "Record_ID"))
193             . append("=").append(getRecord_ID())
194             .append(" - ").append(Msg.translate(ctx, "IsDependentEntities"))
195                 .append("=").append(isDependentEntities())
196             .append(" (").append(Msg.translate(ctx, "IsReadOnly")).append("=").append(isReadOnly())
197             .append(") - ").append(isExclude() ? ex : in);
198         return sb.toString();
199     } // toStringX
200

201     /** TableName */
202     private String JavaDoc m_tableName;
203
204     /**
205      * Get Table Name
206      * @param AD_Table_ID id
207      * @param ctx context
208      * @return table name
209      */

210     public String JavaDoc getTableName (Properties ctx)
211     {
212         if (m_tableName == null)
213         {
214             String JavaDoc sql = "SELECT TableName FROM AD_Table WHERE AD_Table_ID=?";
215             PreparedStatement pstmt = null;
216             try
217             {
218                 pstmt = DB.prepareCall(sql);
219                 pstmt.setInt(1, getAD_Table_ID());
220                 ResultSet rs = pstmt.executeQuery();
221                 if (rs.next())
222                 {
223                     m_tableName = rs.getString(1);
224                 }
225                 rs.close();
226                 pstmt.close();
227                 pstmt = null;
228             }
229             catch (Exception JavaDoc e)
230             {
231                 log.error("getColumnName", e);
232             }
233             try
234             {
235                 if (pstmt != null)
236                     pstmt.close();
237                 pstmt = null;
238             }
239             catch (Exception JavaDoc e)
240             {
241                 pstmt = null;
242             }
243             // Get Clear Text
244
String JavaDoc realName = Msg.translate(ctx, m_tableName + "_ID");
245             if (!realName.equals(m_tableName + "_ID"))
246                 m_tableName = realName;
247         }
248         return m_tableName;
249     } // getTableName
250

251 } // MRecordAccess
252
Popular Tags