using System; using System.Collections.Generic; namespace ${glPackageBaseCommonCBean} { public class ${glPagingInvokerName} { // =============================================================================== // Attribute // ========= protected String _tableDbName; protected bool _countLater; // =============================================================================== // Constructor // =========== public ${glPagingInvokerName}(String tableDbName) { this._tableDbName = tableDbName; } // =============================================================================== // Invoke // ====== public ${glPagingResultBeanName} InvokePaging(${glPagingHandlerName} handler) { AssertObjectNotNull("handler", handler); ${glPagingBeanInterfaceName} pagingBean = handler.PagingBean; AssertObjectNotNull("handler.getPagingBean()", pagingBean); if (!pagingBean.IsFetchScopeEffective) { String msg = "The paging bean is not effective about fetch-scope!"; msg = msg + " When you select page, you should set up fetch-scope of paging bean(Should invoke fetchFirst() and fetchPage()!)."; msg = msg + " The paging bean is: " + pagingBean; throw new SystemException(msg); } int allRecordCount; IList selectedList; if (_countLater) { selectedList = handler.Paging(); allRecordCount = handler.Count(); } else { allRecordCount = handler.Count(); selectedList = handler.Paging(); } ${glPagingResultBeanName} rb = new ${glResultBeanBuilderName}(_tableDbName).BuildPagingResultBean(pagingBean, allRecordCount, selectedList); if (IsNecessaryToReadPageAgain(rb)) { pagingBean.FetchPage(rb.AllPageCount); int reAllRecordCount = handler.Count(); IList reSelectedList = handler.Paging(); return new ${glResultBeanBuilderName}(_tableDbName).BuildPagingResultBean(pagingBean, reAllRecordCount, reSelectedList); } else { return rb; } } protected bool IsNecessaryToReadPageAgain(${glPagingResultBeanName} rb) { return rb.AllRecordCount > 0 && rb.SelectedList.Count == 0; } // =============================================================================== // Option // ====== public ${glPagingInvokerName} CountLater() { _countLater = true; return this; } // =============================================================================== // Helper // ====== protected void AssertObjectNotNull(String variableName, Object value) { if (variableName == null) { String msg = "The value should not be null: variableName=" + variableName + " value=" + value; throw new SystemException(msg); } if (value == null) { String msg = "The value should not be null: variableName=" + variableName; throw new SystemException(msg); } } } }