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