details:   https://code.openbravo.com/erp/devel/pi/rev/992caa87c40b
changeset: 29983:992caa87c40b
user:      Gorka Ion Damián <gorkaion.damian <at> openbravo.com>
date:      Wed Aug 31 15:02:29 2016 +0200
summary:   Fixed issue 33210.Related to issue 32950. Use import entry instead 
of ProcessBundle

Performance fixes on EventHandler.
- Use import entry instead of ProcessBundle.

diffstat:

 src-db/database/sourcedata/AD_REF_LIST.xml                          |  11 +
 src/org/openbravo/event/ProductCharacteristicValueEventHandler.java |  59 
++----
 src/org/openbravo/materialmgmt/VariantChDescUpdateProcessor.java    |  83 
++++++++++
 3 files changed, 116 insertions(+), 37 deletions(-)

diffs (212 lines):

diff -r 9a7c89992e60 -r 992caa87c40b src-db/database/sourcedata/AD_REF_LIST.xml
--- a/src-db/database/sourcedata/AD_REF_LIST.xml        Mon Aug 29 13:14:09 
2016 -0400
+++ b/src-db/database/sourcedata/AD_REF_LIST.xml        Wed Aug 31 15:02:29 
2016 +0200
@@ -11927,6 +11927,17 @@
 <!--C51B76C267CA4E098F5558F3E0BAC2C2-->  
<AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
 <!--C51B76C267CA4E098F5558F3E0BAC2C2--></AD_REF_LIST>
 
+<!--C5834B6433874B9683A1DDB595843ADA--><AD_REF_LIST>
+<!--C5834B6433874B9683A1DDB595843ADA-->  
<AD_REF_LIST_ID><![CDATA[C5834B6433874B9683A1DDB595843ADA]]></AD_REF_LIST_ID>
+<!--C5834B6433874B9683A1DDB595843ADA-->  
<AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--C5834B6433874B9683A1DDB595843ADA-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--C5834B6433874B9683A1DDB595843ADA-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--C5834B6433874B9683A1DDB595843ADA-->  
<VALUE><![CDATA[VariantChDescUpdate]]></VALUE>
+<!--C5834B6433874B9683A1DDB595843ADA-->  <NAME><![CDATA[Variant Characteristic 
description update]]></NAME>
+<!--C5834B6433874B9683A1DDB595843ADA-->  
<AD_REFERENCE_ID><![CDATA[11F86B630ECB4A57B28927193F8AB99D]]></AD_REFERENCE_ID>
+<!--C5834B6433874B9683A1DDB595843ADA-->  
<AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--C5834B6433874B9683A1DDB595843ADA--></AD_REF_LIST>
+
 <!--C6756C85CF3F4AFBB78DFB994952548A--><AD_REF_LIST>
 <!--C6756C85CF3F4AFBB78DFB994952548A-->  
<AD_REF_LIST_ID><![CDATA[C6756C85CF3F4AFBB78DFB994952548A]]></AD_REF_LIST_ID>
 <!--C6756C85CF3F4AFBB78DFB994952548A-->  
<AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
diff -r 9a7c89992e60 -r 992caa87c40b 
src/org/openbravo/event/ProductCharacteristicValueEventHandler.java
--- a/src/org/openbravo/event/ProductCharacteristicValueEventHandler.java       
Mon Aug 29 13:14:09 2016 -0400
+++ b/src/org/openbravo/event/ProductCharacteristicValueEventHandler.java       
Wed Aug 31 15:02:29 2016 +0200
@@ -18,35 +18,36 @@
  */
 package org.openbravo.event;
 
-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Set;
 
 import javax.enterprise.event.Observes;
+import javax.inject.Inject;
 
 import org.apache.log4j.Logger;
+import org.codehaus.jettison.json.JSONArray;
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONObject;
 import org.openbravo.base.model.Entity;
 import org.openbravo.base.model.ModelProvider;
-import org.openbravo.base.secureApp.VariablesSecureApp;
-import org.openbravo.client.kernel.RequestContext;
 import org.openbravo.client.kernel.event.EntityDeleteEvent;
 import org.openbravo.client.kernel.event.EntityNewEvent;
 import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
 import org.openbravo.client.kernel.event.EntityUpdateEvent;
 import org.openbravo.client.kernel.event.TransactionBeginEvent;
 import org.openbravo.client.kernel.event.TransactionCompletedEvent;
-import org.openbravo.dal.core.OBContext;
-import org.openbravo.materialmgmt.VariantChDescUpdateProcess;
+import org.openbravo.dal.core.SessionHandler;
+import org.openbravo.erpCommon.utility.SequenceIdData;
 import org.openbravo.model.common.plm.ProductCharacteristicValue;
-import org.openbravo.scheduling.OBScheduler;
-import org.openbravo.scheduling.ProcessBundle;
-import org.openbravo.service.db.DalConnectionProvider;
+import org.openbravo.service.importprocess.ImportEntryManager;
 
 public class ProductCharacteristicValueEventHandler extends 
