001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.shared.ldap.message;
021
022 import org.apache.directory.shared.ldap.name.LdapDN;
023 import org.apache.directory.shared.ldap.name.Rdn;
024
025
026 /**
027 * Lockable ModifyDNRequest implementation.
028 *
029 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
030 * @version $Rev: 764131 $
031 */
032 public class ModifyDnRequestImpl extends AbstractAbandonableRequest implements InternalModifyDnRequest
033 {
034 static final long serialVersionUID = 1233507339633051696L;
035
036 /** PDU's modify Dn candidate <b>entry</b> distinguished name property */
037 private LdapDN name;
038
039 /** PDU's <b>newrdn</b> relative distinguished name property */
040 private Rdn newRdn;
041
042 /** PDU's <b>newSuperior</b> distinguished name property */
043 private LdapDN newSuperior;
044
045 /** PDU's <b>deleteOldRdn</b> flag */
046 private boolean deleteOldRdn = false;
047
048 private InternalModifyDnResponse response;
049
050
051 // -----------------------------------------------------------------------
052 // Constructors
053 // -----------------------------------------------------------------------
054
055 /**
056 * Creates a Lockable ModifyDnRequest implementing object used to perform a
057 * dn change on an entry potentially resulting in an entry move.
058 *
059 * @param id
060 * the seq id of this message
061 */
062 public ModifyDnRequestImpl(final int id)
063 {
064 super( id, TYPE );
065 }
066
067
068 // -----------------------------------------------------------------------
069 // ModifyDnRequest Interface Method Implementations
070 // -----------------------------------------------------------------------
071
072 /**
073 * Gets the flag which determines if the old Rdn attribute is to be removed
074 * from the entry when the new Rdn is used in its stead. This property
075 * corresponds to the <b>deleteoldrdn
076 * </p>
077 * PDU field.
078 *
079 * @return true if the old rdn is to be deleted, false if it is not
080 */
081 public boolean getDeleteOldRdn()
082 {
083 return deleteOldRdn;
084 }
085
086
087 /**
088 * Sets the flag which determines if the old Rdn attribute is to be removed
089 * from the entry when the new Rdn is used in its stead. This property
090 * corresponds to the <b>deleteoldrdn
091 * </p>
092 * PDU field.
093 *
094 * @param deleteOldRdn
095 * true if the old rdn is to be deleted, false if it is not
096 */
097 public void setDeleteOldRdn( boolean deleteOldRdn )
098 {
099 this.deleteOldRdn = deleteOldRdn;
100 }
101
102
103 /**
104 * Gets whether or not this request is a DN change resulting in a move
105 * operation. Setting the newSuperior property to a non-null name, toggles
106 * this flag.
107 *
108 * @return true if the newSuperior property is <b>NOT</b> null, false
109 * otherwise.
110 */
111 public boolean isMove()
112 {
113 return newSuperior != null;
114 }
115
116
117 /**
118 * Gets the entry's distinguished name representing the <b>entry</b> PDU
119 * field.
120 *
121 * @return the distinguished name of the entry.
122 */
123 public LdapDN getName()
124 {
125 return name;
126 }
127
128
129 /**
130 * Sets the entry's distinguished name representing the <b>entry</b> PDU
131 * field.
132 *
133 * @param name
134 * the distinguished name of the entry.
135 */
136 public void setName( LdapDN name )
137 {
138 this.name = name;
139 }
140
141
142 /**
143 * Gets the new relative distinguished name for the entry which represents
144 * the PDU's <b>newrdn</b> field.
145 *
146 * @return the relative dn with one component
147 */
148 public Rdn getNewRdn()
149 {
150 return newRdn;
151 }
152
153
154 /**
155 * Sets the new relative distinguished name for the entry which represents
156 * the PDU's <b>newrdn</b> field.
157 *
158 * @param newRdn
159 * the relative dn with one component
160 */
161 public void setNewRdn( Rdn newRdn )
162 {
163 this.newRdn = newRdn;
164 }
165
166
167 /**
168 * Gets the optional distinguished name of the new superior entry where the
169 * candidate entry is to be moved. This property corresponds to the PDU's
170 * <b>newSuperior</b> field. May be null representing a simple Rdn change
171 * rather than a move operation.
172 *
173 * @return the dn of the superior entry the candidate entry is moved under.
174 */
175 public LdapDN getNewSuperior()
176 {
177 return newSuperior;
178 }
179
180
181 /**
182 * Sets the optional distinguished name of the new superior entry where the
183 * candidate entry is to be moved. This property corresponds to the PDU's
184 * <b>newSuperior</b> field. May be null representing a simple Rdn change
185 * rather than a move operation. Setting this property to a non-null value
186 * toggles the move flag obtained via the <code>isMove</code> method.
187 *
188 * @param newSuperior
189 * the dn of the superior entry the candidate entry for DN
190 * modification is moved under.
191 */
192 public void setNewSuperior( LdapDN newSuperior )
193 {
194 this.newSuperior = newSuperior;
195 }
196
197
198 // ------------------------------------------------------------------------
199 // SingleReplyRequest Interface Method Implementations
200 // ------------------------------------------------------------------------
201
202 /**
203 * Gets the protocol response message type for this request which produces
204 * at least one response.
205 *
206 * @return the message type of the response.
207 */
208 public MessageTypeEnum getResponseType()
209 {
210 return RESP_TYPE;
211 }
212
213
214 /**
215 * The result containing response for this request.
216 *
217 * @return the result containing response for this request
218 */
219 public InternalResultResponse getResultResponse()
220 {
221 if ( response == null )
222 {
223 response = new ModifyDnResponseImpl( getMessageId() );
224 }
225
226 return response;
227 }
228
229
230 /**
231 * Checks to see of an object equals this ModifyDnRequest stub. The equality
232 * presumes all ModifyDnRequest specific properties are the same.
233 *
234 * @param obj
235 * the object to compare with this stub
236 * @return true if the obj is equal to this stub, false otherwise
237 */
238 public boolean equals( Object obj )
239 {
240 if ( obj == this )
241 {
242 return true;
243 }
244
245 if ( !super.equals( obj ) )
246 {
247 return false;
248 }
249
250 InternalModifyDnRequest req = ( InternalModifyDnRequest ) obj;
251
252 if ( name != null && req.getName() == null )
253 {
254 return false;
255 }
256
257 if ( name == null && req.getName() != null )
258 {
259 return false;
260 }
261
262 if ( name != null && req.getName() != null )
263 {
264 if ( !name.equals( req.getName() ) )
265 {
266 return false;
267 }
268 }
269
270 if ( deleteOldRdn != req.getDeleteOldRdn() )
271 {
272 return false;
273 }
274
275 if ( newRdn != null && req.getNewRdn() == null )
276 {
277 return false;
278 }
279
280 if ( newRdn == null && req.getNewRdn() != null )
281 {
282 return false;
283 }
284
285 if ( newRdn != null && req.getNewRdn() != null )
286 {
287 if ( !newRdn.equals( req.getNewRdn() ) )
288 {
289 return false;
290 }
291 }
292
293 if ( newSuperior != null && req.getNewSuperior() == null )
294 {
295 return false;
296 }
297
298 if ( newSuperior == null && req.getNewSuperior() != null )
299 {
300 return false;
301 }
302
303 if ( newSuperior != null && req.getNewSuperior() != null )
304 {
305 if ( !newSuperior.equals( req.getNewSuperior() ) )
306 {
307 return false;
308 }
309 }
310
311 return true;
312 }
313
314
315 /**
316 * Get a String representation of a ModifyDNRequest
317 *
318 * @return A ModifyDNRequest String
319 */
320 public String toString()
321 {
322
323 StringBuffer sb = new StringBuffer();
324
325 sb.append( " ModifyDN Response\n" );
326 sb.append( " Entry : '" ).append( name ).append( "'\n" );
327 sb.append( " New RDN : '" ).append( newRdn.toString() ).append( "'\n" );
328 sb.append( " Delete old RDN : " ).append( deleteOldRdn ).append( "\n" );
329
330 if ( newSuperior != null )
331 {
332 sb.append( " New superior : '" ).append( newSuperior.toString() ).append( "'\n" );
333 }
334
335 return sb.toString();
336 }
337 }