1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.ldap.server.db;
18
19
20 import org.apache.ldap.common.message.LockableAttributesImpl;
21
22 import javax.naming.NamingEnumeration;
23 import javax.naming.NamingException;
24 import javax.naming.directory.Attribute;
25 import javax.naming.directory.Attributes;
26
27
28 /***
29 * An enumeration that transforms another underlying enumeration over a set of
30 * IndexRecords into an enumeration over a set of SearchResults. Note that the
31 * SearchResult created may not be complete and other parts of the system may
32 * modify it before return. This enumeration simply creates a new copy of the
33 * entry to return stuffing it with the attributes that were specified. This is
34 * all that it does now but this may change later.
35 *
36 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37 * @version $Rev: 159259 $
38 */
39 public class SearchResultEnumeration implements NamingEnumeration
40 {
41 /*** Database used to lookup entries from */
42 private Database db = null;
43 /*** base of search for relative names */
44 /*** the attributes to return */
45 private final String [] attrIds;
46 /*** underlying enumeration over IndexRecords */
47 private final NamingEnumeration underlying;
48
49
50 /***
51 * Creates an enumeration that returns entries packaged within SearchResults
52 * using the search parameters supplied to a search call.
53 *
54 * @param attrIds the returned attributes
55 * @param underlying the enumeration over IndexRecords
56 */
57 public SearchResultEnumeration( String [] attrIds,
58 NamingEnumeration underlying,
59 Database db )
60 {
61 this.db = db;
62 this.attrIds = attrIds;
63 this.underlying = underlying;
64 }
65
66
67 /***
68 * @see javax.naming.NamingEnumeration#close()
69 */
70 public void close() throws NamingException
71 {
72 underlying.close();
73 }
74
75
76 /***
77 * @see javax.naming.NamingEnumeration#hasMore()
78 */
79 public boolean hasMore() throws NamingException
80 {
81 return underlying.hasMore();
82 }
83
84
85 /***
86 * @see javax.naming.NamingEnumeration#next()
87 */
88 public Object next() throws NamingException
89 {
90 IndexRecord rec = ( IndexRecord ) underlying.next();
91 Attributes entry;
92 String name = db.getEntryUpdn( rec.getEntryId() );
93
94 if ( null == rec.getAttributes() )
95 {
96 rec.setAttributes( db.lookup( rec.getEntryId() ) );
97 }
98
99 if ( attrIds == null )
100 {
101 entry = ( Attributes ) rec.getAttributes().clone();
102 }
103 else
104 {
105 entry = new LockableAttributesImpl();
106
107 for ( int ii = 0; ii < attrIds.length; ii++ )
108 {
109
110 if ( null == rec.getAttributes().get( attrIds[ii] ) )
111 {
112 continue;
113 }
114
115
116 Attribute attr = ( Attribute ) rec.getAttributes().get( attrIds[ii] ).clone();
117 entry.put( attr );
118 }
119 }
120
121 return new DbSearchResult( rec.getEntryId(), name, null, entry );
122 }
123
124
125 /***
126 * @see java.util.Enumeration#hasMoreElements()
127 */
128 public boolean hasMoreElements()
129 {
130 return underlying.hasMoreElements();
131 }
132
133
134 /***
135 * @see java.util.Enumeration#nextElement()
136 */
137 public Object nextElement()
138 {
139 return underlying.nextElement();
140 }
141 }