[Tree Extension] use admin mode where needed
authorAsier Lostalé <asier.lostale@openbravo.com>
Fri, 28 Feb 2014 08:00:23 +0100
changeset 22587 ce18dd09a76c
parent 22586 4dd47343e21d
child 22588 a3110b245b28
[Tree Extension] use admin mode where needed
modules/org.openbravo.client.application/src/org/openbravo/client/application/event/TreeTablesEventHandler.java
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/event/TreeTablesEventHandler.java	Thu Feb 27 21:20:13 2014 +0100
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/event/TreeTablesEventHandler.java	Fri Feb 28 08:00:23 2014 +0100
@@ -37,6 +37,7 @@
 import org.openbravo.client.kernel.event.EntityDeleteEvent;
 import org.openbravo.client.kernel.event.EntityNewEvent;
 import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
+import org.openbravo.dal.core.OBContext;
 import org.openbravo.dal.service.OBCriteria;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.model.ad.datamodel.Table;
@@ -76,30 +77,40 @@
     if (!isValidEvent(event)) {
       return;
     }
-    BaseOBObject bob = event.getTargetInstance();
-    DataSourceService dataSource = getDataSource(bob.getEntity().getTableId());
-    if (dataSource == null) {
-      return;
+    OBContext.setAdminMode(true);
+    try {
+      BaseOBObject bob = event.getTargetInstance();
+      DataSourceService dataSource = getDataSource(bob.getEntity().getTableId());
+      if (dataSource == null) {
+        return;
+      }
+      JSONObject jsonBob = this.fromBobToJSONObject(bob);
+      Map<String, String> parameters = new HashMap<String, String>();
+      parameters.put("jsonBob", jsonBob.toString());
+      dataSource.add(parameters, null);
+    } finally {
+      OBContext.restorePreviousMode();
     }
-    JSONObject jsonBob = this.fromBobToJSONObject(bob);
-    Map<String, String> parameters = new HashMap<String, String>();
-    parameters.put("jsonBob", jsonBob.toString());
-    dataSource.add(parameters, null);
   }
 
   public void onDelete(@Observes EntityDeleteEvent event) {
     if (!isValidEvent(event)) {
       return;
     }
-    BaseOBObject bob = event.getTargetInstance();
-    DataSourceService dataSource = getDataSource(bob.getEntity().getTableId());
-    if (dataSource == null) {
-      return;
+    OBContext.setAdminMode(true);
+    try {
+      BaseOBObject bob = event.getTargetInstance();
+      DataSourceService dataSource = getDataSource(bob.getEntity().getTableId());
+      if (dataSource == null) {
+        return;
+      }
+      JSONObject jsonBob = this.fromBobToJSONObject(bob);
+      Map<String, String> parameters = new HashMap<String, String>();
+      parameters.put("jsonBob", jsonBob.toString());
+      dataSource.remove(parameters);
+    } finally {
+      OBContext.restorePreviousMode();
     }
-    JSONObject jsonBob = this.fromBobToJSONObject(bob);
-    Map<String, String> parameters = new HashMap<String, String>();
-    parameters.put("jsonBob", jsonBob.toString());
-    dataSource.remove(parameters);
   }
 
   private DataSourceService getDataSource(String tableId) {