Search in sources :

Example 1 with Treeitem

use of org.zkoss.zul.Treeitem in project adempiere by adempiere.

the class ADTreePanel method nodeChanged.

//
/**************************************************************************
	 *  Node Changed - synchronize Node
	 *
	 *  @param  save    true the node was saved (changed/added), false if the row was deleted
	 *  @param  keyID   the ID of the row changed
	 *  @param  name	name
	 *  @param  description	description
	 *  @param  isSummary	summary node
	 *  @param  imageIndicator image indicator
	 */
@SuppressWarnings("unchecked")
public void nodeChanged(boolean save, int keyID, String name, String description, boolean isSummary, String imageIndicator) {
    if (tree == null)
        return;
    //	if ID==0=root - don't update it
    if (keyID == 0)
        return;
    //  try to find the node
    TreeModel<Object> model = tree.getModel();
    DefaultTreeNode<Object> root = (DefaultTreeNode<Object>) model.getRoot();
    SimpleTreeModel sModel = new SimpleTreeModel(root);
    DefaultTreeNode<Object> node = sModel.find(null, keyID);
    //  Node not found and saved -> new
    if (node == null && save) {
        MTreeNode rootData = (MTreeNode) root.getData();
        MTreeNode mTreeNode = new MTreeNode(keyID, 0, name, description, rootData.getNode_ID(), isSummary, imageIndicator, false, null);
        DefaultTreeNode<Object> newNode = new DefaultTreeNode<Object>(mTreeNode);
        sModel.addNode(root, newNode, 0);
        int[] path = sModel.getPath(newNode);
        Treeitem ti = tree.renderItemByPath(path);
        tree.setSelectedItem(ti);
    } else //  Node found and saved -> change
    if (node != null && save) {
        MTreeNode mTreeNode = (MTreeNode) node.getData();
        mTreeNode.setName(name);
        mTreeNode.setAllowsChildren(isSummary);
        int[] path = sModel.getPath(node);
        Treeitem ti = tree.renderItemByPath(path);
        tree.setSelectedItem(ti);
    } else //  Node found and not saved -> delete
    if (node != null && !save) {
        sModel.removeNode(node);
    } else //  Error
    {
        node = null;
    }
    //  Nothing to display
    if (node == null)
        return;
}
Also used : MTreeNode(org.compiere.model.MTreeNode) Treeitem(org.zkoss.zul.Treeitem) SimpleTreeModel(org.adempiere.webui.component.SimpleTreeModel) DefaultTreeNode(org.zkoss.zul.DefaultTreeNode)

Example 2 with Treeitem

use of org.zkoss.zul.Treeitem 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 3 with Treeitem

use of org.zkoss.zul.Treeitem 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 4 with Treeitem

use of org.zkoss.zul.Treeitem 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)

Example 5 with Treeitem

use of org.zkoss.zul.Treeitem in project adempiere by adempiere.

the class WDeleteEntity method createNodes.

//  dynInit
/**
	 * Create Nodes
	 * @param root
	 */
private void createNodes(DeleteEntitiesModel currentNode, Treechildren root) {
    //	Load from parent
    loadChilds(currentNode, root);
    @SuppressWarnings("unchecked") Collection<Treeitem> collItemChild = (Collection<Treeitem>) root.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()).isMandatoryLink()) {
            DeleteEntitiesModel itemTableData = (DeleteEntitiesModel) node.getValue();
            Treechildren nodeChild = new Treechildren();
            createNodes(itemTableData, nodeChild);
            //	
            if (nodeChild.getItemCount() != 0) {
                node.appendChild(nodeChild);
            }
        }
    }
}
Also used : Treeitem(org.zkoss.zul.Treeitem) DeleteEntitiesModel(org.adempiere.util.DeleteEntitiesModel) Treechildren(org.zkoss.zul.Treechildren) Collection(java.util.Collection)

Aggregations

Treeitem (org.zkoss.zul.Treeitem)27 DefaultTreeNode (org.zkoss.zul.DefaultTreeNode)8 MTreeNode (org.compiere.model.MTreeNode)7 Treechildren (org.zkoss.zul.Treechildren)7 DeleteEntitiesModel (org.adempiere.util.DeleteEntitiesModel)5 DropEvent (org.zkoss.zk.ui.event.DropEvent)5 Component (org.zkoss.zk.ui.Component)4 Treerow (org.zkoss.zul.Treerow)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 SimpleTreeModel (org.adempiere.webui.component.SimpleTreeModel)3 SchemaNodeData (org.openforis.collect.designer.component.SchemaTreeModel.SchemaNodeData)3 SQLException (java.sql.SQLException)2 Collection (java.util.Collection)2 AdempiereException (org.adempiere.exceptions.AdempiereException)2 SimpleListModel (org.adempiere.webui.component.SimpleListModel)2 SchemaTreeNode (org.openforis.collect.designer.component.SchemaTreeModel.SchemaTreeNode)2 Event (org.zkoss.zk.ui.event.Event)2 Menupopup (org.zkoss.zul.Menupopup)2 IOException (java.io.IOException)1