KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > teamkonzept > webman > mainint > TreeUtils


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 JavaDoc;
11 import java.sql.*;
12
13 /**
14     provides several Util methods for trees
15  * @author $Author: alex $
16  * @version $Revision: 1.5 $
17 */

18 public class TreeUtils implements ParameterTypes
19 {
20     /**
21      * mischen zweier teilbaeume (zwei TKDBResults)
22      *
23      * @param
24      */

25     public static TKDBResult mergeIntoTree( TKDBResult tree, TKDBResult branch, String JavaDoc whichNodeId ) throws Throwable JavaDoc
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     /**
70      * ermitteln der maximalen tiefe eines baumes
71      *
72      * @param
73      */

74     public static int getMaxDepth( TKDBResult tree, String JavaDoc treeName ) throws Throwable JavaDoc
75     {
76         int depth = 0;
77         int maxDepth = 0;
78         int lastLeft = -1;
79         int lastRight = -1;
80         int thisParent;
81         Stack JavaDoc parents = new Stack JavaDoc();
82         for(int i = 0; i < tree.size(); i++) {
83             TKDBResultRow resultRow = (TKDBResultRow)(tree.get( i ));
84             int thisLeft = Integer.parseInt( (String JavaDoc) resultRow.getColumn( "LEFT_NR" ) );
85             int thisRight = Integer.parseInt( (String JavaDoc) resultRow.getColumn( "RIGHT_NR" ) );
86             String JavaDoc tmp = (String JavaDoc) resultRow.getColumn( treeName+"_NODE_PARENT" );
87             thisParent = ( tmp == null || tmp.equals("") )?Integer.parseInt( (String JavaDoc) resultRow.getColumn( treeName+"_NODE_ID" ) ):Integer.parseInt( tmp );
88             if( thisLeft > lastLeft && thisRight < lastRight ) { // absteigen
89
depth++;
90                 parents.push( new Integer JavaDoc( thisParent ) );
91             }
92             else if( !parents.empty() ) { // aufsteigen
93
if( !(parents.peek().equals( new Integer JavaDoc (thisParent))) ) {
94                     while( !parents.empty() ) {
95                         if( parents.pop().equals( new Integer JavaDoc( thisParent )) ) {
96                             parents.push( new Integer JavaDoc( 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     /**
111      *
112      * lesen einer spalte einer TKDBResultRow und als int zurueck
113      *
114      * @param
115      */

116     public static int getDBResultRowColumnVal( TKDBResultRow row, String JavaDoc name )
117     {
118         if( row != null ) {
119             String JavaDoc sVal = (String JavaDoc) 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     /**
128      * keepOpenNodes
129      *
130      * schleift Liste der offenen nodes durch: Lesen des Vectors und einsetzen ins templ.
131      *
132      * @param Template
133      */

134     public static void keepOpenNodes(TKEvent evt, TKHTMLTemplate tmpl ) throws Throwable JavaDoc
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" )==null?"-1":evt.getParameter( PARAMETER, "OPEN_NODES" ) );
142
openNodes.put( 0, evt.getParameter( PARAMETER, "OPEN_NODES" ) );
143         }
144         // die liste der offenen nodes kommt ins template
145
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     /**
152      *
153      * CHECK fuers Schliessen oeder LOESCHEN
154      * liste von offenen nodes holen und fuer jeden offenen node checken, ob er kind
155      * des closeNodeId ist: ist er kind, dann raus aus der liste der offenen nodes
156      * openNodeId kommt dann zu der liste dazu
157      *
158      * CHECK fuers Oeffnen
159      * fuer den zu oeffnenden node nachsehen, ob er Kind eines schon offenen node
160      * ist, wenn ja, dann den schon offenen raus und schliesslich den neuen rein in die Liste
161      *
162      * @param
163      */

164     public static TKVector updateOpenNodes( TKVector openNodes, String JavaDoc openNodeId, String JavaDoc closeNodeId, String JavaDoc me, Class JavaDoc isChildQuery ) throws Throwable JavaDoc
165     {
166         if( !closeNodeId.equals( "-1" ) )
167         { // beim schliessen/loeschen checken, ob offene nodes vom
168
int size = openNodes.size(); // schliessen/loeschen des me betroffen sind
169
TKQuery q;
170             int i = 0;
171             while( i<size ) {
172                 String JavaDoc id = (String JavaDoc) 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 JavaDoc(me) );
184                         q.setQueryParams( "CHILD_ID", new Integer JavaDoc(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 ); // dieser knoten ist nach dem
191
size--; // schliessen nich mehr offen -> raus aus der liste
192
}
193                             else {
194                                 i++;
195                             }
196                         }
197                     }
198                 }
199             }
200         }
201         else { // check beim Oeffnen: ist einer der offenen nodes Parent des openNodeId?
202
int size = openNodes.size();
203             TKQuery q;
204             int i = 0;
205             while( i<size ) {
206                 String JavaDoc id = (String JavaDoc) openNodes.get( i );
207                 if( id!=null ) {
208                     q = TKDBManager.newQuery( isChildQuery );
209                     q.setQueryParams( "PARENT_ID", new Integer JavaDoc(id) );
210                     q.setQueryParams( "CHILD_ID", new Integer JavaDoc(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 ); // dieser knoten ist im gleichen Ast wie der zu oeffnende
217
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             //log.println("------ entferne -1 -----");
230
}
231         return openNodes;
232     }
233     
234 }
Popular Tags