Related To ISSUE-42784:Apply new HQL style in CharacteristicValueEventHandler
authorAtul Gaware <atul.gaware@openbravo.com>
Tue, 14 Jan 2020 07:50:35 +0530
changeset 36931 c449b2991a44
parent 36930 e4c208a269ae
child 36932 7fba53bcd24f
Related To ISSUE 42784:Apply new HQL style in CharacteristicValueEventHandler

**Applied SonarLint suggesstion
**Avoid try catch nesting to improve readability and understand well which
block will catch which exception.
*ThreadLocal variables should be cleaned up when no longer used, calling
set(null) to remove the value might keep the reference to this pointer in
the map, which can cause memory leak in some scenarios. Using remove()
is safer to avoid this issue.
src/org/openbravo/event/CharacteristicValueEventHandler.java
--- a/src/org/openbravo/event/CharacteristicValueEventHandler.java	Mon Jan 13 14:23:11 2020 +0530
+++ b/src/org/openbravo/event/CharacteristicValueEventHandler.java	Tue Jan 14 07:50:35 2020 +0530
@@ -57,7 +57,7 @@
   }
 
   public void onTransactionBegin(@Observes TransactionBeginEvent event) {
-    chvalueUpdated.set(null);
+    chvalueUpdated.remove();
   }
 
   public void onUpdate(@Observes EntityUpdateEvent event) {
@@ -103,23 +103,13 @@
 
   public void onTransactionCompleted(@Observes TransactionCompletedEvent event) {
     String strChValueId = chvalueUpdated.get();
-    chvalueUpdated.set(null);
+    chvalueUpdated.remove();
     if (StringUtils.isBlank(strChValueId)
         || event.getTransaction().getStatus() == TransactionStatus.ROLLED_BACK) {
       return;
     }
     try {
-      VariablesSecureApp vars = null;
-      try {
-        vars = RequestContext.get().getVariablesSecureApp();
-      } catch (Exception e) {
-        vars = new VariablesSecureApp(OBContext.getOBContext().getUser().getId(),
-            OBContext.getOBContext().getCurrentClient().getId(),
-            OBContext.getOBContext().getCurrentOrganization().getId(),
-            OBContext.getOBContext().getRole().getId(),
-            OBContext.getOBContext().getLanguage().getLanguage());
-      }
-
+      VariablesSecureApp vars = initializeVars();
       ProcessBundle pb = new ProcessBundle(VariantChDescUpdateProcess.AD_PROCESS_ID, vars)
           .init(new DalConnectionProvider(false));
       HashMap<String, Object> parameters = new HashMap<>();
@@ -131,4 +121,19 @@
       logger.error("Error executing process", e);
     }
   }
+
+  private VariablesSecureApp initializeVars() {
+    VariablesSecureApp vars = null;
+    try {
+      vars = RequestContext.get().getVariablesSecureApp();
+    } catch (Exception e) {
+      logger.info("Vars could not be initialized from RequestContext, initializing from OBContext");
+      vars = new VariablesSecureApp(OBContext.getOBContext().getUser().getId(),
+          OBContext.getOBContext().getCurrentClient().getId(),
+          OBContext.getOBContext().getCurrentOrganization().getId(),
+          OBContext.getOBContext().getRole().getId(),
+          OBContext.getOBContext().getLanguage().getLanguage());
+    }
+    return vars;
+  }
 }