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 ldifLines.add(LDIFWriter.encodeNameAndValue("dn", 167 new ASN1OctetString(getDN()))); 168 169 for (final Control c : getControls()) 170 { 171 ldifLines.add(LDIFWriter.encodeNameAndValue("control", 172 encodeControlString(c))); 173 } 174 175 ldifLines.add("changetype: delete"); 176 177 if (wrapColumn > 2) 178 { 179 ldifLines = LDIFWriter.wrapLines(wrapColumn, ldifLines); 180 } 181 182 final String[] ldifArray = new String[ldifLines.size()]; 183 ldifLines.toArray(ldifArray); 184 return ldifArray; 185 } 186 187 188 189 /** 190 * {@inheritDoc} 191 */ 192 @Override() 193 public void toLDIF(final ByteStringBuffer buffer, final int wrapColumn) 194 { 195 LDIFWriter.encodeNameAndValue("dn", new ASN1OctetString(getDN()), buffer, 196 wrapColumn); 197 buffer.append(EOL_BYTES); 198 199 for (final Control c : getControls()) 200 { 201 LDIFWriter.encodeNameAndValue("control", encodeControlString(c), buffer, 202 wrapColumn); 203 buffer.append(EOL_BYTES); 204 } 205 206 LDIFWriter.encodeNameAndValue("changetype", new ASN1OctetString("delete"), 207 buffer, wrapColumn); 208 buffer.append(EOL_BYTES); 209 } 210 211 212 213 /** 214 * {@inheritDoc} 215 */ 216 @Override() 217 public void toLDIFString(final StringBuilder buffer, final int wrapColumn) 218 { 219 LDIFWriter.encodeNameAndValue("dn", new ASN1OctetString(getDN()), buffer, 220 wrapColumn); 221 buffer.append(EOL); 222 223 for (final Control c : getControls()) 224 { 225 LDIFWriter.encodeNameAndValue("control", encodeControlString(c), buffer, 226 wrapColumn); 227 buffer.append(EOL); 228 } 229 230 LDIFWriter.encodeNameAndValue("changetype", new ASN1OctetString("delete"), 231 buffer, wrapColumn); 232 buffer.append(EOL); 233 } 234 235 236 237 /** 238 * {@inheritDoc} 239 */ 240 @Override() 241 public int hashCode() 242 { 243 try 244 { 245 return getParsedDN().hashCode(); 246 } 247 catch (Exception e) 248 { 249 debugException(e); 250 return toLowerCase(getDN()).hashCode(); 251 } 252 } 253 254 255 256 /** 257 * {@inheritDoc} 258 */ 259 @Override() 260 public boolean equals(final Object o) 261 { 262 if (o == null) 263 { 264 return false; 265 } 266 267 if (o == this) 268 { 269 return true; 270 } 271 272 if (! (o instanceof LDIFDeleteChangeRecord)) 273 { 274 return false; 275 } 276 277 final LDIFDeleteChangeRecord r = (LDIFDeleteChangeRecord) o; 278 279 final HashSet<Control> c1 = new HashSet<Control>(getControls()); 280 final HashSet<Control> c2 = new HashSet<Control>(r.getControls()); 281 if (! c1.equals(c2)) 282 { 283 return false; 284 } 285 286 try 287 { 288 return getParsedDN().equals(r.getParsedDN()); 289 } 290 catch (Exception e) 291 { 292 debugException(e); 293 return toLowerCase(getDN()).equals(toLowerCase(r.getDN())); 294 } 295 } 296 297 298 299 /** 300 * {@inheritDoc} 301 */ 302 @Override() 303 public void toString(final StringBuilder buffer) 304 { 305 buffer.append("LDIFDeleteChangeRecord(dn='"); 306 buffer.append(getDN()); 307 buffer.append('\''); 308 309 final List<Control> controls = getControls(); 310 if (! controls.isEmpty()) 311 { 312 buffer.append(", controls={"); 313 314 final Iterator<Control> iterator = controls.iterator(); 315 while (iterator.hasNext()) 316 { 317 iterator.next().toString(buffer); 318 if (iterator.hasNext()) 319 { 320 buffer.append(','); 321 } 322 } 323 324 buffer.append('}'); 325 } 326 327 buffer.append(')'); 328 } 329 }