From b9e895852290799d8bdaa3ff81e4d47709bf2326 Mon Sep 17 00:00:00 2001
From: Nicolas Capponi <nicolas.capponi@forgerock.com>
Date: Mon, 30 May 2016 10:43:09 +0000
Subject: [PATCH] OPENDJ-2987 Adapt SomeSchemaElement and SchemaUtils

---
 opendj-server-legacy/src/main/java/org/opends/server/util/SchemaUtils.java         |   45 +++++++++++++++++++++-
 opendj-server-legacy/src/main/java/org/opends/server/schema/SomeSchemaElement.java |   63 +++++++++++++------------------
 2 files changed, 69 insertions(+), 39 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/schema/SomeSchemaElement.java b/opendj-server-legacy/src/main/java/org/opends/server/schema/SomeSchemaElement.java
index dbea4e3..72e98ee 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/schema/SomeSchemaElement.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/schema/SomeSchemaElement.java
@@ -22,13 +22,13 @@
 import java.util.Map;
 
 import org.forgerock.opendj.ldap.schema.AttributeType;
+import org.forgerock.opendj.ldap.schema.ObjectClass;
 import org.forgerock.opendj.ldap.schema.Schema;
 import org.forgerock.opendj.ldap.schema.SchemaBuilder;
 import org.forgerock.opendj.ldap.schema.SchemaElement;
 import org.opends.server.config.ConfigConstants;
 import org.opends.server.core.ServerContext;
 import org.opends.server.types.CommonSchemaElements;
-import org.forgerock.opendj.ldap.schema.ObjectClass;
 import org.opends.server.util.RemoveOnceSDKSchemaIsUsed;
 import org.opends.server.util.ServerConstants;
 
@@ -43,7 +43,7 @@
     + " to manage in the same way SDK and server schema element classes")
 public class SomeSchemaElement implements SchemaElement
 {
-  private final ObjectClass objectClass;
+  private ObjectClass objectClass;
   private AttributeType attributeType;
 
   /**
@@ -100,14 +100,9 @@
     return attributeType != null;
   }
 
-  /**
-   * Returns whether the wrapped element is an object class.
-   *
-   * @return {@code true} when the wrapped element is an object class, {@code false} otherwise
-   */
-  public boolean isObjectClass()
+  private SchemaElement asSchemaElement()
   {
-    return objectClass != null;
+    return attributeType != null ? attributeType : objectClass;
   }
 
   /**
@@ -137,19 +132,25 @@
    */
   public Iterable<String> getNames()
   {
-    return attributeType != null ? attributeType.getNames() : objectClass.getNormalizedNames();
+    return attributeType != null ? attributeType.getNames() : objectClass.getNames();
+  }
+
+  @Override
+  public String getDescription()
+  {
+    return asSchemaElement().getDescription();
   }
 
   @Override
   public Map<String, List<String>> getExtraProperties()
   {
-    return attributeType != null ? attributeType.getExtraProperties() : objectClass.getExtraProperties();
+    return asSchemaElement().getExtraProperties();
   }
 
   @Override
   public String toString()
   {
-    return attributeType != null ? attributeType.toString() : objectClass.toString();
+    return asSchemaElement().toString();
   }
 
   /**
@@ -205,12 +206,7 @@
 
   private String getExtraPropertySingleValue(String schemaPropertyOrigin)
   {
-    if (objectClass != null)
-    {
-      return CommonSchemaElements.getSingleValueProperty(objectClass, schemaPropertyOrigin);
-    }
-    List<String> values = attributeType.getExtraProperties().get(schemaPropertyOrigin);
-    return values != null && !values.isEmpty() ? values.get(0) : null;
+    return CommonSchemaElements.getSingleValueProperty(asSchemaElement(), schemaPropertyOrigin);
   }
 
   /**
@@ -219,7 +215,7 @@
    */
   public String getAttributeName()
   {
-    return attributeType!= null ? ConfigConstants.ATTR_ATTRIBUTE_TYPES : ConfigConstants.ATTR_OBJECTCLASSES;
+    return attributeType != null ? ConfigConstants.ATTR_ATTRIBUTE_TYPES : ConfigConstants.ATTR_OBJECTCLASSES;
   }
 
   /**
@@ -234,15 +230,8 @@
    */
   public void setExtraPropertySingleValue(ServerContext serverContext, String property, String value)
   {
-    if (attributeType != null)
-    {
-      List<String> values = value != null ?  Arrays.asList(value) : null;
-      setExtraPropertyMultipleValues(serverContext, property, values);
-    }
-    else
-    {
-      CommonSchemaElements.setExtraProperty(objectClass, property, value);
-    }
+    List<String> values = value != null ? Arrays.asList(value) : null;
+    setExtraPropertyMultipleValues(serverContext, property, values);
   }
 
   /**
@@ -257,10 +246,10 @@
    */
   public void setExtraPropertyMultipleValues(ServerContext serverContext, String property, List<String> values)
   {
+    Schema schemaNG = serverContext != null ? serverContext.getSchemaNG() : Schema.getDefaultSchema();
+    SchemaBuilder schemaBuilder = new SchemaBuilder(schemaNG);
     if (attributeType != null)
     {
-      SchemaBuilder schemaBuilder = serverContext != null ?
-          new SchemaBuilder(serverContext.getSchemaNG()) : new SchemaBuilder(Schema.getDefaultSchema());
       AttributeType.Builder builder =
           schemaBuilder.buildAttributeType(attributeType).removeExtraProperty(property, (String) null);
       if (values != null  && !values.isEmpty())
@@ -271,7 +260,13 @@
     }
     else
     {
-      objectClass.setExtraProperty(property, values);
+      ObjectClass.Builder builder =
+          schemaBuilder.buildObjectClass(objectClass).removeExtraProperty(property, (String) null);
+      if (values != null && !values.isEmpty())
+      {
+        builder.extraProperties(property, values);
+      }
+      objectClass = builder.addToSchemaOverwrite().toSchema().getObjectClass(objectClass.getNameOrOID());
     }
   }
 
@@ -294,10 +289,4 @@
       .toSchema();
     return schema.getAttributeType(attributeType.getNameOrOID());
   }
