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        grammar = 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    public void decorate( PagedResults control )
081    {
082        if ( control instanceof PagedResultsDecorator )
083        {
084            this.control = ( PagedResultsDecorator ) control;
085        }
086        else
087        {
088            this.control = new PagedResultsDecorator( codec, control );
089        }
090    }
091
092
093    /**
094     * Set a PagedSearchControl Object into the container. It will be completed by
095     * the ldapDecoder.
096     * 
097     * @param control the PagedSearchControl to set.
098     */
099    public void setPagedSearchControl( PagedResultsDecorator control )
100    {
101        this.control = control;
102    }
103
104
105    /**
106     * Clean the container
107     */
108    public void clean()
109    {
110        super.clean();
111        control = null;
112    }
113}