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.response;
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 the current response
061     */
062    public DsmlDecorator<? extends Response> getCurrentResponse()
063    {
064        return responses.get( responses.size() - 1 );
065    }
066
067
068    /**
069     * Adds a request to the Batch Response DSML.
070     *
071     * @param response the request to add
072     * @return true (as per the general contract of the Collection.add method).
073     */
074    public boolean addResponse( DsmlDecorator<? extends Response> response )
075    {
076        return responses.add( response );
077    }
078
079
080    /**
081     * Removes a request from the Batch Response DSML.
082     *
083     * @param response the request to remove
084     * @return true if this list contained the specified element.
085     */
086    public boolean removeResponse( DsmlDecorator<Response> response )
087    {
088        return responses.remove( response );
089    }
090
091
092    /**
093     * Gets the ID of the response
094     * 
095     * @return the ID of the response
096     */
097    public int getRequestID()
098    {
099        return requestID;
100    }
101
102
103    /**
104     * Sets the ID of the response
105     *
106     * @param requestID
107     *      the ID to set
108     */
109    public void setRequestID( int requestID )
110    {
111        this.requestID = requestID;
112    }
113
114
115    /**
116     * Gets the List of all the responses
117     *
118     * @return
119     *      the List of all the responses
120     */
121    public List<DsmlDecorator<? extends Response>> getResponses()
122    {
123        return responses;
124    }
125
126
127    /**
128     * Converts this Batch Response to its XML representation in the DSMLv2 format.
129     * The XML document will be formatted for pretty printing by default. 
130     * 
131     * @return the XML representation in DSMLv2 format
132     */
133    public String toDsml()
134    {
135       return toDsml( true ); 
136    }
137    
138    
139    /**
140     * Converts this Batch Response to its XML representation in the DSMLv2 format.
141     * 
142     * @param prettyPrint if true, formats the document for pretty printing
143     * @return the XML representation in DSMLv2 format
144     */
145    public String toDsml( boolean prettyPrint )
146    {
147        Document document = DocumentHelper.createDocument();
148        Element element = document.addElement( "batchResponse" );
149
150        element.add( ParserUtils.DSML_NAMESPACE );
151        element.add( ParserUtils.XSD_NAMESPACE );
152        element.add( ParserUtils.XSI_NAMESPACE );
153
154        // RequestID
155        if ( requestID != 0 )
156        {
157            element.addAttribute( "requestID", Integer.toString( requestID ) );
158        }
159
160        for ( DsmlDecorator<? extends Response> response : responses )
161        {
162            response.toDsml( element );
163        }
164
165        if ( prettyPrint )
166        {
167            document = ParserUtils.styleDocument( document );
168        }
169        
170        return document.asXML();
171    }
172}