org.alfresco.ibatis
Class RollupResultHandler

java.lang.Object
  extended by org.alfresco.ibatis.RollupResultHandler
All Implemented Interfaces:
org.apache.ibatis.session.ResultHandler

public class RollupResultHandler
extends java.lang.Object
implements org.apache.ibatis.session.ResultHandler

A ResultHandler that collapses multiple rows based on a set of properties.

This class is derived from earlier RollupRowHandler used to workaround the groupBy and nested ResultMap behaviour in iBatis (2.3.4.726) IBATIS-503.

The set of properties given act as a unique key. When the unique key changes, the collection values from the nested ResultMap are coalesced and the given ResultHandler is called. It is possible to embed several instances of this handler for deeply-nested ResultMap declarations.

Use this instance as a regular ResultHandler, but with one big exception: call RollupResultHandler.processLastResults() after executing the SQL statement. Remove the groupBy attribute from the iBatis ResultMap declaration.

Example iBatis 2.x (TODO migrate example to MyBatis 3.x):

    <resultMap id="result_AuditQueryAllValues"
               extends="alfresco.audit.result_AuditQueryNoValues"
               class="AuditQueryResult">
        <result property="auditValues" resultMap="alfresco.propval.result_PropertyIdSearchRow"/>
    </resultMap>
 
Example usage:
        RowHandler rowHandler = new RowHandler()
        {
            public void handleRow(Object valueObject)
            {
                // DO SOMETHING
            }
        };
        RollupRowHandler rollupRowHandler = new RollupRowHandler(
                new String[] {"auditEntryId"},
                "auditValues",
                rowHandler,
                maxResults);

        if (maxResults > 0)
        {
            // Calculate the maximum results required
            int sqlMaxResults = (maxResults > 0 ? ((maxResults+1) * 20) : Integer.MAX_VALUE);

            List rows = template.queryForList(SELECT_ENTRIES_WITH_VALUES, params, 0, sqlMaxResults);
            for (AuditQueryResult row : rows)
            {
                rollupRowHandler.handleRow(row);
            }
            // Don't process last result:
            //    rollupRowHandler.processLastResults();
            //    The last result may be incomplete
        }
        else
        {
            template.queryWithRowHandler(SELECT_ENTRIES_WITH_VALUES, params, rollupRowHandler);
            rollupRowHandler.processLastResults();
        }
 

This class is not thread-safe; use a new instance for each use.

Since:
4.0

Constructor Summary
RollupResultHandler(java.lang.String[] keyProperties, java.lang.String collectionProperty, org.apache.ibatis.session.ResultHandler resultHandler)
           
RollupResultHandler(java.lang.String[] keyProperties, java.lang.String collectionProperty, org.apache.ibatis.session.ResultHandler resultHandler, int maxResults)
           
 
Method Summary
 void handleResult(org.apache.ibatis.session.ResultContext context)
           
 void processLastResults()
          Client code must call this method once the query returns so that the final results can be passed to the inner RowHandler.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RollupResultHandler

public RollupResultHandler(java.lang.String[] keyProperties,
                           java.lang.String collectionProperty,
                           org.apache.ibatis.session.ResultHandler resultHandler)
Parameters:
keyProperties - the properties that make up the unique key
collectionProperty - the property mapped using a nested ResultMap
resultHandler - the result handler that will receive the rolled-up results

RollupResultHandler

public RollupResultHandler(java.lang.String[] keyProperties,
                           java.lang.String collectionProperty,
                           org.apache.ibatis.session.ResultHandler resultHandler,
                           int maxResults)
Parameters:
keyProperties - the properties that make up the unique key
collectionProperty - the property mapped using a nested ResultMap
resultHandler - the result handler that will receive the rolled-up results
maxResults - the maximum number of results to retrieve (-1 for no limit). Make sure that the query result limit is large enough to produce this at least this number of results
Method Detail

handleResult

public void handleResult(org.apache.ibatis.session.ResultContext context)
Specified by:
handleResult in interface org.apache.ibatis.session.ResultHandler

processLastResults

public void processLastResults()
Client code must call this method once the query returns so that the final results can be passed to the inner RowHandler. If a query is limited by size, then it is possible that the unprocessed results represent an incomplete final object; in this case it would be best to ignore the last results. If the query is complete (i.e. all results are returned) then this method should be called.

If you want X results and each result is made up of N rows (on average), then set the query limit to:
L = X * (N+1)
and don't call this method.



Copyright © 2005 - 2010 Alfresco Software, Inc. All Rights Reserved.