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.dsmlv2.reponse;
021
022
023import java.util.ArrayList;
024import java.util.List;
025
026import org.apache.directory.api.dsmlv2.DsmlDecorator;
027import org.apache.directory.api.dsmlv2.ParserUtils;
028import org.apache.directory.api.ldap.model.message.Response;
029import org.dom4j.Document;
030import org.dom4j.DocumentHelper;
031import org.dom4j.Element;
032
033
034/**
035 * This class represents the Batch Response. It can be used to generate an the XML String of a BatchResponse.
036 *
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039public class BatchResponseDsml
040{
041    /** The Responses list */
042    private List<DsmlDecorator<? extends Response>> responses;
043
044    /** The ID of the response */
045    private int requestID;
046
047
048    /**
049     * Creates a new instance of BatchResponseDsml.
050     */
051    public BatchResponseDsml()
052    {
053        responses = new ArrayList<DsmlDecorator<? extends Response>>();
054    }
055
056
057    /**
058     * Gets the current response
059     *
060     * @return
061     *      the current response
062     */
063    public DsmlDecorator<? extends Response> getCurrentResponse()
064    {
065        return responses.get( responses.size() - 1 );
066    }
067
068
069    /**
070     * Adds a request to the Batch Response DSML.
071     *
072     * @param response
073     *      the request to add
074     * @return
075     *      true (as per the general contract of the Collection.add method).
076     */
077    public boolean addResponse( DsmlDecorator<? extends Response> response )
078    {
079        return responses.add( response );
080    }
081
082
083    /**
084     * Removes a request from the Batch Response DSML.
085     *
086     * @param response
087     *      the request to remove
088     * @return
089     *      true if this list contained the specified element.
090     */
091    public boolean removeResponse( DsmlDecorator<Response> response )
092    {
093        return responses.remove( response );
094    }
095
096
097    /**
098     * Gets the ID of the response
099     * @return
100     *      the ID of the response
101     */
102    public int getRequestID()
103    {
104        return requestID;
105    }
106
107
108    /**
109     * Sets the ID of the response
110     *
111     * @param requestID
112     *      the ID to set
113     */
114    public void setRequestID( int requestID )
115    {
116        this.requestID = requestID;
117    }
118
119
120    /**
121     * Gets the List of all the responses
122     *
123     * @return
124     *      the List of all the responses
125     */
126    public List<DsmlDecorator<? extends Response>> getResponses()
127    {
128        return responses;
129    }
130
131
132    /**
133     * Converts this Batch Response to its XML representation in the DSMLv2 format.
134     * The XML document will be formatted for pretty printing by default. 
135     * 
136     * @see {@link #toDsml(boolean)}
137     * 
138     * @return the XML representation in DSMLv2 format
139     */
140    public String toDsml()
141    {
142       return toDsml( true ); 
143    }
144    
145    
146    /**
147     * Converts this Batch Response to its XML representation in the DSMLv2 format.
148     * 
149     * @param prettyPrint if true, formats the document for pretty printing
150     * @return the XML representation in DSMLv2 format
151     */
152    public String toDsml( boolean prettyPrint )
153    {
154        Document document = DocumentHelper.createDocument();
155        Element element = document.addElement( "batchResponse" );
156
157        element.add( ParserUtils.DSML_NAMESPACE );
158        element.add( ParserUtils.XSD_NAMESPACE );
159        element.add( ParserUtils.XSI_NAMESPACE );
160
161        // RequestID
162        if ( requestID != 0 )
163        {
164            element.addAttribute( "requestID", "" + requestID );
165        }
166
167        for ( DsmlDecorator<? extends Response> response : responses )
168        {
169            response.toDsml( element );
170        }
171
172        if( prettyPrint )
173        {
174            document = ParserUtils.styleDocument( document );
175        }
176        
177        return document.asXML();
178    }
179}