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.codec.controls.search.pagedSearch;
021
022
023import org.apache.directory.api.asn1.ber.AbstractContainer;
024import org.apache.directory.api.ldap.codec.api.LdapApiService;
025import org.apache.directory.api.ldap.model.message.controls.PagedResults;
026
027
028/**
029 * A container for the Paged Search Control.
030 * 
031 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
032 */
033public class PagedResultsContainer extends AbstractContainer
034{
035    /** PagedSearchControl */
036    private PagedResultsDecorator control;
037
038    private LdapApiService codec;
039
040
041    /**
042     * Creates a new PagedSearchControl container object. We will store one grammar,
043     * it's enough ...
044     * @param codec The encoder decoder for this container
045     */
046    public PagedResultsContainer( LdapApiService codec )
047    {
048        super();
049        this.codec = codec;
050        setGrammar( PagedResultsGrammar.getInstance() );
051        setTransition( PagedResultsStates.START_STATE );
052    }
053
054
055    /**
056     * Creates a new PagedSearchControl container object to contain a PagedResults
057     * Control, which is optionally decorated if is not a decorator already. If it
058     * is a decorator then it is used as the decorator for this container.
059     *
060     * @param codec The encoder decoder for this container
061     * @param control A PagedResults Control to optionally be wrapped.
062     */
063    public PagedResultsContainer( LdapApiService codec, PagedResults control )
064    {
065        this( codec );
066        decorate( control );
067    }
068
069
070    /**
071     * @return Returns the paged search control.
072     */
073    public PagedResultsDecorator getDecorator()
074    {
075
076        return control;
077    }
078
079
080    /**
081     * Decorate the PageResult control
082     * 
083     * @param control The PageResult control instance
084     */
085    public void decorate( PagedResults control )
086    {
087        if ( control instanceof PagedResultsDecorator )
088        {
089            this.control = ( PagedResultsDecorator ) control;
090        }
091        else
092        {
093            this.control = new PagedResultsDecorator( codec, control );
094        }
095    }
096
097
098    /**
099     * Set a PagedSearchControl Object into the container. It will be completed by
100     * the ldapDecoder.
101     * 
102     * @param control the PagedSearchControl to set.
103     */
104    public void setPagedSearchControl( PagedResultsDecorator control )
105    {
106        this.control = control;
107    }
108
109
110    /**
111     * Clean the container
112     */
113    @Override
114    public void clean()
115    {
116        super.clean();
117        control = null;
118    }
119}