View Javadoc

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