diff --git a/applications/content/src/main/java/org/apache/ofbiz/content/content/ContentSearch.java b/applications/content/src/main/java/org/apache/ofbiz/content/content/ContentSearch.java index 834f74dea1..77060a0b0e 100644 --- a/applications/content/src/main/java/org/apache/ofbiz/content/content/ContentSearch.java +++ b/applications/content/src/main/java/org/apache/ofbiz/content/content/ContentSearch.java @@ -168,14 +168,11 @@ public class ContentSearch { long startMillis = System.currentTimeMillis(); // do the query - EntityListIterator eli = this.doQuery(delegator); - ArrayList contentIds = this.makeContentIdList(eli); - if (eli != null) { - try { - eli.close(); - } catch (GenericEntityException e) { - Debug.logError(e, "Error closing ContentSearch EntityListIterator"); - } + ArrayList contentIds = null; + try (EntityListIterator eli = this.doQuery(delegator)) { + contentIds = this.makeContentIdList(eli); + } catch (GenericEntityException e) { + Debug.logError(e, "Error closing ContentSearch EntityListIterator"); } long endMillis = System.currentTimeMillis(); diff --git a/applications/order/src/main/java/org/apache/ofbiz/order/order/OrderServices.java b/applications/order/src/main/java/org/apache/ofbiz/order/order/OrderServices.java index c115ecb1a1..b8066d97a5 100644 --- a/applications/order/src/main/java/org/apache/ofbiz/order/order/OrderServices.java +++ b/applications/order/src/main/java/org/apache/ofbiz/order/order/OrderServices.java @@ -1503,43 +1503,36 @@ public class OrderServices { cond = EntityCondition.makeCondition(exprs, EntityOperator.OR); } - EntityListIterator eli = null; - try { - eli = EntityQuery.use(delegator).select("orderId").from("OrderHeader").where(cond).queryIterator(); + try (EntityListIterator eli = EntityQuery.use(delegator) + .select("orderId") + .from("OrderHeader") + .where(cond) + .queryIterator()) { + + if (eli != null) { + // reset each order + GenericValue orderHeader = null; + while ((orderHeader = eli.next()) != null) { + String orderId = orderHeader.getString("orderId"); + Map resetResult = null; + try { + resetResult = dispatcher.runSync("resetGrandTotal", UtilMisc.toMap("orderId", orderId, "userLogin", userLogin)); + } catch (GenericServiceException e) { + Debug.logError(e, "ERROR: Cannot reset order totals - " + orderId, module); + } + + if (resetResult != null && ServiceUtil.isError(resetResult)) { + Debug.logWarning(UtilProperties.getMessage(resource_error, + "OrderErrorCannotResetOrderTotals", + UtilMisc.toMap("orderId",orderId,"resetResult",ServiceUtil.getErrorMessage(resetResult)), locale), module); + } else { + Debug.logInfo("No orders found for reset processing", module); + } + } + } } catch (GenericEntityException e) { Debug.logError(e, module); - return ServiceUtil.returnError(e.getMessage()); } - - if (eli != null) { - // reset each order - GenericValue orderHeader = null; - while ((orderHeader = eli.next()) != null) { - String orderId = orderHeader.getString("orderId"); - Map resetResult = null; - try { - resetResult = dispatcher.runSync("resetGrandTotal", UtilMisc.toMap("orderId", orderId, "userLogin", userLogin)); - } catch (GenericServiceException e) { - Debug.logError(e, "ERROR: Cannot reset order totals - " + orderId, module); - } - - if (resetResult != null && ServiceUtil.isError(resetResult)) { - Debug.logWarning(UtilProperties.getMessage(resource_error, - "OrderErrorCannotResetOrderTotals", - UtilMisc.toMap("orderId",orderId,"resetResult",ServiceUtil.getErrorMessage(resetResult)), locale), module); - } - } - - // close the ELI - try { - eli.close(); - } catch (GenericEntityException e) { - Debug.logError(e, module); - } - } else { - Debug.logInfo("No orders found for reset processing", module); - } - return ServiceUtil.returnSuccess(); } @@ -1955,7 +1948,7 @@ public class OrderServices { } } } catch (GenericEntityException e) { - String errMsg = UtilProperties.getMessage(resource_error, "OrderDatabaseErrorCheckingIfWeShouldChangeOrderHeaderStatusToApproved", UtilMisc.toMap("errorString", e.toString()), locale); + String errMsg = UtilProperties.getMessage(resource_error, "OrderDatabaseErrorCheckingIfWeShouldChangeOrderHeaderStatusToApproved", UtilMisc.toMap("errorString", e.toString()), locale); Debug.logError(e, errMsg, module); return ServiceUtil.returnError(errMsg); } @@ -2014,11 +2007,11 @@ public class OrderServices { Map itemCommentMap = UtilGenerics.checkMap(context.get("itemCommentMap")); Map itemQuantityMap = UtilGenerics.checkMap(context.get("itemQtyMap")); if ((cancelQuantity == null) && UtilValidate.isNotEmpty(itemQuantityMap)) { - String key = orderItemSeqId+":"+shipGroupSeqId; - if (UtilValidate.isNotEmpty(itemQuantityMap.get(key))) { - cancelQuantity = new BigDecimal(itemQuantityMap.get(key)); - } - + String key = orderItemSeqId+":"+shipGroupSeqId; + if (UtilValidate.isNotEmpty(itemQuantityMap.get(key))) { + cancelQuantity = new BigDecimal(itemQuantityMap.get(key)); + } + } // debugging message info String itemMsgInfo = orderId + " / " + orderItemSeqId + " / " + shipGroupSeqId; @@ -2119,7 +2112,7 @@ public class OrderServices { "orderItemSeqId", orderItem.getString("orderItemSeqId"), "shipGroupSeqId", orderItemShipGroupAssoc.getString("shipGroupSeqId")); if (availableQuantity.compareTo(thisCancelQty) == 0) { - try { + try { dispatcher.runSync("deleteOrderItemShipGroupAssoc", localCtx); } catch (GenericServiceException e) { Debug.logError(e, module); @@ -5569,15 +5562,15 @@ public class OrderServices { Map result = null; boolean beganTransaction = false; - try { + List exprs = UtilMisc.toList(EntityCondition.makeCondition("automaticExtend", EntityOperator.EQUALS, "Y"), + EntityCondition.makeCondition("orderId", EntityOperator.NOT_EQUAL, null), + EntityCondition.makeCondition("productId", EntityOperator.NOT_EQUAL, null)); + try (EntityListIterator eli = EntityQuery.use(delegator) + .from("Subscription") + .where(exprs) + .queryIterator()) { + beganTransaction = TransactionUtil.begin(); - - List exprs = UtilMisc.toList(EntityCondition.makeCondition("automaticExtend", EntityOperator.EQUALS, "Y"), - EntityCondition.makeCondition("orderId", EntityOperator.NOT_EQUAL, null), - EntityCondition.makeCondition("productId", EntityOperator.NOT_EQUAL, null)); - EntityListIterator eli = null; - eli = EntityQuery.use(delegator).from("Subscription").where(exprs).queryIterator(); - if (eli != null) { GenericValue subscription; while (((subscription = eli.next()) != null)) { @@ -5655,7 +5648,6 @@ public class OrderServices { count++; } } - eli.close(); } } catch (GenericServiceException e) { @@ -6202,17 +6194,17 @@ public class OrderServices { @Override public List call() throws Exception { List orderIds = new LinkedList(); - EntityListIterator eli = null; - try { - eli = EntityQuery.use(delegator).select("orderId").from("OrderHeader").where(cond).orderBy("entryDate ASC").queryIterator(); + try (EntityListIterator eli = EntityQuery.use(delegator) + .select("orderId") + .from("OrderHeader") + .where(cond) + .orderBy("entryDate ASC") + .queryIterator()){ + GenericValue orderHeader; while ((orderHeader = eli.next()) != null) { orderIds.add(orderHeader.getString("orderId")); } - } finally { - if (eli != null) { - eli.close(); - } } return orderIds; } diff --git a/applications/order/src/main/java/org/apache/ofbiz/order/shoppinglist/ShoppingListServices.java b/applications/order/src/main/java/org/apache/ofbiz/order/shoppinglist/ShoppingListServices.java index d1d414cb2a..41029c8ee8 100644 --- a/applications/order/src/main/java/org/apache/ofbiz/order/shoppinglist/ShoppingListServices.java +++ b/applications/order/src/main/java/org/apache/ofbiz/order/shoppinglist/ShoppingListServices.java @@ -125,12 +125,13 @@ public class ShoppingListServices { Locale locale = (Locale) context.get("locale"); boolean beganTransaction = false; - try { + try (EntityListIterator eli = EntityQuery.use(delegator) + .from("ShoppingList") + .where("shoppingListTypeId", "SLT_AUTO_REODR", "isActive", "Y") + .orderBy("-lastOrderedDate") + .queryIterator()) { + beganTransaction = TransactionUtil.begin(); - - EntityListIterator eli = null; - eli = EntityQuery.use(delegator).from("ShoppingList").where("shoppingListTypeId", "SLT_AUTO_REODR", "isActive", "Y").orderBy("-lastOrderedDate").queryIterator(); - if (eli != null) { GenericValue shoppingList; while (((shoppingList = eli.next()) != null)) { @@ -197,8 +198,6 @@ public class ShoppingListServices { recurrence.incrementCurrentCount(); } } - - eli.close(); } return ServiceUtil.returnSuccess(); diff --git a/applications/product/src/main/java/org/apache/ofbiz/product/product/ProductSearchSession.java b/applications/product/src/main/java/org/apache/ofbiz/product/product/ProductSearchSession.java index 3ba9f3165f..39a03b36b6 100644 --- a/applications/product/src/main/java/org/apache/ofbiz/product/product/ProductSearchSession.java +++ b/applications/product/src/main/java/org/apache/ofbiz/product/product/ProductSearchSession.java @@ -1229,33 +1229,26 @@ public class ProductSearchSession { dynamicViewEntity.addViewLink("PFAC", "PFC", Boolean.FALSE, ModelKeyMap.makeKeyMapList("productFeatureId")); entityConditionList.add(EntityCondition.makeCondition("pfcProductFeatureTypeId", EntityOperator.EQUALS, productFeatureTypeId)); - EntityListIterator eli = null; - try { - eli = EntityQuery.use(delegator) + List> featureCountList = null; + try (EntityListIterator eli = EntityQuery.use(delegator) .select(UtilMisc.toSet("pfacProductFeatureId", "featureCount", "pfcDescription", "pfcProductFeatureTypeId")) .from(dynamicViewEntity) .where(entityConditionList) .orderBy(productSearchContext.orderByList) .cursorScrollInsensitive() - .queryIterator(); + .queryIterator()) { + + featureCountList = new LinkedList>(); + GenericValue searchResult = null; + while ((searchResult = eli.next()) != null) { + featureCountList.add(UtilMisc.toMap("productFeatureId", (String) searchResult.get("pfacProductFeatureId"), "productFeatureTypeId", (String) searchResult.get("pfcProductFeatureTypeId"), "description", (String) searchResult.get("pfcDescription"), "featureCount", Long.toString((Long) searchResult.get("featureCount")))); + } } catch (GenericEntityException e) { Debug.logError(e, "Error in product search", module); return null; } - List> featureCountList = new LinkedList>(); - GenericValue searchResult = null; - while ((searchResult = eli.next()) != null) { - featureCountList.add(UtilMisc.toMap("productFeatureId", (String) searchResult.get("pfacProductFeatureId"), "productFeatureTypeId", (String) searchResult.get("pfcProductFeatureTypeId"), "description", (String) searchResult.get("pfcDescription"), "featureCount", Long.toString((Long) searchResult.get("featureCount")))); - } - if (eli != null) { - try { - eli.close(); - } catch (GenericEntityException e) { - Debug.logError(e, "Error closing ProductSearch EntityListIterator"); - } - } return featureCountList; } @@ -1308,31 +1301,21 @@ public class ProductSearchSession { entityConditionList.add(EntityCondition.makeCondition("ppcPrice", EntityOperator.LESS_THAN_EQUAL_TO, priceHigh)); entityConditionList.add(EntityCondition.makeCondition("ppcProductPriceTypeId", EntityOperator.EQUALS, "LIST_PRICE")); - EntityListIterator eli = null; - try { - eli = EntityQuery.use(delegator).select(UtilMisc.toSet(fieldsToSelect)) - .from(dynamicViewEntity) - .where(entityConditionList) - .orderBy(productSearchContext.orderByList) - .cursorScrollInsensitive() - .queryIterator(); + Long priceRangeCount = Long.valueOf(0); + try (EntityListIterator eli = EntityQuery.use(delegator) + .select(UtilMisc.toSet(fieldsToSelect)) + .from(dynamicViewEntity) + .where(entityConditionList) + .orderBy(productSearchContext.orderByList) + .cursorScrollInsensitive() + .queryIterator()) { + + GenericValue searchResult = null; + while ((searchResult = eli.next()) != null) { + priceRangeCount = searchResult.getLong("priceRangeCount"); + } } catch (GenericEntityException e) { Debug.logError(e, "Error in product search", module); - return 0; - } - - GenericValue searchResult = null; - Long priceRangeCount = Long.valueOf(0); - while ((searchResult = eli.next()) != null) { - priceRangeCount = searchResult.getLong("priceRangeCount"); - } - - if (eli != null) { - try { - eli.close(); - } catch (GenericEntityException e) { - Debug.logError(e, "Error closing ProductSearch EntityListIterator"); - } } return priceRangeCount; } @@ -1374,31 +1357,21 @@ public class ProductSearchSession { ProductSearch.getAllSubCategoryIds(productCategoryId, productCategoryIdSet, delegator, productSearchContext.nowTimestamp); entityConditionList.add(EntityCondition.makeCondition("pcmcProductCategoryId", EntityOperator.IN, productCategoryIdSet)); - EntityListIterator eli = null; - try { - eli = EntityQuery.use(delegator).select(UtilMisc.toSet(fieldsToSelect)) - .from(dynamicViewEntity) - .where(entityConditionList) - .orderBy(productSearchContext.orderByList) - .cursorScrollInsensitive() - .queryIterator(); + Long categoryCount = Long.valueOf(0); + try (EntityListIterator eli = EntityQuery.use(delegator) + .select(UtilMisc.toSet(fieldsToSelect)) + .from(dynamicViewEntity) + .where(entityConditionList) + .orderBy(productSearchContext.orderByList) + .cursorScrollInsensitive() + .queryIterator()) { + + GenericValue searchResult = null; + while ((searchResult = eli.next()) != null) { + categoryCount = searchResult.getLong("categoryCount"); + } } catch (GenericEntityException e) { Debug.logError(e, "Error in product search", module); - return 0; - } - - GenericValue searchResult = null; - Long categoryCount = Long.valueOf(0); - while ((searchResult = eli.next()) != null) { - categoryCount = searchResult.getLong("categoryCount"); - } - - if (eli != null) { - try { - eli.close(); - } catch (GenericEntityException e) { - Debug.logError(e, "Error closing ProductSearch EntityListIterator"); - } } return categoryCount; } diff --git a/applications/product/src/main/java/org/apache/ofbiz/shipment/shipment/ShipmentServices.java b/applications/product/src/main/java/org/apache/ofbiz/shipment/shipment/ShipmentServices.java index 782030ca4e..1806e11e29 100644 --- a/applications/product/src/main/java/org/apache/ofbiz/shipment/shipment/ShipmentServices.java +++ b/applications/product/src/main/java/org/apache/ofbiz/shipment/shipment/ShipmentServices.java @@ -670,9 +670,11 @@ public class ShipmentServices { Locale locale = (Locale) context.get("locale"); Map shipmentMap = new HashMap(); - EntityListIterator eli = null; - try { - eli = EntityQuery.use(delegator).from("OdbcPackageIn").orderBy("shipmentId", "shipmentPackageSeqId", "voidIndicator").queryIterator(); + try (EntityListIterator eli = EntityQuery.use(delegator) + .from("OdbcPackageIn") + .orderBy("shipmentId", "shipmentPackageSeqId", "voidIndicator") + .queryIterator()) { + GenericValue pkgInfo; while ((pkgInfo = eli.next()) != null) { String packageSeqId = pkgInfo.getString("shipmentPackageSeqId"); @@ -779,14 +781,6 @@ public class ShipmentServices { } catch (GenericEntityException e) { Debug.logError(e, module); return ServiceUtil.returnError(e.getMessage()); - } finally { - if (eli != null) { - try { - eli.close(); - } catch (GenericEntityException e) { - Debug.logError(e, module); - } - } } // update the status of each shipment diff --git a/framework/entity/src/main/java/org/apache/ofbiz/entity/GenericDelegator.java b/framework/entity/src/main/java/org/apache/ofbiz/entity/GenericDelegator.java index 5c1f30c1c6..0116fe870d 100644 --- a/framework/entity/src/main/java/org/apache/ofbiz/entity/GenericDelegator.java +++ b/framework/entity/src/main/java/org/apache/ofbiz/entity/GenericDelegator.java @@ -1581,17 +1581,9 @@ public class GenericDelegator implements Delegator { beganTransaction = TransactionUtil.begin(); } - EntityListIterator eli = null; List list = null; - try { - eli = this.find(entityName, entityCondition, null, fieldsToSelect, orderBy, findOptions); + try (EntityListIterator eli = this.find(entityName, entityCondition, null, fieldsToSelect, orderBy, findOptions)) { list = eli.getCompleteList(); - } finally { - if (eli != null) { - try { - eli.close(); - } catch (Exception exc) {} - } } if (useCache) { diff --git a/framework/entityext/src/main/java/org/apache/ofbiz/entityext/data/EntityDataServices.java b/framework/entityext/src/main/java/org/apache/ofbiz/entityext/data/EntityDataServices.java index 518dedce05..316824a1ec 100644 --- a/framework/entityext/src/main/java/org/apache/ofbiz/entityext/data/EntityDataServices.java +++ b/framework/entityext/src/main/java/org/apache/ofbiz/entityext/data/EntityDataServices.java @@ -32,7 +32,6 @@ import java.util.Locale; import java.util.Map; import org.apache.commons.codec.binary.Base64; -import org.apache.shiro.crypto.AesCipherService; import org.apache.ofbiz.base.crypto.DesCrypt; import org.apache.ofbiz.base.util.Debug; import org.apache.ofbiz.base.util.FileUtil; @@ -55,6 +54,7 @@ import org.apache.ofbiz.service.DispatchContext; import org.apache.ofbiz.service.GenericServiceException; import org.apache.ofbiz.service.LocalDispatcher; import org.apache.ofbiz.service.ServiceUtil; +import org.apache.shiro.crypto.AesCipherService; /** * Entity Data Import/Export Services @@ -422,9 +422,10 @@ public class EntityDataServices { String entityName = (String) context.get("entityName"); String fieldName = (String) context.get("fieldName"); Locale locale = (Locale) context.get("locale"); - EntityListIterator eli = null; - try { - eli = EntityQuery.use(delegator).from(entityName).queryIterator(); + try (EntityListIterator eli = EntityQuery.use(delegator) + .from(entityName) + .queryIterator()) { + GenericValue currentValue; while ((currentValue = eli.next()) != null) { byte[] bytes = currentValue.getBytes(fieldName); @@ -436,15 +437,6 @@ public class EntityDataServices { } catch (GenericEntityException e) { Debug.logError(e, "Error unwrapping ByteWrapper records: " + e.toString(), module); return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtErrorUnwrappingRecords", UtilMisc.toMap("errorString", e.toString()), locale)); - } finally { - if (eli != null) { - try { - eli.close(); - } catch (GenericEntityException e) { - String errMsg = "Error closing EntityListIterator: " + e.toString(); - Debug.logError(e, errMsg, module); - } - } } return ServiceUtil.returnSuccess();