From dff791d6bdd93ee5127094248f5e3cbc0d529653 Mon Sep 17 00:00:00 2001
From: neil_a_wilson <neil_a_wilson@localhost>
Date: Thu, 16 Nov 2006 01:32:41 +0000
Subject: [PATCH] Update the ldapsearch tool to provide a --countEntries option that can be used to count the number of matching entries.  The count will be displayed as a comment at the end of the results, and will also be used as the exit code for the tool.  This addresses issue #1013.

---
 opendj-sdk/opends/src/server/org/opends/server/tools/LDAPSearch.java |   46 ++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 38 insertions(+), 8 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPSearch.java b/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPSearch.java
index 538b086..525fbe9 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPSearch.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPSearch.java
@@ -130,18 +130,24 @@
    * @param searchOptions  The constraints for the search.
    * @param wrapColumn     The column at which to wrap long lines.
    *
+   * @return  The number of matching entries returned by the server.  If there
+   *          were multiple search filters provided, then this will be the
+   *          total number of matching entries for all searches.
+   *
    * @throws  IOException  If a problem occurs while attempting to communicate
    *                       with the Directory Server.
    *
    * @throws  LDAPException  If the Directory Server returns an error response.
    */
-  public void executeSearch(LDAPConnection connection, String baseDN,
-                            ArrayList<LDAPFilter> filters,
-                            LinkedHashSet<String> attributes,
-                            LDAPSearchOptions searchOptions,
-                            int wrapColumn )
+  public int executeSearch(LDAPConnection connection, String baseDN,
+                           ArrayList<LDAPFilter> filters,
+                           LinkedHashSet<String> attributes,
+                           LDAPSearchOptions searchOptions,
+                           int wrapColumn )
          throws IOException, LDAPException
   {
+    int matchingEntries = 0;
+
     for (LDAPFilter filter: filters)
     {
       ASN1OctetString asn1OctetStr = new ASN1OctetString(baseDN);
@@ -267,6 +273,7 @@
               StringBuilder sb = new StringBuilder();
               toLDIF(searchEntryOp, sb, wrapColumn, typesOnly);
               out.println(sb.toString());
+              matchingEntries++;
               break;
 
             case OP_TYPE_SEARCH_RESULT_REFERENCE:
@@ -312,6 +319,15 @@
         throw new IOException(ae.getMessage());
       }
     }
+
+    if (searchOptions.countMatchingEntries())
+    {
+      int    msgID   = MSGID_LDAPSEARCH_MATCHING_ENTRY_COUNT;
+      String message = getMessage(msgID, matchingEntries);
+      out.println(message);
+      out.println();
+    }
+    return matchingEntries;
   }
 
   /**
@@ -522,6 +538,7 @@
     LinkedHashSet<String> attributes = new LinkedHashSet<String>();
 
     BooleanArgument   continueOnError          = null;
+    BooleanArgument   countEntries             = null;
     BooleanArgument   dontWrap                 = null;
     BooleanArgument   noop                     = null;
     BooleanArgument   reportAuthzID            = null;
@@ -741,6 +758,10 @@
                                      MSGID_DESCRIPTION_DONT_WRAP);
       argParser.addArgument(dontWrap);
 
+      countEntries = new BooleanArgument("countentries", null, "countEntries",
+                                         MSGID_DESCRIPTION_COUNT_ENTRIES);
+      argParser.addArgument(countEntries);
+
       continueOnError =
            new BooleanArgument("continueOnError", 'c', "continueOnError",
                                MSGID_DESCRIPTION_CONTINUE_ON_ERROR);
@@ -935,6 +956,7 @@
     searchOptions.setVerbose(verbose.isPresent());
     searchOptions.setContinueOnError(continueOnError.isPresent());
     searchOptions.setEncoding(encodingStr.getValue());
+    searchOptions.setCountMatchingEntries(countEntries.isPresent());
     try
     {
       searchOptions.setTimeLimit(timeLimit.getIntValue());
@@ -1276,8 +1298,17 @@
       connection.connectToHost(bindDNValue, bindPasswordValue, nextMessageID);
 
       LDAPSearch ldapSearch = new LDAPSearch(nextMessageID, out, err);
-      ldapSearch.executeSearch(connection, baseDNValue, filters, attributes,
-                               searchOptions, wrapColumn);
+      int matchingEntries = ldapSearch.executeSearch(connection, baseDNValue,
+                                                     filters, attributes,
+                                                     searchOptions, wrapColumn);
+      if (countEntries.isPresent())
+      {
+        return matchingEntries;
+      }
+      else
+      {
+        return 0;
+      }
 
     } catch(LDAPException le)
     {
@@ -1303,7 +1334,6 @@
         connection.close();
       }
     }
-    return 0;
   }
 
 }

--
Gitblit v1.10.0