001/*
002 * Copyright 2016-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2016-2024 Ping Identity Corporation
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020/*
021 * Copyright (C) 2016-2024 Ping Identity Corporation
022 *
023 * This program is free software; you can redistribute it and/or modify
024 * it under the terms of the GNU General Public License (GPLv2 only)
025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
026 * as published by the Free Software Foundation.
027 *
028 * This program is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031 * GNU General Public License for more details.
032 *
033 * You should have received a copy of the GNU General Public License
034 * along with this program; if not, see <http://www.gnu.org/licenses>.
035 */
036package com.unboundid.ldap.sdk.transformations;
037
038
039
040import java.util.ArrayList;
041import java.util.Collection;
042import java.util.Collections;
043import java.util.HashMap;
044import java.util.Map;
045
046import com.unboundid.ldap.sdk.Attribute;
047import com.unboundid.ldap.sdk.Entry;
048import com.unboundid.ldap.sdk.schema.AttributeTypeDefinition;
049import com.unboundid.ldap.sdk.schema.Schema;
050import com.unboundid.util.Debug;
051import com.unboundid.util.NotNull;
052import com.unboundid.util.Nullable;
053import com.unboundid.util.StaticUtils;
054import com.unboundid.util.ThreadSafety;
055import com.unboundid.util.ThreadSafetyLevel;
056
057
058
059/**
060 * This class provides an implementation of an entry transformation that can be
061 * used to replace existing attributes in entries with a default set of values.
062 * The default attributes will not be added to entries that do not have existing
063 * values for the target attributes.
064 */
065@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
066public final class ReplaceAttributeTransformation
067       implements EntryTransformation
068{
069  // The schema to use when processing.
070  @Nullable private final Schema schema;
071
072  // The set of attributes to replace in entries.
073  @NotNull private final Map<String,Attribute> attributes;
074
075
076
077  /**
078   * Creates a new replace attribute transformation that will replace existing
079   * values of the specified attribute with the provided set of default values.
080   *
081   * @param  schema         The schema to use to identify alternate names that
082   *                        may be used to reference the attributes to replace.
083   *                        It may be {@code null} to use a default standard
084   *                        schema.
085   * @param  attributeName  The name of the attribute for which to replace
086   *                        existing values.  It must not be {@code null}.
087   * @param  newValues      The new values to use in place of the existing
088   *                        values for the specified attribute.
089   */
090  public ReplaceAttributeTransformation(@Nullable final Schema schema,
091                                        @NotNull final String attributeName,
092                                        @NotNull final String... newValues)
093  {
094    this(schema, new Attribute(attributeName, schema, newValues));
095  }
096
097
098
099  /**
100   * Creates a new replace attribute transformation that will replace existing
101   * values of the specified attribute with the provided set of default values.
102   *
103   * @param  schema         The schema to use to identify alternate names that
104   *                        may be used to reference the attributes to replace.
105   *                        It may be {@code null} to use a default standard
106   *                        schema.
107   * @param  attributeName  The name of the attribute for which to replace
108   *                        existing values.  It must not be {@code null}.
109   * @param  newValues      The new values to use in place of the existing
110   *                        values for the specified attribute.
111   */
112  public ReplaceAttributeTransformation(@Nullable final Schema schema,
113              @NotNull final String attributeName,
114              @NotNull final Collection<String> newValues)
115  {
116    this(schema, new Attribute(attributeName, schema, newValues));
117  }
118
119
120
121  /**
122   * Creates a new replace attribute transformation that will replace existing
123   * copies of the specified attributes with the provided versions.
124   *
125   * @param  schema      The schema to use to identify alternate names that may
126   *                     be used to reference the attributes to replace.  It may
127   *                     be {@code null} to use a default standard schema.
128   * @param  attributes  The attributes to be used in place of existing
129   *                     attributes of the same type.  It must not be
130   *                     {@code null} or empty.
131   */
132  public ReplaceAttributeTransformation(@Nullable final Schema schema,
133              @NotNull final Attribute... attributes)
134  {
135    this(schema, StaticUtils.toList(attributes));
136  }
137
138
139
140  /**
141   * Creates a new replace attribute transformation that will replace existing
142   * copies of the specified attributes with the provided versions.
143   *
144   * @param  schema      The schema to use to identify alternate names that may
145   *                     be used to reference the attributes to replace.  It may
146   *                     be {@code null} to use a default standard schema.
147   * @param  attributes  The attributes to be used in place of existing
148   *                     attributes of the same type.  It must not be
149   *                     {@code null} or empty.
150   */
151  public ReplaceAttributeTransformation(@Nullable final Schema schema,
152              @NotNull final Collection<Attribute> attributes)
153  {
154    // If a schema was provided, then use it.  Otherwise, use the default
155    // standard schema.
156    Schema s = schema;
157    if (s == null)
158    {
159      try
160      {
161        s = Schema.getDefaultStandardSchema();
162      }
163      catch (final Exception e)
164      {
165        // This should never happen.
166        Debug.debugException(e);
167      }
168    }
169    this.schema = s;
170
171
172    // Identify all of the names that may be used to reference the attributes
173    // to replace.
174    final HashMap<String,Attribute> attrMap =
175         new HashMap<>(StaticUtils.computeMapCapacity(10));
176    for (final Attribute a : attributes)
177    {
178      final String baseName = StaticUtils.toLowerCase(a.getBaseName());
179      attrMap.put(baseName, a);
180
181      if (s != null)
182      {
183        final AttributeTypeDefinition at = s.getAttributeType(baseName);
184        if (at != null)
185        {
186          attrMap.put(StaticUtils.toLowerCase(at.getOID()),
187               new Attribute(at.getOID(), s, a.getValues()));
188          for (final String name : at.getNames())
189          {
190            final String lowerName = StaticUtils.toLowerCase(name);
191            if (! attrMap.containsKey(lowerName))
192            {
193              attrMap.put(lowerName, new Attribute(name, s, a.getValues()));
194            }
195          }
196        }
197      }
198    }
199    this.attributes = Collections.unmodifiableMap(attrMap);
200  }
201
202
203
204  /**
205   * {@inheritDoc}
206   */
207  @Override()
208  @Nullable()
209  public Entry transformEntry(@NotNull final Entry e)
210  {
211    if (e == null)
212    {
213      return null;
214    }
215
216
217    // First, see if the entry has any of the target attributes.  If not, we can
218    // just return the provided entry.
219    boolean hasAttributeToReplace = false;
220    final Collection<Attribute> originalAttributes = e.getAttributes();
221    for (final Attribute a : originalAttributes)
222    {
223      if (attributes.containsKey(StaticUtils.toLowerCase(a.getBaseName())))
224      {
225        hasAttributeToReplace = true;
226        break;
227      }
228    }
229
230    if (! hasAttributeToReplace)
231    {
232      return e;
233    }
234
235
236    // Create a copy of the entry with all appropriate attributes replaced with
237    // the appropriate default versions.
238    final ArrayList<Attribute> newAttributes =
239         new ArrayList<>(originalAttributes.size());
240    for (final Attribute a : originalAttributes)
241    {
242      final Attribute replacement =
243           attributes.get(StaticUtils.toLowerCase(a.getBaseName()));
244      if (replacement == null)
245      {
246        newAttributes.add(a);
247      }
248      else
249      {
250        if (a.hasOptions())
251        {
252          newAttributes.add(new Attribute(a.getName(), schema,
253               replacement.getRawValues()));
254        }
255        else
256        {
257          newAttributes.add(replacement);
258        }
259      }
260    }
261
262    return new Entry(e.getDN(), schema, newAttributes);
263  }
264
265
266
267  /**
268   * {@inheritDoc}
269   */
270  @Override()
271  @Nullable()
272  public Entry translate(@NotNull final Entry original,
273                         final long firstLineNumber)
274  {
275    return transformEntry(original);
276  }
277
278
279
280  /**
281   * {@inheritDoc}
282   */
283  @Override()
284  @Nullable()
285  public Entry translateEntryToWrite(@NotNull final Entry original)
286  {
287    return transformEntry(original);
288  }
289}