IDEMPIERE-2395 NoteDelete leaving orphan attachments

This commit is contained in:
Carlos Ruiz 2018-10-06 23:42:20 +02:00
parent 93fcc43ee5
commit c049ebfeaf
3 changed files with 455 additions and 0 deletions

View File

@ -0,0 +1,159 @@
SET SQLBLANKLINES ON
SET DEFINE OFF
-- IDEMPIERE-2395 NoteDelete leaving orphan attachments
-- Oct 6, 2018 11:05:15 PM CEST
INSERT INTO AD_Process (AD_Process_ID,AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,Name,Help,IsReport,Value,IsDirectPrint,Classname,AccessLevel,EntityType,Statistic_Count,Statistic_Seconds,IsBetaFunctionality,IsServerProcess,ShowHelp,CopyFromProcess,AD_Process_UU) VALUES (200102,0,0,'Y',TO_DATE('2018-10-06 23:05:15','YYYY-MM-DD HH24:MI:SS'),100,TO_DATE('2018-10-06 23:05:15','YYYY-MM-DD HH24:MI:SS'),100,'Clean Orphan Cascade Records','Some processes accessing directly the database can leave orphan records, this process clean those orphan records deleting them','N','CleanOrphanCascade','N','org.idempiere.process.CleanOrphanCascade','4','D',0,0,'N','N','Y','N','d3826a84-1467-4d6e-931b-e395f349f60c')
;
-- Oct 6, 2018 11:05:55 PM CEST
INSERT INTO AD_Menu (AD_Menu_ID,Name,Action,AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,IsSummary,AD_Process_ID,IsSOTrx,IsReadOnly,EntityType,IsCentrallyMaintained,AD_Menu_UU) VALUES (200161,'Clean Orphan Cascade Records','P',0,0,'Y',TO_DATE('2018-10-06 23:05:55','YYYY-MM-DD HH24:MI:SS'),100,TO_DATE('2018-10-06 23:05:55','YYYY-MM-DD HH24:MI:SS'),100,'N',200102,'Y','N','D','Y','67e5ec6c-93d0-47f9-bd45-f0d60d552b16')
;
-- Oct 6, 2018 11:05:55 PM CEST
INSERT INTO AD_TreeNodeMM (AD_Client_ID,AD_Org_ID, IsActive,Created,CreatedBy,Updated,UpdatedBy, AD_Tree_ID, Node_ID, Parent_ID, SeqNo, AD_TreeNodeMM_UU) SELECT t.AD_Client_ID, 0, 'Y', SysDate, 100, SysDate, 100,t.AD_Tree_ID, 200161, 0, 999, Generate_UUID() FROM AD_Tree t WHERE t.AD_Client_ID=0 AND t.IsActive='Y' AND t.IsAllNodes='Y' AND t.TreeType='MM' AND NOT EXISTS (SELECT * FROM AD_TreeNodeMM e WHERE e.AD_Tree_ID=t.AD_Tree_ID AND Node_ID=200161)
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=0, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=218
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=1, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=153
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=2, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=263
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=3, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=166
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=4, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=203
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=5, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=53242
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=6, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=236
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=7, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=183
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=8, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=160
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=9, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=278
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=10, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=345
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=11, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=53296
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=12, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=53014
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=13, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=53108
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=0, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=161
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=1, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=367
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=2, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=456
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=3, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=501
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=4, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=326
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=5, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=566
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=6, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=392
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=7, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=113
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=8, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=220
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=9, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=351
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=10, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=289
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=11, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=302
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=12, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=303
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=13, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=200047
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=14, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=200048
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=15, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=321
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=16, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=461
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=17, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=53193
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=18, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=53322
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=19, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=383
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=20, Updated=SysDate WHERE AD_Tree_ID=10 AND Node_ID=200161
;
SELECT register_migration_script('201810062307_IDEMPIERE-2395.sql') FROM dual
;

View File

