Search in sources :

Example 1 with MTree_NodeMM

use of org.compiere.model.MTree_NodeMM 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_NodeMM

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

the class MenuElementHandler method startElement.

public void startElement(Properties ctx, Element element) throws SAXException {
    String elementValue = element.getElementValue();
    int AD_Backup_ID = -1;
    String Object_Status = null;
    Attributes atts = element.attributes;
    log.info(elementValue + " " + atts.getValue("ADMenuNameID"));
    // String entitytype = atts.getValue("EntityType");
    // if (entitytype.compareTo("U") == 0 || entitytype.compareTo("D") == 0
    // && m_UpdateMode == true ) {
    String name = null;
    int idDetail = 0;
    StringBuffer sqlB = null;
    name = atts.getValue("ADMenuNameID");
    int menuid = get_IDWithColumn(ctx, "AD_Menu", "Name", name);
    X_AD_Menu m_Menu = new X_AD_Menu(ctx, menuid, getTrxName(ctx));
    if (menuid <= 0 && atts.getValue("AD_Menu_ID") != null && Integer.parseInt(atts.getValue("AD_Menu_ID")) <= PackOut.MAX_OFFICIAL_ID)
        m_Menu.setAD_Menu_ID(Integer.parseInt(atts.getValue("AD_Menu_ID")));
    if (menuid > 0) {
        AD_Backup_ID = copyRecord(ctx, "AD_Menu", m_Menu);
        Object_Status = "Update";
    } else {
        Object_Status = "New";
        AD_Backup_ID = 0;
    }
    m_Menu.setName(name);
    name = atts.getValue("ADWindowNameID");
    if (name != null && name.trim().length() > 0) {
        int id = get_IDWithColumn(ctx, "AD_Window", "Name", name);
        if (id <= 0) {
            element.defer = true;
            return;
        }
        m_Menu.setAD_Window_ID(id);
    }
    name = atts.getValue("ADProcessNameID");
    if (name != null && name.trim().length() > 0) {
        int id = get_IDWithColumn(ctx, "AD_Process", "Name", name);
        if (id <= 0) {
            element.defer = true;
            return;
        }
        m_Menu.setAD_Process_ID(id);
    }
    name = atts.getValue("ADFormNameID");
    if (name != null && name.trim().length() > 0) {
        int id = get_IDWithColumn(ctx, "AD_Form", "Name", name);
        if (id <= 0) {
            element.defer = true;
            return;
        }
        m_Menu.setAD_Form_ID(id);
    }
    name = atts.getValue("ADBrowseNameID");
    if (name != null && name.trim().length() > 0) {
        int id = get_IDWithColumn(ctx, "AD_Browse", "Name", name);
        if (id <= 0) {
            element.defer = true;
            return;
        }
        m_Menu.setAD_Browse_ID(id);
    }
    name = atts.getValue("ADTaskNameID");
    if (name != null && name.trim().length() > 0) {
        int id = get_IDWithColumn(ctx, "AD_Task", "Name", name);
        if (id <= 0) {
            element.defer = true;
            return;
        }
        m_Menu.setAD_Task_ID(id);
    }
    name = atts.getValue("ADWorkbenchNameID");
    if (name != null && name.trim().length() > 0) {
        int id = get_IDWithColumn(ctx, "AD_Workbench", "Name", name);
        if (id <= 0) {
            element.defer = true;
            return;
        }
        m_Menu.setAD_Workbench_ID(id);
    }
    name = atts.getValue("ADWorkflowNameID");
    if (name != null && name.trim().length() > 0) {
        int id = get_IDWithColumn(ctx, "AD_Workflow", "Name", name);
        if (id <= 0) {
            element.defer = true;
            return;
        }
        m_Menu.setAD_Workflow_ID(id);
    }
    String action = (atts.getValue("Action") != null ? atts.getValue("Action") : " ");
    if (action.compareTo(" ") > -1)
        m_Menu.setAction(action);
    m_Menu.setDescription(getStringValue(atts, "Description"));
    m_Menu.setEntityType(atts.getValue("EntityType"));
    m_Menu.setIsReadOnly(Boolean.valueOf(atts.getValue("isReadOnly")).booleanValue());
    m_Menu.setIsSOTrx(Boolean.valueOf(atts.getValue("isSOTrx")).booleanValue());
    m_Menu.setIsSummary(Boolean.valueOf(atts.getValue("isSummary")).booleanValue());
    m_Menu.setIsActive(Boolean.valueOf(atts.getValue("isActive")).booleanValue());
    if (m_Menu.save(getTrxName(ctx)) == true) {
        try {
            idDetail = record_log(ctx, 1, m_Menu.getName(), "Menu", m_Menu.get_ID(), AD_Backup_ID, Object_Status, "AD_Menu", get_IDWithColumn(ctx, "AD_Table", "TableName", "AD_Menu"));
        } catch (SAXException e) {
            log.info("setmenu:" + e);
        }
    } else {
        try {
            idDetail = record_log(ctx, 0, m_Menu.getName(), "Menu", m_Menu.get_ID(), AD_Backup_ID, Object_Status, "AD_Menu", get_IDWithColumn(ctx, "AD_Table", "TableName", "AD_Menu"));
        } catch (SAXException e) {
            log.info("setmenu:" + e);
        }
    }
    name = atts.getValue("ADParentMenuNameID");
    int id = get_ID(ctx, "AD_Menu", name);
    String sql2 = "SELECT count(Parent_ID) FROM AD_TREENODEMM WHERE AD_Tree_ID = 10" + " AND Node_ID = " + menuid;
    int countRecords = DB.getSQLValue(getTrxName(ctx), sql2);
    if (countRecords > 0) {
        StringBuffer sqlC = new StringBuffer("select * from AD_TREENODEMM where AD_Tree_ID = 10 and " + " Node_ID =?");
        try {
            PreparedStatement pstmt1 = DB.prepareStatement(sqlC.toString(), getTrxName(ctx));
            pstmt1.setInt(1, menuid);
            ResultSet rs1 = pstmt1.executeQuery();
            if (rs1.next()) {
                String colValue = null;
                ResultSetMetaData meta = rs1.getMetaData();
                int columns = meta.getColumnCount();
                int tableID = get_IDWithColumn(ctx, "AD_Table", "TableName", "AD_TreeNodeMM");
                for (int q = 1; q <= columns; q++) {
                    String col_Name = meta.getColumnName(q);
                    StringBuffer sql = new StringBuffer("SELECT AD_Column_ID FROM AD_column WHERE Upper(ColumnName) = '" + col_Name + "' AND AD_Table_ID = ?");
                    int columnID = DB.getSQLValue(getTrxName(ctx), sql.toString(), tableID);
                    sql = new StringBuffer("SELECT AD_Reference_ID FROM AD_COLUMN WHERE AD_Column_ID = " + (columnID == -1 ? "null" : columnID));
                    int referenceID = DB.getSQLValue(getTrxName(ctx), sql.toString());
                    int idBackup = DB.getNextID(Env.getAD_Client_ID(ctx), "AD_Package_Imp_Backup", getTrxName(ctx));
                    if (referenceID == 20 || referenceID == 28)
                        if (rs1.getObject(q).equals("Y"))
                            colValue = "true";
                        else
                            colValue = "false";
                    else
                        colValue = rs1.getObject(q).toString();
                    StringBuffer sqlD = new StringBuffer("INSERT INTO AD_Package_Imp_Backup" + "(AD_Client_ID, AD_Org_ID, CreatedBy, UpdatedBy, " + "AD_PACKAGE_IMP_BACKUP_ID, AD_PACKAGE_IMP_DETAIL_ID, AD_PACKAGE_IMP_ID," + " AD_TABLE_ID, AD_COLUMN_ID, AD_REFERENCE_ID, COLVALUE)" + "VALUES(" + " " + Env.getAD_Client_ID(ctx) + ", " + Env.getAD_Org_ID(ctx) + ", " + Env.getAD_User_ID(ctx) + ", " + Env.getAD_User_ID(ctx) + ", " + idBackup + ", " + idDetail + ", " + getPackageImpId(ctx) + ", " + tableID + ", " + (columnID == -1 ? "null" : columnID) + ", " + (referenceID == -1 ? "null" : referenceID) + ", '" + colValue + "')");
                    int no = DB.executeUpdate(sqlD.toString(), getTrxName(ctx));
                    if (no == -1)
                        log.info("Insert to import backup failed");
                }
            }
            rs1.close();
            pstmt1.close();
            pstmt1 = null;
        } catch (Exception e) {
            log.info("get_IDWithMasterID:" + e);
        }
        MTree tree = new MTree(ctx, 10, getTrxName(ctx));
        MTree_NodeMM treeNode = MTree_NodeMM.get(tree, m_Menu.getAD_Menu_ID());
        treeNode.setSeqNo(Integer.valueOf(atts.getValue("ADParentSeqno")));
        treeNode.set_CustomColumn("Parent_ID", id);
        treeNode.saveEx();
    } else {
        MTree tree = new MTree(ctx, 10, getTrxName(ctx));
        MTree_NodeMM treeNode = new MTree_NodeMM(tree, m_Menu.getAD_Menu_ID());
        treeNode.setSeqNo(Integer.valueOf(atts.getValue("ADParentSeqno")));
        treeNode.set_CustomColumn("Parent_ID", id);
        treeNode.setNode_ID(m_Menu.getAD_Menu_ID());
        treeNode.saveEx();
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) X_AD_Menu(org.compiere.model.X_AD_Menu) Attributes(org.xml.sax.Attributes) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) MTree(org.compiere.model.MTree) MTree_NodeMM(org.compiere.model.MTree_NodeMM)

Example 3 with MTree_NodeMM

use of org.compiere.model.MTree_NodeMM 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_NodeMM (org.compiere.model.MTree_NodeMM)3 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 MTree_Node (org.compiere.model.MTree_Node)2 MTree_NodeBP (org.compiere.model.MTree_NodeBP)2 MTree_NodePR (org.compiere.model.MTree_NodePR)2 BigDecimal (java.math.BigDecimal)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 MTree (org.compiere.model.MTree)1 PO (org.compiere.model.PO)1 X_AD_Menu (org.compiere.model.X_AD_Menu)1 Attributes (org.xml.sax.Attributes)1 SAXException (org.xml.sax.SAXException)1