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.db.gui;
18  
19  
20  import org.apache.ldap.common.filter.ExprNode;
21  import org.apache.ldap.common.name.LdapName;
22  import org.apache.ldap.server.db.Database;
23  import org.apache.ldap.server.db.IndexRecord;
24  import org.apache.ldap.server.db.SearchEngine;
25  
26  import javax.naming.NamingEnumeration;
27  import javax.naming.NamingException;
28  import javax.naming.directory.Attributes;
29  import javax.swing.tree.TreeNode;
30  import java.math.BigInteger;
31  import java.util.*;
32  
33  
34  /***
35   * A node representing an entry.
36   * 
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   * @version $Rev: 159259 $
39   */
40  public class EntryNode
41  	implements TreeNode
42  {
43      private final Database db;
44      private final EntryNode parent;
45  	private final Attributes entry;
46      private final ArrayList children;
47      private final BigInteger id;
48  
49  
50      public EntryNode( BigInteger id, EntryNode parent, Database db,
51          Attributes entry, HashMap map )
52      {
53          this( id, parent, db, entry, map, null, null );
54      }
55  
56  
57      public EntryNode( BigInteger id, EntryNode parent, Database db,
58          Attributes entry, HashMap map, ExprNode exprNode,
59          SearchEngine engine )
60      {
61          this.db = db;
62          this.id = id;
63  		this.entry = entry;
64          this.children = new ArrayList();
65  
66          if ( parent == null )
67          {
68              this.parent = this;
69          } 
70          else 
71          {
72              this.parent = parent;
73          }
74  
75  		try {
76              ArrayList records = new ArrayList();
77              NamingEnumeration childList = db.list( id );
78              while( childList.hasMore() )
79              {
80                  IndexRecord old = ( IndexRecord ) childList.next();
81                  IndexRecord newRec = new IndexRecord();
82                  newRec.copy( old );
83                  records.add( newRec );
84              }
85              childList.close();
86  
87              Iterator list = records.iterator();
88              
89              while( list.hasNext() )
90              {
91                  IndexRecord rec = ( IndexRecord ) list.next();
92  
93                  if ( engine != null && exprNode != null )
94                  {
95                      if ( db.getChildCount( rec.getEntryId() ) == 0 )
96                      {
97                          if( engine.evaluate( exprNode, rec.getEntryId() ) )
98                          {
99                              Attributes newEntry = db.lookup( rec.getEntryId() );
100                             EntryNode child = new EntryNode( rec.getEntryId(),
101                                     this, db, newEntry, map, exprNode, engine);
102                             children.add(child);
103                         }
104                         else
105                         {
106                             continue;
107                         }
108                     }
109                     else
110                     {
111                         Attributes newEntry = db.lookup( rec.getEntryId() );
112                         EntryNode child = new EntryNode( rec.getEntryId(),
113                             this, db, newEntry, map, exprNode, engine );
114                         children.add( child );
115                     }
116                 } 
117                 else
118                 {
119                     Attributes newEntry = db.lookup( rec.getEntryId() );
120                     EntryNode child = new EntryNode( rec.getEntryId(), this,
121                         db, newEntry, map );
122                     children.add( child );
123                 }
124             }
125         } 
126         catch ( Exception e ) 
127         {
128             e.printStackTrace();
129         }
130 
131         map.put( id, this );
132     }
133 
134 
135     public Enumeration children()
136     {
137         return Collections.enumeration( children );
138     }
139 
140 
141     public boolean getAllowsChildren()
142     {
143         return true;
144     }
145 
146 
147     public TreeNode getChildAt( int childIndex )
148     {
149 		return ( TreeNode ) children.get( childIndex );
150     }
151 
152 
153     public int getChildCount()
154     {
155         return children.size();
156     }
157 
158 
159     public int getIndex( TreeNode child )
160     {
161         return children.indexOf( child );
162     }
163 
164 
165     public TreeNode getParent()
166     {
167         return parent;
168     }
169 
170 
171     public boolean isLeaf()
172     {
173         return children.size() <= 0;
174     }
175 
176 
177     public String getEntryDn()
178         throws NamingException
179     {
180         return db.getEntryDn( id );
181     }
182     
183 
184     public String toString()
185     {
186         StringBuffer buf = new StringBuffer();
187         
188         try
189         {
190             LdapName dn = new LdapName( db.getEntryDn( id ) );
191             buf.append( "(" ).append( id ).append( ") " );
192             buf.append( dn.getRdn() );
193         }
194         catch( NamingException e )
195         {
196             e.printStackTrace();
197             buf.append( "ERROR: " + e.getMessage() );
198         }
199         
200         if ( children.size() > 0 )
201         {
202             buf.append( " [" ).append( children.size() ).append( "]" );
203         }
204         
205         return buf.toString();
206     }
207 
208 
209     public Attributes getLdapEntry()
210     {
211         return entry;
212     }
213     
214     
215     public BigInteger getEntryId()
216     {
217         return id;
218     }
219 }