@ -0,0 +1,156 @@
-- IDEMPIERE-2395 NoteDelete leaving orphan attachments
-- Oct 6, 2018 11:05:15 PM CEST
INSERT INTO AD_Process (AD_Process_ID,AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,Name,Help,IsReport,Value,IsDirectPrint,Classname,AccessLevel,EntityType,Statistic_Count,Statistic_Seconds,IsBetaFunctionality,IsServerProcess,ShowHelp,CopyFromProcess,AD_Process_UU) VALUES (200102,0,0,'Y',TO_TIMESTAMP('2018-10-06 23:05:15','YYYY-MM-DD HH24:MI:SS'),100,TO_TIMESTAMP('2018-10-06 23:05:15','YYYY-MM-DD HH24:MI:SS'),100,'Clean Orphan Cascade Records','Some processes accessing directly the database can leave orphan records, this process clean those orphan records deleting them','N','CleanOrphanCascade','N','org.idempiere.process.CleanOrphanCascade','4','D',0,0,'N','N','Y','N','d3826a84-1467-4d6e-931b-e395f349f60c')
;
-- Oct 6, 2018 11:05:55 PM CEST
INSERT INTO AD_Menu (AD_Menu_ID,Name,"action",AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,IsSummary,AD_Process_ID,IsSOTrx,IsReadOnly,EntityType,IsCentrallyMaintained,AD_Menu_UU) VALUES (200161,'Clean Orphan Cascade Records','P',0,0,'Y',TO_TIMESTAMP('2018-10-06 23:05:55','YYYY-MM-DD HH24:MI:SS'),100,TO_TIMESTAMP('2018-10-06 23:05:55','YYYY-MM-DD HH24:MI:SS'),100,'N',200102,'Y','N','D','Y','67e5ec6c-93d0-47f9-bd45-f0d60d552b16')
;
-- Oct 6, 2018 11:05:55 PM CEST
INSERT INTO AD_TreeNodeMM (AD_Client_ID,AD_Org_ID, IsActive,Created,CreatedBy,Updated,UpdatedBy, AD_Tree_ID, Node_ID, Parent_ID, SeqNo, AD_TreeNodeMM_UU) SELECT t.AD_Client_ID, 0, 'Y', statement_timestamp(), 100, statement_timestamp(), 100,t.AD_Tree_ID, 200161, 0, 999, Generate_UUID() FROM AD_Tree t WHERE t.AD_Client_ID=0 AND t.IsActive='Y' AND t.IsAllNodes='Y' AND t.TreeType='MM' AND NOT EXISTS (SELECT * FROM AD_TreeNodeMM e WHERE e.AD_Tree_ID=t.AD_Tree_ID AND Node_ID=200161)
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=0, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=218
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=1, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=153
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=2, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=263
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=3, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=166
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=4, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=203
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=5, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=53242
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=6, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=236
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=7, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=183
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=8, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=160
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=9, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=278
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=10, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=345
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=11, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=53296
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=12, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=53014
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=0, SeqNo=13, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=53108
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=0, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=161
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=1, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=367
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=2, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=456
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=3, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=501
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=4, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=326
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=5, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=566
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=6, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=392
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=7, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=113
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=8, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=220
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=9, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=351
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=10, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=289
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=11, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=302
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=12, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=303
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=13, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=200047
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=14, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=200048
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=15, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=321
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=16, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=461
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=17, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=53193
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=18, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=53322
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=19, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=383
;
-- Oct 6, 2018 11:07:01 PM CEST
UPDATE AD_TreeNodeMM SET Parent_ID=155, SeqNo=20, Updated=statement_timestamp() WHERE AD_Tree_ID=10 AND Node_ID=200161
;
SELECT register_migration_script('201810062307_IDEMPIERE-2395.sql') FROM dual
;

View File

