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.extras.extended.endTransaction;
021
022
023import java.util.ArrayList;
024import java.util.List;
025
026import org.apache.directory.api.ldap.model.message.AbstractExtendedResponse;
027import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
028
029
030/**
031 * The End Transaction Extended Response implementation. It's described in RFC 5805 :
032 * 
033 * <pre>
034 * ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
035 *            COMPONENTS OF LDAPResult,
036 *            responseName     [10] LDAPOID OPTIONAL,
037 *            responseValue    [11] OCTET STRING OPTIONAL }
038 * </pre>
039 * 
040 * where the responseName is not present, and the responseValue contains
041 * a BER encoded value, defined by the following grammar :
042 * 
043 * <pre>
044 * txnEndRes ::= SEQUENCE {
045 *         messageID MessageID OPTIONAL,
046 *              -- msgid associated with non-success resultCode
047 *         updatesControls SEQUENCE OF updateControls SEQUENCE {
048 *              messageID MessageID,
049 *                   -- msgid associated with controls
050 *              controls  Controls
051 *         } OPTIONAL
052 *    }
053 * </pre>
054 *
055 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
056 */
057public class EndTransactionResponseImpl extends AbstractExtendedResponse implements EndTransactionResponse
058{
059    /** The faulty Message ID, if any */
060    private int failedMessageId = -1;
061    
062    /** The list of update controls for the message processed in the transaction */
063    private List<UpdateControls> updateControls = new ArrayList<>();
064
065    /**
066     * Create a new EndTransactionResponseImpl object
067     * 
068     * @param failedMessageId The faulty messageId
069     * @param resultCode the result code
070     */
071    public EndTransactionResponseImpl( int failedMessageId, ResultCodeEnum resultCode )
072    {
073        super( failedMessageId );
074
075        if ( resultCode == ResultCodeEnum.SUCCESS )
076        {
077            this.failedMessageId = -1;
078        }
079        else
080        {
081            this.failedMessageId = failedMessageId;
082        }
083
084        super.getLdapResult().setMatchedDn( null );
085        super.getLdapResult().setResultCode( resultCode );
086    }
087
088
089    /**
090     * Create a new EndTransactionResponseImpl instance
091     * 
092     * @param messageId The request's messageId
093     */
094    public EndTransactionResponseImpl( int messageId )
095    {
096        super( messageId );
097        super.getLdapResult().setMatchedDn( null );
098        super.getLdapResult().setResultCode( ResultCodeEnum.SUCCESS );
099    }
100
101
102    /**
103     * Create a new StartTransactionResponseImpl instance
104     */
105    public EndTransactionResponseImpl()
106    {
107        super( EndTransactionRequest.EXTENSION_OID );
108        super.getLdapResult().setMatchedDn( null );
109        super.getLdapResult().setResultCode( ResultCodeEnum.SUCCESS );
110    }
111    
112    
113    /**
114     * {@inheritDoc}
115     */
116    @Override
117    public int getFailedMessageId()
118    {
119        return failedMessageId;
120    }
121    
122    
123    /**
124     * {@inheritDoc}
125     */
126    @Override
127    public void setFailedMessageId( int failedMessageId )
128    {
129        this.failedMessageId = failedMessageId;
130    }
131    
132    /**
133     * @return the updateControls
134     */
135    @Override
136    public List<UpdateControls> getUpdateControls()
137    {
138        return updateControls;
139    }
140
141
142    /**
143     * @param updateControls the updateControls to set
144     */
145    public void setUpdateControls( List<UpdateControls> updateControls )
146    {
147        this.updateControls = updateControls;
148    }
149
150
151    /**
152     * {@inheritDoc}
153     */
154    @Override
155    public int hashCode()
156    {
157        int hash = 37;
158
159        hash = hash * 17 + failedMessageId;
160        
161        for ( UpdateControls updateControl : updateControls )
162        {
163            hash = hash * 17 + updateControl.hashCode();
164        }
165
166        return hash;
167    }
168
169
170    /**
171     * @see Object#equals(Object)
172     */
173    @Override
174    public boolean equals( Object obj )
175    {
176        if ( obj == this )
177        {
178            return true;
179        }
180
181        if ( !( obj instanceof EndTransactionResponse ) )
182        {
183            return false;
184        }
185        
186        EndTransactionResponse that = ( EndTransactionResponse ) obj;
187        
188        if ( failedMessageId != that.getFailedMessageId() )
189        {
190            return false;
191        }
192        
193        for ( UpdateControls updateControl : updateControls )
194        {
195            if ( !that.getUpdateControls().contains( updateControl ) )
196            {
197                return false;
198            }
199        }
200        
201        return true;
202    }
203}