Merged from PI
authorUnai Martirena <unai.martirena@openbravo.com>
Fri, 04 Mar 2016 14:44:51 +0100
changeset 28829 798b079b6972
parent 28824 4b9f7badfbad (current diff)
parent 28828 d7b14fb65c2b (diff)
child 28830 637037788ad7
Merged from PI
--- a/src-test/src/org/openbravo/test/preference/PreferenceTest.java	Fri Mar 04 08:49:57 2016 +0100
+++ b/src-test/src/org/openbravo/test/preference/PreferenceTest.java	Fri Mar 04 14:44:51 2016 +0100
@@ -11,16 +11,18 @@
  * under the License. 
  * The Original Code is Openbravo ERP. 
  * The Initial Developer of the Original Code is Openbravo SLU 
- * All portions are Copyright (C) 2010-2014 Openbravo SLU 
+ * All portions are Copyright (C) 2010-2016 Openbravo SLU
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
  */
 package org.openbravo.test.preference;
 
+import static org.hamcrest.Matchers.is;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 
 import java.sql.Connection;
@@ -282,6 +284,35 @@
   }
 
   @Test
+  public void testH1ClientVisibility() throws PropertyException {
+    setSystemAdministratorContext();
+    Client testClient = OBDal.getInstance().getProxy(Client.class, TEST_CLIENT_ID);
+    Client systemClient = OBDal.getInstance().getProxy(Client.class, "0");
+
+    final String propAttribute = "clientPropertyTest";
+    Preference p1 = Preferences.setPreferenceValue(propAttribute, "test client", false, testClient,
+        null, null, null, null, null);
+    Preference p2 = Preferences.setPreferenceValue(propAttribute, "system", false, systemClient,
+        null, null, null, null, null);
+
+    OBDal.getInstance().flush();
+
+    String valueClient = Preferences.getPreferenceValue(propAttribute, false, testClient, OBContext
+        .getOBContext().getCurrentOrganization(), OBContext.getOBContext().getUser(), OBContext
+        .getOBContext().getRole(), null);
+    assertThat("preference for client", valueClient, is("test client"));
+
+    String valueSystem = Preferences.getPreferenceValue(propAttribute, false, systemClient, OBDal
+        .getInstance().getProxy(Organization.class, "0"), OBContext.getOBContext().getUser(),
+        OBContext.getOBContext().getRole(), null);
+
+    assertThat("preference for system", valueSystem, is("system"));
+
+    OBDal.getInstance().remove(p1);
+    OBDal.getInstance().remove(p2);
+  }
+
+  @Test
   public void testIPLOrgVisibility() throws SQLException {
 
     setSystemAdministratorContext();
--- a/src/org/openbravo/erpCommon/businessUtility/Preferences.java	Fri Mar 04 08:49:57 2016 +0100
+++ b/src/org/openbravo/erpCommon/businessUtility/Preferences.java	Fri Mar 04 14:44:51 2016 +0100
@@ -11,7 +11,7 @@
  * under the License. 
  * The Original Code is Openbravo ERP. 
  * The Initial Developer of the Original Code is Openbravo SLU 
- * All portions are Copyright (C) 2010-2015 Openbravo SLU 
+ * All portions are Copyright (C) 2010-2016 Openbravo SLU
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -47,6 +47,7 @@
  */
 public class Preferences {
   private static final Logger log4j = Logger.getLogger(Preferences.class);
+  private static final String SYSTEM = "0";
 
   /**
    * Obtains a list of all preferences that are applicable at the given visibility level (client,
@@ -72,7 +73,7 @@
         } else {
           // There is a preference for the current property, check whether it is higher priority and
           // if so replace it. In case of conflict leave current preference.
-          if (isHigherPriority(pref, existentPreference, parentTree) == 1) {
+          if (getHighestPriority(pref, existentPreference, parentTree) == 1) {
             preferences.remove(existentPreference);
             preferences.add(pref);
           }
@@ -196,7 +197,7 @@
           selectedPreference = preference;
           continue;
         }
-        int higherPriority = isHigherPriority(selectedPreference, preference, parentTree);
+        int higherPriority = getHighestPriority(selectedPreference, preference, parentTree);
         switch (higherPriority) {
         case 1:
           // do nothing, selected one has higher priority
@@ -449,6 +450,8 @@
       parameters.add(property);
     }
 
+    hql.append(" order by p.id");
+
     OBQuery<Preference> qPref = OBDal.getInstance().createQuery(Preference.class, hql.toString());
     qPref.setParameters(parameters);
     qPref.setFilterOnActive(activeFilterEnabled);
@@ -487,13 +490,22 @@
    *         <li>0 in case of conflict (both have identical visibility and value)
    *         </ul>
    */
-  private static int isHigherPriority(Preference pref1, Preference pref2, List<String> parentTree) {
+  private static int getHighestPriority(Preference pref1, Preference pref2, List<String> parentTree) {
     // Check priority by client
-    if ((pref2.getVisibleAtClient() == null || pref2.getVisibleAtClient().getId().equals("0"))
-        && pref1.getVisibleAtClient() != null && !pref1.getVisibleAtClient().getId().equals("0")) {
+
+    // undefined client visibility is handled as system
+    String clientId1 = pref1.getVisibleAtClient() == null ? SYSTEM : (String) DalUtil.getId(pref1
+        .getVisibleAtClient());
+    String clientId2 = pref2.getVisibleAtClient() == null ? SYSTEM : (String) DalUtil.getId(pref2
+        .getVisibleAtClient());
+    if (!SYSTEM.equals(clientId1) && SYSTEM.equals(clientId2)) {
       return 1;
     }
 
+    if (SYSTEM.equals(clientId1) && !SYSTEM.equals(clientId2)) {
+      return 2;
+    }
+
     // Check priority by organization
     Organization org1 = pref1.getVisibleAtOrganization();
     Organization org2 = pref2.getVisibleAtOrganization();
@@ -501,7 +513,7 @@
       return 1;
     }
 
-    if ((org1 == null && org2 != null)) {
+    if (org1 == null && org2 != null) {
       return 2;
     }