[roleInheritance] Fix when adding a new inheritance with FieldAccess
authorCarlos Aristu <carlos.aristu@openbravo.com>
Mon, 28 Sep 2015 19:39:00 +0200
changeset 27777 e1c088010156
parent 27776 3c065d295883
child 27778 0bd6da797c24
[roleInheritance] Fix when adding a new inheritance with FieldAccess
src-test/src/org/openbravo/test/role/HorizontalInheritanceTest.java
src-test/src/org/openbravo/test/role/RoleInheritanceTestUtils.java
src-test/src/org/openbravo/test/role/VerticalInheritanceTest.java
src/org/openbravo/roleInheritance/RoleInheritanceManager.java
--- a/src-test/src/org/openbravo/test/role/HorizontalInheritanceTest.java	Mon Sep 28 18:05:27 2015 +0200
+++ b/src-test/src/org/openbravo/test/role/HorizontalInheritanceTest.java	Mon Sep 28 19:39:00 2015 +0200
@@ -52,6 +52,8 @@
       "Purchase Order", "Sales Invoice", "Sales Order");
   private final List<String> TABS = Arrays.asList("Bank Account", "Basic Discount", "Contact",
       "Customer", "Employee");
+  private final List<String> FIELDS = Arrays.asList("Business Partner Category", "Commercial Name",
+      "Credit Line Limit", "Description", "URL");
   private final List<String> REPORTS = Arrays.asList("Alert Process", "Create Variants",
       "Journal Entries Report", "Print orders process", "Set as Ready");
   private final List<String> FORMS = Arrays.asList("About", "Heartbeat", "Logout", "Menu",
@@ -70,8 +72,8 @@
       "AllowMultiTab", "OBSERDS_CSVTextEncoding", "StartPage");
 
   @SuppressWarnings("unchecked")
-  private final List<List<String>> ACCESSES = Arrays.asList(ORGANIZATIONS, WINDOWS, TABS, REPORTS,
-      FORMS, WIDGETS, VIEWS, PROCESSES, ALERTS, PREFERENCES);
+  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. */
@@ -124,8 +126,14 @@
         RoleInheritanceTestUtils.addAccess(parameter, template3, accesses.get(0));
         RoleInheritanceTestUtils.addAccess(parameter, template3, accesses.get(1));
       }
+      OBDal.getInstance().commitAndClose();
 
-      // Save
+      template1 = OBDal.getInstance().get(Role.class, template1Id);
+      template2 = OBDal.getInstance().get(Role.class, template2Id);
+      template3 = OBDal.getInstance().get(Role.class, template3Id);
+      inheritedRole = OBDal.getInstance().get(Role.class, inheritedRoleId);
+
+      // Save Inheritances
       RoleInheritanceTestUtils.addInheritance(inheritedRole, template1, new Long(10));
       RoleInheritanceTestUtils.addInheritance(inheritedRole, template2, new Long(20));
       RoleInheritanceTestUtils.addInheritance(inheritedRole, template3, new Long(30));
@@ -149,7 +157,7 @@
       String[] result = RoleInheritanceTestUtils.getOrderedAccessNames(parameter, inheritedRole);
       assertThat("Inheritances added. Accesses have been inherited", result, equalTo(expected));
 
-      // Delete
+      // Delete Inheritance
       RoleInheritanceTestUtils.removeInheritance(inheritedRole, template3);
       OBDal.getInstance().commitAndClose();
 
--- a/src-test/src/org/openbravo/test/role/RoleInheritanceTestUtils.java	Mon Sep 28 18:05:27 2015 +0200
+++ b/src-test/src/org/openbravo/test/role/RoleInheritanceTestUtils.java	Mon Sep 28 19:39:00 2015 +0200
@@ -30,6 +30,7 @@
 import org.openbravo.dal.core.DalUtil;
 import org.openbravo.dal.service.OBCriteria;
 import org.openbravo.dal.service.OBDal;
