1、InputPageUtil
2、EditPageUtil
----------------------------------------------------------------------------------------------------------
1、InputPageUtil
功能简述:在输入画面中,总是显示最后一页,本页输入满了则自动跳到下一页(如果还有)。
参数:
int recordsOfPage —— 每页显示的记录条数(不能小于1,默认10)
int inputtedCount —— 已经输入产品的个数(不能小于 0)
int totalCount —— 订单关联的产品总个数(不能小于 1)
各方法描述:
a、计算总页数
int totalPageCount(){ if( totalCount % recordsOfPage == 0 ){ return totalCount / recordsOfPage; } return totalCount / recordsOfPage + 1; }
b、计算当前页数
注意:如果 inputtedCount 刚好满页。则要继续判断 --->
b-1、如果全部输入完了,那么当前页就是本页
b-2、如果没有全部输入完,那么当前页就为 (本页+1)
特殊值,inputtedCount 为 0 。计算结果为1,也正确。
int currentPageCount(){ if( inputtedCount % recordsOfPage == 0 ){ if( inputtedCount == totalCount ){ return inputtedCount / recordsOfPage; } else{ return inputtedCount / recordsOfPage + 1; } } return inputtedCount / recordsOfPage + 1; }c、计算最后页的第一行对应已经输入的 products (List)的索引(用来将最后部分数据 Copy 出来)
显然这个起始索引只能是,0、10、20、30....
int startIndexForLastPage( int currentPageCount ){ return ( currentPageCount - 1 ) * recordsOfPage; }d、最后页的 Number 数(转换成List,方便strut2 的循环标签使用)
int lastPageNumberCount( int currentPageCount, int totalPageCount ){ if( currentPageCount < totalPageCount ){ return recordsOfPage; } else if( currentPageCount == totalPageCount ){ if( totalCount % recordsOfPage == 0 ){ return recordsOfPage; } return totalCount % recordsOfPage; } else{ // this can't happen return -1; } }
d、判断是否已经全部输入
boolean isAllProductsInputted(){ return inputtedCount == totalCount; }
完整代码:
package jp.co.snjp.kddi.ht.util;
public class InputPageUtil {
private static final int DEFAULT_RECORDS_OF_PAGE = 10;
private final int recordsOfPage;
private final int inputtedCount;
private final int totalCount;
private InputPageUtil( int inputtedCount, int totalCount ){
this( inputtedCount, totalCount, DEFAULT_RECORDS_OF_PAGE );
}
private InputPageUtil( int inputtedCount, int totalCount, int recordsOfPage ){
if( isInvalidParams( inputtedCount, totalCount, recordsOfPage ) ){
throw new IllegalStateException("Parameters state error.");
}
this.inputtedCount = inputtedCount;
this.totalCount = totalCount;
this.recordsOfPage = recordsOfPage;
}
private boolean isInvalidParams( int inputtedCount, int totalCount, int recordsOfPage ){
if( inputtedCount < 0 || totalCount < 1 || recordsOfPage < 1){
return true;
}
if( totalCount < inputtedCount){
return true;
}
return false;
}
public int getRecordsOfPage(){
return recordsOfPage;
}
public int getInputtedCount() {
return inputtedCount;
}
public int getTotalCount() {
return totalCount;
}
public static InputPageUtil newInstanceDefaultRecordsOfPage( int inputtedCount, int totalCount ){
return new InputPageUtil( inputtedCount, totalCount );
}
public static InputPageUtil newInstanceSetRecordsOfPage( int inputtedCount, int totalCount, int recordsOfPage ){
return new InputPageUtil( inputtedCount, totalCount, recordsOfPage );
}
boolean isAllProductsInputted(){
return inputtedCount == totalCount;
}
int totalPageCount(){
if( totalCount % recordsOfPage == 0 ){
return totalCount / recordsOfPage;
}
return totalCount / recordsOfPage + 1;
}
int currentPageCount(){
if( inputtedCount % recordsOfPage == 0 ){
if( inputtedCount == totalCount ){
return inputtedCount / recordsOfPage;
}
else{
return inputtedCount / recordsOfPage + 1;
}
}
return inputtedCount / recordsOfPage + 1;
}
int startIndexForLastPage( int currentPageCount ){
return ( currentPageCount - 1 ) * recordsOfPage;
}
int lastPageNumberCount( int currentPageCount, int totalPageCount ){
if( currentPageCount < totalPageCount ){
return recordsOfPage;
}
else if( currentPageCount == totalPageCount ){
if( totalCount % recordsOfPage == 0 ){
return recordsOfPage;
}
return totalCount % recordsOfPage;
}
else{
// this can't happen
return -1;
}
}
public Status computeAllStatus(){
int totalPageCount = totalPageCount();
int currentPageCount = currentPageCount();
int startIndexForLastPage = startIndexForLastPage( currentPageCount );
int lastPageNumberCount = lastPageNumberCount( currentPageCount, totalPageCount );
boolean allProductsInputted = isAllProductsInputted();
return new Status( currentPageCount, totalPageCount,
startIndexForLastPage, lastPageNumberCount, allProductsInputted );
}
public static class Status{
private final int totalPageCount;
private final int currentPageCount;
private final int startIndexForLastPage;
private final int lastPageNumberCount;
private final boolean allProductsInputted;
private Status( int currentPageCount, int totalPageCount,
int startIndexForLastPage, int lastPageNumberCount, boolean allProductsInputted ){
this.currentPageCount = currentPageCount;
this.totalPageCount = totalPageCount;
this.startIndexForLastPage =startIndexForLastPage;
this.lastPageNumberCount = lastPageNumberCount;
this.allProductsInputted = allProductsInputted;
}
public int getCurrentPageCount() {
return currentPageCount;
}
public int getLastPageNumberCount() {
return lastPageNumberCount;
}
public int getStartIndexForLastPage() {
return startIndexForLastPage;
}
public int getTotalPageCount() {
return totalPageCount;
}
public boolean isAllProductsInputted() {
return allProductsInputted;
}
}
}
使用示例:
void initContinueManualInputParam( T03SlipWk orderForm, List<T04CaseDtlWk> products ){ assert orderForm != null; assert products != null; assert products.size() != 0; productName = orderForm.getProdNm1(); productCount = orderForm.getShpVol1().intValue(); InputPageUtil.Status inputPageStatus = InputPageUtil.newInstanceDefaultRecordsOfPage( products.size(), productCount ).computeAllStatus(); allProductsInput = inputPageStatus.isAllProductsInputted(); totalPageCount = inputPageStatus.getTotalPageCount(); currentPageCount = inputPageStatus.getCurrentPageCount(); int startIndexForLastPage = inputPageStatus.getStartIndexForLastPage(); int lastPageNumberCount = inputPageStatus.getLastPageNumberCount(); lastPageProducts = lastPageProducts( startIndexForLastPage, products ); lastPageRecords = lastPageRecords( lastPageNumberCount ); } // 相关的两个方法 List<String> lastPageProducts( int startIndexForLastPage, List<T04CaseDtlWk> products ){ List<String> result = new ArrayList<String>(); while( startIndexForLastPage < products.size() ){ T04CaseDtlWk product = (T04CaseDtlWk)products.get( startIndexForLastPage ); result.add( formatNumber( product.getShpWt2() ) ); startIndexForLastPage++; } return result; } List<Integer> lastPageRecords( int lastPageNumberCount ){ List<Integer> result = new ArrayList<Integer>(); for( int i = 0 ; i < lastPageNumberCount ; i++ ){ result.add( i ); } return result; }
功能简述:
进入编辑页面,根据 page_line 参数来确定显示的“当前页”和 选中状态的“当前行”,如果 page_line 为 null (从输入页面进入编辑页面),则默认显示第一页,选中第一行。
参数:
int recordsOfPage —— 页面记录条数(不能小于1,默认为10)
int inputtedCount —— 已经输入产品个数(不能小于1,这里如果一个都没有输入会构造一个 BlankProduct,这样进入编辑页就可以直接输入)
int totalCount —— 订单关联的产品总个数(不能小于 1)
String page_line —— JSP 页面传递的参数,用来确定页和行
各方法描述:
a、计算总页数
int totalPageCount(){ if( totalCount % recordsOfPage == 0 ){ return totalCount / recordsOfPage; } return totalCount / recordsOfPage + 1; }b、计算当前页
// 1. 第一次从输入页面->编辑页面 ( page_line为null, 默认第一页面) // 2. 编辑修改保存后->再次进入 (根据 page_line 参数解析) int currentPageCount(){ // 1 if( page_line == null ){ return 1; } // 2 int page = parsePage(); if( page < 1){ return 1; } else if( maxEditablePageCount() < page ){ return maxEditablePageCount(); } else{ return page; } }c、计算当前行
int currentLine( int currentPageCount ){ // 1 if( page_line == null ){ return 1; } // 2 int line = parseLine(); if( line < 1 ){ return 1; } else if( maxEditableLine( currentPageCount ) < line ){ return maxEditableLine( currentPageCount ); } else{ return line; } }d、计算当前页的记录条数(也就是 JSP 页面,HT 的 Select控件 option 的个数)
// select 控件要用 int productRecordCountOfCurrentPage( int currentPageCount ){ if( inputtedCount < ( currentPageCount * recordsOfPage ) ){ return inputtedCount - ( currentPageCount - 1 ) * recordsOfPage; } return recordsOfPage; }e、计算当前页的第一行对应已经输入的 products (List)的索引(用来将最后部分数据 Copy 出来)
显然这个起始索引只能是,0、10、20、30....
int startIndexForCopy( int currentPageCount ){ return ( currentPageCount - 1 ) * recordsOfPage; }f、判断是否有上一页 和是否有下一页
boolean hasPrePage( int currentPageCount ){ return 1 < currentPageCount ? true : false; } boolean hasNextPage( int currentPageCount ){ return currentPageCount < maxEditablePageCount() ? true : false; }g、构造 page_line 参数
String pageLine( int currentPageCount, int currentLine ){ return currentPageCount + "_" + currentLine; }
package jp.co.snjp.kddi.ht.util; import java.util.regex.Matcher; import java.util.regex.Pattern; public class EditPageUtil { private static final int DEFAULT_RECORDS_OF_PAGE = 10; private final int recordsOfPage; private final int inputtedCount; private final int totalCount; private final String page_line; public int getInputtedCount() { return inputtedCount; } public int getRecordsOfPage() { return recordsOfPage; } public int getTotalCount() { return totalCount; } private EditPageUtil( int inputtedCount, int totalCount, String page_line ){ this( inputtedCount, totalCount, page_line, DEFAULT_RECORDS_OF_PAGE ); } private EditPageUtil( int inputtedCount, int totalCount, String page_line, int recordsOfPage ){ if( isInvalidParams( inputtedCount, totalCount, page_line, recordsOfPage ) ){ throw new IllegalStateException("Parameters state error."); } this.inputtedCount = inputtedCount; this.totalCount = totalCount; this.page_line = page_line; this.recordsOfPage = recordsOfPage; } private boolean isInvalidParams( int inputtedCount, int totalCount, String page_line, int recordsOfPage ){ // 这里 inputtedCount 至少会有一个 BlankProduct, 用于在编辑模式下输入 if( inputtedCount < 1 || totalCount < 1 || recordsOfPage < 1){ return true; } if( totalCount < inputtedCount){ return true; } if( page_line != null && !page_line.trim().equals("") ){ return notMatcher( page_line, "\\d+_\\d+" ); } return false; } private boolean notMatcher( String sequence, String regex ){ Pattern p = Pattern.compile(regex); Matcher m = p.matcher( sequence.trim() ); return !m.matches(); } public static EditPageUtil newInstanceDefaultRecordsOfPage( int inputtedCount, int totalCount, String page_line ){ return new EditPageUtil( inputtedCount, totalCount, page_line ); } public static EditPageUtil newInstanceSetRecordsOfPage( int inputtedCount, int totalCount, String page_line, int recordsOfPage ){ return new EditPageUtil( inputtedCount, totalCount,page_line, recordsOfPage ); } int totalPageCount(){ if( totalCount % recordsOfPage == 0 ){ return totalCount / recordsOfPage; } return totalCount / recordsOfPage + 1; } // 1. 第一次从输入页面->编辑页面 ( page_line为null, 默认第一页面) // 2. 编辑修改保存后->再次进入 (根据 page_line 参数解析) int currentPageCount(){ // 1 if( page_line == null ){ return 1; } // 2 int page = parsePage(); if( page < 1){ return 1; } else if( maxEditablePageCount() < page ){ return maxEditablePageCount(); } else{ return page; } } int currentLine( int currentPageCount ){ // 1 if( page_line == null ){ return 1; } // 2 int line = parseLine(); if( line < 1 ){ return 1; } else if( maxEditableLine( currentPageCount ) < line ){ return maxEditableLine( currentPageCount ); } else{ return line; } } private int maxEditableLine( int currentPageCount ){ return productRecordCountOfCurrentPage( currentPageCount ); } // select 控件要用 int productRecordCountOfCurrentPage( int currentPageCount ){ if( inputtedCount < ( currentPageCount * recordsOfPage ) ){ return inputtedCount - ( currentPageCount - 1 ) * recordsOfPage; } return recordsOfPage; } int startIndexForCopy( int currentPageCount ){ return ( currentPageCount - 1 ) * recordsOfPage; } boolean hasPrePage( int currentPageCount ){ return 1 < currentPageCount ? true : false; } boolean hasNextPage( int currentPageCount ){ return currentPageCount < maxEditablePageCount() ? true : false; } String pageLine( int currentPageCount, int currentLine ){ return currentPageCount + "_" + currentLine; } // 可以"翻页"的最大页数 private int maxEditablePageCount(){ if( inputtedCount % recordsOfPage == 0 ){ return inputtedCount / recordsOfPage; } return inputtedCount / recordsOfPage + 1; } private int parsePage(){ String[] params = page_line.split( "_" ); String page = params[0].trim(); return Integer.valueOf( page ).intValue(); } private int parseLine(){ String[] params = page_line.split( "_" ); String line = params[1].trim(); return Integer.valueOf( line ).intValue(); } public Status computeAllStatus(){ int totalPageCount = totalPageCount(); int currentPageCount = currentPageCount(); int currentLine = currentLine( currentPageCount ); int productRecordCountOfCurrentPage = productRecordCountOfCurrentPage( currentPageCount ); int startIndexForCopy = startIndexForCopy( currentPageCount ); String pageLine = pageLine( currentPageCount, currentLine ); boolean hasPrePage = hasPrePage( currentPageCount ); boolean hasNextPage = hasNextPage( currentPageCount ); return new Status( totalPageCount, currentPageCount, currentLine, productRecordCountOfCurrentPage, startIndexForCopy, pageLine, hasPrePage, hasNextPage); } public static class Status{ private final int totalPageCount; private final int currentPageCount; private final int currentLine; private final int productRecordCountOfCurrentPage; private final int startIndexForCopy; private final String pageLine; private final boolean hasPrePage; private final boolean hasNextPage; private Status( int totalPageCount, int currentPageCount, int currentLine, int productRecordCountOfCurrentPage, int startIndexForCopy, String pageLine, boolean hasPrePage, boolean hasNextPage) { this.totalPageCount = totalPageCount; this.currentPageCount = currentPageCount; this.currentLine = currentLine; this.productRecordCountOfCurrentPage = productRecordCountOfCurrentPage; this.startIndexForCopy = startIndexForCopy; this.pageLine = pageLine; this.hasPrePage = hasPrePage; this.hasNextPage = hasNextPage; } public int getCurrentLine() { return currentLine; } public int getCurrentPageCount() { return currentPageCount; } public boolean getHasNextPage() { return hasNextPage; } public boolean getHasPrePage() { return hasPrePage; } public String getPageLine() { return pageLine; } public int getProductRecordCountOfCurrentPage() { return productRecordCountOfCurrentPage; } public int getStartIndexForCopy() { return startIndexForCopy; } public int getTotalPageCount() { return totalPageCount; } } }
void initEditJspParam( ){ T03SlipWk orderForm = (T03SlipWk) session.get( "SLIP_WK" ); List<T04CaseDtlWk> products = getEditableProducts( orderForm ); LOG.info( "products.size()=" + products.size() ); productName = orderForm.getProdNm1(); productCount = orderForm.getShpVol1().intValue(); String page_line = request.getParameter( "page_line" ); EditPageUtil.Status editPageStatus = EditPageUtil.newInstanceDefaultRecordsOfPage( products.size(), productCount, page_line).computeAllStatus(); totalPageCount = editPageStatus.getTotalPageCount(); currentPageCount = editPageStatus.getCurrentPageCount(); currentLine = editPageStatus.getCurrentLine(); currentPageProductRecords = editPageStatus.getProductRecordCountOfCurrentPage(); currentPageProducts = currentPageProducts( products, editPageStatus ); hasPrePage = editPageStatus.getHasPrePage(); hasNextPage = editPageStatus.getHasNextPage(); pageLine = editPageStatus.getPageLine(); LOG.info( "currentPageCount=" + currentPageCount ); LOG.info( "currentLine=" + currentLine ); LOG.info( "currentPageProductRecords=" + currentPageProductRecords ); LOG.info( "hasPrePage=" + hasPrePage ); LOG.info( "hasNextPage=" + hasNextPage ); } // 计算显示在当前页面的商品 List<String> currentPageProducts( List<T04CaseDtlWk> products, EditPageUtil.Status status ){ List<String> results = new ArrayList<String>(); int currentPageProductRecords = status.getProductRecordCountOfCurrentPage(); int startIndexForCopy = status.getStartIndexForCopy(); int copyedCount = 0; final String BLANK = " "; while( copyedCount < currentPageProductRecords ){ T04CaseDtlWk product = products.get( startIndexForCopy ); BigDecimal wt2 = product.getShpWt2(); if( wt2 == null ){ results.add( BLANK ); } else{ results.add( manualInputAction.formatNumber( wt2 ) ); } startIndexForCopy++; copyedCount++; } return results; } // 获取可编辑 的商品************************************ 注意这个方法对BlankProduct 的操作 List<T04CaseDtlWk> getEditableProducts( T03SlipWk orderForm ){ List<T04CaseDtlWk> products = manualInputAction.getProductsFromDB( orderForm ); // 商品没有完全输入, 在最后加入一个 BlankProduct, 用于输入 if( products.size() < orderForm.getShpVol1().intValue() ){ String slipId = orderForm.getSlipId(); products.add( constructBlankProduct( slipId ) ); } return products; } T04CaseDtlWk constructBlankProduct( String slipId ){ T04CaseDtlWk product = new T04CaseDtlWk(); product.setSlipId( slipId ); // 要保证通过 page_line 计算出来的 caseId 找不到这个BlankProduct product.setCaseId( -1 ); product.setShpWt2( null ); return product; }