@ -0,0 +1,140 @@
/**********************************************************************
* This file is part of iDempiere ERP Open Source *
* http://www.idempiere.org *
* *
* Copyright (C) Contributors *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, *
* MA 02110-1301, USA. *
* *
* Contributors: *
* - Carlos Ruiz *
**********************************************************************/
package org.idempiere.process;
import java.math.BigDecimal;
import java.util.List;
import java.util.logging.Level;
import org.compiere.model.MArchive;
import org.compiere.model.MAttachment;
import org.compiere.model.MTable;
import org.compiere.model.Query;
import org.compiere.process.ProcessInfoParameter;
import org.compiere.process.SvrProcess;
import org.compiere.util.DB;
import org.compiere.util.Msg;
/**
* IDEMPIERE-2395
* @author Carlos Ruiz - globalqss
*/
public class CleanOrphanCascade extends SvrProcess
{
/**
* Prepare - e.g., get Parameters.
*/
protected void prepare()
{
for (ProcessInfoParameter para : getParameter())
{
String name = para.getParameterName();
log.log(Level.SEVERE, "Unknown Parameter: " + name);
}
} // prepare
/**
* Perform process.
* @return Message
* @throws Exception
*/
protected String doIt() throws Exception
{
if (log.isLoggable(Level.INFO)) log.info("");
String whereTables = ""
+ " IsView = 'N' "
+ "AND TableName != 'AD_ChangeLog' "
+ "AND EXISTS (SELECT 1 "
+ " FROM AD_Column ct "
+ " WHERE ct.IsActive='Y' AND ct.AD_Table_ID = AD_Table.AD_Table_ID "
+ " AND ct.ColumnName = 'AD_Table_ID') "
+ "AND EXISTS (SELECT 1 "
+ " FROM AD_Column cr "
+ " WHERE cr.IsActive='Y' AND cr.AD_Table_ID = AD_Table.AD_Table_ID "
+ " AND cr.ColumnName = 'Record_ID') "
+ "AND EXISTS (SELECT 1 "
+ " FROM AD_Column ck "
+ " WHERE ck.IsActive='Y' AND ck.AD_Table_ID = AD_Table.AD_Table_ID "
+ " AND ck.ColumnName = AD_Table.TableName || '_ID')";
List<MTable> tables = new Query(getCtx(), "AD_Table", whereTables, get_TrxName())
.setOnlyActiveRecords(true)
.setOrderBy("TableName")
.list();
for (MTable table : tables) {
String tableName = table.getTableName();
StringBuilder sqlRef = new StringBuilder();
sqlRef.append("SELECT DISTINCT t.AD_Table_ID, ");
sqlRef.append(" t.TableName ");
sqlRef.append("FROM ").append(tableName).append(" r ");
sqlRef.append(" JOIN AD_Table t ON ( r.AD_Table_ID = t.AD_Table_ID ) ");
sqlRef.append("ORDER BY t.Tablename");
List<List<Object>> rowTables = DB.getSQLArrayObjectsEx(get_TrxName(), sqlRef.toString());
if (rowTables != null) {
for (List<Object> row : rowTables) {
int refTableID = ((BigDecimal) row.get(0)).intValue();
String refTableName = row.get(1).toString();
StringBuilder whereClause = new StringBuilder();
whereClause.append("AD_Table_ID = ").append(refTableID);
whereClause.append(" AND NOT EXISTS (SELECT ").append(refTableName).append("_ID ");
whereClause.append(" FROM ").append(refTableName).append(" ");
whereClause.append(" WHERE ").append(refTableName).append(".").append(refTableName).append("_ID = ").append(tableName).append(".Record_ID)");
int noDel = 0;
if (MAttachment.Table_Name.equals(tableName)) {
// special case for attachment because of store
List<MAttachment> attachments = new Query(getCtx(), tableName, whereClause.toString(), get_TrxName()).list();
for (MAttachment attachment : attachments) {
attachment.deleteEx(true, get_TrxName());
noDel++;
}
} else if (MArchive.Table_Name.equals(tableName)) {
// special case for archive because of store
List<MArchive> archives = new Query(getCtx(), tableName, whereClause.toString(), get_TrxName()).list();
for (MArchive archive : archives) {
archive.deleteEx(true, get_TrxName());
noDel++;
}
} else {
StringBuilder sqlDelete = new StringBuilder();
sqlDelete.append("DELETE FROM ").append(tableName).append(" WHERE ").append(whereClause);
noDel = DB.executeUpdateEx(sqlDelete.toString(), get_TrxName());
}
if (noDel > 0) {
addLog(Msg.parseTranslation(getCtx(), noDel + " " + tableName + " " + "@Deleted@ -> " + refTableName));
}
}
}
}
return "@OK@";
} // doIt
} // CleanOrphanCascade