+import org.openbravo.model.ad.access.FieldAccess;
 import org.openbravo.model.ad.access.FormAccess;
 import org.openbravo.model.ad.access.Role;
 import org.openbravo.model.ad.access.RoleInheritance;
@@ -40,6 +41,7 @@
 import org.openbravo.model.ad.alert.AlertRule;
 import org.openbravo.model.ad.domain.Preference;
 import org.openbravo.model.ad.system.Client;
+import org.openbravo.model.ad.ui.Field;
 import org.openbravo.model.ad.ui.Form;
 import org.openbravo.model.ad.ui.Process;
 import org.openbravo.model.ad.ui.Tab;
@@ -50,7 +52,7 @@
   public final static String CLIENT_ID = "23C59575B9CF467C9620760EB255B389";
   public final static String ASTERISK_ORG_ID = "0";
   public final static List<String> ACCESS_NAMES = Arrays.asList("ORGANIZATION", "WINDOW", "TAB",
-      "REPORT", "FORM", "WIDGET", "VIEW", "PROCESS", "ALERT", "PREFERENCE");
+      "FIELD", "REPORT", "FORM", "WIDGET", "VIEW", "PROCESS", "ALERT", "PREFERENCE");
 
   public static Role createRole(String name, String clientId, String organizationId,
       String userLevel, boolean isManual, boolean isTemplate) {
@@ -93,8 +95,6 @@
     obCriteria.setMaxResults(1);
     RoleInheritance roleInheritance = (RoleInheritance) obCriteria.uniqueResult();
     OBDal.getInstance().remove(roleInheritance);
-    // OBDal.getInstance().flush();
-    // OBDal.getInstance().refresh(role);
   }
 
   public static void addAccess(String type, Role role, String accessName) {
@@ -105,6 +105,10 @@
     } else if ("TAB".equals(type)) {
       // Create tab access for Business Partner window
       addTabAccess(role, "Business Partner", accessName, true, true);
+    } else if ("FIELD".equals(type)) {
+      // Create field access for header tab of Business Partner window
+      addFieldAccess(role, "Business Partner", "Business Partner", accessName, true, true, true,
+          true);
     } else if ("REPORT".equals(type)) {
       addReportAndProcessAccess(role, accessName);
     } else if ("FORM".equals(type)) {
@@ -146,7 +150,7 @@
     return windowAccess;
   }
 
