From d14f9e08b56dda53fc2073b9caf031cbbd52fd4e Mon Sep 17 00:00:00 2001
From: Nicolas Capponi <nicolas.capponi@forgerock.com>
Date: Wed, 22 Oct 2014 15:06:34 +0000
Subject: [PATCH] OPENDJ-1590 Add default matching rule for each collation matching rules

---
 opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/CoreSchemaImpl.java                    |   26 +++++++++++++++++++-------
 opendj-core/src/test/java/org/forgerock/opendj/ldap/schema/CollationEqualityMatchingRuleTest.java |    4 ++++
 2 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/CoreSchemaImpl.java b/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/CoreSchemaImpl.java
index 50f3f57..fd9a040 100644
--- a/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/CoreSchemaImpl.java
+++ b/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/CoreSchemaImpl.java
@@ -918,7 +918,11 @@
             final Locale locale = localesCache.get(names.get(0));
             addCollationMatchingRule(builder, oid, names, 1, "lt", collationLessThanMatchingRule(locale));
             addCollationMatchingRule(builder, oid, names, 2, "lte", collationLessThanOrEqualMatchingRule(locale));
-            addCollationMatchingRule(builder, oid, names, 3, "eq", collationEqualityMatchingRule(locale));
+            MatchingRuleImpl collationEqualityMatchingRule = collationEqualityMatchingRule(locale);
+            addCollationMatchingRule(builder, oid, names, 3, "eq", collationEqualityMatchingRule);
+            // the default oid is registered with equality matching rule
+            final int ignored = 0;
+            addCollationMatchingRule(builder, oid, names, ignored, "", collationEqualityMatchingRule);
             addCollationMatchingRule(builder, oid, names, 4, "gte", collationGreaterThanOrEqualToMatchingRule(locale));
             addCollationMatchingRule(builder, oid, names, 5, "gt", collationGreaterThanMatchingRule(locale));
             addCollationMatchingRule(builder, oid, names, 6, "sub", collationSubstringMatchingRule(locale));
@@ -929,7 +933,8 @@
     private static void addCollationMatchingRule(final SchemaBuilder builder, final String baseOid,
             final List<String> names, final int numericSuffix, final String symbolicSuffix,
             final MatchingRuleImpl matchingRuleImplementation) {
-        builder.buildMatchingRule(baseOid + "." + numericSuffix)
+        final String oid = symbolicSuffix.isEmpty() ? baseOid : baseOid + "." + numericSuffix;
+        builder.buildMatchingRule(oid)
             .names(collationMatchingRuleNames(names, numericSuffix, symbolicSuffix))
             .syntaxOID(SYNTAX_DIRECTORY_STRING_OID)
             .extraProperties(OPENDS_ORIGIN)
@@ -943,17 +948,24 @@
      * @param localeNames
      *            List of locale names that correspond to the matching rule (e.g., "en", "en-US")
      * @param numSuffix
-     *            numeric suffix corresponding to type of matching rule (e.g, 5 for greater than matching rule)
+     *            numeric suffix corresponding to type of matching rule (e.g, 5 for greater than matching rule). It is
+     *            ignored if equal to zero (0).
      * @param symbolicSuffix
-     *            symbolic suffix corresponding to type of matching rule (e.g, "gt" for greater than matching rule)
+     *            symbolic suffix corresponding to type of matching rule (e.g, "gt" for greater than matching rule). It
+     *            may be empty ("") to indicate the default rule.
      * @return the names list (e.g, "en.5", "en.gt", "en-US.5", "en-US.gt")
      */
     private static String[] collationMatchingRuleNames(final List<String> localeNames, final int numSuffix,
-            final String symbolicSuffix) {
+        final String symbolicSuffix) {
         final List<String> names = new ArrayList<String>();
         for (String localeName : localeNames) {
-            names.add(localeName + "." + numSuffix);
-            names.add(localeName + "." + symbolicSuffix);
+            if (symbolicSuffix.isEmpty()) {
+                // the default rule
+                names.add(localeName);
+            } else {
+                names.add(localeName + "." + numSuffix);
+                names.add(localeName + "." + symbolicSuffix);
+            }
         }
         return names.toArray(new String[names.size()]);
     }
diff --git a/opendj-core/src/test/java/org/forgerock/opendj/ldap/schema/CollationEqualityMatchingRuleTest.java b/opendj-core/src/test/java/org/forgerock/opendj/ldap/schema/CollationEqualityMatchingRuleTest.java
index 4587d38..4954347 100644
--- a/opendj-core/src/test/java/org/forgerock/opendj/ldap/schema/CollationEqualityMatchingRuleTest.java
+++ b/opendj-core/src/test/java/org/forgerock/opendj/ldap/schema/CollationEqualityMatchingRuleTest.java
@@ -100,6 +100,10 @@
         // 'ar' does not share oid with another locale
         MatchingRule rule3 = Schema.getCoreSchema().getMatchingRule("ar.3");
         assertThat(rule3.getNames()).containsOnly("ar.eq", "ar.3");
+
+        // equality matching rule is also the default matching rule
+        MatchingRule rule4 = Schema.getCoreSchema().getMatchingRule("ar");
+        assertThat(rule4.getNames()).containsOnly("ar");
     }
 
     @Test

--
Gitblit v1.10.0