Search in sources :

Example 1 with MTree_Node

use of org.compiere.model.MTree_Node in project adempiere by adempiere.

the class TreeMaintenance method verifyTree.

//	doIt
/**
	 *  Verify Tree
	 * 	@param tree tree
	 */
private String verifyTree(MTree tree) {
    String nodeTableName = tree.getNodeTableName();
    String sourceTableName = tree.getSourceTableName(true);
    String sourceTableKey = sourceTableName + "_ID";
    int AD_Client_ID = tree.getAD_Client_ID();
    int C_Element_ID = 0;
    if (MTree.TREETYPE_ElementValue.equals(tree.getTreeType())) {
        String sql = "SELECT C_Element_ID FROM C_Element " + "WHERE AD_Tree_ID=" + tree.getAD_Tree_ID();
        C_Element_ID = DB.getSQLValue(null, sql);
        if (C_Element_ID <= 0)
            throw new IllegalStateException("No Account Element found");
    }
    //	Delete unused
    StringBuffer sql = new StringBuffer();
    sql.append("DELETE ").append(nodeTableName).append(" WHERE AD_Tree_ID=").append(tree.getAD_Tree_ID()).append(" AND Node_ID NOT IN (SELECT ").append(sourceTableKey).append(" FROM ").append(sourceTableName).append(" WHERE AD_Client_ID=").append(AD_Client_ID);
    if (C_Element_ID > 0)
        sql.append(" AND C_Element_ID=").append(C_Element_ID);
    sql.append(")");
    log.finer(sql.toString());
    //
    int deletes = DB.executeUpdate(sql.toString(), get_TrxName());
    addLog(0, null, new BigDecimal(deletes), tree.getName() + " Deleted");
    if (!tree.isAllNodes())
        return tree.getName() + " OK";
    //	Insert new
    int inserts = 0;
    sql = new StringBuffer();
    sql.append("SELECT ").append(sourceTableKey).append(" FROM ").append(sourceTableName).append(" WHERE AD_Client_ID=").append(AD_Client_ID);
    if (C_Element_ID > 0)
        sql.append(" AND C_Element_ID=").append(C_Element_ID);
    sql.append(" AND ").append(sourceTableKey).append("  NOT IN (SELECT Node_ID FROM ").append(nodeTableName).append(" WHERE AD_Tree_ID=").append(tree.getAD_Tree_ID()).append(")");
    log.finer(sql.toString());
    //
    boolean ok = true;
    PreparedStatement pstmt = null;
    try {
        pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            int Node_ID = rs.getInt(1);
            PO node = null;
            if (nodeTableName.equals("AD_TreeNode"))
                node = new MTree_Node(tree, Node_ID);
            else if (nodeTableName.equals("AD_TreeNodeBP"))
                node = new MTree_NodeBP(tree, Node_ID);
            else if (nodeTableName.equals("AD_TreeNodePR"))
                node = new MTree_NodePR(tree, Node_ID);
            else if (nodeTableName.equals("AD_TreeNodeMM"))
                node = new MTree_NodeMM(tree, Node_ID);
            //				
            if (node == null)
                log.log(Level.SEVERE, "No Model for " + nodeTableName);
            else {
                if (node.save())
                    inserts++;
                else
                    log.log(Level.SEVERE, "Could not add to " + tree + " Node_ID=" + Node_ID);
            }
        }
        rs.close();
        pstmt.close();
        pstmt = null;
    } catch (Exception e) {
        log.log(Level.SEVERE, "verifyTree", e);
        ok = false;
    }
    try {
        if (pstmt != null)
            pstmt.close();
        pstmt = null;
    } catch (Exception e) {
        pstmt = null;
    }
    addLog(0, null, new BigDecimal(inserts), tree.getName() + " Inserted");
    return tree.getName() + (ok ? " OK" : " Error");
}
Also used : MTree_NodePR(org.compiere.model.MTree_NodePR) PreparedStatement(java.sql.PreparedStatement) BigDecimal(java.math.BigDecimal) MTree_NodeMM(org.compiere.model.MTree_NodeMM) MTree_Node(org.compiere.model.MTree_Node) ResultSet(java.sql.ResultSet) MTree_NodeBP(org.compiere.model.MTree_NodeBP) PO(org.compiere.model.PO)

Example 2 with MTree_Node

use of org.compiere.model.MTree_Node in project adempiere by adempiere.

the class TreeMaintenance method addNode.

/**
	 * 	Action: Add Node to Tree
	 * 	@param item item
	 */
public void addNode(ListItem item) {
    if (item != null) {
        //	May cause Error if in tree
        if (m_tree.isProduct()) {
            MTree_NodePR node = new MTree_NodePR(m_tree, item.id);
            node.saveEx();
        } else if (m_tree.isBPartner()) {
            MTree_NodeBP node = new MTree_NodeBP(m_tree, item.id);
            node.saveEx();
        } else if (m_tree.isMenu()) {
            MTree_NodeMM node = new MTree_NodeMM(m_tree, item.id);
            node.saveEx();
        } else {
            MTree_Node node = new MTree_Node(m_tree, item.id);
            node.saveEx();
        }
    }
}
Also used : MTree_NodePR(org.compiere.model.MTree_NodePR) MTree_Node(org.compiere.model.MTree_Node) MTree_NodeBP(org.compiere.model.MTree_NodeBP) MTree_NodeMM(org.compiere.model.MTree_NodeMM)

Aggregations

MTree_Node (org.compiere.model.MTree_Node)2 MTree_NodeBP (org.compiere.model.MTree_NodeBP)2 MTree_NodeMM (org.compiere.model.MTree_NodeMM)2 MTree_NodePR (org.compiere.model.MTree_NodePR)2 BigDecimal (java.math.BigDecimal)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 PO (org.compiere.model.PO)1