001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2006-2008 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2016 ForgeRock AS.
016 */
017package org.opends.server.protocols.ldap;
018
019import org.opends.server.types.LDAPException;
020import org.opends.server.types.Modification;
021import org.forgerock.opendj.ldap.ModificationType;
022import org.opends.server.types.RawAttribute;
023import org.opends.server.types.RawModification;
024
025import static org.opends.server.util.ServerConstants.*;
026
027/**
028 * This class defines the data structures and methods to use when interacting
029 * with an LDAP modification, which describes a change that should be made to an
030 * attribute.
031 */
032public class LDAPModification
033       extends RawModification
034{
035  /** The modification type for this modification. */
036  private ModificationType modificationType;
037
038  /** The attribute for this modification. */
039  private RawAttribute attribute;
040
041
042
043  /**
044   * Creates a new LDAP modification with the provided type and attribute.
045   *
046   * @param  modificationType  The modification type for this modification.
047   * @param  attribute         The attribute for this modification.
048   */
049  public LDAPModification(ModificationType modificationType,
050                          RawAttribute attribute)
051  {
052    this.modificationType = modificationType;
053    this.attribute        = attribute;
054  }
055
056
057
058  /**
059   * Retrieves the modification type for this modification.
060   *
061   * @return  The modification type for this modification.
062   */
063  @Override
064  public ModificationType getModificationType()
065  {
066    return modificationType;
067  }
068
069
070
071  /**
072   * Specifies the modification type for this modification.
073   *
074   * @param  modificationType  The modification type for this modification.
075   */
076  @Override
077  public void setModificationType(ModificationType modificationType)
078  {
079    this.modificationType = modificationType;
080  }
081
082
083
084  /**
085   * Retrieves the attribute for this modification.
086   *
087   * @return  The attribute for this modification.
088   */
089  @Override
090  public RawAttribute getAttribute()
091  {
092    return attribute;
093  }
094
095
096
097  /**
098   * Specifies the attribute for this modification.
099   *
100   * @param  attribute  The attribute for this modification.
101   */
102  @Override
103  public void setAttribute(RawAttribute attribute)
104  {
105    this.attribute = attribute;
106  }
107
108
109
110  /**
111   * Creates a new core <CODE>Modification</CODE> object from this LDAP
112   * modification.
113   *
114   * @return  The decoded modification.
115   *
116   * @throws  LDAPException  If a problem occurs while trying to convert the
117   *                         LDAP attribute to a core <CODE>Attribute</CODE>.
118   */
119  @Override
120  public Modification toModification()
121         throws LDAPException
122  {
123    return new Modification(modificationType, attribute.toAttribute());
124  }
125
126
127
128  /**
129   * Retrieves a string representation of this modification.
130   *
131   * @return  A string representation of this modification.
132   */
133  @Override
134  public String toString()
135  {
136    StringBuilder buffer = new StringBuilder();
137    toString(buffer);
138    return buffer.toString();
139  }
140
141
142
143  /**
144   * Appends a string representation of this modification to the provided
145   * buffer.
146   *
147   * @param  buffer  The buffer to which the information should be appended.
148   */
149  @Override
150  public void toString(StringBuilder buffer)
151  {
152    buffer.append("LDAPModification(type=").append(modificationType);
153    buffer.append(", attr=");
154    attribute.toString(buffer);
155    buffer.append("})");
156  }
157
158
159
160  /**
161   * Appends a multi-line string representation of this LDAP modification to the
162   * provided buffer.
163   *
164   * @param  buffer  The buffer to which the information should be appended.
165   * @param  indent  The number of spaces from the margin that the lines should
166   *                 be indented.
167   */
168  @Override
169  public void toString(StringBuilder buffer, int indent)
170  {
171    StringBuilder indentBuf = new StringBuilder(indent);
172    for (int i=0 ; i < indent; i++)
173    {
174      indentBuf.append(' ');
175    }
176
177    buffer.append(indentBuf).append("LDAP Modification").append(EOL);
178
179    buffer.append(indentBuf);
180    buffer.append("  Modification Type:  ").append(modificationType);
181    buffer.append(" (").append(modificationType.intValue()).append(")");
182    buffer.append(EOL);
183
184    buffer.append("  Attribute:");
185    buffer.append(EOL);
186    attribute.toString(buffer, indent+4);
187  }
188}
189