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 */
020package org.apache.directory.api.ldap.model.message;
021
022
023import org.apache.directory.api.ldap.model.name.Dn;
024import org.apache.directory.api.ldap.model.name.Rdn;
025
026
027/**
028 * ModifyDNRequest implementation.
029 * 
030 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
031 */
032public class ModifyDnRequestImpl extends AbstractAbandonableRequest implements ModifyDnRequest
033{
034    static final long serialVersionUID = 1233507339633051696L;
035
036    /** PDU's modify Dn candidate <b>entry</b> distinguished name property */
037    private Dn 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 Dn newSuperior;
044
045    /** PDU's <b>deleteOldRdn</b> flag */
046    private boolean deleteOldRdn = false;
047
048    /** The associated response */
049    private ModifyDnResponse response;
050
051
052    // -----------------------------------------------------------------------
053    // Constructors
054    // -----------------------------------------------------------------------
055    /**
056     * Creates a ModifyDnRequest implementing object used to perform a
057     * dn change on an entry potentially resulting in an entry move.
058     */
059    public ModifyDnRequestImpl()
060    {
061        super( -1, MessageTypeEnum.MODIFYDN_REQUEST );
062    }
063
064
065    // -----------------------------------------------------------------------
066    // ModifyDnRequest Interface Method Implementations
067    // -----------------------------------------------------------------------
068
069    /**
070     * {@inheritDoc}
071     */
072    @Override
073    public boolean getDeleteOldRdn()
074    {
075        return deleteOldRdn;
076    }
077
078
079    /**
080     * {@inheritDoc}
081     */
082    @Override
083    public ModifyDnRequest setDeleteOldRdn( boolean deleteOldRdn )
084    {
085        this.deleteOldRdn = deleteOldRdn;
086
087        return this;
088    }
089
090
091    /**
092     * {@inheritDoc}
093     */
094    @Override
095    public boolean isMove()
096    {
097        return newSuperior != null;
098    }
099
100
101    /**
102     * {@inheritDoc}
103     */
104    @Override
105    public Dn getName()
106    {
107        return name;
108    }
109
110
111    /**
112     * {@inheritDoc}
113     */
114    @Override
115    public ModifyDnRequest setName( Dn name )
116    {
117        this.name = name;
118
119        return this;
120    }
121
122
123    /**
124     * {@inheritDoc}
125     */
126    @Override
127    public Rdn getNewRdn()
128    {
129        return newRdn;
130    }
131
132
133    /**
134     * {@inheritDoc}
135     */
136    @Override
137    public ModifyDnRequest setNewRdn( Rdn newRdn )
138    {
139        this.newRdn = newRdn;
140
141        return this;
142    }
143
144
145    /**
146     * {@inheritDoc}
147     */
148    @Override
149    public Dn getNewSuperior()
150    {
151        return newSuperior;
152    }
153
154
155    /**
156     * {@inheritDoc}
157     */
158    @Override
159    public ModifyDnRequest setNewSuperior( Dn newSuperior )
160    {
161        this.newSuperior = newSuperior;
162
163        return this;
164    }
165
166
167    /**
168     * {@inheritDoc}
169     */
170    @Override
171    public ModifyDnRequest setMessageId( int messageId )
172    {
173        super.setMessageId( messageId );
174
175        return this;
176    }
177
178
179    /**
180     * {@inheritDoc}
181     */
182    @Override
183    public ModifyDnRequest addControl( Control control )
184    {
185        return ( ModifyDnRequest ) super.addControl( control );
186    }
187
188
189    /**
190     * {@inheritDoc}
191     */
192    @Override
193    public ModifyDnRequest addAllControls( Control[] controls )
194    {
195        return ( ModifyDnRequest ) super.addAllControls( controls );
196    }
197
198
199    /**
200     * {@inheritDoc}
201     */
202    @Override
203    public ModifyDnRequest removeControl( Control control )
204    {
205        return ( ModifyDnRequest ) super.removeControl( control );
206    }
207
208
209    // ------------------------------------------------------------------------
210    // SingleReplyRequest Interface Method Implementations
211    // ------------------------------------------------------------------------
212
213    /**
214     * Gets the protocol response message type for this request which produces
215     * at least one response.
216     * 
217     * @return the message type of the response.
218     */
219    @Override
220    public MessageTypeEnum getResponseType()
221    {
222        return MessageTypeEnum.MODIFYDN_RESPONSE;
223    }
224
225
226    /**
227     * The result containing response for this request.
228     * 
229     * @return the result containing response for this request
230     */
231    @Override
232    public ModifyDnResponse getResultResponse()
233    {
234        if ( response == null )
235        {
236            response = new ModifyDnResponseImpl( getMessageId() );
237        }
238
239        return response;
240    }
241
242
243    /**
244     * {@inheritDoc}
245     */
246    @Override
247    public int hashCode()
248    {
249        int hash = 37;
250        if ( name != null )
251        {
252            hash = hash * 17 + name.hashCode();
253        }
254        hash = hash * 17 + ( deleteOldRdn ? 0 : 1 );
255
256        if ( newRdn != null )
257        {
258            hash = hash * 17 + newRdn.hashCode();
259        }
260        if ( newSuperior != null )
261        {
262            hash = hash * 17 + newSuperior.hashCode();
263        }
264        hash = hash * 17 + super.hashCode();
265
266        return hash;
267    }
268
269
270    /**
271     * Checks to see of an object equals this ModifyDnRequest stub. The equality
272     * presumes all ModifyDnRequest specific properties are the same.
273     * 
274     * @param obj the object to compare with this stub
275     * @return true if the obj is equal to this stub, false otherwise
276     */
277    @Override
278    public boolean equals( Object obj )
279    {
280        if ( obj == this )
281        {
282            return true;
283        }
284
285        if ( !super.equals( obj ) )
286        {
287            return false;
288        }
289
290        ModifyDnRequest req = ( ModifyDnRequest ) obj;
291
292        if ( name != null && req.getName() == null )
293        {
294            return false;
295        }
296
297        if ( name == null && req.getName() != null )
298        {
299            return false;
300        }
301
302        if ( name != null && req.getName() != null && !name.equals( req.getName() ) )
303        {
304            return false;
305        }
306
307        if ( deleteOldRdn != req.getDeleteOldRdn() )
308        {
309            return false;
310        }
311
312        if ( newRdn != null && req.getNewRdn() == null )
313        {
314            return false;
315        }
316
317        if ( newRdn == null && req.getNewRdn() != null )
318        {
319            return false;
320        }
321
322        if ( newRdn != null && req.getNewRdn() != null && !newRdn.equals( req.getNewRdn() ) )
323        {
324            return false;
325        }
326
327        if ( newSuperior != null && req.getNewSuperior() == null )
328        {
329            return false;
330        }
331
332        if ( newSuperior == null && req.getNewSuperior() != null )
333        {
334            return false;
335        }
336
337        return ( newSuperior == null ) || ( req.getNewSuperior() == null ) || newSuperior.equals( req
338            .getNewSuperior() );
339    }
340
341
342    /**
343     * Get a String representation of a ModifyDNRequest
344     * 
345     * @return A ModifyDNRequest String
346     */
347    @Override
348    public String toString()
349    {
350
351        StringBuilder sb = new StringBuilder();
352
353        sb.append( "    ModifyDN Response\n" );
354        sb.append( "        Entry : '" ).append( name ).append( "'\n" );
355        if ( newRdn != null )
356        {
357            sb.append( "        New Rdn : '" ).append( newRdn.toString() ).append( "'\n" );
358        }
359        sb.append( "        Delete old Rdn : " ).append( deleteOldRdn ).append( "\n" );
360
361        if ( newSuperior != null )
362        {
363            sb.append( "        New superior : '" ).append( newSuperior.toString() ).append( "'\n" );
364        }
365
366        // The controls
367        sb.append( super.toString() );
368
369        return super.toString( sb.toString() );
370    }
371}