001/*
002 * Copyright 2020-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2020-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) 2020-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.unboundidds.tasks;
037
038
039
040import java.util.Collections;
041import java.util.LinkedHashMap;
042import java.util.List;
043import java.util.Map;
044
045import com.unboundid.ldap.sdk.Attribute;
046import com.unboundid.ldap.sdk.Entry;
047import com.unboundid.util.NotMutable;
048import com.unboundid.util.NotNull;
049import com.unboundid.util.StaticUtils;
050import com.unboundid.util.ThreadSafety;
051import com.unboundid.util.ThreadSafetyLevel;
052
053import static com.unboundid.ldap.sdk.unboundidds.tasks.TaskMessages.*;
054
055
056
057/**
058 * This class defines a Directory Server task that can be used to safely remove
059 * an attribute type from the server schema.  It will make sure that the
060 * attribute type is not in use in the server before removing it.
061 * <BR>
062 * <BLOCKQUOTE>
063 *   <B>NOTE:</B>  This class, and other classes within the
064 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
065 *   supported for use against Ping Identity, UnboundID, and
066 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
067 *   for proprietary functionality or for external specifications that are not
068 *   considered stable or mature enough to be guaranteed to work in an
069 *   interoperable way with other types of LDAP servers.
070 * </BLOCKQUOTE>
071 * <BR>
072 * The properties that are available for use with this type of task include:
073 * <UL>
074 *   <LI>The name or OID of the attribute type to remove from the server
075 *       schema.</LI>
076 * </UL>
077 */
078@NotMutable()
079@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
080public final class RemoveAttributeTypeTask
081       extends Task
082{
083  /**
084   * The fully-qualified name of the Java class that is used for the remove
085   * attribute type task.
086   */
087  @NotNull static final String REMOVE_ATTRIBUTE_TYPE_TASK_CLASS =
088       "com.unboundid.directory.server.tasks.RemoveAttributeTypeTask";
089
090
091
092  /**
093   * The name of the attribute used to specify the name or OID of the attribute
094   * type to remove from the server schema.
095   */
096  @NotNull public static final String ATTR_ATTRIBUTE_TYPE =
097       "ds-task-remove-attribute-type-attribute";
098
099
100
101  /**
102   * The name of the object class used in remove attribute type task entries.
103   */
104  @NotNull public static final String OC_REMOVE_ATTRIBUTE_TYPE_TASK =
105       "ds-task-remove-attribute-type";
106
107
108
109  /**
110   * The task property that will be used for the attribute type name or OID.
111   */
112  @NotNull static final TaskProperty PROPERTY_ATTRIBUTE_TYPE =
113     new TaskProperty(ATTR_ATTRIBUTE_TYPE,
114          INFO_REMOVE_ATTR_TYPE_DISPLAY_NAME_ATTRIBUTE_TYPE.get(),
115          INFO_REMOVE_ATTR_TYPE_DESCRIPTION_ATTRIBUTE_TYPE.get(),
116          String.class, true, false, false);
117
118
119
120  /**
121   * The serial version UID for this serializable class.
122   */
123  private static final long serialVersionUID = -3118507632013307187L;
124
125
126
127  // The name or OID for the attribute type to remove.
128  @NotNull private final String attributeType;
129
130
131
132  /**
133   * Creates a new uninitialized remove attribute type task instance that should
134   * only be used for obtaining general information about this task, including
135   * the task name, description, and supported properties.  Attempts to use a
136   * task created with this constructor for any other reason will likely fail.
137   */
138  RemoveAttributeTypeTask()
139  {
140    super();
141
142    attributeType = null;
143  }
144
145
146
147  /**
148   * Creates a new remove attribute type task instance that will remove the
149   * specified attribute type from the server schema and will use the default
150   * values for all other properties.
151   *
152   * @param  attributeType  The name or OID of the attribute type to remove from
153   *                        the server schema.
154   */
155  public RemoveAttributeTypeTask(@NotNull final String attributeType)
156  {
157    this(new RemoveAttributeTypeTaskProperties(attributeType));
158  }
159
160
161
162  /**
163   * Creates a new remove attribute type task instance using the provided
164   * properties.
165   *
166   * @param  properties  The properties to use to create the remove attribute
167   *                     type task.  It must not be {@code null}.
168   */
169  public RemoveAttributeTypeTask(
170              @NotNull final RemoveAttributeTypeTaskProperties properties)
171  {
172    super(properties.getTaskID(), REMOVE_ATTRIBUTE_TYPE_TASK_CLASS,
173         properties.getScheduledStartTime(), properties.getDependencyIDs(),
174         properties.getFailedDependencyAction(), properties.getNotifyOnStart(),
175         properties.getNotifyOnCompletion(), properties.getNotifyOnSuccess(),
176         properties.getNotifyOnError(), properties.getAlertOnStart(),
177         properties.getAlertOnSuccess(), properties.getAlertOnError());
178
179    attributeType = properties.getAttributeType();
180  }
181
182
183
184  /**
185   * Creates a new remove attribute type task from the provided entry.
186   *
187   * @param  entry  The entry to use to create this remove attribute type task.
188   *
189   * @throws  TaskException  If the provided entry cannot be parsed as a remove
190   *                         attribute tyep task entry.
191   */
192  public RemoveAttributeTypeTask(@NotNull final Entry entry)
193         throws TaskException
194  {
195    super(entry);
196
197    attributeType = entry.getAttributeValue(ATTR_ATTRIBUTE_TYPE);
198    if (attributeType == null)
199    {
200      throw new TaskException(ERR_REMOVE_ATTR_TYPE_ENTRY_MISSING_ATTR_TYPE.get(
201           entry.getDN(), ATTR_ATTRIBUTE_TYPE));
202    }
203  }
204
205
206
207  /**
208   * Creates a new remove attribute type task from the provided set of task
209   * properties.
210   *
211   * @param  properties  The set of task properties and their corresponding
212   *                     values to use for the task.  It must not be
213   *                     {@code null}.
214   *
215   * @throws  TaskException  If the provided set of properties cannot be used to
216   *                         create a valid remove attribute type task.
217   */
218  public RemoveAttributeTypeTask(
219              @NotNull final Map<TaskProperty,List<Object>> properties)
220         throws TaskException
221  {
222    super(REMOVE_ATTRIBUTE_TYPE_TASK_CLASS, properties);
223
224    String attrType = null;
225    for (final Map.Entry<TaskProperty,List<Object>> entry :
226         properties.entrySet())
227    {
228      final TaskProperty p = entry.getKey();
229      final String attrName = StaticUtils.toLowerCase(p.getAttributeName());
230      final List<Object> values = entry.getValue();
231
232      if (attrName.equals(ATTR_ATTRIBUTE_TYPE))
233      {
234        attrType = parseString(p, values, attrType);
235      }
236    }
237
238    attributeType = attrType;
239    if (attributeType == null)
240    {
241      throw new TaskException(ERR_REMOVE_ATTR_TYPE_PROPS_MISSING_ATTR_TYPE.get(
242           ATTR_ATTRIBUTE_TYPE));
243    }
244  }
245
246
247
248  /**
249   * {@inheritDoc}
250   */
251  @Override()
252  @NotNull()
253  public String getTaskName()
254  {
255    return INFO_REMOVE_ATTR_TYPE_TASK_NAME.get();
256  }
257
258
259
260  /**
261   * {@inheritDoc}
262   */
263  @Override()
264  @NotNull()
265  public String getTaskDescription()
266  {
267    return INFO_REMOVE_ATTR_TYPE_TASK_DESCRIPTION.get();
268  }
269
270
271
272  /**
273   * Retrieves the name or OID of the attribute type to remove from the server
274   * schema.
275   *
276   * @return  The name or OID of the attribute type to remove from the server
277   *          schema.
278   */
279  @NotNull()
280  public String getAttributeType()
281  {
282    return attributeType;
283  }
284
285
286
287  /**
288   * {@inheritDoc}
289   */
290  @Override()
291  @NotNull()
292  protected List<String> getAdditionalObjectClasses()
293  {
294    return Collections.singletonList(OC_REMOVE_ATTRIBUTE_TYPE_TASK);
295  }
296
297
298
299  /**
300   * {@inheritDoc}
301   */
302  @Override()
303  @NotNull()
304  protected List<Attribute> getAdditionalAttributes()
305  {
306    return Collections.singletonList(
307         new Attribute(ATTR_ATTRIBUTE_TYPE, attributeType));
308  }
309
310
311
312  /**
313   * {@inheritDoc}
314   */
315  @Override()
316  @NotNull()
317  public List<TaskProperty> getTaskSpecificProperties()
318  {
319    return Collections.singletonList(PROPERTY_ATTRIBUTE_TYPE);
320  }
321
322
323
324  /**
325   * {@inheritDoc}
326   */
327  @Override()
328  @NotNull()
329  public Map<TaskProperty,List<Object>> getTaskPropertyValues()
330  {
331    final Map<TaskProperty,List<Object>> props =
332         new LinkedHashMap<>(StaticUtils.computeMapCapacity(20));
333    props.put(PROPERTY_ATTRIBUTE_TYPE,
334         Collections.<Object>singletonList(attributeType));
335
336    props.putAll(super.getTaskPropertyValues());
337    return Collections.unmodifiableMap(props);
338  }
339}