001 /*
002 * Copyright 2007-2016 UnboundID Corp.
003 * All Rights Reserved.
004 */
005 /*
006 * Copyright (C) 2008-2016 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.ArrayList;
026 import java.util.HashSet;
027 import java.util.Iterator;
028 import java.util.List;
029
030 import com.unboundid.asn1.ASN1OctetString;
031 import com.unboundid.ldap.sdk.ChangeType;
032 import com.unboundid.ldap.sdk.Control;
033 import com.unboundid.ldap.sdk.DeleteRequest;
034 import com.unboundid.ldap.sdk.LDAPException;
035 import com.unboundid.ldap.sdk.LDAPInterface;
036 import com.unboundid.ldap.sdk.LDAPResult;
037 import com.unboundid.util.ByteStringBuffer;
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 * {@code LDIFChangeRecord} class for an example demonstrating the process for
048 * interacting with LDIF change records.
049 */
050 public final class LDIFDeleteChangeRecord
051 extends LDIFChangeRecord
052 {
053 /**
054 * The serial version UID for this serializable class.
055 */
056 private static final long serialVersionUID = 9173178539060889790L;
057
058
059
060 /**
061 * Creates a new LDIF delete change record with the provided DN.
062 *
063 * @param dn The DN of the entry to delete. It must not be {@code null}.
064 */
065 public LDIFDeleteChangeRecord(final String dn)
066 {
067 this(dn, null);
068 }
069
070
071
072 /**
073 * Creates a new LDIF delete change record with the provided DN.
074 *
075 * @param dn The DN of the entry to delete. It must not be
076 * {@code null}.
077 * @param controls The set of controls for this LDIF delete change record.
078 * It may be {@code null} or empty if there are no controls.
079 */
080 public LDIFDeleteChangeRecord(final String dn, final List<Control> controls)
081 {
082 super(dn, controls);
083 }
084
085
086
087 /**
088 * Creates a new LDIF delete change record from the provided delete request.
089 *
090 * @param deleteRequest The delete request to use to create this LDIF delete
091 * change record. It must not be {@code null}.
092 */
093 public LDIFDeleteChangeRecord(final DeleteRequest deleteRequest)
094 {
095 super(deleteRequest.getDN(), deleteRequest.getControlList());
096 }
097
098
099
100 /**
101 * Creates a delete request from this LDIF delete change record. Any change
102 * record controls will be included in the request
103 *
104 * @return The delete request created from this LDIF delete change record.
105 */
106 public DeleteRequest toDeleteRequest()
107 {
108 return toDeleteRequest(true);
109 }
110
111
112
113 /**
114 * Creates a delete request from this LDIF delete change record, optionally
115 * including any change record controls in the request.
116 *
117 * @param includeControls Indicates whether to include any controls in the
118 * request.
119 *
120 * @return The delete request created from this LDIF delete change record.
121 */
122 public DeleteRequest toDeleteRequest(final boolean includeControls)
123 {
124 final DeleteRequest deleteRequest = new DeleteRequest(getDN());
125 if (includeControls)
126 {
127 deleteRequest.setControls(getControls());
128 }
129
130 return deleteRequest;
131 }
132
133
134
135 /**
136 * {@inheritDoc}
137 */
138 @Override()
139 public ChangeType getChangeType()
140 {
141 return ChangeType.DELETE;
142 }
143
144
145
146 /**
147 * {@inheritDoc}
148 */
149 @Override()
150 public LDAPResult processChange(final LDAPInterface connection,
151 final boolean includeControls)
152 throws LDAPException
153 {
154 return connection.delete(toDeleteRequest(includeControls));
155 }
156
157
158
159 /**
160 * {@inheritDoc}
161 */
162 @Override()
163 public String[] toLDIF(final int wrapColumn)
164 {
165 List<String> ldifLines = new ArrayList<String>(5);
166 encodeNameAndValue("dn", new ASN1OctetString(getDN()), ldifLines);
167
168 for (final Control c : getControls())
169 {
170 encodeNameAndValue("control", encodeControlString(c), ldifLines);
171 }
172
173 ldifLines.add("changetype: delete");
174
175 if (wrapColumn > 2)
176 {
177 ldifLines = LDIFWriter.wrapLines(wrapColumn, ldifLines);
178 }
179
180 final String[] ldifArray = new String[ldifLines.size()];
181 ldifLines.toArray(ldifArray);
182 return ldifArray;
183 }
184
185
186
187 /**
188 * {@inheritDoc}
189 */
190 @Override()
191 public void toLDIF(final ByteStringBuffer buffer, final int wrapColumn)
192 {
193 LDIFWriter.encodeNameAndValue("dn", new ASN1OctetString(getDN()), buffer,
194 wrapColumn);
195 buffer.append(EOL_BYTES);
196
197 for (final Control c : getControls())
198 {
199 LDIFWriter.encodeNameAndValue("control", encodeControlString(c), buffer,
200 wrapColumn);
201 buffer.append(EOL_BYTES);
202 }
203
204 LDIFWriter.encodeNameAndValue("changetype", new ASN1OctetString("delete"),
205 buffer, wrapColumn);
206 buffer.append(EOL_BYTES);
207 }
208
209
210
211 /**
212 * {@inheritDoc}
213 */
214 @Override()
215 public void toLDIFString(final StringBuilder buffer, final int wrapColumn)
216 {
217 LDIFWriter.encodeNameAndValue("dn", new ASN1OctetString(getDN()), buffer,
218 wrapColumn);
219 buffer.append(EOL);
220
221 for (final Control c : getControls())
222 {
223 LDIFWriter.encodeNameAndValue("control", encodeControlString(c), buffer,
224 wrapColumn);
225 buffer.append(EOL);
226 }
227
228 LDIFWriter.encodeNameAndValue("changetype", new ASN1OctetString("delete"),
229 buffer, wrapColumn);
230 buffer.append(EOL);
231 }
232
233
234
235 /**
236 * {@inheritDoc}
237 */
238 @Override()
239 public int hashCode()
240 {
241 try
242 {
243 return getParsedDN().hashCode();
244 }
245 catch (Exception e)
246 {
247 debugException(e);
248 return toLowerCase(getDN()).hashCode();
249 }
250 }
251
252
253
254 /**
255 * {@inheritDoc}
256 */
257 @Override()
258 public boolean equals(final Object o)
259 {
260 if (o == null)
261 {
262 return false;
263 }
264
265 if (o == this)
266 {
267 return true;
268 }
269
270 if (! (o instanceof LDIFDeleteChangeRecord))
271 {
272 return false;
273 }
274
275 final LDIFDeleteChangeRecord r = (LDIFDeleteChangeRecord) o;
276
277 final HashSet<Control> c1 = new HashSet<Control>(getControls());
278 final HashSet<Control> c2 = new HashSet<Control>(r.getControls());
279 if (! c1.equals(c2))
280 {
281 return false;
282 }
283
284 try
285 {
286 return getParsedDN().equals(r.getParsedDN());
287 }
288 catch (Exception e)
289 {
290 debugException(e);
291 return toLowerCase(getDN()).equals(toLowerCase(r.getDN()));
292 }
293 }
294
295
296
297 /**
298 * {@inheritDoc}
299 */
300 @Override()
301 public void toString(final StringBuilder buffer)
302 {
303 buffer.append("LDIFDeleteChangeRecord(dn='");
304 buffer.append(getDN());
305 buffer.append('\'');
306
307 final List<Control> controls = getControls();
308 if (! controls.isEmpty())
309 {
310 buffer.append(", controls={");
311
312 final Iterator<Control> iterator = controls.iterator();
313 while (iterator.hasNext())
314 {
315 iterator.next().toString(buffer);
316 if (iterator.hasNext())
317 {
318 buffer.append(',');
319 }
320 }
321
322 buffer.append('}');
323 }
324
325 buffer.append(')');
326 }
327 }