Search in sources :

Example 1 with DeleteEntitiesModel

use of org.adempiere.util.DeleteEntitiesModel in project adempiere by adempiere.

the class VDeleteEntity method createNodes.

//	init
/**
	 * Create Nodes for parent
	 * @param root
	 */
@SuppressWarnings("unchecked")
private void createNodes(DefaultMutableTreeNode root) {
    DeleteEntitiesModel currentNode = (DeleteEntitiesModel) root.getUserObject();
    //	Load from parent
    loadChilds(currentNode, root);
    //	
    Enumeration<DefaultMutableTreeNode> kids = root.children();
    while (kids.hasMoreElements()) {
        DefaultMutableTreeNode node = kids.nextElement();
        if (root.isNodeAncestor(node)) {
            log.log(Level.WARNING, "Loop detected, escaping.");
            break;
        } else if (((DeleteEntitiesModel) node.getUserObject()).isMandatoryLink()) {
            createNodes(node);
        }
    }
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) DeleteEntitiesModel(org.adempiere.util.DeleteEntitiesModel)

Example 2 with DeleteEntitiesModel

use of org.adempiere.util.DeleteEntitiesModel in project adempiere by adempiere.

the class DeleteEntityControler method deleteSelectedNodes.

/**
	 * De;ete Selected nodes
	 * @param commit
	 * @return
	 */
public int deleteSelectedNodes() {
    int totalCount = 0;
    Trx trx = Trx.get(Trx.createTrxName("delete"), true);
    String errorMsg = null;
    try {
        //	
        while (!stack.empty()) {
            DeleteEntitiesModel tableData = (DeleteEntitiesModel) stack.pop();
            totalCount += tableData.delete(trx);
        }
        //	Commit changes
        if (!isDryRun()) {
            trx.commit(true);
        } else {
            trx.rollback(true);
        }
    } catch (Exception ex) {
        errorMsg = ex.getLocalizedMessage();
        log.log(Level.WARNING, "Cascade delete failed.", ex);
        totalCount = 0;
        trx.rollback();
    } finally {
        trx.close();
    }
    //	Exception
    if (errorMsg != null)
        throw new AdempiereException(errorMsg);
    //	Return
    return totalCount;
}
Also used : AdempiereException(org.adempiere.exceptions.AdempiereException) DeleteEntitiesModel(org.adempiere.util.DeleteEntitiesModel) Trx(org.compiere.util.Trx) SQLException(java.sql.SQLException) AdempiereException(org.adempiere.exceptions.AdempiereException)

Example 3 with DeleteEntitiesModel

use of org.adempiere.util.DeleteEntitiesModel in project adempiere by adempiere.

the class WDelete method createNodes.

