Exemplo n.º 1
0
  public static void updateTreeNode(PO po) {
    int nodeId = po.get_ID();
    if (nodeId < 0) {
      return; // nothing to do, because our PO has no ID to match against a tree node
    }
    final int AD_Table_ID = po.get_Table_ID();
    if (!MTree.hasTree(AD_Table_ID)) {
      return;
    }
    final String nodeTableName = MTree.getNodeTableName(AD_Table_ID);
    if (nodeTableName == null) {
      return;
    }

    //
    // services
    final IPOTreeSupportFactory treeSupportFactory = Services.get(IPOTreeSupportFactory.class);

    final String trxName = po.get_TrxName();
    final IPOTreeSupport treeSupport = treeSupportFactory.get(po.get_TableName());

    if (po.is_ValueChanged("IsSummary") && !po.get_ValueAsBoolean("IsSummary")) {
      // Move all its children to parent
      final List<MTreeNode> children =
          fetchNodes(AD_Table_ID, "Parent_ID=?", new Object[] {nodeId}, trxName);
      if (children.size() > 0) {
        throw new AdempiereException("TreeNodeHasChildren"); // TODO: translate
      }
    }

    if (treeSupport.isParentChanged(po)) {
      int parentId = treeSupport.getParent_ID(po);
      int oldParentId = treeSupport.getOldParent_ID(po);
      int seqNo = -1; // compute

      final String sql =
          "SELECT AD_Tree_ID, TreeType FROM AD_Tree WHERE AD_Table_ID=? AND AD_Client_ID=?";
      PreparedStatement pstmt = null;
      ResultSet rs = null;
      try {
        pstmt = DB.prepareStatement(sql, trxName);
        DB.setParameters(pstmt, new Object[] {AD_Table_ID, po.getAD_Client_ID()});
        rs = pstmt.executeQuery();
        while (rs.next()) {
          int AD_Tree_ID = rs.getInt(COLUMNNAME_AD_Tree_ID);
          String treeType = rs.getString(COLUMNNAME_TreeType);
          updateNode(
              po, AD_Tree_ID, treeType, AD_Table_ID, nodeId, parentId, oldParentId, seqNo, trxName);
        }
      } catch (SQLException e) {
        throw new DBException(e);
      } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
      }
    }
  }
Exemplo n.º 2
0
  /**
   * ************************************************************************ Insert id data into
   * Tree
   *
   * @return true if inserted
   */
  protected static boolean insertTreeNode(PO po) {
    //
    // services
    final IPOTreeSupportFactory treeSupportFactory = Services.get(IPOTreeSupportFactory.class);

    // TODO: check self contained tree
    final int AD_Table_ID = po.get_Table_ID();
    if (!MTree.hasTree(AD_Table_ID)) {
      return false;
    }
    final int id = po.get_ID();
    final int AD_Client_ID = po.getAD_Client_ID();
    final String treeTableName = MTree.getNodeTableName(AD_Table_ID);
    final String trxName = po.get_TrxName();
    final Logger log = po.get_Logger();

    final IPOTreeSupport treeSupport = treeSupportFactory.get(po.get_TableName());
    final int AD_Tree_ID = treeSupport.getAD_Tree_ID(po);
    int parentId = treeSupport.getParent_ID(po);
    if (parentId < 0 || parentId == IPOTreeSupport.UNKNOWN_ParentID) {
      parentId = ROOT_Node_ID;
    }
    //
    // Insert
    final StringBuilder sb =
        new StringBuilder("INSERT  INTO ")
            .append(treeTableName)
            .append(" (AD_Client_ID,AD_Org_ID, IsActive,Created,CreatedBy,Updated,UpdatedBy, ")
            .append("AD_Tree_ID, Node_ID, Parent_ID, SeqNo) ")
            //
            .append(
                "SELECT t.AD_Client_ID,0, 'Y', now(), "
                    + po.getUpdatedBy()
                    + ", now(), "
                    + po.getUpdatedBy()
                    + ",")
            .append("t.AD_Tree_ID, ")
            .append(id)
            .append(", ")
            .append(parentId)
            .append(", 999 ")
            .append("FROM AD_Tree t ")
            .append("WHERE t.AD_Client_ID=")
            .append(AD_Client_ID)
            .append(" AND t.IsActive='Y'");
    if (AD_Tree_ID > 0) {
      sb.append(" AND t.AD_Tree_ID=").append(AD_Tree_ID);
    } else
    // std trees
    {
      sb.append(" AND t.IsAllNodes='Y' AND t.AD_Table_ID=").append(AD_Table_ID);
    }
    // Duplicate Check
    sb.append(" AND NOT EXISTS (SELECT * FROM ")
        .append(treeTableName)
        .append(" e ")
        .append("WHERE e.AD_Tree_ID=t.AD_Tree_ID AND Node_ID=")
        .append(id)
        .append(")");
    //
    int no = DB.executeUpdateEx(sb.toString(), trxName);
    if (no < 0) {
      log.warn("#" + no + " - AD_Table_ID=" + AD_Table_ID);
      return false;
    }
    log.debug("#" + no + " - AD_Table_ID=" + AD_Table_ID);
    // MigrationLogger.instance.logMigrationSQL(po, sb.toString()); // metas: not needed because
    // it's called directly from PO

    listeners.onNodeInserted(po);

    return true;
  } // insert_Tree