public boolean hasLoopInTree(){
  int productCategoryId=getM_Product_Category_ID();
  int newParentCategoryId=getM_Product_Category_Parent_ID();
  ResultSet rs=null;
  PreparedStatement pstmt=null;
  String sql=" SELECT M_Product_Category_ID, M_Product_Category_Parent_ID FROM M_Product_Category";
  final Vector<SimpleTreeNode> categories=new Vector<SimpleTreeNode>(100);
  try {
    pstmt=DB.prepareStatement(sql,null);
    rs=pstmt.executeQuery();
    while (rs.next()) {
      if (rs.getInt(1) == productCategoryId)       categories.add(new SimpleTreeNode(rs.getInt(1),newParentCategoryId));
      categories.add(new SimpleTreeNode(rs.getInt(1),rs.getInt(2)));
    }
    if (hasLoop(newParentCategoryId,categories,productCategoryId))     return true;
  }
 catch (  SQLException e) {
    s_log.log(Level.SEVERE,sql,e);
    return true;
  }
 finally {
    DB.close(rs,pstmt);
    rs=null;
    pstmt=null;
  }
  return false;
}

public boolean hasLoopInTree ( ) { int productCategoryId = getM_Product_Category_ID ( ) ; int newParentCategoryId = getM_Product_Category_Parent_ID ( ) ; ResultSet rs = null ; PreparedStatement pstmt = null ; String sql = " SELECT M_Product_Category_ID, M_Product_Category_Parent_ID FROM M_Product_Category" ; final Vector < SimpleTreeNode > categories = new Vector < SimpleTreeNode > ( 100 ) ; try { pstmt = DB . prepareStatement ( sql , null ) ; rs = pstmt . executeQuery ( ) ; while ( rs . next ( ) ) { if ( rs . getInt ( 1 ) == productCategoryId ) categories . add ( new SimpleTreeNode ( rs . getInt ( 1 ) , newParentCategoryId ) ) ; categories . add ( new SimpleTreeNode ( rs . getInt ( 1 ) , rs . getInt ( 2 ) ) ) ; } if ( hasLoop ( newParentCategoryId , categories , productCategoryId ) ) return true ; } catch ( SQLException e ) { s_log . log ( Level . SEVERE , sql , e ) ; return true ; } finally { DB . close ( rs , pstmt ) ; rs = null ; pstmt = null ; } return false ; }
loop detection of product category tree .