-  private static void addTabAccess(Role role, String windowName, String tabName,
+  private static TabAccess addTabAccess(Role role, String windowName, String tabName,
       boolean editableField, boolean editableTab) {
 
     final OBCriteria<Window> windowCriteria = OBDal.getInstance().createCriteria(Window.class);
@@ -180,6 +184,59 @@
     OBDal.getInstance().save(tabAccess);
     OBDal.getInstance().flush();
     OBDal.getInstance().refresh(role);
+    return tabAccess;
+  }
+
+  private static void addFieldAccess(Role role, String windowName, String tabName,
+      String fieldName, boolean editableField, boolean editableTab, boolean editableInField,
+      boolean checkOnSave) {
+
+    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_NAME, tabName));
+    tabCriteria.add(Restrictions.eq(Tab.PROPERTY_WINDOW, window));
+    tabCriteria.setMaxResults(1);
+    Tab tab = (Tab) tabCriteria.uniqueResult();
+
+    final OBCriteria<TabAccess> taCriteria = OBDal.getInstance().createCriteria(TabAccess.class);
+    taCriteria.add(Restrictions.eq(TabAccess.PROPERTY_WINDOWACCESS,
+        (WindowAccess) waCriteria.uniqueResult()));
+    taCriteria.add(Restrictions.eq(TabAccess.PROPERTY_TAB, tab));
+    taCriteria.setMaxResults(1);
+
+    TabAccess ta = (TabAccess) taCriteria.uniqueResult();
+    if (ta == null) {
+      // Window access does not exists, create it
+      ta = addTabAccess(role, windowName, tabName, editableField, editableTab);
+    }
+
+    final FieldAccess fieldAccess = OBProvider.getInstance().get(FieldAccess.class);
+
+    final OBCriteria<Field> obCriteria = OBDal.getInstance().createCriteria(Field.class);
+    obCriteria.add(Restrictions.eq(Field.PROPERTY_NAME, fieldName));
+    obCriteria.add(Restrictions.eq(Field.PROPERTY_TAB, tab));
+    obCriteria.setMaxResults(1);
+    Field field = (Field) obCriteria.uniqueResult();
+
+    fieldAccess.setClient(role.getClient());
+    fieldAccess.setOrganization(role.getOrganization());
+    fieldAccess.setTabAccess(ta);
+    fieldAccess.setField(field);
+    fieldAccess.setEditableField(editableInField);
+    fieldAccess.setCheckonsave(checkOnSave);
+    OBDal.getInstance().save(fieldAccess);
+    OBDal.getInstance().flush();
+    OBDal.getInstance().refresh(role);
   }
 
   private static void addOrgAccess(Role role, String orgName, boolean orgAdmin) {
@@ -329,6 +386,9 @@
     } else if ("TAB".equals(type)) {
       // Get tab accesses for Business Partner window
       return getTabFromTabAccesses(role, "Business Partner");
+    } else if ("FIELD".equals(type)) {
+      // Get field accesses for Business Partner header tab
+      return getFieldFromFieldAccesses(role, "Business Partner", "Business Partner");
     } else if ("REPORT".equals(type)) {
       return getReportsFromReportAccesses(role);
     } else if ("FORM".equals(type)) {
@@ -410,6 +470,48 @@
     return result;
   }
 
