1 package com.teamkonzept.webman.mainint; 2 3 import com.teamkonzept.db.*; 4 import com.teamkonzept.lib.*; 5 import com.teamkonzept.webman.*; 6 import com.teamkonzept.web.*; 7 import com.teamkonzept.webman.db.*; 8 import com.teamkonzept.webman.mainint.events.*; 9 10 import java.util.Stack ; 11 import java.sql.*; 12 13 18 public class TreeUtils implements ParameterTypes 19 { 20 25 public static TKDBResult mergeIntoTree( TKDBResult tree, TKDBResult branch, String whichNodeId ) throws Throwable  26 { 27 int i = 0; 28 int j = 0; 29 TKDBResult resTree = (TKDBResult) tree.clone(); 30 resTree.removeAllElements(); 31 TKDBResultRow treeRow = (tree==null)? null : (TKDBResultRow)(tree.get( i++ )); 32 TKDBResultRow branchRow = (branch==null)? null : (TKDBResultRow)(branch.get( j++ )); 33 while( true ) { 34 if( treeRow == null && branchRow == null ) { break; } 35 int treeLeft = getDBResultRowColumnVal( treeRow, "LEFT_NR" ); 36 int branchLeft = getDBResultRowColumnVal( branchRow, "LEFT_NR" ); 37 if( treeRow == null ) { 38 resTree.addElement( branchRow ); 39 branchRow = (TKDBResultRow)(branch.get( j++ )); 40 } 41 else if( branchRow == null ) { 42 resTree.addElement( treeRow ); 43 treeRow = (TKDBResultRow)(tree.get( i++ )); 44 } 45 else if( treeLeft == branchLeft ) { 46 int branchNodeId = getDBResultRowColumnVal( branchRow, "NODE_ID" ); 47 int branchConNodeId = getDBResultRowColumnVal( branchRow, whichNodeId ); 48 if( branchNodeId != branchConNodeId ) { 49 resTree.addElement( branchRow ); 50 } 51 else { 52 resTree.addElement( treeRow ); 53 } 54 treeRow = (TKDBResultRow)(tree.get( i++ )); 55 branchRow = (TKDBResultRow)(branch.get( j++ )); 56 } 57 else if( treeLeft < branchLeft ) { 58 resTree.addElement( treeRow ); 59 treeRow = (TKDBResultRow)(tree.get( i++ )); 60 } 61 else if( treeLeft > branchLeft ) { 62 resTree.addElement( branchRow ); 63 branchRow = (TKDBResultRow)(branch.get( j++ )); 64 } 65 } 66 return resTree; 67 } 68 69 74 public static int getMaxDepth( TKDBResult tree, String treeName ) throws Throwable  75 { 76 int depth = 0; 77 int maxDepth = 0; 78 int lastLeft = -1; 79 int lastRight = -1; 80 int thisParent; 81 Stack parents = new Stack (); 82 for(int i = 0; i < tree.size(); i++) { 83 TKDBResultRow resultRow = (TKDBResultRow)(tree.get( i )); 84 int thisLeft = Integer.parseInt( (String ) resultRow.getColumn( "LEFT_NR" ) ); 85 int thisRight = Integer.parseInt( (String ) resultRow.getColumn( "RIGHT_NR" ) ); 86 String tmp = (String ) resultRow.getColumn( treeName+"_NODE_PARENT" ); 87 thisParent = ( tmp == null || tmp.equals("") )?Integer.parseInt( (String ) resultRow.getColumn( treeName+"_NODE_ID" ) ):Integer.parseInt( tmp ); 88 if( thisLeft > lastLeft && thisRight < lastRight ) { depth++; 90 parents.push( new Integer ( thisParent ) ); 91 } 92 else if( !parents.empty() ) { if( !(parents.peek().equals( new Integer (thisParent))) ) { 94 while( !parents.empty() ) { 95 if( parents.pop().equals( new Integer ( thisParent )) ) { 96 parents.push( new Integer ( thisParent ) ); 97 break; 98 } 99 depth--; 100 } 101 } 102 } 103 maxDepth = (depth >= maxDepth) ? depth : maxDepth; 104 lastLeft = thisLeft; 105 lastRight = thisRight; 106 } 107 return maxDepth; 108 } 109 110 116 public static int getDBResultRowColumnVal( TKDBResultRow row, String name ) 117 { 118 if( row != null ) { 119 String sVal = (String ) row.getColumn( name ); 120 if( sVal.equals( "" ) ) return -1; 121 int val = Integer.parseInt( sVal ); 122 return val; 123 } 124 else return -1; 125 } 126 127 134 public static void keepOpenNodes(TKEvent evt, TKHTMLTemplate tmpl ) throws Throwable  135 { 136 TKVector openNodes = new TKVector(); 137 if( evt.getParams().hasMultiple( PARAMETER, "OPEN_NODES" ) ) { 138 openNodes = evt.getParams().getVector( PARAMETER, "OPEN_NODES" ); 139 } 140 else { 141 openNodes.put( 0, evt.getParameter( PARAMETER, "OPEN_NODES" ) ); 143 } 144 if( openNodes.get( 0 ) != null ) { 146 TKStandardIterator iterator1 = new TKStandardIterator( openNodes, tmpl.getListIterator(), "OPEN_NODES", "OPEN_NODES" ); 147 tmpl.setListIterator( iterator1 ); 148 } 149 } 150 151 164 public static TKVector updateOpenNodes( TKVector openNodes, String openNodeId, String closeNodeId, String me, Class isChildQuery ) throws Throwable  165 { 166 if( !closeNodeId.equals( "-1" ) ) 167 { int size = openNodes.size(); TKQuery q; 170 int i = 0; 171 while( i<size ) { 172 String id = (String ) openNodes.get( i ); 173 if( id!=null ) 174 { 175 if (id.equals(me)) 176 { 177 openNodes.removeElementAt( i ); 178 size--; 179 } 180 else 181 { 182 q = TKDBManager.newQuery( isChildQuery ); 183 q.setQueryParams( "PARENT_ID", new Integer (me) ); 184 q.setQueryParams( "CHILD_ID", new Integer (id) ); 185 q.execute(); 186 ResultSet rs = q.fetchResultSet(); 187 if( rs.next() ) { 188 int isChild = rs.getInt("ISCHILD"); 189 if( isChild == 1 ) { 190 openNodes.removeElementAt( i ); size--; } 193 else { 194 i++; 195 } 196 } 197 } 198 } 199 } 200 } 201 else { int size = openNodes.size(); 203 TKQuery q; 204 int i = 0; 205 while( i<size ) { 206 String id = (String ) openNodes.get( i ); 207 if( id!=null ) { 208 q = TKDBManager.newQuery( isChildQuery ); 209 q.setQueryParams( "PARENT_ID", new Integer (id) ); 210 q.setQueryParams( "CHILD_ID", new Integer (openNodeId) ); 211 q.execute(); 212 ResultSet rs = q.fetchResultSet(); 213 if( rs.next() ) { 214 int isChild = rs.getInt("ISCHILD"); 215 if( isChild == 1 ) { 216 openNodes.removeElementAt( i ); size--; 218 } 219 else { 220 i++; 221 } 222 } 223 } 224 } 225 } 226 if( !openNodes.contains( openNodeId ) && openNodeId != null ) openNodes.addElement( openNodeId ); 227 if( openNodes.size() >= 2 && openNodes.contains("-1") ) { 228 openNodes.removeElement("-1"); 229 } 231 return openNodes; 232 } 233 234 }
| Popular Tags
|