[roleInheritance] Add access propagation tests for test suite
authorCarlos Aristu <carlos.aristu@openbravo.com>
Tue, 29 Sep 2015 12:49:29 +0200
changeset 27778 0bd6da797c24
parent 27777 e1c088010156
child 27779 d33c94ef7e1f
[roleInheritance] Add access propagation tests for test suite
src-test/src/org/openbravo/test/role/AccessPropagationNotInheritedTest.java
src-test/src/org/openbravo/test/role/AccessPropagationTest.java
src-test/src/org/openbravo/test/role/BasicADWindowInheritanceTest.java
src-test/src/org/openbravo/test/role/RoleInheritanceTestUtils.java
src/org/openbravo/roleInheritance/RoleInheritanceManager.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-test/src/org/openbravo/test/role/AccessPropagationNotInheritedTest.java	Tue Sep 29 12:49:29 2015 +0200
@@ -0,0 +1,145 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.1  (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) 2015 Openbravo SLU 
+ * All Rights Reserved. 
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+package org.openbravo.test.role;
+
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.Assert.assertThat;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.openbravo.base.weld.test.ParameterCdiTest;
+import org.openbravo.base.weld.test.ParameterCdiTestRule;
+import org.openbravo.base.weld.test.WeldBaseTest;
+import org.openbravo.dal.core.DalUtil;
+import org.openbravo.dal.core.OBContext;
+import org.openbravo.dal.service.OBDal;
+import org.openbravo.model.ad.access.Role;
+
+/**
+ * Test case to check that non inherited accesses are not affected by access propagation
+ * 
+ * We start having Role "role" which inherits from Role "template"
+ * 
+ * We add access A1 for "role", before adding the same access A1 for "template"
+ * 
+ * If we update access A1 for "template" this change must not affect A1 access for "role" because it
+ * has not been inherited
+ * 
+ */
+public class AccessPropagationNotInheritedTest extends WeldBaseTest {
+  private final List<String> ORGANIZATIONS = Arrays.asList("F&B España - Región Norte");
+  private final List<String> WINDOWS = Arrays.asList("Sales Invoice");
+  private final List<String> TABS = Arrays.asList("Bank Account");
+  private final List<String> FIELDS = Arrays.asList("Business Partner Category");
+  private final List<String> REPORTS = Arrays.asList("Alert Process");
+  private final List<String> FORMS = Arrays.asList("About");
+  private final List<String> WIDGETS = Arrays.asList("Best Sellers");
+  private final List<String> VIEWS = Arrays.asList("OBUIAPP_AlertManagement");
+  private final List<String> PROCESSES = Arrays.asList("Create Purchase Order Lines");
+  private final List<String> ALERTS = Arrays.asList("Alert Taxes: Inversión del Sujeto Pasivo");
+  private final List<String> PREFERENCES = Arrays.asList("AllowAttachment");
+
+  @SuppressWarnings("unchecked")
+  private final List<List<String>> ACCESSES = Arrays.asList(ORGANIZATIONS, WINDOWS, TABS, FIELDS,
+      REPORTS, FORMS, WIDGETS, VIEWS, PROCESSES, ALERTS, PREFERENCES);
+  private static int counterHorizontalTest = 0;
+
+  /** defines the values the parameter will take. */
+  @Rule
+  public ParameterCdiTestRule<String> parameterValuesRule = new ParameterCdiTestRule<String>(
+      RoleInheritanceTestUtils.ACCESS_NAMES);
+
+  /** this field will take the values defined by parameterValuesRule field. */
+  private @ParameterCdiTest
+  String parameter;
+
+  @Test
+  public void testNotInheritedAccessPropagation() {
+    Role role = null;
+    Role template = null;
+    try {
+      OBContext.setAdminMode(true);
+      // Create roles
+      role = RoleInheritanceTestUtils.createRole("role", RoleInheritanceTestUtils.CLIENT_ID,
+          RoleInheritanceTestUtils.ASTERISK_ORG_ID, " C", true, false);
+      String roleId = (String) DalUtil.getId(role);
+      template = RoleInheritanceTestUtils.createRole("template",
+          RoleInheritanceTestUtils.CLIENT_ID, RoleInheritanceTestUtils.ASTERISK_ORG_ID, " C", true,
+          true);
+      String templateId = (String) DalUtil.getId(template);
+
+      // Add inheritance
+      RoleInheritanceTestUtils.addInheritance(role, template, new Long(10));
+
+      OBDal.getInstance().commitAndClose();
+      role = OBDal.getInstance().get(Role.class, roleId);
+      template = OBDal.getInstance().get(Role.class, templateId);
+
+      List<String> accesses = ACCESSES.get(counterHorizontalTest);
+      // Add accesses
+      RoleInheritanceTestUtils.addAccess(parameter, role, accesses.get(0));
+      RoleInheritanceTestUtils.addAccess(parameter, template, accesses.get(0));
+
+      String[] expected = { accesses.get(0), "" };
+      String[] result = RoleInheritanceTestUtils.getOrderedAccessNames(parameter, role);
+      assertThat("New access has not affected non inherited access", result, equalTo(expected));
+
+      boolean value = false;
+      boolean active = false;
+      if (parameter.equals("ALERT") || parameter.equals("PREFERENCE")) {
+        value = true;
+      }
+      // Perform an update in the access of the parent
+      RoleInheritanceTestUtils.updateAccess(parameter, template, accesses.get(0), value, active);
+      OBDal.getInstance().commitAndClose();
+
+      role = OBDal.getInstance().get(Role.class, roleId);
+      template = OBDal.getInstance().get(Role.class, templateId);
+
+      String editedValue = !value + "";
+      String isActive = !active + "";
+      if (parameter.equals("REPORT") || parameter.equals("FORM") || parameter.equals("WIDGET")
+          || parameter.equals("VIEW") || parameter.equals("PROCESS")) {
+        // Accesses for Report, Form, Widget, View and Process just have the active flag
+        editedValue = "";
+      }
+
+      String[] expected2 = { editedValue, isActive, "" };
+      String[] result2 = RoleInheritanceTestUtils.getAccessInfo(parameter, role, accesses.get(0));
+      assertThat("Updated access has not affected non inherited access", result2,
+          equalTo(expected2));
+
+      RoleInheritanceTestUtils.removeAccesses(parameter, template);
+      RoleInheritanceTestUtils.removeAccesses(parameter, role);
+      counterHorizontalTest++;
+
+    } finally {
+      // Delete roles
+      RoleInheritanceTestUtils.deleteRole(role);
+      RoleInheritanceTestUtils.deleteRole(template);
+
+      OBDal.getInstance().commitAndClose();
+
+      OBContext.restorePreviousMode();
+    }
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-test/src/org/openbravo/test/role/AccessPropagationTest.java	Tue Sep 29 12:49:29 2015 +0200
@@ -0,0 +1,159 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.1  (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) 2015 Openbravo SLU 
+ * All Rights Reserved. 
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+package org.openbravo.test.role;
+
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.Assert.assertThat;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.openbravo.base.weld.test.ParameterCdiTest;
+import org.openbravo.base.weld.test.ParameterCdiTestRule;
+import org.openbravo.base.weld.test.WeldBaseTest;
+import org.openbravo.dal.core.DalUtil;
+import org.openbravo.dal.core.OBContext;
+import org.openbravo.dal.service.OBDal;
+import org.openbravo.model.ad.access.Role;
+
+/**
+ * Test case for access propagation
+ * 
+ * We start having Role "role" which inherits from Role "template"
+ * 
+ * We add access A1 for "template" and access A2 for "role"
+ * 
+ * If we update access A1 for "template" this change must be propagated for "role". In addition, A2
+ * access for "role" must remain without changes
+ * 
+ */
+public class AccessPropagationTest extends WeldBaseTest {
+  private final List<String> ORGANIZATIONS = Arrays.asList("F&B España - Región Norte",
+      "F&B España - Región Sur");
+  private final List<String> WINDOWS = Arrays.asList("Sales Invoice", "Sales Order");
+  private final List<String> TABS = Arrays.asList("Bank Account", "Basic Discount");
+  private final List<String> FIELDS = Arrays.asList("Business Partner Category", "Commercial Name");
+  private final List<String> REPORTS = Arrays.asList("Alert Process", "Create Variants");
+  private final List<String> FORMS = Arrays.asList("About", "Heartbeat");
+  private final List<String> WIDGETS = Arrays.asList("Best Sellers", "Invoices to collect");
+  private final List<String> VIEWS = Arrays.asList("OBUIAPP_AlertManagement",
+      "OBUIAPP_RegistrationView");
+  private final List<String> PROCESSES = Arrays.asList("Create Purchase Order Lines",
+      "Grant Portal Access");
+  private final List<String> ALERTS = Arrays.asList("Alert Taxes: Inversión del Sujeto Pasivo",
+      "CUSTOMER WITHOUT ACCOUNTING");
+  private final List<String> PREFERENCES = Arrays.asList("AllowAttachment", "AllowDelete");
+
+  @SuppressWarnings("unchecked")
+  private final List<List<String>> ACCESSES = Arrays.asList(ORGANIZATIONS, WINDOWS, TABS, FIELDS,
+      REPORTS, FORMS, WIDGETS, VIEWS, PROCESSES, ALERTS, PREFERENCES);
+  private static int counterHorizontalTest = 0;
+
+  /** defines the values the parameter will take. */
+  @Rule
+  public ParameterCdiTestRule<String> parameterValuesRule = new ParameterCdiTestRule<String>(
+      RoleInheritanceTestUtils.ACCESS_NAMES);
+
+  /** this field will take the values defined by parameterValuesRule field. */
+  private @ParameterCdiTest
+  String parameter;
+
+  @Test
+  public void testAccessPropagation() {
+    Role role = null;
+    Role template = null;
+    try {
+      OBContext.setAdminMode(true);
+      // Create roles
+      role = RoleInheritanceTestUtils.createRole("role", RoleInheritanceTestUtils.CLIENT_ID,
+          RoleInheritanceTestUtils.ASTERISK_ORG_ID, " C", true, false);
+      String roleId = (String) DalUtil.getId(role);
+      template = RoleInheritanceTestUtils.createRole("template",
+          RoleInheritanceTestUtils.CLIENT_ID, RoleInheritanceTestUtils.ASTERISK_ORG_ID, " C", true,
+          true);
+      String templateId = (String) DalUtil.getId(template);
+
+      // Add inheritance
+      RoleInheritanceTestUtils.addInheritance(role, template, new Long(10));
+
+      OBDal.getInstance().commitAndClose();
+      role = OBDal.getInstance().get(Role.class, roleId);
+      template = OBDal.getInstance().get(Role.class, templateId);
+
+      List<String> accesses = ACCESSES.get(counterHorizontalTest);
+      // Add accesses
+      RoleInheritanceTestUtils.addAccess(parameter, template, accesses.get(0));
+      RoleInheritanceTestUtils.addAccess(parameter, role, accesses.get(1));
+
+      String[] expected = { accesses.get(0), templateId, accesses.get(1), "" };
+      String[] result = RoleInheritanceTestUtils.getOrderedAccessNames(parameter, role);
+      assertThat("New access has been propagated", result, equalTo(expected));
+
+      boolean value = false;
+      if (parameter.equals("ALERT") || parameter.equals("PREFERENCE")) {
+        value = true;
+      }
+      // Perform an update in the access of the parent
+      RoleInheritanceTestUtils.updateAccess(parameter, template, accesses.get(0), value, false);
+      OBDal.getInstance().commitAndClose();
+
+      role = OBDal.getInstance().get(Role.class, roleId);
+      template = OBDal.getInstance().get(Role.class, templateId);
+
+      String editedValue = value + "";
+      String isActive = "false";
+      if (parameter.equals("REPORT") || parameter.equals("FORM") || parameter.equals("WIDGET")
+          || parameter.equals("VIEW") || parameter.equals("PROCESS")) {
+        // Accesses for Report, Form, Widget, View and Process just have the active flag
+        editedValue = "";
+      }
+
+      String[] expected2 = { editedValue, isActive, templateId };
+      String[] result2 = RoleInheritanceTestUtils.getAccessInfo(parameter, role, accesses.get(0));
+      assertThat("Updated access has been propagated", result2, equalTo(expected2));
+
+      editedValue = !value + "";
+      isActive = "true";
+      if (parameter.equals("REPORT") || parameter.equals("FORM") || parameter.equals("WIDGET")
+          || parameter.equals("VIEW") || parameter.equals("PROCESS")) {
+        editedValue = "";
+      }
+
+      String[] expected3 = { editedValue, isActive, "" };
+      String[] result3 = RoleInheritanceTestUtils.getAccessInfo(parameter, role, accesses.get(1));
+      assertThat("Non inherited access remains unchanged after propagation", result3,
+          equalTo(expected3));
+
+      RoleInheritanceTestUtils.removeAccesses(parameter, template);
+      RoleInheritanceTestUtils.removeAccesses(parameter, role);
+      counterHorizontalTest++;
+
+    } finally {
+      // Delete roles
+      RoleInheritanceTestUtils.deleteRole(role);
+      RoleInheritanceTestUtils.deleteRole(template);
+
+      OBDal.getInstance().commitAndClose();
+
+      OBContext.restorePreviousMode();
+    }
+  }
+}
--- a/src-test/src/org/openbravo/test/role/BasicADWindowInheritanceTest.java	Mon Sep 28 19:39:00 2015 +0200
+++ b/src-test/src/org/openbravo/test/role/BasicADWindowInheritanceTest.java	Tue Sep 29 12:49:29 2015 +0200
@@ -43,68 +43,6 @@
   public final static String PURCHASE_INVOICE_ID = "183";
 
   @Test
-  public void testBasicAccessPropagation() {
-    Role role = null;
-    Role template = null;
-    try {
-      OBContext.setAdminMode(true);
-      // Create roles
-      role = RoleInheritanceTestUtils.createRole("role", RoleInheritanceTestUtils.CLIENT_ID,
-          RoleInheritanceTestUtils.ASTERISK_ORG_ID, " C", true, false);
-      String roleId = (String) DalUtil.getId(role);
-      template = RoleInheritanceTestUtils.createRole("template",
-          RoleInheritanceTestUtils.CLIENT_ID, RoleInheritanceTestUtils.ASTERISK_ORG_ID, " C", true,
-          true);
-      String templateId = (String) DalUtil.getId(template);
-
-      // Add inheritance
-      RoleInheritanceTestUtils.addInheritance(role, template, new Long(10));
-
-      OBDal.getInstance().commitAndClose();
-      role = OBDal.getInstance().get(Role.class, roleId);
-      template = OBDal.getInstance().get(Role.class, templateId);
-
-      // Add window access
-      addWindowAccess(template, "Sales Invoice", true);
-      addWindowAccess(role, "Sales Order", true);
-
-      String[] expected = { SALES_INVOICE_ID, templateId, SALES_ORDER_ID, "" };
-      String[] result = getWindowAccessesOrderedByWindowName(role);
-      assertThat("New window access has been propagated", result, equalTo(expected));
-
-      // Perform an update in the window access of the parent
-      updateWindowAccess(template, "Sales Invoice", false, false);
-      OBDal.getInstance().commitAndClose();
-
-      role = OBDal.getInstance().get(Role.class, roleId);
-      template = OBDal.getInstance().get(Role.class, templateId);
-
-      WindowAccess wa = getWindowAccessForWindowName(role.getADWindowAccessList(), "Sales Invoice");
-      String[] expected2 = { "false", "false", templateId };
-      String[] result2 = { wa.isEditableField().toString(), wa.isActive().toString(),
-          wa.getInheritedFrom().getId() };
-      assertThat("Updated window access has been propagated", result2, equalTo(expected2));
-
-      WindowAccess wa2 = getWindowAccessForWindowName(role.getADWindowAccessList(), "Sales Order");
-      String[] expected3 = { "true", "true", "" };
-      String inheritedFromid = wa2.getInheritedFrom() != null ? wa2.getInheritedFrom().getId() : "";
-      String[] result3 = { wa2.isEditableField().toString(), wa2.isActive().toString(),
-          inheritedFromid };
-      assertThat("Non inherited access remains unchanged after propagation", result3,
-          equalTo(expected3));
-
-    } finally {
-      // Delete roles
-      RoleInheritanceTestUtils.deleteRole(role);
-      RoleInheritanceTestUtils.deleteRole(template);
-
-      OBDal.getInstance().commitAndClose();
-
-      OBContext.restorePreviousMode();
-    }
-  }
-
-  @Test
   public void testBasicAccessDeletePropagation() {
     Role role = null;
     Role template1 = null;
@@ -216,22 +154,6 @@
     OBDal.getInstance().remove(wa);
   }
 
-  public static void updateWindowAccess(Role role, String windowName, boolean editableField,
-      boolean isActive) {
-    final OBCriteria<Window> windowCriteria = OBDal.getInstance().createCriteria(Window.class);
-    windowCriteria.add(Restrictions.eq(Window.PROPERTY_NAME, windowName));
-    windowCriteria.setMaxResults(1);
-    final OBCriteria<WindowAccess> windowAccessCriteria = OBDal.getInstance().createCriteria(
-        WindowAccess.class);
-    windowAccessCriteria.add(Restrictions.eq(WindowAccess.PROPERTY_ROLE, role));
-    windowAccessCriteria.add(Restrictions.eq(WindowAccess.PROPERTY_WINDOW,
-        (Window) windowCriteria.uniqueResult()));
-    windowAccessCriteria.setMaxResults(1);
-    WindowAccess wa = (WindowAccess) windowAccessCriteria.uniqueResult();
-    wa.setEditableField(editableField);
-    wa.setActive(isActive);
-  }
-
   private String[] getWindowAccessesOrderedByWindowName(Role role) {
     final OBCriteria<WindowAccess> obCriteria = OBDal.getInstance().createCriteria(
         WindowAccess.class);
--- a/src-test/src/org/openbravo/test/role/RoleInheritanceTestUtils.java	Mon Sep 28 19:39:00 2015 +0200
+++ b/src-test/src/org/openbravo/test/role/RoleInheritanceTestUtils.java	Tue Sep 29 12:49:29 2015 +0200
@@ -134,6 +134,111 @@
     }
   }
 
+  public static void updateAccess(String type, Role role, String accessName, boolean editedValue,
+      boolean isActive) {
+    if ("ORGANIZATION".equals(type)) {
+      updateOrgAccess(role, accessName, editedValue, isActive);
+    } else if ("WINDOW".equals(type)) {
+      updateWindowAccess(role, accessName, editedValue, isActive);
+    } else if ("TAB".equals(type)) {
+      // Update information for Business Partner window tabs
+      updateTabAccess(role, "Business Partner", accessName, editedValue, isActive);
+    } else if ("FIELD".equals(type)) {
+      // Update information for fields in header tab of Business Partner window
+      updateFieldAccess(role, "Business Partner", "Business Partner", accessName, editedValue,
+          isActive);
+    } else if ("REPORT".equals(type)) {
+      updateReportAndProcessAccess(role, accessName, isActive);
+    } else if ("FORM".equals(type)) {
+      updateFormAccess(role, accessName, isActive);
+    } else if ("WIDGET".equals(type)) {
+      updateWidgetAccess(role, accessName, isActive);
+    } else if ("VIEW".equals(type)) {
+      updateViewImplementationAccess(role, accessName, isActive);
+    } else if ("PROCESS".equals(type)) {
+      updateProcessDefinitionAccess(role, accessName, isActive);
+    } else if ("ALERT".equals(type)) {
+      updateAlertRecipientAccess(role, accessName, editedValue, isActive);
+    } else if ("PREFERENCE".equals(type)) {
+      updatePreference(role, accessName, editedValue, isActive);
+    }
+  }
+
+  public static String[] getAccessInfo(String type, Role role, String accessName) {
+    if ("ORGANIZATION".equals(type)) {
+      return getOrgAccessInfo(role, accessName);
+    } else if ("WINDOW".equals(type)) {
+      return getWindowAccessInfo(role, accessName);
+    } else if ("TAB".equals(type)) {
+      // Get information for Business Partner window tabs
+      return getTabAccessInfo(role, "Business Partner", accessName);
+    } else if ("FIELD".equals(type)) {
+      // Get information for fields in header tab of Business Partner window
+      return getFieldAccessInfo(role, "Business Partner", "Business Partner", accessName);
+    } else if ("REPORT".equals(type)) {
+      return getReportAndProcessAccessInfo(role, accessName);
+    } else if ("FORM".equals(type)) {
+      return getFormAccessInfo(role, accessName);
+    } else if ("WIDGET".equals(type)) {
+      return getWidgetAccessInfo(role, accessName);
+    } else if ("VIEW".equals(type)) {
+      return getViewImplementationAccessInfo(role, accessName);
+    } else if ("PROCESS".equals(type)) {
+      return getProcessDefinitonAccessInfo(role, accessName);
+    } else if ("ALERT".equals(type)) {
+      return getAlertRecipientAccessInfo(role, accessName);
+    } else if ("PREFERENCE".equals(type)) {
+      return getPreferenceInfo(role, accessName);
+    } else {
+      return null;
+    }
+  }
+
+  private static void addOrgAccess(Role role, String orgName, boolean orgAdmin) {
+    final RoleOrganization orgAccess = OBProvider.getInstance().get(RoleOrganization.class);
+    final OBCriteria<Organization> obCriteria = OBDal.getInstance().createCriteria(
+        Organization.class);
+    obCriteria.add(Restrictions.eq(Organization.PROPERTY_NAME, orgName));
+    obCriteria.setMaxResults(1);
+    orgAccess.setClient(role.getClient());
+    orgAccess.setRole(role);
+    orgAccess.setOrganization((Organization) obCriteria.uniqueResult());
+    orgAccess.setOrgAdmin(orgAdmin);
+    OBDal.getInstance().save(orgAccess);
+    OBDal.getInstance().flush();
+    OBDal.getInstance().refresh(role);
+  }
+
+  private static void updateOrgAccess(Role role, String orgName, boolean isOrgAdmin,
+      boolean isActive) {
+    final OBCriteria<Organization> orgCriteria = OBDal.getInstance().createCriteria(
+        Organization.class);
+    orgCriteria.add(Restrictions.eq(Window.PROPERTY_NAME, orgName));
+    orgCriteria.setMaxResults(1);
+    final OBCriteria<RoleOrganization> orgAccessCriteria = OBDal.getInstance().createCriteria(
+        RoleOrganization.class);
+    orgAccessCriteria.add(Restrictions.eq(RoleOrganization.PROPERTY_ROLE, role));
+    orgAccessCriteria.add(Restrictions.eq(RoleOrganization.PROPERTY_ORGANIZATION,
+        (Organization) orgCriteria.uniqueResult()));
+    orgAccessCriteria.setMaxResults(1);
+    RoleOrganization ro = (RoleOrganization) orgAccessCriteria.uniqueResult();
+    ro.setOrgAdmin(isOrgAdmin);
+    ro.setActive(isActive);
+  }
+
+  private static String[] getOrgAccessInfo(Role role, String orgName) {
+    String result[] = new String[3];
+    for (RoleOrganization ro : role.getADRoleOrganizationList()) {
+      if (orgName.equals(ro.getOrganization().getName())) {
+        result[0] = ro.isOrgAdmin().toString();
+        result[1] = ro.isActive().toString();
+        result[2] = ro.getInheritedFrom() != null ? ro.getInheritedFrom().getId() : "";
+        break;
+      }
+    }
+    return result;
+  }
+
   private static WindowAccess addWindowAccess(Role role, String windowName, boolean editableField) {
     final WindowAccess windowAccess = OBProvider.getInstance().get(WindowAccess.class);
     final OBCriteria<Window> obCriteria = OBDal.getInstance().createCriteria(Window.class);
@@ -150,6 +255,35 @@
     return windowAccess;
   }
 
+  private static void updateWindowAccess(Role role, String windowName, boolean editableField,
+      boolean isActive) {
+    final OBCriteria<Window> windowCriteria = OBDal.getInstance().createCriteria(Window.class);
+    windowCriteria.add(Restrictions.eq(Window.PROPERTY_NAME, windowName));
+    windowCriteria.setMaxResults(1);
+    final OBCriteria<WindowAccess> windowAccessCriteria = OBDal.getInstance().createCriteria(
+        WindowAccess.class);
+    windowAccessCriteria.add(Restrictions.eq(WindowAccess.PROPERTY_ROLE, role));
+    windowAccessCriteria.add(Restrictions.eq(WindowAccess.PROPERTY_WINDOW,
+        (Window) windowCriteria.uniqueResult()));
+    windowAccessCriteria.setMaxResults(1);
+    WindowAccess wa = (WindowAccess) windowAccessCriteria.uniqueResult();
+    wa.setEditableField(editableField);
+    wa.setActive(isActive);
+  }
+
+  private static String[] getWindowAccessInfo(Role role, String windowName) {
+    String result[] = new String[3];
+    for (WindowAccess wa : role.getADWindowAccessList()) {
+      if (windowName.equals(wa.getWindow().getName())) {
+        result[0] = wa.isEditableField().toString();
+        result[1] = wa.isActive().toString();
+        result[2] = wa.getInheritedFrom() != null ? wa.getInheritedFrom().getId() : "";
+        break;
+      }
+    }
+    return result;
+  }
+
   private static TabAccess addTabAccess(Role role, String windowName, String tabName,
       boolean editableField, boolean editableTab) {
 
@@ -187,6 +321,53 @@
     return tabAccess;
   }
 
+  private static void updateTabAccess(Role role, String windowName, String tabName,
+      boolean editableTab, boolean isActive) {
+    final OBCriteria<Window> windowCriteria = OBDal.getInstance().createCriteria(Window.class);
+    windowCriteria.add(Restrictions.eq(Window.PROPERTY_NAME, windowName));
+    windowCriteria.setMaxResults(1);
+    Window window = (Window) windowCriteria.uniqueResult();
+
+    final OBCriteria<WindowAccess> waCriteria = OBDal.getInstance().createCriteria(
+        WindowAccess.class);
+    waCriteria.add(Restrictions.eq(WindowAccess.PROPERTY_ROLE, role));
+    waCriteria.add(Restrictions.eq(WindowAccess.PROPERTY_WINDOW, window));
+    waCriteria.setMaxResults(1);
+
+    final OBCriteria<Tab> tabCriteria = OBDal.getInstance().createCriteria(Tab.class);
+    tabCriteria.add(Restrictions.eq(Tab.PROPERTY_WINDOW, window));
+    tabCriteria.add(Restrictions.eq(Tab.PROPERTY_NAME, tabName));
+    tabCriteria.setMaxResults(1);
+
+    final OBCriteria<TabAccess> tabAccessCriteria = OBDal.getInstance().createCriteria(
+        TabAccess.class);
+    tabAccessCriteria.add(Restrictions.eq(TabAccess.PROPERTY_WINDOWACCESS,
+        (WindowAccess) waCriteria.uniqueResult()));
+    tabAccessCriteria
+        .add(Restrictions.eq(TabAccess.PROPERTY_TAB, (Tab) tabCriteria.uniqueResult()));
+    tabAccessCriteria.setMaxResults(1);
+    TabAccess ta = (TabAccess) tabAccessCriteria.uniqueResult();
+    ta.setEditableField(editableTab);
+    ta.setActive(isActive);
+  }
+
+  private static String[] getTabAccessInfo(Role role, String windowName, String tabName) {
+    String result[] = new String[3];
+    for (WindowAccess wa : role.getADWindowAccessList()) {
+      if (windowName.equals(wa.getWindow().getName())) {
+        for (TabAccess ta : wa.getADTabAccessList()) {
+          if (tabName.equals(ta.getTab().getName())) {
+            result[0] = ta.isEditableField().toString();
+            result[1] = ta.isActive().toString();
+            result[2] = ta.getInheritedFrom() != null ? ta.getInheritedFrom().getId() : "";
+            break;
+          }
+        }
+      }
+    }
+    return result;
+  }
+
   private static void addFieldAccess(Role role, String windowName, String tabName,
       String fieldName, boolean editableField, boolean editableTab, boolean editableInField,
       boolean checkOnSave) {
@@ -239,19 +420,70 @@
     OBDal.getInstance().refresh(role);
   }
 
-  private static void addOrgAccess(Role role, String orgName, boolean orgAdmin) {
-    final RoleOrganization orgAccess = OBProvider.getInstance().get(RoleOrganization.class);
-    final OBCriteria<Organization> obCriteria = OBDal.getInstance().createCriteria(
-        Organization.class);
-    obCriteria.add(Restrictions.eq(Organization.PROPERTY_NAME, orgName));
-    obCriteria.setMaxResults(1);
-    orgAccess.setClient(role.getClient());
-    orgAccess.setRole(role);
-    orgAccess.setOrganization((Organization) obCriteria.uniqueResult());
-    orgAccess.setOrgAdmin(orgAdmin);
-    OBDal.getInstance().save(orgAccess);
-    OBDal.getInstance().flush();
-    OBDal.getInstance().refresh(role);
+  private static void updateFieldAccess(Role role, String windowName, String tabName,
+      String fieldName, boolean editableField, boolean isActive) {
+    final OBCriteria<Window> windowCriteria = OBDal.getInstance().createCriteria(Window.class);
+    windowCriteria.add(Restrictions.eq(Window.PROPERTY_NAME, windowName));
+    windowCriteria.setMaxResults(1);
+    Window window = (Window) windowCriteria.uniqueResult();
+
+    final OBCriteria<WindowAccess> waCriteria = OBDal.getInstance().createCriteria(
+        WindowAccess.class);
+    waCriteria.add(Restrictions.eq(WindowAccess.PROPERTY_ROLE, role));
+    waCriteria.add(Restrictions.eq(WindowAccess.PROPERTY_WINDOW, window));
+    waCriteria.setMaxResults(1);
+
+    final OBCriteria<Tab> tabCriteria = OBDal.getInstance().createCriteria(Tab.class);
+    tabCriteria.add(Restrictions.eq(Tab.PROPERTY_WINDOW, window));
+    tabCriteria.add(Restrictions.eq(Tab.PROPERTY_NAME, tabName));
+    tabCriteria.setMaxResults(1);
+    Tab tab = (Tab) tabCriteria.uniqueResult();
+
+    final OBCriteria<TabAccess> tabAccessCriteria = OBDal.getInstance().createCriteria(
+        TabAccess.class);
+    tabAccessCriteria.add(Restrictions.eq(TabAccess.PROPERTY_WINDOWACCESS,
+        (WindowAccess) waCriteria.uniqueResult()));
+    tabAccessCriteria.add(Restrictions.eq(TabAccess.PROPERTY_TAB, tab));
+    tabAccessCriteria.setMaxResults(1);
+    TabAccess ta = (TabAccess) tabAccessCriteria.uniqueResult();
+
+    final OBCriteria<Field> fieldCriteria = OBDal.getInstance().createCriteria(Field.class);
+    fieldCriteria.add(Restrictions.eq(Field.PROPERTY_TAB, tab));
+    fieldCriteria.add(Restrictions.eq(Field.PROPERTY_NAME, fieldName));
+    fieldCriteria.setMaxResults(1);
+
+    final OBCriteria<FieldAccess> fieldAccessCriteria = OBDal.getInstance().createCriteria(
+        FieldAccess.class);
+    fieldAccessCriteria.add(Restrictions.eq(FieldAccess.PROPERTY_TABACCESS, ta));
+    fieldAccessCriteria.add(Restrictions.eq(FieldAccess.PROPERTY_FIELD,
+        (Field) fieldCriteria.uniqueResult()));
+    fieldAccessCriteria.setMaxResults(1);
+
+    FieldAccess fa = (FieldAccess) fieldAccessCriteria.uniqueResult();
+    fa.setEditableField(editableField);
+    fa.setActive(isActive);
+  }
+
+  private static String[] getFieldAccessInfo(Role role, String windowName, String tabName,
+      String fieldName) {
+    String result[] = new String[3];
+    for (WindowAccess wa : role.getADWindowAccessList()) {
+      if (windowName.equals(wa.getWindow().getName())) {
+        for (TabAccess ta : wa.getADTabAccessList()) {
+          if (tabName.equals(ta.getTab().getName())) {
+            for (FieldAccess fa : ta.getADFieldAccessList()) {
+              if (fieldName.equals(fa.getField().getName())) {
+                result[0] = fa.isEditableField().toString();
+                result[1] = fa.isActive().toString();
+                result[2] = fa.getInheritedFrom() != null ? fa.getInheritedFrom().getId() : "";
+                break;
+              }
+            }
+          }
+        }
+      }
+    }
+    return result;
   }
 
   private static void addReportAndProcessAccess(Role role, String reportName) {
@@ -269,6 +501,36 @@
     OBDal.getInstance().refresh(role);
   }
 
+  private static void updateReportAndProcessAccess(Role role, String reportName, boolean isActive) {
+    final OBCriteria<Process> processCriteria = OBDal.getInstance().createCriteria(Process.class);
+    processCriteria.add(Restrictions.eq(Window.PROPERTY_NAME, reportName));
+    processCriteria.setMaxResults(1);
+    final OBCriteria<org.openbravo.model.ad.access.ProcessAccess> processAccessCriteria = OBDal
+        .getInstance().createCriteria(org.openbravo.model.ad.access.ProcessAccess.class);
+    processAccessCriteria.add(Restrictions.eq(
+        org.openbravo.model.ad.access.ProcessAccess.PROPERTY_ROLE, role));
+    processAccessCriteria.add(Restrictions.eq(
+        org.openbravo.model.ad.access.ProcessAccess.PROPERTY_PROCESS,
+        (Process) processCriteria.uniqueResult()));
+    processAccessCriteria.setMaxResults(1);
+    org.openbravo.model.ad.access.ProcessAccess pa = (org.openbravo.model.ad.access.ProcessAccess) processAccessCriteria
+        .uniqueResult();
+    pa.setActive(isActive);
+  }
+
+  private static String[] getReportAndProcessAccessInfo(Role role, String reportName) {
+    String result[] = new String[3];
+    for (org.openbravo.model.ad.access.ProcessAccess pa : role.getADProcessAccessList()) {
+      if (reportName.equals(pa.getProcess().getName())) {
+        result[0] = "";
+        result[1] = pa.isActive().toString();
+        result[2] = pa.getInheritedFrom() != null ? pa.getInheritedFrom().getId() : "";
+        break;
+      }
+    }
+    return result;
+  }
+
   private static void addFormAccess(Role role, String formName) {
     final FormAccess formAccess = OBProvider.getInstance().get(FormAccess.class);
     final OBCriteria<Form> obCriteria = OBDal.getInstance().createCriteria(Form.class);
@@ -283,6 +545,33 @@
     OBDal.getInstance().refresh(role);
   }
 
+  private static void updateFormAccess(Role role, String formName, boolean isActive) {
+    final OBCriteria<Form> formCriteria = OBDal.getInstance().createCriteria(Form.class);
+    formCriteria.add(Restrictions.eq(Form.PROPERTY_NAME, formName));
+    formCriteria.setMaxResults(1);
+    final OBCriteria<FormAccess> formAccessCriteria = OBDal.getInstance().createCriteria(
+        FormAccess.class);
+    formAccessCriteria.add(Restrictions.eq(FormAccess.PROPERTY_ROLE, role));
+    formAccessCriteria.add(Restrictions.eq(FormAccess.PROPERTY_SPECIALFORM,
+        (Form) formCriteria.uniqueResult()));
+    formAccessCriteria.setMaxResults(1);
+    FormAccess fa = (FormAccess) formAccessCriteria.uniqueResult();
+    fa.setActive(isActive);
+  }
+
+  private static String[] getFormAccessInfo(Role role, String formName) {
+    String result[] = new String[3];
+    for (FormAccess fa : role.getADFormAccessList()) {
+      if (formName.equals(fa.getSpecialForm().getName())) {
+        result[0] = "";
+        result[1] = fa.isActive().toString();
+        result[2] = fa.getInheritedFrom() != null ? fa.getInheritedFrom().getId() : "";
+        break;
+      }
+    }
+    return result;
+  }
+
   private static void addWidgetAccess(Role role, String widgetTitle) {
     final WidgetClassAccess widgetAccess = OBProvider.getInstance().get(WidgetClassAccess.class);
     final OBCriteria<WidgetClass> obCriteria = OBDal.getInstance()
@@ -298,6 +587,34 @@
     OBDal.getInstance().refresh(role);
   }
 
+  private static void updateWidgetAccess(Role role, String widgetTitle, boolean isActive) {
+    final OBCriteria<WidgetClass> widgetCriteria = OBDal.getInstance().createCriteria(
+        WidgetClass.class);
+    widgetCriteria.add(Restrictions.eq(WidgetClass.PROPERTY_WIDGETTITLE, widgetTitle));
+    widgetCriteria.setMaxResults(1);
+    final OBCriteria<WidgetClassAccess> widgetAccessCriteria = OBDal.getInstance().createCriteria(
+        WidgetClassAccess.class);
+    widgetAccessCriteria.add(Restrictions.eq(WidgetClassAccess.PROPERTY_ROLE, role));
+    widgetAccessCriteria.add(Restrictions.eq(WidgetClassAccess.PROPERTY_WIDGETCLASS,
+        (WidgetClass) widgetCriteria.uniqueResult()));
+    widgetAccessCriteria.setMaxResults(1);
+    WidgetClassAccess wa = (WidgetClassAccess) widgetAccessCriteria.uniqueResult();
+    wa.setActive(isActive);
+  }
+
+  private static String[] getWidgetAccessInfo(Role role, String widgetTitle) {
+    String result[] = new String[3];
+    for (WidgetClassAccess wa : role.getOBKMOWidgetClassAccessList()) {
+      if (widgetTitle.equals(wa.getWidgetClass().getWidgetTitle())) {
+        result[0] = "";
+        result[1] = wa.isActive().toString();
+        result[2] = wa.getInheritedFrom() != null ? wa.getInheritedFrom().getId() : "";
+        break;
+      }
+    }
+    return result;
+  }
+
   private static void addViewImplementationAccess(Role role, String viewImplementationName) {
     final ViewRoleAccess viewAccess = OBProvider.getInstance().get(ViewRoleAccess.class);
     final OBCriteria<OBUIAPPViewImplementation> obCriteria = OBDal.getInstance().createCriteria(
@@ -314,6 +631,34 @@
     OBDal.getInstance().refresh(role);
   }
 
+  private static void updateViewImplementationAccess(Role role, String viewName, boolean isActive) {
+    final OBCriteria<OBUIAPPViewImplementation> viewCriteria = OBDal.getInstance().createCriteria(
+        OBUIAPPViewImplementation.class);
+    viewCriteria.add(Restrictions.eq(OBUIAPPViewImplementation.PROPERTY_NAME, viewName));
+    viewCriteria.setMaxResults(1);
+    final OBCriteria<ViewRoleAccess> viewAccessCriteria = OBDal.getInstance().createCriteria(
+        ViewRoleAccess.class);
+    viewAccessCriteria.add(Restrictions.eq(ViewRoleAccess.PROPERTY_ROLE, role));
+    viewAccessCriteria.add(Restrictions.eq(ViewRoleAccess.PROPERTY_VIEWIMPLEMENTATION,
+        (OBUIAPPViewImplementation) viewCriteria.uniqueResult()));
+    viewAccessCriteria.setMaxResults(1);
+    ViewRoleAccess va = (ViewRoleAccess) viewAccessCriteria.uniqueResult();
+    va.setActive(isActive);
+  }
+
+  private static String[] getViewImplementationAccessInfo(Role role, String viewName) {
+    String result[] = new String[3];
+    for (ViewRoleAccess va : role.getObuiappViewRoleAccessList()) {
+      if (viewName.equals(va.getViewImplementation().getName())) {
+        result[0] = "";
+        result[1] = va.isActive().toString();
+        result[2] = va.getInheritedFrom() != null ? va.getInheritedFrom().getId() : "";
+        break;
+      }
+    }
+    return result;
+  }
+
   private static void addProcessDefinitionAccess(Role role, String processName) {
     final org.openbravo.client.application.ProcessAccess processAccess = OBProvider.getInstance()
         .get(org.openbravo.client.application.ProcessAccess.class);
@@ -332,6 +677,38 @@
     OBDal.getInstance().refresh(role);
   }
 
+  private static void updateProcessDefinitionAccess(Role role, String processName, boolean isActive) {
+    final OBCriteria<org.openbravo.client.application.Process> processCriteria = OBDal
+        .getInstance().createCriteria(org.openbravo.client.application.Process.class);
+    processCriteria.add(Restrictions.eq(org.openbravo.client.application.Process.PROPERTY_NAME,
+        processName));
+    processCriteria.setMaxResults(1);
+    final OBCriteria<org.openbravo.client.application.ProcessAccess> processAccessCriteria = OBDal
+        .getInstance().createCriteria(org.openbravo.client.application.ProcessAccess.class);
+    processAccessCriteria.add(Restrictions.eq(
+        org.openbravo.client.application.ProcessAccess.PROPERTY_ROLE, role));
+    processAccessCriteria.add(Restrictions.eq(
+        org.openbravo.client.application.ProcessAccess.PROPERTY_OBUIAPPPROCESS,
+        (org.openbravo.client.application.Process) processCriteria.uniqueResult()));
+    processAccessCriteria.setMaxResults(1);
+    org.openbravo.client.application.ProcessAccess pa = (org.openbravo.client.application.ProcessAccess) processAccessCriteria
+        .uniqueResult();
+    pa.setActive(isActive);
+  }
+
+  private static String[] getProcessDefinitonAccessInfo(Role role, String processName) {
+    String result[] = new String[3];
+    for (org.openbravo.client.application.ProcessAccess pa : role.getOBUIAPPProcessAccessList()) {
+      if (processName.equals(pa.getObuiappProcess().getName())) {
+        result[0] = "";
+        result[1] = pa.isActive().toString();
+        result[2] = pa.getInheritedFrom() != null ? pa.getInheritedFrom().getId() : "";
+        break;
+      }
+    }
+    return result;
+  }
+
   private static void addAlertRecipient(Role role, String alertName) {
     final AlertRecipient alertRecipient = OBProvider.getInstance().get(AlertRecipient.class);
     final OBCriteria<AlertRule> obCriteria = OBDal.getInstance().createCriteria(AlertRule.class);
@@ -346,10 +723,40 @@
     OBDal.getInstance().refresh(role);
   }
 
+  private static void updateAlertRecipientAccess(Role role, String alertName, boolean isSendEmail,
+      boolean isActive) {
+    final OBCriteria<AlertRule> alertCriteria = OBDal.getInstance().createCriteria(AlertRule.class);
+    alertCriteria.add(Restrictions.eq(AlertRule.PROPERTY_NAME, alertName));
+    alertCriteria.setMaxResults(1);
+    final OBCriteria<AlertRecipient> alertRecipientCriteria = OBDal.getInstance().createCriteria(
+        AlertRecipient.class);
+    alertRecipientCriteria.add(Restrictions.eq(AlertRecipient.PROPERTY_ROLE, role));
+    alertRecipientCriteria.add(Restrictions.eq(AlertRecipient.PROPERTY_ALERTRULE,
+        (AlertRule) alertCriteria.uniqueResult()));
+    alertRecipientCriteria.setMaxResults(1);
+    AlertRecipient ar = (AlertRecipient) alertRecipientCriteria.uniqueResult();
+    ar.setSendEMail(isSendEmail);
+    ar.setActive(isActive);
+  }
+
+  private static String[] getAlertRecipientAccessInfo(Role role, String alertName) {
+    String result[] = new String[3];
+    for (AlertRecipient ar : role.getADAlertRecipientList()) {
+      if (alertName.equals(ar.getAlertRule().getName())) {
+        result[0] = ar.isSendEMail().toString();
+        result[1] = ar.isActive().toString();
+        result[2] = ar.getInheritedFrom() != null ? ar.getInheritedFrom().getId() : "";
+        break;
+      }
+    }
+    return result;
+  }
+
   private static void removeAlertRecipients(Role role) {
     final OBCriteria<AlertRecipient> obCriteria = OBDal.getInstance().createCriteria(
         AlertRecipient.class);
     obCriteria.add(Restrictions.eq(AlertRecipient.PROPERTY_ROLE, role));
+    obCriteria.setFilterOnActive(false);
     for (AlertRecipient ar : obCriteria.list()) {
       OBDal.getInstance().remove(ar);
     }
@@ -369,9 +776,35 @@
     OBDal.getInstance().refresh(role);
   }
 
+  private static void updatePreference(Role role, String propertyName, boolean isSelected,
+      boolean isActive) {
+    final OBCriteria<Preference> preferenceCriteria = OBDal.getInstance().createCriteria(
+        Preference.class);
+    preferenceCriteria.add(Restrictions.eq(Preference.PROPERTY_VISIBLEATROLE, role));
+    preferenceCriteria.add(Restrictions.eq(Preference.PROPERTY_PROPERTY, propertyName));
+    preferenceCriteria.setMaxResults(1);
+    Preference p = (Preference) preferenceCriteria.uniqueResult();
+    p.setSelected(isSelected);
+    p.setActive(isActive);
+  }
+
+  private static String[] getPreferenceInfo(Role role, String propertyName) {
+    String result[] = new String[3];
+    for (Preference pf : role.getADPreferenceVisibleAtRoleList()) {
+      if (propertyName.equals(pf.getProperty())) {
+        result[0] = pf.isSelected().toString();
+        result[1] = pf.isActive().toString();
+        result[2] = pf.getInheritedFrom() != null ? pf.getInheritedFrom().getId() : "";
+        break;
+      }
+    }
+    return result;
+  }
+
   private static void removePreferences(Role role) {
     final OBCriteria<Preference> obCriteria = OBDal.getInstance().createCriteria(Preference.class);
     obCriteria.add(Restrictions.eq(Preference.PROPERTY_VISIBLEATROLE, role));
+    obCriteria.setFilterOnActive(false);
     for (Preference p : obCriteria.list()) {
       OBDal.getInstance().remove(p);
     }
--- a/src/org/openbravo/roleInheritance/RoleInheritanceManager.java	Mon Sep 28 19:39:00 2015 +0200
+++ b/src/org/openbravo/roleInheritance/RoleInheritanceManager.java	Tue Sep 29 12:49:29 2015 +0200
@@ -394,7 +394,7 @@
   /**
    * Removes references to child elements (TabAccess and FieldAccess) from the parent list. Using
    * this method prevents the "deleted object would be re-saved by cascade" error after deleting an
-   * inherited TabAccess or a FieldAccess
+   * inherited TabAccess or FieldAccess
    * 
    * @param access
    *          The access to be removed from the parent list