From eb384076c69a10db7dc66fb97b8c9c7db28dd273 Mon Sep 17 00:00:00 2001
From: Jean-Noël Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Thu, 04 Aug 2016 15:06:06 +0000
Subject: [PATCH] Make CustomSearchResult closer to SDK's Entry

---
 opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/datamodel/CustomSearchResult.java |   54 +------------
 opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/ManageTasksPanel.java          |   13 +-
 opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/SimplifiedViewEntryPanel.java  |   97 ++++++++++++-----------
 opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/TableViewEntryPanel.java       |    7 +
 opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/ViewEntryPanel.java            |   17 ++--
 5 files changed, 74 insertions(+), 114 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/datamodel/CustomSearchResult.java b/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/datamodel/CustomSearchResult.java
index 9cc008b..85ffff1 100644
--- a/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/datamodel/CustomSearchResult.java
+++ b/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/datamodel/CustomSearchResult.java
@@ -16,21 +16,13 @@
  */
 package org.opends.guitools.controlpanel.datamodel;
 
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.SortedSet;
-import java.util.TreeSet;
-
 import javax.naming.NamingException;
 
 import org.forgerock.opendj.adapter.server3x.Converters;
 import org.forgerock.opendj.ldap.Attribute;
 import org.forgerock.opendj.ldap.AttributeDescription;
-import org.forgerock.opendj.ldap.ByteString;
 import org.forgerock.opendj.ldap.DN;
 import org.forgerock.opendj.ldap.Entry;
-import org.forgerock.opendj.ldap.LinkedAttribute;
 import org.forgerock.opendj.ldap.LinkedHashMapEntry;
 import org.forgerock.opendj.ldap.responses.SearchResultEntry;
 import org.opends.server.types.OpenDsException;
@@ -75,28 +67,6 @@
     return entry.getName();
   }
 
-  /**
-   * Returns the values for a given attribute.  It returns an empty Set if
-   * the attribute is not defined.
-   * @param name the name of the attribute.
-   * @return the values for a given attribute.  It returns an empty Set if
-   * the attribute is not defined.
-   */
-  public List<ByteString> getAttributeValues(String name) {
-    Attribute attr = entry.getAttribute(name);
-    return attr != null ? toList(attr) : Collections.<ByteString> emptyList();
-  }
-
-  private List<ByteString> toList(Attribute attr)
-  {
-    final List<ByteString> results = new ArrayList<>();
-    for (ByteString value : attr)
-    {
-      results.add(value);
-    }
-    return results;
-  }
-
   public Attribute getAttribute(AttributeDescription attributeDescription)
   {
     return entry.getAttribute(attributeDescription);
@@ -117,19 +87,6 @@
     return entry;
   }
 
-  /**
-   * Returns all the attribute names of the entry.
-   * @return the attribute names of the entry.
-   */
-  public SortedSet<String> getAttributeNames() {
-    SortedSet<String> results = new TreeSet<>();
-    for (Attribute attr : entry.getAllAttributes())
-    {
-      results.add(attr.getAttributeDescriptionAsString());
-    }
-    return results;
-  }
-
   @Override
   public int compareTo(CustomSearchResult o) {
     if (this.equals(o))
@@ -163,15 +120,12 @@
     return entry.hashCode();
   }
 
-  /**
-   * Sets the values for a given attribute name.
-   * @param attrName the name of the attribute.
-   * @param values the values for the attribute.
+  /** 
+   * Sets the given attribute.
+   * @param attr the attribute.
    */
-  public void set(String attrName, List<ByteString> values)
+  public void set(final Attribute attr)
   {
-    final LinkedAttribute attr = new LinkedAttribute(attrName);
-    attr.addAll(values);
     entry.removeAttribute(attr.getAttributeDescription());
     entry.addAttribute(attr);
   }
diff --git a/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/ManageTasksPanel.java b/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/ManageTasksPanel.java
index 813d90d..e27d203 100644
--- a/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/ManageTasksPanel.java
+++ b/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/ManageTasksPanel.java
@@ -18,7 +18,6 @@
 
 import static org.forgerock.util.Utils.*;
 import static org.opends.messages.AdminToolMessages.*;
-import static org.opends.server.util.CollectionUtils.*;
 
 import java.awt.Component;
 import java.awt.GridBagConstraints;
