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;
024
025
026/**
027 * Delete request implementation.
028 * 
029 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
030 */
031public class DeleteRequestImpl extends AbstractAbandonableRequest implements DeleteRequest
032{
033    static final long serialVersionUID = 3187847454305567542L;
034
035    /** The distinguished name of the entry to delete */
036    private Dn name;
037
038    /** The deleteResponse associated with this request */
039    private DeleteResponse response;
040
041
042    // ------------------------------------------------------------------------
043    // Constructors
044    // ------------------------------------------------------------------------
045    /**
046     * Creates a DeleteRequest implementing object used to delete a
047     * leaf entry from the DIT.
048     */
049    public DeleteRequestImpl()
050    {
051        super( -1, MessageTypeEnum.DEL_REQUEST );
052    }
053
054
055    // ------------------------------------------------------------------------
056    // DeleteRequest Interface Method Implementations
057    // ------------------------------------------------------------------------
058
059    /**
060     * Gets the distinguished name of the leaf entry to be deleted by this
061     * request.
062     * 
063     * @return the Dn of the leaf entry to delete.
064     */
065    @Override
066    public Dn getName()
067    {
068        return name;
069    }
070
071
072    /**
073     * {@inheritDoc}
074     */
075    @Override
076    public DeleteRequest setName( Dn name )
077    {
078        this.name = name;
079
080        return this;
081    }
082
083
084    /**
085     * {@inheritDoc}
086     */
087    @Override
088    public DeleteRequest setMessageId( int messageId )
089    {
090        super.setMessageId( messageId );
091
092        return this;
093    }
094
095
096    /**
097     * {@inheritDoc}
098     */
099    @Override
100    public DeleteRequest addControl( Control control )
101    {
102        return ( DeleteRequest ) super.addControl( control );
103    }
104
105
106    /**
107     * {@inheritDoc}
108     */
109    @Override
110    public DeleteRequest addAllControls( Control[] controls )
111    {
112        return ( DeleteRequest ) super.addAllControls( controls );
113    }
114
115
116    /**
117     * {@inheritDoc}
118     */
119    @Override
120    public DeleteRequest removeControl( Control control )
121    {
122        return ( DeleteRequest ) super.removeControl( control );
123    }
124
125
126    // ------------------------------------------------------------------------
127    // SingleReplyRequest Interface Method Implementations
128    // ------------------------------------------------------------------------
129
130    /**
131     * Gets the protocol response message type for this request which produces
132     * at least one response.
133     * 
134     * @return the message type of the response.
135     */
136    @Override
137    public MessageTypeEnum getResponseType()
138    {
139        return MessageTypeEnum.DEL_RESPONSE;
140    }
141
142
143    /**
144     * The result containing response for this request.
145     * 
146     * @return the result containing response for this request
147     */
148    @Override
149    public DeleteResponse getResultResponse()
150    {
151        if ( response == null )
152        {
153            response = new DeleteResponseImpl( getMessageId() );
154        }
155
156        return response;
157    }
158
159
160    /**
161     * {@inheritDoc}
162     */
163    @Override
164    public int hashCode()
165    {
166        int hash = 37;
167
168        if ( name != null )
169        {
170            hash = hash * 17 + name.hashCode();
171        }
172
173        hash = hash * 17 + super.hashCode();
174
175        return hash;
176    }
177
178
179    /**
180     * Checks to see if an object is equivalent to this DeleteRequest. First
181     * there's a quick test to see if the obj is the same object as this one -
182     * if so true is returned. Next if the super method fails false is returned.
183     * Then the name of the entry is compared - if not the same false is
184     * returned. Finally the method exists returning true.
185     * 
186     * @param obj the object to test for equality to this
187     * @return true if the obj is equal to this DeleteRequest, false otherwise
188     */
189    @Override
190    public boolean equals( Object obj )
191    {
192        if ( this == obj )
193        {
194            return true;
195        }
196
197        if ( !super.equals( obj ) )
198        {
199            return false;
200        }
201
202        DeleteRequest req = ( DeleteRequest ) obj;
203
204        if ( name != null && req.getName() == null )
205        {
206            return false;
207        }
208
209        if ( name == null && req.getName() != null )
210        {
211            return false;
212        }
213
214        if ( ( name != null ) && ( req.getName() != null ) && !name.equals( req.getName() ) )
215        {
216            return false;
217        }
218
219        return true;
220    }
221
222
223    /**
224     * Return a String representing a DelRequest
225     * 
226     * @return A DelRequest String
227     */
228    @Override
229    public String toString()
230    {
231
232        StringBuilder sb = new StringBuilder();
233
234        sb.append( "    Del request\n" );
235        sb.append( "        Entry : '" ).append( name.toString() ).append( "'\n" );
236        sb.append( super.toString() );
237
238        return super.toString( sb.toString() );
239    }
240}