Fixed issue 27787: Create new icon is shown when windows are readOnly.
authorNaroa Iriarte <naroa.iriarte@openbravo.com>
Thu, 27 Nov 2014 09:46:03 +0100
changeset 25481 d60c4bb642ea
parent 25480 e976b465ba65
child 25482 79198ab4f09b
Fixed issue 27787: Create new icon is shown when windows are readOnly.

When the window/tab access for roles was readOnly, the create new icon was shown by the name of the windows, in the recent views layout.

For fixing this the "MenuManager.java" has been changed.
The logic for the window/tab access for the roles has been created. This is for knowing if the window/tab access for roles is read only or not.
Now, if a role has read only access the "create new" icon is not shown in the recent views layout.
It is good to know that the role access data is cached in the quick launch menu and in the recent views layout, so at first the "create new"
green icon by the name of the window in the recent view layout still appears. It is not a problem because when the window is cached again the
correct status is shown.
modules/org.openbravo.client.application/src/org/openbravo/client/application/MenuManager.java
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/MenuManager.java	Tue Dec 02 17:05:27 2014 +0100
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/MenuManager.java	Thu Nov 27 09:46:03 2014 +0100
@@ -30,13 +30,18 @@
 
 import org.hibernate.Hibernate;
 import org.hibernate.Query;
+import org.hibernate.criterion.Restrictions;
 import org.openbravo.base.session.SessionFactoryController;
 import org.openbravo.dal.core.DalUtil;
 import org.openbravo.dal.core.OBContext;
+import org.openbravo.dal.service.OBCriteria;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.erpCommon.obps.ActivationKey;
 import org.openbravo.erpCommon.obps.ActivationKey.FeatureRestriction;
 import org.openbravo.erpCommon.utility.Utility;
+import org.openbravo.model.ad.access.Role;
+import org.openbravo.model.ad.access.TabAccess;
+import org.openbravo.model.ad.access.WindowAccess;
 import org.openbravo.model.ad.ui.Form;
 import org.openbravo.model.ad.ui.Menu;
 import org.openbravo.model.ad.ui.MenuTrl;
@@ -312,7 +317,50 @@
     }
 
     public boolean isReadOnly() {
-      return getTab() != null && getTab().getUIPattern().equals("RO");
+      boolean tabIsReadOnlyForAll = getTab() != null && getTab().getUIPattern().equals("RO");
+      boolean tabIsReadOnlyForRole = isTabReadOnlyforRole();
+      return tabIsReadOnlyForAll || tabIsReadOnlyForRole;
+    }
+
+    public boolean isTabReadOnlyforRole() {
+      // OBCriteria
+      boolean isReadOnly = false;
+
+      if (getTab() == null) {
+        return false;
+      }
+
+      Role role = OBContext.getOBContext().getRole();
+      OBCriteria<WindowAccess> windowAccessCriteria = OBDal.getInstance().createCriteria(
+          WindowAccess.class);
+      windowAccessCriteria.add(Restrictions.eq(WindowAccess.PROPERTY_ROLE, role));
+      windowAccessCriteria.add(Restrictions.eq(WindowAccess.PROPERTY_WINDOW, getTab().getWindow()));
+
+      WindowAccess windowAccess = (WindowAccess) windowAccessCriteria.uniqueResult();
+      if (windowAccess != null) {
+        // there is a window access defined for this window and this role
+        OBCriteria<TabAccess> tabAccessCriteria = OBDal.getInstance().createCriteria(
+            TabAccess.class);
+        tabAccessCriteria.add(Restrictions.eq(TabAccess.PROPERTY_TAB, tab));
+        tabAccessCriteria.add(Restrictions.eq(TabAccess.PROPERTY_WINDOWACCESS, windowAccess));
+        TabAccess tabAccess = (TabAccess) tabAccessCriteria.uniqueResult();
+        if (tabAccess != null) {
+          // there is a window access defined and a tab access defined too
+          windowAccess.isEditableField();
+          tabAccess.isEditableField();
+
+          isReadOnly = !tabAccess.isEditableField();
+          //
+        } else {
+          // There is a window access defined but there is not a tab access defined
+          isReadOnly = !windowAccess.isEditableField();
+        }
+      } else {
+        // there is not a window access defined. the user should not even be capable of opening the
+        // window
+        isReadOnly = true;
+      }
+      return isReadOnly;
     }
 
     public String getReadOnlyStringValue() {