@@ -51,8 +50,8 @@
 
 import org.forgerock.i18n.LocalizableMessage;
 import org.forgerock.i18n.slf4j.LocalizedLogger;
-import org.forgerock.opendj.ldap.ByteString;
 import org.forgerock.opendj.ldap.DN;
+import org.forgerock.opendj.ldap.LinkedAttribute;
 import org.opends.guitools.controlpanel.datamodel.ControlPanelInfo;
 import org.opends.guitools.controlpanel.datamodel.CustomSearchResult;
 import org.opends.guitools.controlpanel.datamodel.ServerDescriptor;
@@ -496,8 +495,9 @@
       };
       for (int j=0; j < attrNames.length; j++)
       {
-        ByteString o = ByteString.valueOfUtf8(values[j] + r.nextInt());
-        csr.set(attrNames[j], newArrayList(o));
+        final LinkedAttribute attr = new LinkedAttribute(attrNames[j]);
+        attr.add(values[j] + r.nextInt());
+        csr.set(attr);
       }
       try
       {
@@ -559,8 +559,9 @@
       };
       for (int j=0; j < attrNames.length; j++)
       {
-        ByteString o = ByteString.valueOfUtf8(values[j]);
-        csr.set(attrNames[j], newArrayList(o));
+        final LinkedAttribute attr = new LinkedAttribute(attrNames[j]);
+        attr.add(values[j]);
+        csr.set(attr);
       }
       try
       {
diff --git a/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/SimplifiedViewEntryPanel.java b/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/SimplifiedViewEntryPanel.java
index 932cedf..fbb561c 100644
--- a/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/SimplifiedViewEntryPanel.java
+++ b/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/SimplifiedViewEntryPanel.java
@@ -73,9 +73,11 @@
 import org.forgerock.i18n.LocalizableMessage;
 import org.forgerock.i18n.LocalizableMessageBuilder;
 import org.forgerock.opendj.ldap.AVA;
+import org.forgerock.opendj.ldap.Attribute;
 import org.forgerock.opendj.ldap.AttributeDescription;
 import org.forgerock.opendj.ldap.ByteString;
 import org.forgerock.opendj.ldap.DN;
+import org.forgerock.opendj.ldap.LinkedAttribute;
 import org.forgerock.opendj.ldap.RDN;
 import org.forgerock.opendj.ldap.schema.AttributeType;
 import org.forgerock.opendj.ldap.schema.ObjectClass;
@@ -436,13 +438,14 @@
     requiredAttrs.clear();
 
     // Build the attributes panel.
-    Collection<String> sortedAttributes = getSortedAttributes(sr, isReadOnly);
+    Collection<AttributeDescription> sortedAttributes = getSortedAttributes(sr, isReadOnly);
     if (isReadOnly)
     {
-      for (String attrName : sortedAttributes)
+      for (AttributeDescription attrDesc : sortedAttributes)
       {
+        String attrName = attrDesc.toString();
         JLabel label = getLabelForAttribute(attrName, sr);
-        List<ByteString> values = toList(sr.getAttribute(attrName));
+        List<ByteString> values = toList(sr.getAttribute(attrDesc));
         JComponent comp = getReadOnlyComponent(attrName, values);
         gbc.weightx = 0.0;
         gbc.anchor = anchor1(values);
@@ -458,16 +461,17 @@
     }
     else
     {
-      for (final String attrName : sortedAttributes)
+      for (AttributeDescription attrDesc : sortedAttributes)
       {
+        final String attrName = attrDesc.toString();
         String lcAttr = attrName.toLowerCase();
         JLabel label = getLabelForAttribute(attrName, sr);
-        if (isRequired(attrName, sr))
+        if (isRequired(attrDesc, sr))
         {
           Utilities.setRequiredIcon(label);
           requiredAttrs.add(lcAttr);
         }
-        List<ByteString> values = toList(sr.getAttribute(attrName));
+        List<ByteString> values = toList(sr.getAttribute(attrDesc));
         if (values.isEmpty())
         {
           values = newArrayList(ByteString.empty());
@@ -662,20 +666,20 @@
     return Utilities.createPrimaryLabel(l.toMessage());
   }
 
-  private Collection<String> getSortedAttributes(CustomSearchResult sr, boolean isReadOnly)
+  private LinkedHashSet<AttributeDescription> getSortedAttributes(CustomSearchResult sr, boolean isReadOnly)
   {
     // Get all attributes that the entry can have
-    Set<String> attributes = new LinkedHashSet<>();
-    List<String> entryAttrs = new ArrayList<>(sr.getAttributeNames());
+    List<AttributeDescription> entryAttrs = new ArrayList<>();
     List<String> attrsWithNoOptions = new ArrayList<>();
-    for (String attr : entryAttrs)
+    for (Attribute attr : sr.getSdkEntry().getAllAttributes())
     {
-      AttributeDescription attrDesc = AttributeDescription.valueOf(attr);
+      AttributeDescription attrDesc = attr.getAttributeDescription();
+      entryAttrs.add(attrDesc);
       attrsWithNoOptions.add(attrDesc.getNameOrOID().toLowerCase());
     }
 
     // Put first the attributes associated with the objectclass in hmOrderedAttrNames
-    LinkedHashSet<String> attrNames = new LinkedHashSet<>();
+    LinkedHashSet<AttributeDescription> attrNames = new LinkedHashSet<>();
     for (ByteString ocName : sr.getAttribute(OBJECTCLASS_ATTRIBUTE_TYPE_NAME))
     {
       String[] attrs = hmOrdereredAttrNames.get(ocName.toString().toLowerCase());
@@ -684,7 +688,7 @@
         for (String attr : attrs)
         {
           int index = attrsWithNoOptions.indexOf(attr.toLowerCase());
-          attrNames.add(index != -1 ? entryAttrs.get(index) : attr);
+          attrNames.add(index != -1 ? entryAttrs.get(index) : AttributeDescription.valueOf(attr));
         }
       }
     }
@@ -696,11 +700,11 @@
     {
       List<String> attrsNotToAdd = Arrays.asList("entryuuid", "hassubordinates",
           "numsubordinates", "subschemasubentry", "entrydn");
-      for (String attr : sr.getAttributeNames())
+      for (AttributeDescription attrDesc : entryAttrs)
       {
-        if (!find(attrNames, attr) && !find(attrsNotToAdd, attr))
+        if (!attrNames.contains(attrDesc) && !contains(attrsNotToAdd, attrDesc))
         {
-          attrNames.add(attr);
+          attrNames.add(attrDesc);
         }
       }
     }
@@ -711,8 +715,8 @@
       // alphabetical order) the attributes with no friendly name.  Finally
       // do the same with the other attributes.
 
-      SortedSet<String> requiredAttributes = new TreeSet<>();
-      SortedSet<String> allowedAttributes = new TreeSet<>();
+      SortedSet<AttributeType> requiredAttributes = new TreeSet<>();
+      SortedSet<AttributeType> allowedAttributes = new TreeSet<>();
 
       if (schema != null)
       {
@@ -723,11 +727,11 @@
           {
             for (AttributeType attr : objectClass.getRequiredAttributes())
             {
-              requiredAttributes.add(attr.getNameOrOID());
+              requiredAttributes.add(attr);
             }
             for (AttributeType attr : objectClass.getOptionalAttributes())
             {
-              allowedAttributes.add(attr.getNameOrOID());
+              allowedAttributes.add(attr);
             }
           }
         }
@@ -735,11 +739,12 @@
 
       // Now try to put first the attributes for which we have a friendly
       // name (the most common ones).
+      Set<AttributeDescription> attributes = new LinkedHashSet<>();
       updateAttributes(attributes, requiredAttributes, entryAttrs, attrsWithNoOptions);
       updateAttributes(attributes, allowedAttributes, entryAttrs, attrsWithNoOptions);
 
       attributes.addAll(entryAttrs);
-      attributes.add("aci");
+      attributes.add(AttributeDescription.valueOf("aci"));
 
       // In read-only mode display only the attributes with values
       if (isReadOnly)
@@ -747,10 +752,9 @@
         attributes.retainAll(entryAttrs);
       }
 
-      for (String attr : attributes)
+      for (AttributeDescription attr : attributes)
       {
-        boolean canAdd = isEditable(AttributeDescription.valueOf(attr), schema);
-        if (canAdd && !find(attrNames, attr))
+        if (isEditable(attr, schema) && !attrNames.contains(attr))
         {
           attrNames.add(attr);
         }
@@ -759,11 +763,11 @@
     return attrNames;
   }
 
-  private boolean find(Collection<String> attrNames, String attrNameToFind)
+  private boolean contains(List<String> attrNames, AttributeDescription attrDesc)
   {
     for (String attrName : attrNames)
     {
-      if (attrName.equalsIgnoreCase(attrNameToFind))
+      if (attrDesc.getAttributeType().hasName(attrName))
       {
         return true;
       }
@@ -772,37 +776,37 @@
   }
 
   private void updateAttributes(
-      Collection<String> attributes,
-      Set<String> newAttributes,
-      List<String> entryAttrs,
+      Collection<AttributeDescription> attributes,
+      Set<AttributeType> newAttributes,
+      List<AttributeDescription> entryAttrs,
       List<String> attrsWithNoOptions)
   {
-    for (String attr : newAttributes)
+    for (AttributeType attr : newAttributes)
     {
-      int index = attrsWithNoOptions.indexOf(attr.toLowerCase());
+      int index = attrsWithNoOptions.indexOf(attr.getNameOrOID().toLowerCase());
       if (index != -1)
       {
         attributes.add(entryAttrs.get(index));
       }
       else if (hasCertificateSyntax(attr))
       {
-        attributes.add(attr + ";binary");
+        attributes.add(AttributeDescription.create(attr).withOption("binary"));
       }
       else
       {
-        attributes.add(attr);
+        attributes.add(AttributeDescription.create(attr));
       }
     }
   }
 
-  private boolean hasCertificateSyntax(String attrName)
+  private boolean hasCertificateSyntax(AttributeType attrType)
   {
     Schema schema = getInfo().getServerDescriptor().getSchema();
     boolean isCertificate = false;
     // Check all the attributes that we consider binaries.
     if (schema != null)
     {
-      String attributeName = AttributeDescription.valueOf(attrName).getNameOrOID().toLowerCase();
+      String attributeName = attrType.getNameOrOID().toLowerCase();
       if (schema.hasAttributeType(attributeName))
       {
         AttributeType attr = schema.getAttributeType(attributeName);
@@ -1135,12 +1139,11 @@
     return false;
   }
 
-  private boolean isRequired(String attrName, CustomSearchResult sr)
+  private boolean isRequired(AttributeDescription attrDesc, CustomSearchResult sr)
   {
     Schema schema = getInfo().getServerDescriptor().getSchema();
     if (schema != null)
     {
-      AttributeDescription attrDesc = AttributeDescription.valueOf(attrName, schema.getSchemaNG());
       AttributeType attrType = attrDesc.getAttributeType();
       if (!attrType.isPlaceHolder())
       {
@@ -1304,8 +1307,8 @@
         List<String> newPwds = getNewPasswords(attrName);
         if (newPwds.equals(lastUserPasswords.get(attrName.toLowerCase())))
         {
-          List<ByteString> oldValues = searchResult.getAttributeValues(attrName);
-          if (!oldValues.isEmpty())
+          Attribute oldValues = searchResult.getAttribute(attrName);
+          if (oldValues != null && !oldValues.isEmpty())
           {
             appendLDIFLines(sb, attrName, oldValues);
           }
@@ -1325,8 +1328,8 @@
     // Add the attributes that are not displayed
     for (String attrName : schemaReadOnlyAttributesLowerCase)
     {
-      List<ByteString> values = searchResult.getAttributeValues(attrName);
-      if (!values.isEmpty())
+      Attribute values = searchResult.getAttribute(attrName);
+      if (values != null && !values.isEmpty())
       {
         appendLDIFLines(sb, attrName, values);
       }
@@ -1391,7 +1394,7 @@
     appendLDIFLines(sb, attrName, getValues(attrName));
   }
 
-  private void appendLDIFLines(StringBuilder sb, String attrName, List<?> values)
+  private void appendLDIFLines(StringBuilder sb, String attrName, Iterable<?> values)
   {
     for (Object value : values)
     {
@@ -1593,10 +1596,10 @@
 
     for (String attrName : schemaReadOnlyAttributesLowerCase)
     {
-      List<ByteString> values = searchResult.getAttributeValues(attrName);
-      if (!values.isEmpty())
+      Attribute attr = searchResult.getAttribute(attrName);
+      if (attr != null && !attr.isEmpty())
       {
-        newResult.set(attrName, values);
+        newResult.set(new LinkedAttribute(attr));
       }
     }
     ignoreEntryChangeEvents = true;
@@ -1645,8 +1648,8 @@
           List<String> newPwds = getNewPasswords(attrName);
           if (newPwds.equals(lastUserPasswords.get(attrName)))
           {
-            List<ByteString> oldValues = searchResult.getAttributeValues(attrName);
-            newResult.set(attrName, oldValues);
+            Attribute oldValues = searchResult.getAttribute(attrName);
+            newResult.set(oldValues != null ? new LinkedAttribute(oldValues) : new LinkedAttribute(attrName));
           }
           else
           {
diff --git a/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/TableViewEntryPanel.java b/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/TableViewEntryPanel.java
index 874f987..ed64d99 100644
--- a/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/TableViewEntryPanel.java
+++ b/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/TableViewEntryPanel.java
@@ -51,6 +51,7 @@
 import org.forgerock.opendj.ldap.AttributeDescription;
 import org.forgerock.opendj.ldap.ByteString;
 import org.forgerock.opendj.ldap.DN;
+import org.forgerock.opendj.ldap.LinkedAttribute;
 import org.forgerock.opendj.ldap.RDN;
 import org.forgerock.opendj.ldap.schema.AttributeType;
 import org.forgerock.opendj.ldap.schema.ObjectClass;
@@ -721,10 +722,10 @@
 
       for (String attrName : schemaReadOnlyAttributesLowerCase)
       {
-        List<ByteString> values = searchResult.getAttributeValues(attrName);
-        if (!values.isEmpty())
+        Attribute attr = searchResult.getAttribute(attrName);
+        if (attr != null && !attr.isEmpty())
         {
-          newResult.set(attrName, values);
+          newResult.set(new LinkedAttribute(attr));
         }
       }
       ignoreEntryChangeEvents = true;
diff --git a/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/ViewEntryPanel.java b/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/ViewEntryPanel.java
index 3908639..226b3ec 100644
--- a/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/ViewEntryPanel.java
+++ b/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/ViewEntryPanel.java
@@ -40,6 +40,7 @@
 import org.forgerock.opendj.ldap.AttributeDescription;
 import org.forgerock.opendj.ldap.ByteString;
 import org.forgerock.opendj.ldap.DN;
+import org.forgerock.opendj.ldap.LinkedAttribute;
 import org.forgerock.opendj.ldap.schema.AttributeType;
 import org.forgerock.opendj.ldap.schema.ObjectClass;
 import org.forgerock.opendj.ldap.schema.ObjectClassType;
@@ -443,7 +444,7 @@
   protected void setValues(CustomSearchResult sr, String attrName)
   {
     List<Object> values = getValues(attrName);
-    List<ByteString> valuesToSet = new ArrayList<>();
+    final LinkedAttribute attr = new LinkedAttribute(attrName);
     for (Object value : values)
     {
       if (value instanceof ObjectClassValue)
@@ -451,23 +452,23 @@
         ObjectClassValue ocValue = (ObjectClassValue)value;
         if (ocValue.getStructural() != null)
         {
-          valuesToSet.add(ByteString.valueOfUtf8(ocValue.getStructural()));
+          attr.add(ocValue.getStructural());
         }
         SortedSet<String> auxiliaries = ocValue.getAuxiliary();
         for (String auxiliary : auxiliaries)
         {
-          valuesToSet.add(ByteString.valueOfUtf8(auxiliary));
+          attr.add(auxiliary);
         }
       }
       else if (value instanceof byte[])
       {
-        valuesToSet.add(ByteString.wrap((byte[]) value));
+        attr.add((byte[]) value);
       }
       else if (value instanceof BinaryValue)
       {
         try
         {
-          valuesToSet.add(ByteString.wrap(((BinaryValue) value).getBytes()));
+          attr.add(((BinaryValue) value).getBytes());
         }
         catch (ParseException pe)
         {
@@ -479,13 +480,13 @@
         String s = String.valueOf(value);
         if (s.trim().length() > 0)
         {
-          valuesToSet.add(ByteString.valueOfUtf8(s));
+          attr.add(s);
         }
       }
     }
-    if (!valuesToSet.isEmpty())
+    if (!attr.isEmpty())
     {
-      sr.set(attrName, valuesToSet);
+      sr.set(attr);
     }
   }
 

--
Gitblit v1.10.0