From b5adf03b38b8a911bc557ff7a1e8fb135335a6d5 Mon Sep 17 00:00:00 2001
From: Jean-Noël Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Mon, 11 Jan 2016 10:18:33 +0000
Subject: [PATCH] OPENDJ-1632 Migrate AttributeType in one shot

---
 opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/SchemaBuilder.java         |    6 +-
 opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldap/schema/SchemaBuilderTestCase.java |   12 +++---
 opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/AttributeType.java         |   24 +++++++++--
 opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/Schema.java                |   50 ++++++++++++++++++++++++
 4 files changed, 77 insertions(+), 15 deletions(-)

diff --git a/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/AttributeType.java b/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/AttributeType.java
index 78f1d83..d1d4897 100644
--- a/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/AttributeType.java
+++ b/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/AttributeType.java
@@ -450,12 +450,14 @@
      * attribute will be the normalized attribute type name followed by the
      * suffix "-oid".
      *
-     * @param schema
-     *            The parent schema.
      * @param name
      *            The name of the place-holder attribute type.
+     * @param syntax
+     *            The syntax of the place-holder attribute type.
+     * @param equalityMatchingRule
+     *            The equality matching rule of the place-holder attribute type.
      */
-    AttributeType(final Schema schema, final String name) {
+    AttributeType(final String name, final Syntax syntax, final MatchingRule equalityMatchingRule) {
         final StringBuilder builder = new StringBuilder(name.length() + 4);
         StaticUtils.toLowerCase(name, builder);
         builder.append("-oid");
@@ -465,12 +467,12 @@
         this.isObsolete = false;
         this.superiorTypeOID = null;
         this.superiorType = null;
-        this.equalityMatchingRule = schema.getDefaultMatchingRule();
+        this.equalityMatchingRule = equalityMatchingRule;
         this.equalityMatchingRuleOID = equalityMatchingRule.getOID();
         this.orderingMatchingRuleOID = null;
         this.substringMatchingRuleOID = null;
         this.approximateMatchingRuleOID = null;
-        this.syntax = schema.getDefaultSyntax();
+        this.syntax = syntax;
         this.syntaxOID = syntax.getOID();
         this.isSingleValue = false;
         this.isCollective = false;
@@ -580,6 +582,18 @@
     }
 
     /**
+     * Returns the normalized name or OID for this schema definition.
+     * <p>
+     * If it has one or more names, then the lower case primary name will be returned.
+     * If it does not have any names, then the lower case OID will be returned.
+     *
+     * @return The normalized name or OID for this schema definition.
+     */
+    public String getNormalizedNameOrOID() {
+        return normalizedName;
+    }
+
+    /**
      * Returns an unmodifiable list containing the user-defined names that may
      * be used to reference this schema definition.
      *
diff --git a/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/Schema.java b/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/Schema.java
index 39504b1..7861515 100644
--- a/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/Schema.java
+++ b/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/Schema.java
@@ -88,6 +88,8 @@
 
         AttributeType getAttributeType(Schema schema, String name);
 
+        AttributeType getAttributeType(String name, Syntax syntax);
+
         Collection<AttributeType> getAttributeTypes();
 
         List<AttributeType> getAttributeTypesWithName(String name);
@@ -202,8 +204,17 @@
 
         @Override
         public AttributeType getAttributeType(final Schema schema, final String name) {
+            return getAttributeType0(name, schema.getDefaultSyntax(), schema.getDefaultMatchingRule());
+        }
+
+        @Override
+        public AttributeType getAttributeType(final String name, final Syntax syntax) {
+            return getAttributeType0(name, syntax, syntax.getEqualityMatchingRule());
+        }
+
+        private AttributeType getAttributeType0(String name, Syntax syntax, MatchingRule equalityMatchingRule) {
             final AttributeType type = strictImpl.getAttributeType0(name);
-            return type != null ? type : new AttributeType(schema, name);
+            return type != null ? type : new AttributeType(name, syntax, equalityMatchingRule);
         }
 
         @Override
@@ -511,6 +522,11 @@
         }
 
         @Override
+        public AttributeType getAttributeType(String name, Syntax syntax) {
+            return getAttributeType(null, name);
+        }
+
+        @Override
         public AttributeType getAttributeType(final Schema schema, final String name) {
             final AttributeType type = getAttributeType0(name);
             if (type != null) {
@@ -1139,6 +1155,30 @@
     }
 
     /**
+     * Returns the attribute type with the specified name or numeric OID.
+     * <p>
+     * If the requested attribute type is not registered in this schema and this
+     * schema is non-strict then a temporary "place-holder" attribute type will
+     * be created and returned. Place holder attribute types have an OID which
+     * is the normalized attribute name with the string {@code -oid} appended.
+     * In addition, they will use the provided syntax and the default matching
+     * rule associated with the syntax.
+     *
+     * @param name
+     *            The name or OID of the attribute type to retrieve.
+     * @param syntax
+     *            The syntax to use when creating the temporary "place-holder" attribute type.
+     * @return The requested attribute type.
+     * @throws UnknownSchemaElementException
+     *             If this is a strict schema and the requested attribute type
+     *             was not found or if the provided name is ambiguous.
+     * @see AttributeType#isPlaceHolder()
+     */
+    public AttributeType getAttributeType(final String name, final Syntax syntax) {
+        return impl.getAttributeType(name, syntax);
+    }
+
+    /**
      * Returns an unmodifiable collection containing all of the attribute types
      * contained in this schema.
      *
@@ -2170,4 +2210,12 @@
         }
         return parentStructuralObjectClass;
     }
+
+    @Override
+    public String toString() {
+        return "Schema " + getSchemaName()
+                + " mr=" + getMatchingRules().size()
+                + " syntaxes=" + getSyntaxes().size()
+                + " at=" + getAttributeTypes().size();
+    }
 }
diff --git a/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/SchemaBuilder.java b/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/SchemaBuilder.java
index 1dba03a..f8d5f88 100644
--- a/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/SchemaBuilder.java
+++ b/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/SchemaBuilder.java
@@ -1854,7 +1854,7 @@
                     syntaxBuilder.addToSchema(overwrite);
 
                     buildMatchingRule(enumImpl.getOrderingMatchingRule())
-                        .names(OMR_GENERIC_ENUM_NAME + oid)
+                        .names(OMR_GENERIC_ENUM_NAME + "." + oid)
                         .syntaxOID(oid)
                         .extraProperties(CoreSchemaImpl.OPENDS_ORIGIN)
                         .implementation(new EnumOrderingMatchingRule(enumImpl))
@@ -2107,9 +2107,9 @@
 
         final String localSchemaName;
         if (schemaName != null) {
-            localSchemaName = schemaName;
+            localSchemaName = schemaName + "-" + NEXT_SCHEMA_ID.getAndIncrement();
         } else {
-            localSchemaName = String.format("Schema#%d", NEXT_SCHEMA_ID.getAndIncrement());
+            localSchemaName = "Schema#" + NEXT_SCHEMA_ID.getAndIncrement();
         }
 
         Syntax defaultSyntax = numericOID2Syntaxes.get(options.get(DEFAULT_SYNTAX_OID));
diff --git a/opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldap/schema/SchemaBuilderTestCase.java b/opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldap/schema/SchemaBuilderTestCase.java
index 2402b86..3f7c8b7 100644
--- a/opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldap/schema/SchemaBuilderTestCase.java
+++ b/opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldap/schema/SchemaBuilderTestCase.java
@@ -21,7 +21,7 @@
  * CDDL HEADER END
  *
  *
- *      Portions Copyright 2012-2015 ForgeRock AS.
+ *      Portions Copyright 2012-2016 ForgeRock AS.
  */
 package org.forgerock.opendj.ldap.schema;
 
@@ -35,6 +35,8 @@
 import static org.mockito.Matchers.*;
 import static org.mockito.Mockito.*;
 
+import java.util.ArrayList;
+
 import org.forgerock.i18n.LocalizedIllegalArgumentException;
 import org.forgerock.opendj.ldap.ByteString;
 import org.forgerock.opendj.ldap.Connection;
@@ -184,18 +186,16 @@
      */
     @Test
     public void testCopyOnWriteWithChanges() {
-
         final Schema baseSchema = Schema.getCoreSchema();
         final Schema schema =
                 new SchemaBuilder(baseSchema).addAttributeType(
                         "( testtype-oid NAME 'testtype' SUP name )", false).toSchema();
         assertThat(schema).isNotSameAs(baseSchema);
-        assertThat(schema.getObjectClasses().containsAll(baseSchema.getObjectClasses()));
-        assertThat(schema.getObjectClasses().size())
-                .isEqualTo(baseSchema.getObjectClasses().size());
+        assertThat(new ArrayList<>(schema.getObjectClasses())).isEqualTo(
+                new ArrayList<>(baseSchema.getObjectClasses()));
         assertThat(schema.getAttributeTypes().containsAll(baseSchema.getAttributeTypes()));
         assertThat(schema.getAttributeType("testtype")).isNotNull();
-        assertThat(schema.getSchemaName()).isEqualTo(baseSchema.getSchemaName());
+        assertThat(schema.getSchemaName()).startsWith(baseSchema.getSchemaName());
         assertThat(schema.getOption(ALLOW_MALFORMED_NAMES_AND_OPTIONS))
                 .isEqualTo(baseSchema.getOption(ALLOW_MALFORMED_NAMES_AND_OPTIONS));
     }

--
Gitblit v1.10.0