1 14 package org.compiere.model; 15 16 import java.sql.*; 17 import java.util.*; 18 19 import org.compiere.util.*; 20 21 27 public class MRecordAccess extends X_AD_Record_Access 28 { 29 34 public MRecordAccess (Properties ctx, ResultSet rs) 35 { 36 super(ctx, rs); 37 } 39 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 setIsExclude (true); 54 setIsReadOnly (false); 55 setIsDependentEntities(false); 56 } 58 private String m_keyColumnName = null; 60 61 65 public String getKeyColumnName() 66 { 67 if (m_keyColumnName != null) 68 return m_keyColumnName; 69 String 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 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 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 e) 102 { 103 pstmt = null; 104 } 105 if (m_keyColumnName == null) 106 log.error("Record Access requires Table with one key column"); 107 return m_keyColumnName; 109 } 111 115 public String 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 return null; 123 } 125 129 public boolean isSynonym() 130 { 131 return getSynonym() == null; 132 } 134 139 public String getKeyColumnName (AccessSqlParser.TableInfo[] tableInfo) 140 { 141 String columnSyn = getSynonym(); 142 if (columnSyn == null) 143 return m_keyColumnName; 144 for (int i = 0; i < tableInfo.length; i++) 146 { 147 if (m_keyColumnName.equals("AD_User_ID")) 148 { 149 if (tableInfo[i].getTableName().equals("AD_User")) 151 return m_keyColumnName; 152 } 153 else if (m_keyColumnName.equals("AD_ElementValue_ID")) 154 { 155 if (tableInfo[i].getTableName().equals("AD_ElementValue")) 157 return m_keyColumnName; 158 } 159 } return columnSyn; 161 } 163 167 public String toString() 168 { 169 StringBuffer sb = new StringBuffer ("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 } 181 185 public String toStringX (Properties ctx) 186 { 187 String in = Msg.getMsg(ctx, "Include"); 188 String ex = Msg.getMsg(ctx, "Exclude"); 189 StringBuffer sb = new StringBuffer (); 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 } 201 202 private String m_tableName; 203 204 210 public String getTableName (Properties ctx) 211 { 212 if (m_tableName == null) 213 { 214 String 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 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 e) 240 { 241 pstmt = null; 242 } 243 String realName = Msg.translate(ctx, m_tableName + "_ID"); 245 if (!realName.equals(m_tableName + "_ID")) 246 m_tableName = realName; 247 } 248 return m_tableName; 249 } 251 }
| Popular Tags
|