-
-  @Override
-  public String getDescription()
-  {
-    return attributeType != null ? attributeType.getDescription() : objectClass.getDescription();
-  }
 }
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/util/SchemaUtils.java b/opendj-server-legacy/src/main/java/org/opends/server/util/SchemaUtils.java
index 7a7dea9..d0fa47d 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/util/SchemaUtils.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/util/SchemaUtils.java
@@ -15,11 +15,16 @@
  */
 package org.opends.server.util;
 
-import org.forgerock.opendj.ldap.schema.AttributeType;
-
 import static org.opends.server.schema.SchemaConstants.SYNTAX_AUTH_PASSWORD_OID;
 import static org.opends.server.schema.SchemaConstants.SYNTAX_USER_PASSWORD_OID;
 
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.forgerock.opendj.ldap.schema.AttributeType;
+import org.forgerock.opendj.ldap.schema.ObjectClass;
+
 /** Utility methods related to schema. */
 public class SchemaUtils
 {
@@ -60,4 +65,40 @@
     }
     return PasswordType.NOT_A_PASSWORD;
   }
+
+  /**
+   * Returns a new collection with the result of calling {@link ObjectClass#getNameOrOID()} on each
+   * element of the provided collection.
+   *
+   * @param objectClasses
+   *          the schema elements on which to act
+   * @return a new collection comprised of the names or OIDs of each element
+   */
+  public static Collection<String> getNameOrOIDsForOCs(Collection<ObjectClass> objectClasses)
+  {
+    Set<String> results = new HashSet<>(objectClasses.size());
+    for (ObjectClass objectClass : objectClasses)
+    {
+      results.add(objectClass.getNameOrOID());
+    }
+    return results;
+  }
+
+  /**
+   * Returns a new collection with the result of calling {@link AttributeType#getNameOrOID()} on
+   * each element of the provided collection.
+   *
+   * @param attributeTypes
+   *          the schema elements on which to act
+   * @return a new collection comprised of the names or OIDs of each element
+   */
+  public static Collection<String> getNameOrOIDsForATs(Collection<AttributeType> attributeTypes)
+  {
+    Set<String> results = new HashSet<>(attributeTypes.size());
+    for (AttributeType attrType : attributeTypes)
+    {
+      results.add(attrType.getNameOrOID());
+    }
+    return results;
+  }
 }

--
Gitblit v1.10.0