001    /*
002     * Copyright 2007-2013 UnboundID Corp.
003     * All Rights Reserved.
004     */
005    /*
006     * Copyright (C) 2008-2013 UnboundID Corp.
007     *
008     * This program is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU General Public License (GPLv2 only)
010     * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011     * as published by the Free Software Foundation.
012     *
013     * This program is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program; if not, see <http://www.gnu.org/licenses>.
020     */
021    package com.unboundid.ldif;
022    
023    
024    
025    import java.util.Arrays;
026    import java.util.List;
027    
028    import com.unboundid.asn1.ASN1OctetString;
029    import com.unboundid.ldap.sdk.ChangeType;
030    import com.unboundid.ldap.sdk.DeleteRequest;
031    import com.unboundid.ldap.sdk.LDAPException;
032    import com.unboundid.ldap.sdk.LDAPInterface;
033    import com.unboundid.ldap.sdk.LDAPResult;
034    import com.unboundid.util.ByteStringBuffer;
035    import com.unboundid.util.NotMutable;
036    import com.unboundid.util.ThreadSafety;
037    import com.unboundid.util.ThreadSafetyLevel;
038    
039    import static com.unboundid.util.Debug.*;
040    import static com.unboundid.util.StaticUtils.*;
041    
042    
043    
044    /**
045     * This class defines an LDIF delete change record, which can be used to
046     * represent an LDAP delete request.  See the documentation for the
047     * {@link LDIFChangeRecord} class for an example demonstrating the process for
048     * interacting with LDIF change records.
049     */
050    @NotMutable()
051    @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
052    public final class LDIFDeleteChangeRecord
053           extends LDIFChangeRecord
054    {
055      /**
056       * The serial version UID for this serializable class.
057       */
058      private static final long serialVersionUID = 486284031156138191L;
059    
060    
061    
062      /**
063       * Creates a new LDIF delete change record with the provided DN.
064       *
065       * @param  dn  The DN of the entry to delete.  It must not be {@code null}.
066       */
067      public LDIFDeleteChangeRecord(final String dn)
068      {
069        super(dn);
070      }
071    
072    
073    
074      /**
075       * Creates a new LDIF delete change record from the provided delete request.
076       *
077       * @param  deleteRequest  The delete request to use to create this LDIF delete
078       *                        change record.  It must not be {@code null}.
079       */
080      public LDIFDeleteChangeRecord(final DeleteRequest deleteRequest)
081      {
082        super(deleteRequest.getDN());
083      }
084    
085    
086    
087      /**
088       * Creates a delete request from this LDIF delete change record.
089       *
090       * @return  The delete request created from this LDIF delete change record.
091       */
092      public DeleteRequest toDeleteRequest()
093      {
094        return new DeleteRequest(getDN());
095      }
096    
097    
098    
099      /**
100       * {@inheritDoc}
101       */
102      @Override()
103      public ChangeType getChangeType()
104      {
105        return ChangeType.DELETE;
106      }
107    
108    
109    
110      /**
111       * {@inheritDoc}
112       */
113      @Override()
114      public LDAPResult processChange(final LDAPInterface connection)
115             throws LDAPException
116      {
117        return connection.delete(toDeleteRequest());
118      }
119    
120    
121    
122      /**
123       * {@inheritDoc}
124       */
125      @Override()
126      public String[] toLDIF(final int wrapColumn)
127      {
128        if (wrapColumn > 0)
129        {
130          List<String> ldifLines = Arrays.asList(
131               LDIFWriter.encodeNameAndValue("dn", new ASN1OctetString(getDN())),
132               "changetype: delete");
133    
134          ldifLines = LDIFWriter.wrapLines(wrapColumn, ldifLines);
135    
136          final String[] lineArray = new String[ldifLines.size()];
137          return ldifLines.toArray(lineArray);
138        }
139        else
140        {
141          return new String[]
142          {
143            LDIFWriter.encodeNameAndValue("dn", new ASN1OctetString(getDN())),
144            "changetype: delete"
145          };
146        }
147      }
148    
149    
150    
151      /**
152       * {@inheritDoc}
153       */
154      @Override()
155      public void toLDIF(final ByteStringBuffer buffer, final int wrapColumn)
156      {
157        LDIFWriter.encodeNameAndValue("dn", new ASN1OctetString(getDN()), buffer,
158                                      wrapColumn);
159        buffer.append(EOL_BYTES);
160        LDIFWriter.encodeNameAndValue("changetype", new ASN1OctetString("delete"),
161                                      buffer, wrapColumn);
162        buffer.append(EOL_BYTES);
163      }
164    
165    
166    
167      /**
168       * {@inheritDoc}
169       */
170      @Override()
171      public void toLDIFString(final StringBuilder buffer, final int wrapColumn)
172      {
173        LDIFWriter.encodeNameAndValue("dn", new ASN1OctetString(getDN()), buffer,
174                                      wrapColumn);
175        buffer.append(EOL);
176        LDIFWriter.encodeNameAndValue("changetype", new ASN1OctetString("delete"),
177                                      buffer, wrapColumn);
178        buffer.append(EOL);
179      }
180    
181    
182    
183      /**
184       * {@inheritDoc}
185       */
186      @Override()
187      public int hashCode()
188      {
189        try
190        {
191          return getParsedDN().hashCode();
192        }
193        catch (Exception e)
194        {
195          debugException(e);
196          return toLowerCase(getDN()).hashCode();
197        }
198      }
199    
200    
201    
202      /**
203       * {@inheritDoc}
204       */
205      @Override()
206      public boolean equals(final Object o)
207      {
208        if (o == null)
209        {
210          return false;
211        }
212    
213        if (o == this)
214        {
215          return true;
216        }
217    
218        if (! (o instanceof LDIFDeleteChangeRecord))
219        {
220          return false;
221        }
222    
223        final LDIFDeleteChangeRecord r = (LDIFDeleteChangeRecord) o;
224    
225        try
226        {
227          return getParsedDN().equals(r.getParsedDN());
228        }
229        catch (Exception e)
230        {
231          debugException(e);
232          return toLowerCase(getDN()).equals(toLowerCase(r.getDN()));
233        }
234      }
235    
236    
237    
238      /**
239       * {@inheritDoc}
240       */
241      @Override()
242      public void toString(final StringBuilder buffer)
243      {
244        buffer.append("LDIFDeleteChangeRecord(dn='");
245        buffer.append(getDN());
246        buffer.append("')");
247      }
248    }