[Date Prev][Date Next]
[Chronological]
[Thread]
[Top]
Re: sorting results in JLDAP
Hi,
I agree with you partially regarding the solution for client side
sorting,
but you can sort the results WITHOUT SUBCLASSING LDAPEntry class.
see the contained source code for details.
we use an attribute "company" to sort the Person Objects, using a
"comparator".
See java.util.Comparator and collections for further details on
comparator.
The steps include using a synchronous search for search entries
,copying all entries to a list
and then using a comparator to sort them.
Note: 'company' is not a default attribute and has been specifically
added for this example.
regards,
saurabh
Sample Java Code
<javacode>
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import com.novell.ldap.LDAPAttribute;
import com.novell.ldap.LDAPAttributeSet;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPEntry;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPReferralException;
import com.novell.ldap.LDAPSearchConstraints;
import com.novell.ldap.LDAPSearchResults;
public class SortClientSample {
public static void main(String[] args) {
if (args.length != 4) {
System.err.println(
"Usage: java SortClient <host name> "
+ "<login dn> <password> <container>");
System.err.println(
"Example: java SortClient Acme.com \"cn=admin,"
+ "o=Acme\" secret \"ou=Sales,o=Acme\"");
System.exit(1);
}
// Read command line arguments
String ldapHost = args[0];
String loginDN = args[1];
String password = args[2];
String searchBase = args[3];
String searchFilter = "(objectclass=inetorgperson)";
SortClientSample client = new SortClientSample();
client.execute(
ldapHost,
loginDN,
password,
searchBase,
searchFilter);
}
private static void printdata(List data) {
Iterator it = data.iterator();
// Loop on results until finished
while (it.hasNext()) {
// Get next directory entry - this next object can
// be an LDAPException if something went wrong
LDAPEntry nextEntry;
nextEntry = (LDAPEntry) it.next();
// Print out the entry DN
System.out.println();
System.out.println("Entry dn: " + nextEntry.getDN());
// Get the attributes of the entry
LDAPAttributeSet findAttrs = nextEntry.getAttributeSet();
Iterator enumAttrs = findAttrs.iterator();
System.out.println("Attribute(s):");
// Loop on attributes
while (enumAttrs.hasNext()) {
LDAPAttribute anAttr = (LDAPAttribute)
enumAttrs.next();
// get attribute name
String attrName = anAttr.getName();
if (attrName.equals("company")) {
// get attribute value
Enumeration enumVals = anAttr.getStringValues();
while (enumVals.hasMoreElements()) {
String aVal = (String) enumVals.nextElement();
// print out attribute name and value
System.out.println(
" " + attrName + ": " + aVal);
}
}
}
}
}
public void execute(
String ldapHost,
String loginDN,
String password,
String searchBase,
String searchFilter) {
int ldapPort = LDAPConnection.DEFAULT_PORT;
int ldapVersion = LDAPConnection.LDAP_V3;
LDAPConnection lc = new LDAPConnection();
try {
// connect to the server
lc.connect(ldapHost, ldapPort);
// bind to the server
lc.bind(ldapVersion, loginDN, password.getBytes("UTF8"));
// Perform the search - SYNCHRONOUS SEARCH USED HERE
System.out.println("\nCalling search request...");
LDAPSearchResults res =
lc
.search(
searchBase,
LDAPConnection.SCOPE_SUB,
searchFilter,
null,
// only return sn
false, // and it's value
(LDAPSearchConstraints) null);
List data = new ArrayList();
while (res.hasMore()) {
LDAPEntry nextEntry;
try {
data.add(res.next());
} catch (LDAPException e) {
if (e instanceof LDAPReferralException)
continue;
else
break;
}
}
System.out.println("\nSearch Result sorted on DN ");
Collections.sort(data);
printdata(data);
System.out.println();
System.out.println("\nSearch Result sorted on company");
Collections.sort(data, new CompanyAttributeComparator());
printdata(data);
// All done - disconnect
if (lc.isConnected())
lc.disconnect();
} catch (LDAPException e) {
System.out.println(e.toString());
} catch (UnsupportedEncodingException e) {
System.out.println("Error: " + e.toString());
}
}
public class CompanyAttributeComparator implements Comparator {
/* (non-Javadoc)
* @see java.util.Comparator#compare(java.lang.Object,
java.lang.Object)
*/
public int compare(Object o1, Object o2) {
if ((o1 instanceof LDAPEntry) && (o2 instanceof LDAPEntry))
{
LDAPEntry entry1 = (LDAPEntry) o1;
LDAPEntry entry2 = (LDAPEntry) o2;
LDAPAttribute company1attr =
entry1.getAttributeSet().getAttribute("company");
LDAPAttribute company2attr =
entry2.getAttributeSet().getAttribute("company");
//Assuming both the entries have a company attribute.
return company1attr.getStringValue().compareTo(
company2attr.getStringValue());
}
//basically i cannot compare other type of objects.
return 0;
}
}
}
</javacode>
>>> Jon Roberts <man@mentata.com> 17-Feb-04 12:56:08 AM >>>
I have just begun working with the JLDAP library, and thus far am able
to connect and search without problem. As someone familiar with the
Netscape LDAP library, I like many of the changes I'm finding in the
newer IETF and JLDAP APIs and would like to continue, but...
I noticed the deprecation of the sort() method in the LDAPSearchResults
class this morning. Are there any examples of sorting search results
using the Comparable interface?
My understanding is that if I get some search results and would like to
sort them client side, I would first need to convert them to a
collection of LDAPEntry instances. Then calling the sort() method on
the
collection would order the entries by dn. Great, but that's actually
not
really something I would likely want. If I instead want to sort by a
given attribute value, it appears I would have to subclass LDAPEntry
and
override the compareTo() method with my own class/routine that extracts
and compares the attribute values. Doesn't that pretty much preclude
defining a sort order at run time?
I would appreciate any guidance on this, whether it be to point me to
an
example, give me a clue about what I'm missing, give me a suggestion on
how to circumvent the issue, or fill me in on future plans on this
requirement. I accept that there may be good reasons for the change,
but
the elimination of effective sorting would be a serious deficiency
weighing against my effort to migrate to the otherwise advantageous
Novell library.
Jon Roberts
www.mentata.com