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