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    import java.util.ArrayList;
024    import java.util.Collection;
025    import java.util.Collections;
026    import java.util.Iterator;
027    import java.util.List;
028    
029    import org.apache.directory.shared.ldap.entry.Modification;
030    import org.apache.directory.shared.ldap.entry.client.ClientModification;
031    import org.apache.directory.shared.ldap.name.LdapDN;
032    import org.slf4j.Logger;
033    import org.slf4j.LoggerFactory;
034    
035    
036    /**
037     * Lockable ModifyRequest implementation.
038     * 
039     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040     * @version $Rev: 764131 $
041     */
042    public class ModifyRequestImpl extends AbstractAbandonableRequest implements InternalModifyRequest
043    {
044        static final long serialVersionUID = -505803669028990304L;
045    
046        /** The logger */
047        private static final transient Logger LOG = LoggerFactory.getLogger( ModifyRequestImpl.class );
048    
049        /** Dn of the entry to modify or PDU's <b>object</b> field */
050        private LdapDN name;
051    
052        /** Sequence of modifications or PDU's <b>modification</b> seqence field */
053        private List<Modification> mods = new ArrayList<Modification>();
054    
055        private InternalModifyResponse response;
056    
057    
058        // ------------------------------------------------------------------------
059        // Constructors
060        // ------------------------------------------------------------------------
061    
062        /**
063         * Creates a Lockable ModifyRequest implementing object used to modify the
064         * attributes of an entry.
065         * 
066         * @param id
067         *            the sequential message identifier
068         */
069        public ModifyRequestImpl(final int id)
070        {
071            super( id, TYPE );
072        }
073    
074    
075        // ------------------------------------------------------------------------
076        // ModifyRequest Interface Method Implementations
077        // ------------------------------------------------------------------------
078        /**
079         * Gets an immutable Collection of modification items representing the
080         * atomic changes to perform on the candidate entry to modify.
081         * 
082         * @return an immutable Collection of Modification instances.
083         */
084        public Collection<Modification> getModificationItems()
085        {
086            return Collections.unmodifiableCollection( mods );
087        }
088    
089    
090        /**
091         * Gets the distinguished name of the entry to be modified by this request.
092         * This property represents the PDU's <b>object</b> field.
093         * 
094         * @return the DN of the modified entry.
095         */
096        public LdapDN getName()
097        {
098            return name;
099        }
100    
101    
102        /**
103         * Sets the distinguished name of the entry to be modified by this request.
104         * This property represents the PDU's <b>object</b> field.
105         * 
106         * @param name
107         *            the DN of the modified entry.
108         */
109        public void setName( LdapDN name )
110        {
111            this.name = name;
112        }
113    
114    
115        /**
116         * Adds a Modification to the set of modifications composing this modify
117         * request.
118         * 
119         * @param mod a Modification to add
120         */
121        public void addModification( Modification mod )
122        {
123            mods.add( mod );
124        }
125    
126    
127        /**
128         * Removes a Modification to the set of modifications composing this
129         * modify request.
130         * 
131         * @param mod a Modification to remove.
132         */
133        public void removeModification( Modification mod )
134        {
135            mods.remove( mod );
136        }
137    
138    
139        // ------------------------------------------------------------------------
140        // SingleReplyRequest Interface Method Implementations
141        // ------------------------------------------------------------------------
142    
143        /**
144         * Gets the protocol response message type for this request which produces
145         * at least one response.
146         * 
147         * @return the message type of the response.
148         */
149        public MessageTypeEnum getResponseType()
150        {
151            return RESP_TYPE;
152        }
153    
154    
155        /**
156         * The result containing response for this request.
157         * 
158         * @return the result containing response for this request
159         */
160        public InternalResultResponse getResultResponse()
161        {
162            if ( response == null )
163            {
164                response = new ModifyResponseImpl( getMessageId() );
165            }
166    
167            return response;
168        }
169    
170    
171        /**
172         * Checks to see if ModifyRequest stub equals another by factoring in checks
173         * for the name and modification items of the request.
174         * 
175         * @param obj
176         *            the object to compare this ModifyRequest to
177         * @return true if obj equals this ModifyRequest, false otherwise
178         */
179        public boolean equals( Object obj )
180        {
181            if ( obj == this )
182            {
183                return true;
184            }
185    
186            if ( !super.equals( obj ) )
187            {
188                return false;
189            }
190    
191            InternalModifyRequest req = ( InternalModifyRequest ) obj;
192    
193            if ( name != null && req.getName() == null )
194            {
195                return false;
196            }
197    
198            if ( name == null && req.getName() != null )
199            {
200                return false;
201            }
202    
203            if ( name != null && req.getName() != null )
204            {
205                if ( !name.equals( req.getName() ) )
206                {
207                    return false;
208                }
209            }
210    
211            if ( req.getModificationItems().size() != mods.size() )
212            {
213                return false;
214            }
215    
216            Iterator<Modification> list = req.getModificationItems().iterator();
217    
218            for ( int i = 0; i < mods.size(); i++ )
219            {
220                Modification item = list.next();
221    
222                if ( item == null )
223                {
224                    if ( mods.get( i ) != null )
225                    {
226                        return false;
227                    }
228                }
229                else
230                    
231                if ( !item.equals((ClientModification) mods.get( i ) ) )
232                {
233                    return false;
234                }
235            }
236    
237            return true;
238        }
239    
240    
241        /**
242         * Get a String representation of a ModifyRequest
243         * 
244         * @return A ModifyRequest String
245         */
246        public String toString()
247        {
248    
249            StringBuffer sb = new StringBuffer();
250    
251            sb.append( "    Modify Request\n" );
252            sb.append( "        Object : '" ).append( name ).append( "'\n" );
253    
254            if ( mods != null )
255            {
256    
257                for ( int i = 0; i < mods.size(); i++ )
258                {
259    
260                    ClientModification modification = ( ClientModification ) mods.get( i );
261    
262                    sb.append( "            Modification[" ).append( i ).append( "]\n" );
263                    sb.append( "                Operation : " );
264    
265                    switch ( modification.getOperation() )
266                    {
267                        case ADD_ATTRIBUTE:
268                            sb.append( " add\n" );
269                            break;
270    
271                        case REPLACE_ATTRIBUTE:
272                            sb.append( " replace\n" );
273                            break;
274    
275                        case REMOVE_ATTRIBUTE:
276                            sb.append( " delete\n" );
277                            break;
278                    }
279    
280                    sb.append( "                Modification\n" );
281                    sb.append( modification.getAttribute() );
282                }
283            }
284    
285            return sb.toString();
286        }
287    }