//  dynInit
private void createNodes(DeleteEntitiesModel tableData, Treechildren ItemChildren) {
    DeleteEntitiesModel currentNode = tableData;
    HashSet<String> tablesIgnored = new HashSet<String>(Arrays.asList(new String[] { "T_Report", "T_ReportStatement", "AD_Attribute_Value", "AD_PInstance_Log", "A_Valid_Asset_Combinations" }));
    if (tablesIgnored.contains(currentNode.tableName))
        return;
    String sql = "SELECT t.TableName, c.ColumnName, c.IsMandatory " + "FROM AD_Table t" + " INNER JOIN AD_Column c ON (t.AD_Table_ID=c.AD_Table_ID) " + "WHERE t.IsView='N'" + " AND c.ColumnName NOT IN ('CreatedBy', 'UpdatedBy') " + // not the same table
    " AND t.TableName NOT IN ('C_TaxDeclarationAcct',?)" + " AND (" + //	#1 - direct
    "(c.ColumnName=? AND c.IsKey='N' AND c.ColumnSQL IS NULL)" + " OR " + //	Table Reference
    "c.AD_Reference_Value_ID IN " + "(SELECT rt.AD_Reference_ID FROM AD_Ref_Table rt" + " INNER JOIN AD_Table tt ON (rt.AD_Table_ID=tt.AD_Table_ID)" + //	#2
    " WHERE tt.TableName = ? ) " + ") " + "ORDER BY t.LoadSeq DESC";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    String keyCol = currentNode.tableName + "_ID";
    try {
        pstmt = DB.prepareStatement(sql, null);
        pstmt.setString(1, currentNode.tableName);
        pstmt.setString(2, keyCol);
        pstmt.setString(3, currentNode.tableName);
        rs = pstmt.executeQuery();
        while (rs.next()) {
            DeleteEntitiesModel data = new DeleteEntitiesModel();
            data.mandatoryLink = "Y".equals(rs.getString(3));
            data.tableName = rs.getString(1);
            data.joinColumn = rs.getString(2);
            data.whereClause = " EXISTS (SELECT 1 FROM " + currentNode.tableName + " WHERE " + currentNode.tableName + "." + currentNode.tableName + // + currentNode.joinColumn 
            "_ID" + " = " + data.tableName + "." + data.joinColumn + " AND " + currentNode.whereClause + ") ";
            int count = data.getCount();
            if (count > 0) {
                Treeitem treeitem = new Treeitem();
                ItemChildren.appendChild(treeitem);
                treeitem.setLabel(data.tableName + "." + data.joinColumn);
                treeitem.setValue(data);
            } else
                log.log(Level.FINE, "No records:" + data.tableName);
        }
    } catch (SQLException e) {
        log.log(Level.INFO, sql);
        throw new AdempiereException("Couldn't load child tables", e);
    } finally {
        DB.close(rs, pstmt);
    }
    @SuppressWarnings("unchecked") Collection<Treeitem> collItemChild = (Collection<Treeitem>) ItemChildren.getItems();
    Iterator<Treeitem> it = collItemChild.iterator();
    while (it.hasNext()) {
        Treeitem node = (Treeitem) it.next();
        Treeitem rootOfNode = node.getParentItem();
        if (rootOfNode != null && rootOfNode.getParentItem() != null && rootOfNode.getParentItem().equals(node)) {
            log.log(Level.WARNING, "Loop detected, escaping.");
            break;
        } else if (((DeleteEntitiesModel) node.getValue()).mandatoryLink) {
            DeleteEntitiesModel itemTableData = (DeleteEntitiesModel) node.getValue();
            Treechildren nodeChild = new Treechildren();
            createNodes(itemTableData, nodeChild);
            if (nodeChild.getItemCount() != 0) {
                node.appendChild(nodeChild);
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) DeleteEntitiesModel(org.adempiere.util.DeleteEntitiesModel) PreparedStatement(java.sql.PreparedStatement) Treeitem(org.zkoss.zul.Treeitem) AdempiereException(org.adempiere.exceptions.AdempiereException) ResultSet(java.sql.ResultSet) Treechildren(org.zkoss.zul.Treechildren) Collection(java.util.Collection) HashSet(java.util.HashSet)

Example 4 with DeleteEntitiesModel

use of org.adempiere.util.DeleteEntitiesModel in project adempiere by adempiere.

the class WDelete method generateTree.

// ValueChange
private void generateTree(Object value, Integer clientID) {
    if (value == null)
        return;
    Integer selectedTableID = ((Integer) value).intValue();
    if (selectedTableID == 0 || clientId == null) {
        FDialog.error(form.getWindowNo(), "ParameterError", "Table or Client cannot be Null.");
        return;
    }
    MTable table = MTable.get(Env.getCtx(), selectedTableID);
    DeleteEntitiesModel data = new DeleteEntitiesModel();
    data.mandatoryLink = true;
    data.tableName = table.getTableName();
    data.joinColumn = table.getKeyColumns()[0];
    data.whereClause = " " + data.tableName + ".AD_Client_ID = " + clientId;
    if (table.getTableName().equals("AD_User")) {
        data.whereClause = data.whereClause + " AND NOT EXISTS (SELECT * FROM C_BPartner bp " + "WHERE AD_User.Link_BPartner_ID=bp.C_BPartner_ID " + "AND (bp.IsEmployee='Y' OR bp.IsSalesRep='Y'))";
    }
    tree.clear();
    if ((tree.getChildren()).size() > 1) {
        @SuppressWarnings("rawtypes") List treePreviousChild = tree.getChildren();
        tree.removeChild((Treechildren) treePreviousChild.get(1));
    }
    Treechildren rootTreeChild = new Treechildren();
    Treeitem rootTreeItem = new Treeitem();
    rootTreeItem.setValue(data);
    rootTreeItem.setLabel(data.tableName + "." + data.joinColumn);
    Treechildren rootTreeItemChild = new Treechildren();
    createNodes(data, rootTreeItemChild);
    rootTreeItem.appendChild(rootTreeItemChild);
    rootTreeChild.appendChild(rootTreeItem);
    tree.appendChild(rootTreeChild);
}
Also used : Treeitem(org.zkoss.zul.Treeitem) MTable(org.compiere.model.MTable) DeleteEntitiesModel(org.adempiere.util.DeleteEntitiesModel) Treechildren(org.zkoss.zul.Treechildren) List(java.util.List) ArrayList(java.util.ArrayList)

Example 5 with DeleteEntitiesModel

use of org.adempiere.util.DeleteEntitiesModel in project adempiere by adempiere.

the class WDelete method onEvent.

// createNodes(arg1, arg2)	
@Override
public void onEvent(Event e) throws Exception {
    boolean commit = !dryRun.isChecked();
    if (e.getTarget().getId().equals(ConfirmPanel.A_CANCEL)) {
        dispose();
    } else if (e.getTarget().equals(clientPick)) {
        String clientIDStr = clientPick.getSelectedItem().getLabel();
        clientId = clientMap.get(clientIDStr);
        Object value = tablePick.getValue();
        generateTree(value, clientId);
    } else if (e.getTarget().getId().equals(ConfirmPanel.A_OK)) {
        //String clientIDStr = clientPick.getText();
        //Integer clientId = clientMap.get(clientIDStr);
        Object objTableID = tablePick.getValue();
        int tableId = 0;
        if (objTableID != null)
            tableId = (Integer) objTableID;
        if (tableId == 0 || clientId == null) {
            FDialog.error(form.getWindowNo(), "Error", "Select client and base table for cascade delete.");
        } else {
            m_totalCount = 0;
            m_trx = Trx.get(Trx.createTrxName("delete"), true);
            String errorMsg = "";
            try {
                @SuppressWarnings("unchecked") Collection<Treeitem> items = tree.getItems();
                Iterator<Treeitem> nodes = items.iterator();
                Stack<DeleteEntitiesModel> stack = new Stack<DeleteEntitiesModel>();
                while (nodes.hasNext()) {
                    stack.push((DeleteEntitiesModel) (((Treeitem) nodes.next()).getValue()));
                }
                while (!stack.empty()) {
                    DeleteEntitiesModel tableData = (DeleteEntitiesModel) stack.pop();
                    m_totalCount += tableData.delete(m_trx);
                }
                if (commit)
                    m_trx.commit(true);
                else
                    m_trx.rollback(true);
            } catch (Exception ex) {
                errorMsg = ex.getLocalizedMessage();
                log.log(Level.WARNING, "Cascade delete failed.", ex);
                m_totalCount = 0;
                m_trx.rollback();
                FDialog.error(form.getWindowNo(), "DeleteError", errorMsg);
                return;
            } finally {
                m_trx.close();
            }
            FDialog.info(form.getWindowNo(), form, "DeleteSuccess", "Records deleted:" + " #" + m_totalCount);
            dispose();
        }
    }
}
Also used : DeleteEntitiesModel(org.adempiere.util.DeleteEntitiesModel) SQLException(java.sql.SQLException) AdempiereException(org.adempiere.exceptions.AdempiereException) Stack(java.util.Stack) Treeitem(org.zkoss.zul.Treeitem)

Aggregations

DeleteEntitiesModel (org.adempiere.util.DeleteEntitiesModel)9 Treeitem (org.zkoss.zul.Treeitem)5 SQLException (java.sql.SQLException)4 AdempiereException (org.adempiere.exceptions.AdempiereException)4 Treechildren (org.zkoss.zul.Treechildren)4 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 List (java.util.List)2 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)2 Stack (java.util.Stack)1 DefaultTreeModel (javax.swing.tree.DefaultTreeModel)1 MTable (org.compiere.model.MTable)1 Trx (org.compiere.util.Trx)1