001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.server.core.partition.impl.btree.gui;
021
022
023 import java.util.ArrayList;
024 import java.util.Collections;
025 import java.util.Enumeration;
026 import java.util.Iterator;
027 import java.util.List;
028 import java.util.Map;
029
030 import javax.swing.tree.TreeNode;
031
032 import org.apache.directory.server.core.partition.impl.btree.BTreePartition;
033 import org.apache.directory.server.xdbm.ForwardIndexEntry;
034 import org.apache.directory.server.xdbm.IndexCursor;
035 import org.apache.directory.server.xdbm.IndexEntry;
036 import org.apache.directory.server.xdbm.search.Evaluator;
037 import org.apache.directory.server.xdbm.search.SearchEngine;
038 import org.apache.directory.shared.ldap.entry.ServerEntry;
039 import org.apache.directory.shared.ldap.filter.ExprNode;
040 import org.apache.directory.shared.ldap.name.DN;
041
042
043 /**
044 * A node representing an entry.
045 *
046 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
047 * @version $Rev: 927146 $
048 */
049 public class EntryNode implements TreeNode
050 {
051 private final BTreePartition partition;
052 private final EntryNode parent;
053 private final ServerEntry entry;
054 private final ArrayList<TreeNode> children;
055 private final Long id;
056
057
058 public EntryNode( Long id, EntryNode parent, BTreePartition partition, ServerEntry entry, Map<Long, EntryNode> map )
059 {
060 this( id, parent, partition, entry, map, null, null );
061 }
062
063
064 public EntryNode( Long id, EntryNode parent, BTreePartition db, ServerEntry entry, Map<Long, EntryNode> map,
065 ExprNode exprNode, SearchEngine engine )
066 {
067 this.partition = db;
068 this.id = id;
069 this.entry = entry;
070 children = new ArrayList<TreeNode>();
071
072 if ( parent == null )
073 {
074 this.parent = this;
075 }
076 else
077 {
078 this.parent = parent;
079 }
080
081 try
082 {
083 List<ForwardIndexEntry> recordForwards = new ArrayList<ForwardIndexEntry>();
084 IndexCursor<Long, ServerEntry, Long> childList = db.list( id );
085
086 while ( childList.next() )
087 {
088 IndexEntry old = childList.get();
089 ForwardIndexEntry newRec = new ForwardIndexEntry();
090 newRec.copy( old );
091 recordForwards.add( newRec );
092 }
093
094 childList.close();
095
096 Iterator list = recordForwards.iterator();
097
098 while ( list.hasNext() )
099 {
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.evaluateId( rec.getId() ) )
108 {
109 ServerEntry newEntry = db.lookup( rec.getId() );
110 EntryNode child = new EntryNode( ( Long ) rec.getId(), this, db, newEntry, map, exprNode,
111 engine );
112 children.add( child );
113 }
114 else
115 {
116 continue;
117 }
118 }
119 else
120 {
121 ServerEntry newEntry = db.lookup( rec.getId() );
122 EntryNode child = new EntryNode( ( Long ) rec.getId(), this, db, newEntry, map, exprNode,
123 engine );
124 children.add( child );
125 }
126 }
127 else
128 {
129 ServerEntry newEntry = db.lookup( ( Long ) rec.getId() );
130 EntryNode child = new EntryNode( ( Long ) rec.getId(), this, db, newEntry, map );
131 children.add( child );
132 }
133 }
134 }
135 catch ( Exception e )
136 {
137 e.printStackTrace();
138 }
139
140 map.put( id, this );
141 }
142
143
144 public Enumeration<TreeNode> children()
145 {
146 return Collections.enumeration( children );
147 }
148
149
150 public boolean getAllowsChildren()
151 {
152 return true;
153 }
154
155
156 public TreeNode getChildAt( int childIndex )
157 {
158 return ( TreeNode ) children.get( childIndex );
159 }
160
161
162 public int getChildCount()
163 {
164 return children.size();
165 }
166
167
168 public int getIndex( TreeNode child )
169 {
170 return children.indexOf( child );
171 }
172
173
174 public TreeNode getParent()
175 {
176 return parent;
177 }
178
179
180 public boolean isLeaf()
181 {
182 return children.size() <= 0;
183 }
184
185
186 public String getEntryDn() throws Exception
187 {
188 return partition.getEntryDn( id );
189 }
190
191
192 public String toString()
193 {
194 StringBuffer buf = new StringBuffer();
195
196 try
197 {
198 DN dn = new DN( partition.getEntryDn( id ) );
199 buf.append( "(" ).append( id ).append( ") " );
200 buf.append( dn.getRdn() );
201 }
202 catch ( Exception e )
203 {
204 buf.append( "ERROR: " + e.getLocalizedMessage() );
205 }
206
207 if ( children.size() > 0 )
208 {
209 buf.append( " [" ).append( children.size() ).append( "]" );
210 }
211
212 return buf.toString();
213 }
214
215
216 public ServerEntry getLdapEntry()
217 {
218 return entry;
219 }
220
221
222 public Long getEntryId()
223 {
224 return id;
225 }
226 }