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    
023    
024    import org.apache.directory.shared.ldap.codec.MessageTypeEnum;
025    import org.apache.directory.shared.ldap.entry.Entry;
026    import org.apache.directory.shared.ldap.entry.client.DefaultClientEntry;
027    import org.apache.directory.shared.ldap.message.internal.InternalAddRequest;
028    import org.apache.directory.shared.ldap.message.internal.InternalAddResponse;
029    import org.apache.directory.shared.ldap.message.internal.InternalResultResponse;
030    import org.apache.directory.shared.ldap.name.DN;
031    
032    
033    /**
034     * Lockable add request implemenation.
035     * 
036     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037     * @version $Rev: 918756 $
038     */
039    public class AddRequestImpl extends AbstractAbandonableRequest implements InternalAddRequest
040    {
041        static final long serialVersionUID = 7534132448349520346L;
042    
043        /** A MultiMap of the new entry's attributes and their values */
044        private Entry entry;
045    
046        private InternalAddResponse response;
047    
048    
049        // ------------------------------------------------------------------------
050        // Constructors
051        // ------------------------------------------------------------------------
052    
053        /**
054         * Creates an AddRequest implementation to create a new entry.
055         * 
056         * @param id
057         *            the sequence identifier of the AddRequest message.
058         */
059        public AddRequestImpl(final int id)
060        {
061            super( id, TYPE );
062            entry = new DefaultClientEntry();
063        }
064    
065    
066        // ------------------------------------------------------------------------
067        // AddRequest Interface Method Implementations
068        // ------------------------------------------------------------------------
069    
070        /**
071         * Gets the distinguished name of the entry to add.
072         * 
073         * @return the Dn of the added entry.
074         */
075        public DN getEntryDn()
076        {
077            return entry.getDn();
078        }
079    
080    
081        /**
082         * Sets the distinguished name of the entry to add.
083         * 
084         * @param entry the Dn of the added entry.
085         */
086        public void setEntryDn( DN dn )
087        {
088            entry.setDn( dn );
089        }
090    
091    
092        /**
093         * Gets the entry to add.
094         * 
095         * @return the added Entry
096         */
097        public Entry getEntry()
098        {
099            return entry;
100        }
101    
102    
103        /**
104         * Sets the Entry to add.
105         * 
106         * @param entry the added Entry
107         */
108        public void setEntry( Entry entry )
109        {
110            this.entry = entry;
111        }
112    
113    
114        // ------------------------------------------------------------------------
115        // SingleReplyRequest Interface Method Implementations
116        // ------------------------------------------------------------------------
117    
118        /**
119         * Gets the protocol response message type for this request which produces
120         * at least one response.
121         * 
122         * @return the message type of the response.
123         */
124        public MessageTypeEnum getResponseType()
125        {
126            return RESP_TYPE;
127        }
128    
129    
130        /**
131         * The result containing response for this request.
132         * 
133         * @return the result containing response for this request
134         */
135        public InternalResultResponse getResultResponse()
136        {
137            if ( response == null )
138            {
139                response = new AddResponseImpl( getMessageId() );
140            }
141    
142            return response;
143        }
144    
145    
146        /**
147         * Checks to see if an object is equivalent to this AddRequest. First
148         * there's a quick test to see if the obj is the same object as this one -
149         * if so true is returned. Next if the super method fails false is returned.
150         * Then the name of the entry is compared - if not the same false is
151         * returned. Lastly the attributes of the entry are compared. If they are
152         * not the same false is returned otherwise the method exists returning
153         * true.
154         * 
155         * @param obj
156         *            the object to test for equality to this
157         * @return true if the obj is equal to this AddRequest, false otherwise
158         */
159        public boolean equals( Object obj )
160        {
161            // Short circuit
162            if ( this == obj )
163            {
164                return true;
165            }
166            
167            // Check the object class. If null, it will exit.
168            if ( !( obj instanceof InternalAddRequest ) )
169            {
170                return false;
171            }
172    
173            if ( !super.equals( obj ) )
174            {
175                return false;
176            }
177    
178            InternalAddRequest req = ( InternalAddRequest ) obj;
179    
180            // Check the entry
181            if ( entry == null )
182            {
183                return ( req.getEntry() == null );
184            }
185            else
186            {
187                return ( entry.equals( req.getEntry() ) );
188            }
189        }
190    
191        /**
192         * @see Object#hashCode()
193         * @return the instance's hash code 
194         */
195        public int hashCode()
196        {
197            int hash = 37;
198            hash = hash*17 + ( entry == null ? 0 : entry.hashCode() );
199            hash = hash*17 + ( response == null ? 0 : response.hashCode() );
200            hash = hash*17 + super.hashCode();
201            
202            return hash;
203        }
204    
205        /**
206         * @see Object#toString()
207         */
208        public String toString()
209        {
210            StringBuilder sb = new StringBuilder();
211    
212            sb.append( "    Add Request :\n" );
213            
214            if ( entry == null )
215            {
216                sb.append( "            No entry\n" );
217            }
218            else
219            {
220                sb.append( entry.toString() );
221            }
222    
223            return sb.toString();
224        }
225    }