View Javadoc

1   /*
2    *   Copyright 2004 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.ldap.server.partition.impl.btree.gui;
18  
19  
20  import java.math.BigInteger;
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.Enumeration;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  
27  import javax.naming.NamingEnumeration;
28  import javax.naming.NamingException;
29  import javax.naming.directory.Attributes;
30  import javax.swing.tree.TreeNode;
31  
32  import org.apache.ldap.common.filter.ExprNode;
33  import org.apache.ldap.common.name.LdapName;
34  import org.apache.ldap.server.partition.impl.btree.BTreeContextPartition;
35  import org.apache.ldap.server.partition.impl.btree.IndexRecord;
36  import org.apache.ldap.server.partition.impl.btree.SearchEngine;
37  
38  
39  /***
40   * A node representing an entry.
41   * 
42   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
43   * @version $Rev: 264732 $
44   */
45  public class EntryNode
46  	implements TreeNode
47  {
48      private final BTreeContextPartition partition;
49      private final EntryNode parent;
50  	private final Attributes entry;
51      private final ArrayList children;
52      private final BigInteger id;
53  
54  
55      public EntryNode( BigInteger id, EntryNode parent, BTreeContextPartition partition,
56          Attributes entry, HashMap map )
57      {
58          this( id, parent, partition, entry, map, null, null );
59      }
60  
61  
62      public EntryNode( BigInteger id, EntryNode parent, BTreeContextPartition db,
63          Attributes entry, HashMap map, ExprNode exprNode,
64          SearchEngine engine )
65      {
66          this.partition = db;
67          this.id = id;
68  		this.entry = entry;
69          this.children = new ArrayList();
70  
71          if ( parent == null )
72          {
73              this.parent = this;
74          } 
75          else 
76          {
77              this.parent = parent;
78          }
79  
80  		try {
81              ArrayList records = new ArrayList();
82              NamingEnumeration childList = db.list( id );
83              while( childList.hasMore() )
84              {
85                  IndexRecord old = ( IndexRecord ) childList.next();
86                  IndexRecord newRec = new IndexRecord();
87                  newRec.copy( old );
88                  records.add( newRec );
89              }
90              childList.close();
91  
92              Iterator list = records.iterator();
93              
94              while( list.hasNext() )
95              {
96                  IndexRecord rec = ( IndexRecord ) list.next();
97  
98                  if ( engine != null && exprNode != null )
99                  {
100                     if ( db.getChildCount( rec.getEntryId() ) == 0 )
101                     {
102                         if( engine.evaluate( exprNode, rec.getEntryId() ) )
103                         {
104                             Attributes newEntry = db.lookup( rec.getEntryId() );
105                             EntryNode child = new EntryNode( rec.getEntryId(),
106                                     this, db, newEntry, map, exprNode, engine);
107                             children.add(child);
108                         }
109                         else
110                         {
111                             continue;
112                         }
113                     }
114                     else
115                     {
116                         Attributes newEntry = db.lookup( rec.getEntryId() );
117                         EntryNode child = new EntryNode( rec.getEntryId(),
118                             this, db, newEntry, map, exprNode, engine );
119                         children.add( child );
120                     }
121                 } 
122                 else
123                 {
124                     Attributes newEntry = db.lookup( rec.getEntryId() );
125                     EntryNode child = new EntryNode( rec.getEntryId(), this,
126                         db, newEntry, map );
127                     children.add( child );
128                 }
129             }
130         } 
131         catch ( Exception e ) 
132         {
133             e.printStackTrace();
134         }
135 
136         map.put( id, this );
137     }
138 
139 
140     public Enumeration children()
141     {
142         return Collections.enumeration( children );
143     }
144 
145 
146     public boolean getAllowsChildren()
147     {
148         return true;
149     }
150 
151 
152     public TreeNode getChildAt( int childIndex )
153     {
154 		return ( TreeNode ) children.get( childIndex );
155     }
156 
157 
158     public int getChildCount()
159     {
160         return children.size();
161     }
162 
163 
164     public int getIndex( TreeNode child )
165     {
166         return children.indexOf( child );
167     }
168 
169 
170     public TreeNode getParent()
171     {
172         return parent;
173     }
174 
175 
176     public boolean isLeaf()
177     {
178         return children.size() <= 0;
179     }
180 
181 
182     public String getEntryDn()
183         throws NamingException
184     {
185         return partition.getEntryDn( id );
186     }
187     
188 
189     public String toString()
190     {
191         StringBuffer buf = new StringBuffer();
192         
193         try
194         {
195             LdapName dn = new LdapName( partition.getEntryDn( id ) );
196             buf.append( "(" ).append( id ).append( ") " );
197             buf.append( dn.getRdn() );
198         }
199         catch( NamingException e )
200         {
201             e.printStackTrace();
202             buf.append( "ERROR: " + e.getMessage() );
203         }
204         
205         if ( children.size() > 0 )
206         {
207             buf.append( " [" ).append( children.size() ).append( "]" );
208         }
209         
210         return buf.toString();
211     }
212 
213 
214     public Attributes getLdapEntry()
215     {
216         return entry;
217     }
218     
219     
220     public BigInteger getEntryId()
221     {
222         return id;
223     }
224 }