EntityPersistenceEventObserver {
   protected Logger logger = Logger.getLogger(this.getClass());
   private static Entity[] entities = { ModelProvider.getInstance().getEntity(
       ProductCharacteristicValue.ENTITY_NAME) };
   private static ThreadLocal<Set<String>> prodchvalueUpdated = new 
ThreadLocal<Set<String>>();
+  @Inject
+  ImportEntryManager importEntryManager;
 
   @Override
   protected Entity[] getObservedEntities() {
@@ -88,36 +89,20 @@
     if (productList == null || productList.isEmpty() || 
event.getTransaction().wasRolledBack()) {
       return;
     }
+    JSONArray productIds = new JSONArray();
+    for (String productId : productList) {
+      productIds.put(productId);
+    }
+    JSONObject entryJson = new JSONObject();
     try {
-      VariablesSecureApp vars = null;
-      try {
-        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()
-            .getCurrentOrganization().getId(), 
OBContext.getOBContext().getRole().getId(),
-            OBContext.getOBContext().getLanguage().getLanguage());
-      }
-
-      for (String strProductId : productList) {
-        ProcessBundle pb = new 
ProcessBundle(VariantChDescUpdateProcess.AD_PROCESS_ID, vars)
-            .init(new DalConnectionProvider(false));
-        HashMap<String, Object> parameters = new HashMap<String, Object>();
-        parameters.put("mProductId", strProductId);
-        parameters.put("mChValueId", null);
-        pb.setParams(parameters);
-        OBScheduler.getInstance().schedule(pb);
-      }
-    } catch (Exception e) {
-      logger.error("Error executing process", e);
+      entryJson.put("productIds", productIds);
+    } catch (JSONException ignore) {
     }
+    if (!SessionHandler.getInstance().isCurrentTransactionActive()) {
+      SessionHandler.getInstance().beginNewTransaction();
+    }
+    importEntryManager.createImportEntry(SequenceIdData.getUUID(), 
"VariantChDescUpdate",
+        entryJson.toString());
   }
 
   private void addProductToList(ProductCharacteristicValue pchv) {
@@ -128,4 +113,4 @@
     productList.add(pchv.getProduct().getId());
     prodchvalueUpdated.set(productList);
   }
-}
\ No newline at end of file
+}
diff -r 9a7c89992e60 -r 992caa87c40b 
src/org/openbravo/materialmgmt/VariantChDescUpdateProcessor.java
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/openbravo/materialmgmt/VariantChDescUpdateProcessor.java  Wed Aug 
31 15:02:29 2016 +0200
@@ -0,0 +1,83 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
+ * Version 1.1  with a permitted attribution clause; you may not  use this
+ * file except in compliance with the License. You  may  obtain  a copy of
+ * the License at http://www.openbravo.com/legal/license.html
+ * Software distributed under the License  is  distributed  on  an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific  language  governing  rights  and  limitations
+ * under the License.
+ * The Original Code is Openbravo ERP.
+ * The Initial Developer of the Original Code is Openbravo SLU
+ * All portions are Copyright (C) 2016 Openbravo SLU
+ * All Rights Reserved.
+ * Contributor(s):  ______________________________________.
+ *************************************************************************
+ */
+package org.openbravo.materialmgmt;
+
+import javax.enterprise.context.ApplicationScoped;
+
+import org.codehaus.jettison.json.JSONArray;
+import org.codehaus.jettison.json.JSONObject;
+import org.openbravo.base.weld.WeldUtils;
+import org.openbravo.dal.core.OBContext;
+import org.openbravo.dal.core.SessionHandler;
+import org.openbravo.dal.service.OBDal;
+import org.openbravo.service.importprocess.ImportEntry;
+import org.openbravo.service.importprocess.ImportEntryManager;
+import 
org.openbravo.service.importprocess.ImportEntryManager.ImportEntryQualifier;
+import org.openbravo.service.importprocess.ImportEntryProcessor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@ImportEntryQualifier(entity = "VariantChDescUpdate")
+@ApplicationScoped
+public class VariantChDescUpdateProcessor extends ImportEntryProcessor {
+  private static final Logger log = 
LoggerFactory.getLogger(VariantChDescUpdateProcess.class);
+
+  @Override
+  protected ImportEntryProcessRunnable createImportEntryProcessRunnable() {
+    return 
WeldUtils.getInstanceFromStaticBeanManager(VariantChDescUpdateRunnable.class);
+  }
+
+  @Override
+  protected boolean canHandleImportEntry(ImportEntry importEntryInformation) {
+    return 
"VariantChDescUpdate".equals(importEntryInformation.getTypeofdata());
+  }
+
+  @Override
+  protected String getProcessSelectionKey(ImportEntry importEntry) {
+    return importEntry.getTypeofdata();
+  }
+
+  protected static class VariantChDescUpdateRunnable extends 
ImportEntryProcessRunnable {
+
+    @Override
+    protected void processEntry(ImportEntry importEntry) throws Exception {
+      OBContext.setAdminMode(true);
+      try {
+        JSONObject jsonInfo = new JSONObject(importEntry.getJsonInfo());
+        JSONArray productIds = jsonInfo.getJSONArray("productIds");
+        for (int i = 0; i < productIds.length(); i++) {
+          VariantChDescUpdateProcess process = WeldUtils
+              
.getInstanceFromStaticBeanManager(VariantChDescUpdateProcess.class);
+          try {
+            process.update(productIds.getString(i), null);
+          } catch (Exception e) {
+            log.error("Error updating product ch description", e);
+          }
+        }
+
+        
ImportEntryManager.getInstance().setImportEntryProcessed(importEntry.getId());
+        if (SessionHandler.isSessionHandlerPresent()) {
+          OBDal.getInstance().commitAndClose();
+        }
+      } finally {
+        OBContext.restorePreviousMode();
+      }
+    }
+  }
+}

------------------------------------------------------------------------------
_______________________________________________
Openbravo-commits mailing list
Openbravo-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to