Package flex.messaging.services.remoting

Source Code of flex.messaging.services.remoting.PageableRowSetCache

package flex.messaging.services.remoting;

import java.sql.SQLException;
import java.util.Map;

import javax.sql.RowSet;

import flex.messaging.FlexContext;
import flex.messaging.FlexSession;
import flex.messaging.io.PageableRowSet;
import flex.messaging.io.PagedRowSet;

/**
* A special RemotingService destination that caches a PageableRowSet
* in the current FlexSession to provide legacy paging functionality for
* the deprecated client RecordSet API.
*
* To create a PageableRowSet, a javax.sql.RowSet must be provided instead
* of a java.sql.ResultSet because the data has to be disconnected and
* cacheable.
*
* The RemotingService's JavaAdapter is expected to manage this instance.
*
* @see flex.messaging.io.PageableRowSet
* @author Peter Farland
*/
public class PageableRowSetCache
{
    private static final int DEFAULT_PAGE_SIZE = 25;
   
    /**
     * A default constructor is required for a RemotingService source.
     */
    public PageableRowSetCache()
    {
    }

    /**
     * Converts a RowSet into a PageableRowSet, stores the PageableRowSet
     * result in the cache and then returns the PageableRowSet. A unique
     * id will be created for the PagedRowSet.
     *
     * @param rowset RowSet to be cached for paged access.
     * @param pageSize Size of the first page to be sent to the client. Default
     * is 25 if pageSize is set to 0 or less. If all records should be sent at once
     * consider not caching the RowSet or specify Integer.MAX_VALUE.
     */
    public static PageableRowSet cacheRowSet(RowSet rowset, int pageSize)
    {
        if (pageSize <= 0)
            pageSize = DEFAULT_PAGE_SIZE;

        PageableRowSet prs = new PagedRowSet(rowset, pageSize, true);
        cachePageableRowSet(prs);
        return prs;
    }

    /**
     * Stores the PageableRowSet result in the session to act as a cache
     * for the legacy client RecordSet paging feature.
     *
     * @param rowset PageableRowSet to be cached for paged access.
     */
    public static void cachePageableRowSet(PageableRowSet rowset)
    {
        if (rowset != null)
        {
            FlexSession session = FlexContext.getFlexSession();
            session.setAttribute(rowset.getID(), rowset);
        }
    }

    /**
     * Get a subset of records that are cached for the given PageableRowSet id.
     *
     * @param id    The PageableRowSet's id, used to locate it in the current session.
     * @param startIndex    The absolute position for the record set cursor.
     * @param count    The size of the page of results to return.
     * @return Map    The resulting sub-set of data or 'page' requested.
     * @see PageableResultSet#getRecords(int, int)
     */
    public Map getRecords(String id, int startIndex, int count) throws SQLException
    {
        Map page = null;
        FlexSession session = FlexContext.getFlexSession();

        if (session != null)
        {
            Object o = session.getAttribute(id);

            if (o != null && o instanceof PageableRowSet)
            {
                PageableRowSet rs = (PageableRowSet) o;
                page = rs.getRecords(startIndex, count);
            }
        }

        return page;
    }

    /**
     * Remove a PageableRowSet from the current session.
     *
     * @param id The id of the PageableRowSet to remove from the current session.
     */
    public void release(String id)
    {
        FlexSession session = FlexContext.getFlexSession();

        if (session != null)
        {
            session.removeAttribute(id);
        }
    }
}
TOP

Related Classes of flex.messaging.services.remoting.PageableRowSetCache

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.