bsh实现ofbiz分页


java 代码
 
  1. delegator = request.getAttribute("delegator");  
  2.   //获取request参数
  3. lookupFlag = request.getParameter("lookupFlag");  
  4. shipmentTypeId = request.getParameter("shipmentTypeId");  
  5. originFacilityId = request.getParameter("originFacilityId");  
  6. destinationFacilityId = request.getParameter("destinationFacilityId");  
  7. statusId = request.getParameter("statusId");  
  8.   
  9. // 设置分页参数
  10. viewIndex = 1;  
  11. try {  
  12.     viewIndex = Integer.valueOf((String) request.getParameter("VIEW_INDEX")).intValue();  
  13. catch (Exception e) {  
  14.     viewIndex = 1;  
  15. }  
  16. context.put("viewIndex", viewIndex);  
  17.   
  18. viewSize = 20;  
  19. try {  
  20.     viewSize = Integer.valueOf((String) request.getParameter("VIEW_SIZE")).intValue();  
  21. catch (Exception e) {  
  22.     viewSize = 20;  
  23. }  
  24. context.put("viewSize", viewSize);  
  25.  
  26. //设置参数链表(paramListBuffer),接受参数
  27. findShipmentExprs = new LinkedList();  
  28. paramListBuffer = new StringBuffer();  
  29. if (UtilValidate.isNotEmpty(shipmentTypeId)) {  
  30.     paramListBuffer.append("&shipmentTypeId=");  
  31.     paramListBuffer.append(shipmentTypeId);  
  32.     findShipmentExprs.add(new EntityExpr("shipmentTypeId", EntityOperator.EQUALS, shipmentTypeId));  
  33.   
  34.     currentShipmentType = delegator.findByPrimaryKeyCache("ShipmentType", UtilMisc.toMap("shipmentTypeId", shipmentTypeId));  
  35.     context.put("currentShipmentType", currentShipmentType);  
  36. }  
  37. if (UtilValidate.isNotEmpty(originFacilityId)) {  
  38.     paramListBuffer.append("&originFacilityId=");  
  39.     paramListBuffer.append(originFacilityId);  
  40.     findShipmentExprs.add(new EntityExpr("originFacilityId", EntityOperator.EQUALS, originFacilityId));  
  41.   
  42.     currentOriginFacility = delegator.findByPrimaryKeyCache("Facility", UtilMisc.toMap("facilityId", originFacilityId));  
  43.     context.put("currentOriginFacility", currentOriginFacility);  
  44. }  
  45. if (UtilValidate.isNotEmpty(destinationFacilityId)) {  
  46.     paramListBuffer.append("&destinationFacilityId=");  
  47.     paramListBuffer.append(destinationFacilityId);  
  48.     findShipmentExprs.add(new EntityExpr("destinationFacilityId", EntityOperator.EQUALS, destinationFacilityId));  
  49.   
  50.     currentDestinationFacility = delegator.findByPrimaryKeyCache("Facility", UtilMisc.toMap("facilityId", destinationFacilityId));  
  51.     context.put("currentDestinationFacility", currentDestinationFacility);  
  52. }  
  53. if (UtilValidate.isNotEmpty(statusId)) {  
  54.     paramListBuffer.append("&statusId=");  
  55.     paramListBuffer.append(statusId);  
  56.     findShipmentExprs.add(new EntityExpr("statusId", EntityOperator.EQUALS, statusId));  
  57.   
  58.     currentStatus = delegator.findByPrimaryKeyCache("StatusItem", UtilMisc.toMap("statusId", statusId));  
  59.     context.put("currentStatus", currentStatus);  
  60. }  
  61.   
  62. if ("Y".equals(lookupFlag)) {  //如果有查看权限,则通过。
  63.     context.put("paramList", paramListBuffer.toString());  
  64.   
  65.     if (findShipmentExprs.size() > 0) {  
  66.         EntityFindOptions findOpts = new EntityFindOptions(true, EntityFindOptions.TYPE_SCROLL_INSENSITIVE, EntityFindOptions.CONCUR_READ_ONLY, true);  
  67.         mainCond = new EntityConditionList(findShipmentExprs, EntityOperator.AND);  
  68.         List orderBy = UtilMisc.toList("-estimatedShipDate");  
  69.   
  70.         boolean beganTransaction = false;  
  71.         try {  
  72.             beganTransaction = TransactionUtil.begin();  
  73.   
  74.             // using list iterator  
  75.             EntityListIterator orli = delegator.findListIteratorByCondition("Shipment", mainCond, nullnull, orderBy, findOpts);  
  76.   
  77.             // get the indexes for the partial list  
  78.             lowIndex = (((viewIndex - 1) * viewSize) + 1);  
  79.             highIndex = viewIndex * viewSize;  
  80.   
  81.             // 如果 highIndex 大于总记录数,则highIndex更改。
  82.             // 否则不变。
  83.             orli.last();  
  84.             shipmentListSize = orli.currentIndex();  
  85.             if (highIndex > shipmentListSize) {  
  86.                 highIndex = shipmentListSize;  
  87.             }  
  88.   
  89.             // get the partial list for this page  
  90.             orli.beforeFirst();  
  91.             if (shipmentListSize > 0) {  
  92.                 shipmentList = orli.getPartialList(lowIndex, viewSize);  
  93.             } else {  
  94.                 shipmentList = new ArrayList();  
  95.             }  
  96.   
  97.             // 在所记录的信息传递给shipmentList后,orli关闭。
  98.             orli.close();  
  99.         } catch (GenericEntityException e) {  
  100.             String errMsg = "Failure in operation, rolling back transaction";  
  101.             Debug.logError(e, errMsg, module);  
  102.             try {  
  103.                 // only rollback the transaction if we started one...  
  104.                 TransactionUtil.rollback(beganTransaction, errMsg, e);  
  105.             } catch (GenericEntityException e2) {  
  106.                 Debug.logError(e2, "Could not rollback transaction: " + e2.toString(), module);  
  107.             }  
  108.             // after rolling back, rethrow the exception  
  109.             throw e;  
  110.         } finally {  
  111.             // only commit the transaction if we started one... this will throw an exception if it fails  
  112.             TransactionUtil.commit(beganTransaction);  
  113.         }  
  114.     } else {  //没有权限
  115.         shipmentList = new ArrayList();  
  116.         shipmentListSize = 0;  
  117.         highIndex = 0;  
  118.         lowIndex = 0;  
  119.     }  
  120.   //设置返回信息。
  121.     context.put("shipmentList", shipmentList);  
  122.     context.put("listSize", shipmentListSize);  
  123.     context.put("highIndex", highIndex);  
  124.     context.put("lowIndex", lowIndex);  
  125. }  


你可能感兴趣的:(ofbiz)