Fixed issue 32950.Use HashSet instead of ArrayList to avoid duplicates
authorGorka Ion Damián <gorkaion.damian@openbravo.com>
Fri, 13 May 2016 11:26:47 +0200
changeset 29505 ea18c2c210a6
parent 29504 84003e2e052d
child 29506 9f9148601089
Fixed issue 32950.Use HashSet instead of ArrayList to avoid duplicates

The product ids are now stored using HashSet as this implementation does not
allow duplicates.

The method to create the vars in some scenarios is throwing a OBException that
is properly catch. It has been changed the try to avoid the OBException and
remove the error message from the logs.
src/org/openbravo/event/ProductCharacteristicValueEventHandler.java
--- a/src/org/openbravo/event/ProductCharacteristicValueEventHandler.java	Fri May 13 07:18:46 2016 +0200
+++ b/src/org/openbravo/event/ProductCharacteristicValueEventHandler.java	Fri May 13 11:26:47 2016 +0200
@@ -18,9 +18,9 @@
  */
 package org.openbravo.event;
 
-import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.List;
+import java.util.HashSet;
+import java.util.Set;
 
 import javax.enterprise.event.Observes;
 
@@ -46,7 +46,7 @@
   protected Logger logger = Logger.getLogger(this.getClass());
   private static Entity[] entities = { ModelProvider.getInstance().getEntity(
       ProductCharacteristicValue.ENTITY_NAME) };
-  private static ThreadLocal<List<String>> prodchvalueUpdated = new ThreadLocal<List<String>>();
+  private static ThreadLocal<Set<String>> prodchvalueUpdated = new ThreadLocal<Set<String>>();
 
   @Override
   protected Entity[] getObservedEntities() {
@@ -55,7 +55,7 @@
 
   @SuppressWarnings("unused")
   public void onTransactionBegin(@Observes TransactionBeginEvent event) {
-    prodchvalueUpdated.set(null);
+    prodchvalueUpdated.set(new HashSet<String>());
   }
 
   public void onNew(@Observes EntityNewEvent event) {
@@ -83,7 +83,7 @@
   }
 
   public void onTransactionCompleted(@Observes TransactionCompletedEvent event) {
-    List<String> productList = prodchvalueUpdated.get();
+    Set<String> productList = prodchvalueUpdated.get();
     prodchvalueUpdated.set(null);
     prodchvalueUpdated.remove();
     if (productList == null || productList.isEmpty() || event.getTransaction().wasRolledBack()) {
@@ -92,7 +92,14 @@
     try {
       VariablesSecureApp vars = null;
       try {
-        vars = RequestContext.get().getVariablesSecureApp();
+        if (RequestContext.get().getRequest() == null) {
+          vars = new VariablesSecureApp(OBContext.getOBContext().getUser().getId(), OBContext
+              .getOBContext().getCurrentClient().getId(), OBContext.getOBContext()
+              .getCurrentOrganization().getId(), OBContext.getOBContext().getRole().getId(),
+              OBContext.getOBContext().getLanguage().getLanguage());
+        } else {
+          vars = RequestContext.get().getVariablesSecureApp();
+        }
       } catch (Exception e) {
         vars = new VariablesSecureApp(OBContext.getOBContext().getUser().getId(), OBContext
             .getOBContext().getCurrentClient().getId(), OBContext.getOBContext()
@@ -115,9 +122,9 @@
   }
 
   private void addProductToList(ProductCharacteristicValue pchv) {
-    List<String> productList = prodchvalueUpdated.get();
+    Set<String> productList = prodchvalueUpdated.get();
     if (productList == null) {
-      productList = new ArrayList<String>();
+      productList = new HashSet<String>();
     }
     productList.add(pchv.getProduct().getId());
     prodchvalueUpdated.set(productList);