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