+  private static String[] getFieldFromFieldAccesses(Role role, String windowName, String tabName) {
+    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<Tab> tabCriteria = OBDal.getInstance().createCriteria(Tab.class);
+    tabCriteria.add(Restrictions.eq(Tab.PROPERTY_NAME, tabName));
+    tabCriteria.add(Restrictions.eq(Tab.PROPERTY_WINDOW, window));
+    tabCriteria.setMaxResults(1);
+    Tab tab = (Tab) tabCriteria.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);
+    WindowAccess windowAccess = (WindowAccess) waCriteria.uniqueResult();
+
+    final OBCriteria<TabAccess> taCriteria = OBDal.getInstance().createCriteria(TabAccess.class);
+    taCriteria.add(Restrictions.eq(TabAccess.PROPERTY_WINDOWACCESS, windowAccess));
+    taCriteria.add(Restrictions.eq(TabAccess.PROPERTY_TAB, tab));
+    taCriteria.setMaxResults(1);
+    TabAccess tabAccess = (TabAccess) taCriteria.uniqueResult();
+
+    final OBCriteria<FieldAccess> faCriteria = OBDal.getInstance()
+        .createCriteria(FieldAccess.class);
+    faCriteria.add(Restrictions.eq(FieldAccess.PROPERTY_TABACCESS, tabAccess));
+    faCriteria.addOrderBy(FieldAccess.PROPERTY_FIELD + "." + Field.PROPERTY_NAME, true);
+
+    List<FieldAccess> list = faCriteria.list();
+    String[] result = new String[list.size() * 2];
+    int i = 0;
+    for (FieldAccess fa : list) {
+      result[i] = fa.getField().getName();
+      result[i + 1] = fa.getInheritedFrom() != null ? (String) DalUtil.getId(fa.getInheritedFrom())
+          : "";
+      i += 2;
+    }
+    return result;
+  }
+
   private static String[] getReportsFromReportAccesses(Role role) {
     final OBCriteria<org.openbravo.model.ad.access.ProcessAccess> obCriteria = OBDal.getInstance()
         .createCriteria(org.openbravo.model.ad.access.ProcessAccess.class);
--- a/src-test/src/org/openbravo/test/role/VerticalInheritanceTest.java	Mon Sep 28 18:05:27 2015 +0200
+++ b/src-test/src/org/openbravo/test/role/VerticalInheritanceTest.java	Mon Sep 28 19:39:00 2015 +0200
@@ -49,6 +49,8 @@
   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("Purchase Order", "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");
@@ -61,8 +63,8 @@
   private final List<String> PREFERENCES = Arrays.asList("AllowAttachment", "AllowDelete");
 
   @SuppressWarnings("unchecked")
-  private final List<List<String>> ACCESSES = Arrays.asList(ORGANIZATIONS, WINDOWS, REPORTS, FORMS,
-      WIDGETS, VIEWS, PROCESSES, ALERTS, PREFERENCES);
+  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. */
@@ -97,6 +99,12 @@
       RoleInheritanceTestUtils.addAccess(parameter, roleA, accesses.get(0));
       RoleInheritanceTestUtils.addAccess(parameter, roleB, accesses.get(1));
 
+      OBDal.getInstance().commitAndClose();
+
+      roleA = OBDal.getInstance().get(Role.class, roleAId);
+      roleB = OBDal.getInstance().get(Role.class, roleBId);
+      roleC = OBDal.getInstance().get(Role.class, roleCId);
+
       // Add inheritances
       RoleInheritanceTestUtils.addInheritance(roleB, roleA, new Long(10));
       RoleInheritanceTestUtils.addInheritance(roleC, roleB, new Long(20));
--- a/src/org/openbravo/roleInheritance/RoleInheritanceManager.java	Mon Sep 28 18:05:27 2015 +0200
+++ b/src/org/openbravo/roleInheritance/RoleInheritanceManager.java	Mon Sep 28 19:39:00 2015 +0200
@@ -135,7 +135,12 @@
     try {
       // TabAccess, FieldAccess and Preference do not have role property as parent
       if ("org.openbravo.model.ad.access.TabAccess".equals(className)) {
-        setParentWindow((TabAccess) newAccess, (TabAccess) parentAccess, role);
+        TabAccess newTabAccess = (TabAccess) newAccess;
+        TabAccess parentTabAccess = (TabAccess) parentAccess;
+        setParentWindow(newTabAccess, parentTabAccess, role);
+        // We need to have the new tab access in memory for the case where we are
+        // adding field accesses also (when adding a new inheritance)
+        newTabAccess.getWindowAccess().getADTabAccessList().add(newTabAccess);
       } else if ("org.openbravo.model.ad.access.FieldAccess".equals(className)) {
         setParentTab((FieldAccess) newAccess, (FieldAccess) parentAccess, role);
       } else if ("org.openbravo.model.ad.domain.Preference".equals(className)) {
@@ -205,18 +210,15 @@
    *          Parent role
    */
   private void setParentTab(FieldAccess newFieldAccess, FieldAccess parentFieldAccess, Role role) {
-    final StringBuilder whereClause = new StringBuilder();
-    whereClause.append(" as ta ");
-    whereClause.append(" where ta.windowAccess.role.id = :roleId");
-    whereClause.append(" and ta.tab.id = :tabId");
-    final OBQuery<TabAccess> query = OBDal.getInstance().createQuery(TabAccess.class,
-        whereClause.toString());
-    query.setNamedParameter("roleId", role.getId());
-    query.setNamedParameter("tabId", parentFieldAccess.getTabAccess().getTab().getId());
-    query.setMaxResult(1);
-    TabAccess parent = (TabAccess) query.uniqueResult();
-    if (parent != null) {
-      newFieldAccess.setTabAccess(parent);
+    String parentTabId = (String) DalUtil.getId(parentFieldAccess.getTabAccess().getTab());
+    for (WindowAccess wa : role.getADWindowAccessList()) {
+      for (TabAccess ta : wa.getADTabAccessList()) {
+        String currentTabId = (String) DalUtil.getId(ta.getTab());
+        if (currentTabId.equals(parentTabId)) {
+          newFieldAccess.setTabAccess(ta);
+          break;
+        }
+      }
     }
   }