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-2015 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  public ModificationType getModificationType()
064  {
065    return modificationType;
066  }
067
068
069
070  /**
071   * Specifies the modification type for this modification.
072   *
073   * @param  modificationType  The modification type for this modification.
074   */
075  public void setModificationType(ModificationType modificationType)
076  {
077    this.modificationType = modificationType;
078  }
079
080
081
082  /**
083   * Retrieves the attribute for this modification.
084   *
085   * @return  The attribute for this modification.
086   */
087  public RawAttribute getAttribute()
088  {
089    return attribute;
090  }
091
092
093
094  /**
095   * Specifies the attribute for this modification.
096   *
097   * @param  attribute  The attribute for this modification.
098   */
099  public void setAttribute(RawAttribute attribute)
100  {
101    this.attribute = attribute;
102  }
103
104
105
106  /**
107   * Creates a new core <CODE>Modification</CODE> object from this LDAP
108   * modification.
109   *
110   * @return  The decoded modification.
111   *
112   * @throws  LDAPException  If a problem occurs while trying to convert the
113   *                         LDAP attribute to a core <CODE>Attribute</CODE>.
114   */
115  public Modification toModification()
116         throws LDAPException
117  {
118    return new Modification(modificationType, attribute.toAttribute());
119  }
120
121
122
123  /**
124   * Retrieves a string representation of this modification.
125   *
126   * @return  A string representation of this modification.
127   */
128  public String toString()
129  {
130    StringBuilder buffer = new StringBuilder();
131    toString(buffer);
132    return buffer.toString();
133  }
134
135
136
137  /**
138   * Appends a string representation of this modification to the provided
139   * buffer.
140   *
141   * @param  buffer  The buffer to which the information should be appended.
142   */
143  public void toString(StringBuilder buffer)
144  {
145    buffer.append("LDAPModification(type=").append(modificationType);
146    buffer.append(", attr=");
147    attribute.toString(buffer);
148    buffer.append("})");
149  }
150
151
152
153  /**
154   * Appends a multi-line string representation of this LDAP modification to the
155   * provided buffer.
156   *
157   * @param  buffer  The buffer to which the information should be appended.
158   * @param  indent  The number of spaces from the margin that the lines should
159   *                 be indented.
160   */
161  public void toString(StringBuilder buffer, int indent)
162  {
163    StringBuilder indentBuf = new StringBuilder(indent);
164    for (int i=0 ; i < indent; i++)
165    {
166      indentBuf.append(' ');
167    }
168
169    buffer.append(indentBuf).append("LDAP Modification").append(EOL);
170
171    buffer.append(indentBuf);
172    buffer.append("  Modification Type:  ").append(modificationType);
173    buffer.append(" (").append(modificationType.intValue()).append(")");
174    buffer.append(EOL);
175
176    buffer.append("  Attribute:");
177    buffer.append(EOL);
178    attribute.toString(buffer, indent+4);
179  }
180}
181