PageRenderTime 36ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/org.adempiere.base/src/org/compiere/model/PO_Record.java

https://bitbucket.org/idempiere/idempiere/
Java | 275 lines | 189 code | 17 blank | 69 comment | 26 complexity | 18a07f7bd6fc92ee7e5e28b8f6e2a031 MD5 | raw file
  1. /******************************************************************************
  2. * Product: Adempiere ERP & CRM Smart Business Solution *
  3. * Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved. *
  4. * This program is free software; you can redistribute it and/or modify it *
  5. * under the terms version 2 of the GNU General Public License as published *
  6. * by the Free Software Foundation. This program is distributed in the hope *
  7. * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
  8. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
  9. * See the GNU General Public License for more details. *
  10. * You should have received a copy of the GNU General Public License along *
  11. * with this program; if not, write to the Free Software Foundation, Inc., *
  12. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
  13. * For the text or an alternative of this public license, you may reach us *
  14. * ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA *
  15. * or via info@compiere.org or http://www.compiere.org/license.html *
  16. *****************************************************************************/
  17. package org.compiere.model;
  18. import java.sql.PreparedStatement;
  19. import java.sql.ResultSet;
  20. import java.util.List;
  21. import java.util.logging.Level;
  22. import org.compiere.util.CLogger;
  23. import org.compiere.util.DB;
  24. import org.compiere.util.DisplayType;
  25. import org.compiere.util.Env;
  26. /**
  27. * Maintain AD_Table_ID/Record_ID constraint
  28. *
  29. * @author Jorg Janke
  30. * @version $Id: PO_Record.java,v 1.4 2006/07/30 00:58:04 jjanke Exp $
  31. */
  32. public class PO_Record
  33. {
  34. /** Parent Tables */
  35. private static int[] s_parents = new int[]{
  36. X_C_Order.Table_ID,
  37. X_CM_Container.Table_ID
  38. };
  39. private static String[] s_parentNames = new String[]{
  40. X_C_Order.Table_Name,
  41. X_CM_Container.Table_Name
  42. };
  43. private static int[] s_parentChilds = new int[]{
  44. X_C_OrderLine.Table_ID,
  45. X_CM_Container_Element.Table_ID
  46. };
  47. private static String[] s_parentChildNames = new String[]{
  48. X_C_OrderLine.Table_Name,
  49. X_CM_Container_Element.Table_Name
  50. };
  51. /** Cascade Table ID */
  52. private static int[] s_cascades = new int[]{
  53. X_AD_Attachment.Table_ID,
  54. X_AD_Archive.Table_ID,
  55. // X_CM_ContainerTTable.Table_ID,
  56. // X_CM_CStageTTable.Table_ID,
  57. X_K_Index.Table_ID,
  58. X_AD_Note.Table_ID,
  59. X_AD_RecentItem.Table_ID
  60. };
  61. /** Cascade Table Names */
  62. private static String[] s_cascadeNames = new String[]{
  63. X_AD_Attachment.Table_Name,
  64. X_AD_Archive.Table_Name,
  65. // X_CM_ContainerTTable.Table_Name,
  66. // X_CM_CStageTTable.Table_Name,
  67. X_K_Index.Table_Name,
  68. X_AD_Note.Table_Name,
  69. X_AD_RecentItem.Table_Name
  70. };
  71. /** Restrict Table ID */
  72. private static int[] s_restricts = new int[]{
  73. X_R_Request.Table_ID,
  74. X_CM_Chat.Table_ID
  75. // X_Fact_Acct.Table_ID
  76. };
  77. /** Restrict Table Names */
  78. private static String[] s_restrictNames = new String[]{
  79. X_R_Request.Table_Name,
  80. X_CM_Chat.Table_Name
  81. // X_Fact_Acct.Table_Name
  82. };
  83. /** Logger */
  84. private static CLogger log = CLogger.getCLogger (PO_Record.class);
  85. /**
  86. * Delete Cascade including (selected)parent relationships
  87. * @param AD_Table_ID table
  88. * @param Record_ID record
  89. * @param trxName transaction
  90. * @return false if could not be deleted
  91. */
  92. static boolean deleteCascade (int AD_Table_ID, int Record_ID, String trxName)
  93. {
  94. // Table Loop
  95. for (int i = 0; i < s_cascades.length; i++)
  96. {
  97. // DELETE FROM table WHERE AD_Table_ID=#1 AND Record_ID=#2
  98. if (s_cascades[i] != AD_Table_ID)
  99. {
  100. Object[] params = new Object[]{new Integer(AD_Table_ID), new Integer(Record_ID)};
  101. StringBuffer sql = new StringBuffer ("DELETE FROM ")
  102. .append(s_cascadeNames[i])
  103. .append(" WHERE AD_Table_ID=? AND Record_ID=?");
  104. int no = DB.executeUpdate(sql.toString(), params, false, trxName);
  105. if (no > 0) {
  106. if (log.isLoggable(Level.CONFIG)) log.config(s_cascadeNames[i] + " (" + AD_Table_ID + "/" + Record_ID + ") #" + no);
  107. } else if (no < 0) {
  108. log.severe(s_cascadeNames[i] + " (" + AD_Table_ID + "/" + Record_ID + ") #" + no);
  109. return false;
  110. }
  111. }
  112. }
  113. // Parent Loop
  114. for (int j = 0; j < s_parents.length; j++)
  115. {
  116. // DELETE FROM AD_Attachment WHERE AD_Table_ID=1 AND Record_ID IN
  117. // (SELECT C_InvoiceLine_ID FROM C_InvoiceLine WHERE C_Invoice_ID=1)
  118. if (s_parents[j] == AD_Table_ID)
  119. {
  120. int AD_Table_IDchild = s_parentChilds[j];
  121. Object[] params = new Object[]{new Integer(AD_Table_IDchild), new Integer(Record_ID)};
  122. for (int i = 0; i < s_cascades.length; i++)
  123. {
  124. StringBuffer sql = new StringBuffer ("DELETE FROM ")
  125. .append(s_cascadeNames[i])
  126. .append(" WHERE AD_Table_ID=? AND Record_ID IN (SELECT ")
  127. .append(s_parentChildNames[j]).append("_ID FROM ")
  128. .append(s_parentChildNames[j]).append(" WHERE ")
  129. .append(s_parentNames[j]).append("_ID=?)");
  130. int no = DB.executeUpdate(sql.toString(), params, false, trxName);
  131. if (no > 0) {
  132. if (log.isLoggable(Level.CONFIG)) log.config(s_cascadeNames[i] + " " + s_parentNames[j]
  133. + " (" + AD_Table_ID + "/" + Record_ID + ") #" + no);
  134. } else if (no < 0) {
  135. log.severe(s_cascadeNames[i] + " " + s_parentNames[j]
  136. + " (" + AD_Table_ID + "/" + Record_ID + ") #" + no);
  137. return false;
  138. }
  139. }
  140. }
  141. }
  142. return true;
  143. } // deleteCascade
  144. //IDEMPIERE-2060
  145. public static void deleteModelCascade(String tableName, int Record_ID, String trxName) {
  146. //find dependent tables to delete cascade
  147. final String sql = ""
  148. + "SELECT t.TableName, "
  149. + " c.ColumnName "
  150. + "FROM AD_Column c "
  151. + " JOIN AD_Table t ON c.AD_Table_ID = t.AD_Table_ID "
  152. + " LEFT JOIN AD_Ref_Table r ON c.AD_Reference_Value_ID = r.AD_Reference_ID "
  153. + " LEFT JOIN AD_Table tr ON r.AD_Table_ID = tr.AD_Table_ID "
  154. + "WHERE t.IsView = 'N' "
  155. + " AND t.IsActive = 'Y' "
  156. + " AND c.IsActive = 'Y' "
  157. + " AND ( ( c.AD_Reference_ID = " + DisplayType.TableDir
  158. + " AND c.ColumnName = ? || '_ID' ) "
  159. + " OR ( c.AD_Reference_ID IN ( " + DisplayType.Table + ", " + DisplayType.Search + " ) "
  160. + " AND ( tr.TableName = ? OR ( tr.TableName IS NULL AND c.ColumnName = ? || '_ID' ) ) ) ) "
  161. + " AND c.FKConstraintType = '" + MColumn.FKCONSTRAINTTYPE_ModelCascade + "' ";
  162. List<List<Object>> dependents = DB.getSQLArrayObjectsEx(trxName, sql, tableName, tableName, tableName);
  163. if (dependents != null) {
  164. for (List<Object> row : dependents) {
  165. String dependentTableName = (String) row.get(0);
  166. String dependentColumnName = (String) row.get(1);
  167. String dependentWhere = dependentColumnName + "=?";
  168. List<PO> poList = new Query(Env.getCtx(),
  169. dependentTableName,
  170. dependentWhere,
  171. trxName).setParameters(Record_ID).list();
  172. for (PO po : poList) {
  173. po.deleteEx(true, trxName);
  174. }
  175. }
  176. }
  177. }
  178. /**
  179. * An entry Exists for restrict table/record combination
  180. * @param AD_Table_ID table
  181. * @param Record_ID record
  182. * @param trxName transaction
  183. * @return error message (Table Name) or null
  184. */
  185. static String exists (int AD_Table_ID, int Record_ID, String trxName)
  186. {
  187. // Table Loop only
  188. for (int i = 0; i < s_restricts.length; i++)
  189. {
  190. // SELECT COUNT(*) FROM table WHERE AD_Table_ID=#1 AND Record_ID=#2
  191. StringBuffer sql = new StringBuffer ("SELECT COUNT(*) FROM ")
  192. .append(s_restrictNames[i])
  193. .append(" WHERE AD_Table_ID=? AND Record_ID=?");
  194. int no = DB.getSQLValue(trxName, sql.toString(), AD_Table_ID, Record_ID);
  195. if (no > 0)
  196. return s_restrictNames[i];
  197. }
  198. return null;
  199. } // exists
  200. /**
  201. * Validate all tables for AD_Table/Record_ID relationships
  202. */
  203. static void validate ()
  204. {
  205. String sql = "SELECT AD_Table_ID, TableName FROM AD_Table WHERE IsView='N' ORDER BY TableName";
  206. PreparedStatement pstmt = null;
  207. ResultSet rs = null;
  208. try
  209. {
  210. pstmt = DB.prepareStatement (sql, null);
  211. rs = pstmt.executeQuery ();
  212. while (rs.next ())
  213. {
  214. validate (rs.getInt(1), rs.getString(2));
  215. }
  216. }
  217. catch (Exception e)
  218. {
  219. log.log (Level.SEVERE, sql, e);
  220. }
  221. finally {
  222. DB.close(rs, pstmt);
  223. rs = null; pstmt = null;
  224. }
  225. } // validate
  226. /**
  227. * Validate all tables for AD_Table/Record_ID relationships
  228. * @param AD_Table_ID table
  229. */
  230. static void validate (int AD_Table_ID)
  231. {
  232. MTable table = new MTable(Env.getCtx(), AD_Table_ID, null);
  233. if (table.isView())
  234. log.warning("Ignored - View " + table.getTableName());
  235. else
  236. validate (table.getAD_Table_ID(), table.getTableName());
  237. } // validate
  238. /**
  239. * Validate Table for Table/Record
  240. * @param AD_Table_ID table
  241. * @param TableName Name
  242. */
  243. static private void validate (int AD_Table_ID, String TableName)
  244. {
  245. for (int i = 0; i < s_cascades.length; i++)
  246. {
  247. StringBuffer sql = new StringBuffer ("DELETE FROM ")
  248. .append(s_cascadeNames[i])
  249. .append(" WHERE AD_Table_ID=").append(AD_Table_ID)
  250. .append(" AND Record_ID NOT IN (SELECT ")
  251. .append(TableName).append("_ID FROM ").append(TableName).append(")");
  252. int no = DB.executeUpdate(sql.toString(), null);
  253. if (no > 0)
  254. if (log.isLoggable(Level.CONFIG)) log.config(s_cascadeNames[i] + " (" + AD_Table_ID + "/" + TableName
  255. + ") Invalid #" + no);
  256. }
  257. } // validate
  258. } // PO_Record