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.ldap.codec.api.LdapApiService;
028import org.apache.directory.api.ldap.model.message.Message;
029import org.apache.directory.api.ldap.model.message.Response;
030import org.apache.directory.api.ldap.model.message.SearchResultDone;
031import org.apache.directory.api.ldap.model.message.SearchResultEntry;
032import org.apache.directory.api.ldap.model.message.SearchResultReference;
033import org.dom4j.Element;
034import org.dom4j.tree.DefaultElement;
035
036
037/**
038 * This class represents the Search Response Dsml Container. 
039 * It is used to store Search Responses (Search Result Entry, 
040 * Search Result Reference and SearchResultDone).
041 *
042 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
043 */
044public class SearchResponseDsml extends AbstractResponseDsml<Response>
045{
046    private static final String SEARCH_RESPONSE_TAG = "searchResponse";
047
048    /** The responses */
049    private List<DsmlDecorator<? extends Response>> responses =
050        new ArrayList<DsmlDecorator<? extends Response>>();
051
052
053    /**
054     * Creates a new getDecoratedMessage() of SearchResponseDsml.
055     */
056    public SearchResponseDsml( LdapApiService codec )
057    {
058        super( codec, new SearchResponse() );
059    }
060
061
062    /**
063     * Creates a new getDecoratedMessage() of SearchResponseDsml.
064     *
065     * @param response the LDAP response message to decorate
066     */
067    public SearchResponseDsml( LdapApiService codec, Message response )
068    {
069        super( codec, ( Response ) response );
070    }
071
072
073    /**
074     * Adds a response.
075     *
076     * @param response
077     *      the response to add
078     * @return
079     *      true (as per the general contract of the Collection.add method).
080     */
081    public boolean addResponse( DsmlDecorator<? extends Response> response )
082    {
083        if ( response instanceof SearchResultEntry )
084        {
085            ( ( SearchResponse ) getDecorated() ).addSearchResultEntry(
086                ( SearchResultEntryDsml ) response );
087        }
088        else if ( response instanceof SearchResultReference )
089        {
090            ( ( SearchResponse ) getDecorated() ).addSearchResultReference(
091                ( SearchResultReferenceDsml ) response );
092        }
093        else if ( response instanceof SearchResultDone )
094        {
095            ( ( SearchResponse ) getDecorated() ).setSearchResultDone(
096                ( SearchResultDoneDsml ) response );
097        }
098        else
099        {
100            throw new IllegalArgumentException( "Unidentified search resp type" );
101        }
102
103        return responses.add( response );
104    }
105
106
107    /**
108     * Removes a response.
109     *
110     * @param response
111     *      the response to remove
112     * @return
113     *      true if this list contained the specified element.
114     */
115    public boolean removeResponse( DsmlDecorator<? extends Response> response )
116    {
117        if ( response instanceof SearchResultEntry )
118        {
119            ( ( SearchResponse ) getDecorated() ).removeSearchResultEntry(
120                ( SearchResultEntryDsml ) response );
121        }
122        else if ( response instanceof SearchResultReference )
123        {
124            ( ( SearchResponse ) getDecorated() ).removeSearchResultReference(
125                ( SearchResultReferenceDsml ) response );
126        }
127        else if ( response instanceof SearchResultDone )
128        {
129            if ( response.equals( ( ( SearchResponse ) getDecorated() ).getSearchResultDone() ) )
130            {
131                ( ( SearchResponse ) getDecorated() ).setSearchResultDone( null );
132            }
133        }
134        else
135        {
136            throw new IllegalArgumentException( "Unidentified search resp type" );
137        }
138
139        return responses.remove( response );
140    }
141
142
143    /**
144     * {@inheritDoc}
145     */
146    public Element toDsml( Element root )
147    {
148        Element element = null;
149
150        if ( root != null )
151        {
152            element = root.addElement( SEARCH_RESPONSE_TAG );
153        }
154        else
155        {
156            element = new DefaultElement( SEARCH_RESPONSE_TAG );
157        }
158
159        // RequestID
160        if ( getDecorated() != null )
161        {
162            int requestID = getDecorated().getMessageId();
163            if ( requestID > 0 )
164            {
165                element.addAttribute( "requestID", "" + requestID );
166            }
167        }
168
169        for ( DsmlDecorator<? extends Response> response : responses )
170        {
171            response.toDsml( element );
172        }
173
174        return element;
175    }
176}