svn commit: r1797792 - /ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/content/ContentEvents.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1797792 - /ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/content/ContentEvents.java

jleroux@apache.org
Author: jleroux
Date: Tue Jun  6 14:58:11 2017
New Revision: 1797792

URL: http://svn.apache.org/viewvc?rev=1797792&view=rev
Log:
No functional changes.

Replaces EntityListIterator.close() in finally by try-with-ressource

With, mmm..., some refactoring. For instance I don't like re-throwing an
exception
and I'm not alone
https://martinfowler.com/articles/replaceThrowWithNotification.html

In case of event I prefer when we use something like
   request.setAttribute("_ERROR_MESSAGE_", errMsg);
like also suggests Martin Fowler

So I did that, of course it's tested, as similar changes done already

Modified:
    ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/content/ContentEvents.java

Modified: ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/content/ContentEvents.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/content/ContentEvents.java?rev=1797792&r1=1797791&r2=1797792&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/content/ContentEvents.java (original)
+++ ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/content/ContentEvents.java Tue Jun  6 14:58:11 2017
@@ -69,73 +69,52 @@ public class ContentEvents {
             return "error";
         }
 
-        EntityListIterator entityListIterator = null;
         int numConts = 0;
         int errConts = 0;
 
         boolean beganTx = false;
+        EntityQuery contentQuery = EntityQuery.use(delegator).from("Content");
+        
         try {
             // begin the transaction
             beganTx = TransactionUtil.begin(7200);
-            try {
-                EntityQuery contentQuery = EntityQuery.use(delegator).from("Content");
-                if (Debug.infoOn()) {
-                    long count = contentQuery.queryCount();
-                    Debug.logInfo("========== Found " + count + " contents to index ==========", module);
-                }
-                entityListIterator = contentQuery.queryIterator();
-            } catch (GenericEntityException gee) {
-                Debug.logWarning(gee, gee.getMessage(), module);
-                Map<String, String> messageMap = UtilMisc.toMap("gee", gee.toString());
-                errMsg = UtilProperties.getMessage(resource,"contentevents.error_getting_content_list", messageMap, UtilHttp.getLocale(request));
-                request.setAttribute("_ERROR_MESSAGE_", errMsg);
-                throw gee;
+            if (Debug.infoOn()) {
+                long count = contentQuery.queryCount();
+                Debug.logInfo("========== Found " + count + " contents to index ==========", module);
             }
-
             GenericValue content;
-            while ((content = entityListIterator.next()) != null) {
-                try {
+            try (EntityListIterator entityListIterator = contentQuery.queryIterator()) {
+                while ((content = entityListIterator.next()) != null) {
                     ContentKeywordIndex.indexKeywords(content, "Y".equals(doAll));
-                } catch (GenericEntityException e) {
-                    Debug.logWarning("[ContentEvents.updateAllContentKeywords] Could not create content-keyword (write error); message: " + e.getMessage(), module);
-                    errConts++;
-                }
-                numConts++;
-                if (numConts % 500 == 0) {
-                    Debug.logInfo("Keywords indexed for " + numConts + " so far", module);
+                    numConts++;
+                    if (numConts % 500 == 0) {
+                        Debug.logInfo("Keywords indexed for " + numConts + " so far", module);
+                    }
                 }
+            } catch (GenericEntityException e) {
+                errMsg = "[ContentEvents.updateAllContentKeywords] Could not create content-keyword (write error); message: " + e.getMessage();
+                Debug.logWarning(errMsg, module);
+                errConts++;
+                request.setAttribute("_ERROR_MESSAGE_", errMsg);
             }
-        } catch (GenericEntityException e) {
+        } catch (GenericEntityException gee) {
+            Debug.logWarning(gee, gee.getMessage(), module);
+            Map<String, String> messageMap = UtilMisc.toMap("gee", gee.toString());
+            errMsg = UtilProperties.getMessage(resource,"contentevents.error_getting_content_list", messageMap, UtilHttp.getLocale(request));
+            request.setAttribute("_ERROR_MESSAGE_", errMsg);
             try {
-                TransactionUtil.rollback(beganTx, e.getMessage(), e);
+                TransactionUtil.rollback(beganTx, gee.getMessage(), gee);
             } catch (GenericTransactionException e1) {
                 Debug.logError(e1, module);
             }
             return "error";
-        } catch (Throwable t) {
-            Debug.logError(t, module);
-            request.setAttribute("_ERROR_MESSAGE_", t.getMessage());
-            try {
-                TransactionUtil.rollback(beganTx, t.getMessage(), t);
-            } catch (GenericTransactionException e2) {
-                Debug.logError(e2, module);
-            }
-            return "error";
-        } finally {
-            if (entityListIterator != null) {
-                try {
-                    entityListIterator.close();
-                } catch (GenericEntityException gee) {
-                    Debug.logError(gee, "Error closing EntityListIterator when indexing content keywords.", module);
-                }
-            }
 
-            // commit the transaction
-            try {
-                TransactionUtil.commit(beganTx);
-            } catch (GenericTransactionException e) {
-                Debug.logError(e, module);
-            }
+        }
+        // commit the transaction
+        try {
+            TransactionUtil.commit(beganTx);
+        } catch (GenericTransactionException e) {
+            Debug.logError(e, module);
         }
 
         if (errConts == 0) {