Author: jleroux
Date: Wed Jun 15 11:23:54 2016
New Revision: 1748560

URL: http://svn.apache.org/viewvc?rev=1748560&view=rev
Log:
A patch from Wai for "EntityUtilProperties" 
https://issues.apache.org/jira/browse/OFBIZ-7112

OFBiz reads properties from either a properties file or the 
entity:SystemProperty. The way it worked previously is that OFBiz reads from 
the entity:SystemProperty first and if there is no value associated with the 
target propertyname, it would then locate the value from the relevant 
properties file.
In other words, if there is a database entry for a property, the database entry 
should override the associated properties file.
The issue is that if a database entry exist but the value is empty, it would 
look for a value from the properties file.  It should not do so.  If a database 
entry exists for the propertyname of interest, the value should be taken from 
the database even if it holds an empty value.

"isExistInDb" is first defined in Map<String, String> 
getSystemPropertyValue(String, String, Delegator).  This method is call by many 
other methods.  "isExistInDb" is used to indicate whether the method is 
returning an empty value for the property that 'exists' in the database rather 
than as a result of an absent database entry for the property.

The other methods...
  boolean propertyValueEqualsIgnoreCase(String, String, String, Delegator)
  String getPropertyValue(String, String, String, Delegator)
  String getPropertyValue(String, String, Delegator) 
  String getMessage(String, String, Locale, Delegator)
uses the above method and needs to decide whether to use the empty value 
returned (ie. empty value from an existing property entry in database) or 
decide if it needs to read from the property file (ie. property entry does not 
exist in database)

jleroux: this also add systemPropertyValue data in CommonSystemPropertyData.xml

Modified:
    ofbiz/trunk/framework/common/data/CommonSystemPropertyData.xml
    
ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityUtilProperties.java

Modified: ofbiz/trunk/framework/common/data/CommonSystemPropertyData.xml
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/data/CommonSystemPropertyData.xml?rev=1748560&r1=1748559&r2=1748560&view=diff
==============================================================================
--- ofbiz/trunk/framework/common/data/CommonSystemPropertyData.xml (original)
+++ ofbiz/trunk/framework/common/data/CommonSystemPropertyData.xml Wed Jun 15 
11:23:54 2016
@@ -18,20 +18,20 @@
     under the License.
 -->
 <entity-engine-xml>
-    <SystemProperty systemResourceId="general" 
systemPropertyId="ORGANIZATION_PARTY" description="the default 
organizationPartyId for used in dropdowns and reports"/>
-    <SystemProperty systemResourceId="general" systemPropertyId="VISUAL_THEME" 
description="ID of the VisualTheme to use if there is no VISUAL_THEME 
UserPreference record for the current user (ie default value)"/>
-    <SystemProperty systemResourceId="general" 
systemPropertyId="currency.uom.id.default" description="The default currency to 
use for prices, etc"/>
-    <SystemProperty systemResourceId="general" 
systemPropertyId="country.geo.id.default" description="the default country for 
drop downs"/>
-    <SystemProperty systemResourceId="general" 
systemPropertyId="partner.trackingCodeId.default" description="the default 
trackingCodeId to use for Partner Managed Tracking Codes"/>
-    <SystemProperty systemResourceId="general" 
systemPropertyId="defaultFromEmailAddress" description="general default 
'fromEmailAddress' can be overridden in: EmailTemplateSetting"/>
-    <SystemProperty systemResourceId="general" 
systemPropertyId="mail.notifications.enabled" description="mail notifications 
enabled (Y|N)"/>
-    <SystemProperty systemResourceId="general" 
systemPropertyId="mail.smtp.relay.host" description="the default mail server to 
use"/>
-    <SystemProperty systemResourceId="general" 
systemPropertyId="mail.smtp.auth.user" description="SMTP Auth username 
setting"/>
-    <SystemProperty systemResourceId="general" 
systemPropertyId="mail.smtp.auth.password" description="SMTP Auth password 
setting"/>
-    <SystemProperty systemResourceId="general" 
systemPropertyId="mail.smtp.port" description="Gmail smtp port can be either 
465 or 587"/>
-    <SystemProperty systemResourceId="general" 
systemPropertyId="mail.smtp.starttls.enable" description="Gmail requires 
StartTLS"/>
-    <SystemProperty systemResourceId="general" 
systemPropertyId="mail.smtp.socketFactory.port" description="Port needs to be 
the same as mail.smtp.port"/>
-    <SystemProperty systemResourceId="general" 
systemPropertyId="mail.smtp.socketFactory.class" description="JSSE socket 
factory"/>
-    <SystemProperty systemResourceId="general" 
systemPropertyId="mail.smtp.socketFactory.fallback" description="Fallback 
[true|false] determines whether you will allow a non secure connection if you 
are unable to get a secure one"/>
-    <SystemProperty systemResourceId="general" 
systemPropertyId="mail.smtp.sendpartial" description="setting this property to 
false will abort sending the message"/>
+    <SystemProperty systemResourceId="general" 
systemPropertyId="ORGANIZATION_PARTY" systemPropertyValue="Company" 
description="the default organizationPartyId for used in dropdowns and 
reports"/>
+    <SystemProperty systemResourceId="general" systemPropertyId="VISUAL_THEME" 
systemPropertyValue="FLAT_GREY" description="ID of the VisualTheme to use if 
there is no VISUAL_THEME UserPreference record for the current user (ie default 
value)"/>
+    <SystemProperty systemResourceId="general" 
systemPropertyId="currency.uom.id.default" systemPropertyValue="USD" 
description="The default currency to use for prices, etc"/>
+    <SystemProperty systemResourceId="general" 
systemPropertyId="country.geo.id.default" systemPropertyValue="USA" 
description="the default country for drop downs"/>
+    <SystemProperty systemResourceId="general" 
systemPropertyId="partner.trackingCodeId.default" systemPropertyValue="" 
description="the default trackingCodeId to use for Partner Managed Tracking 
Codes"/>
+    <SystemProperty systemResourceId="general" 
systemPropertyId="defaultFromEmailAddress" 
systemPropertyValue="[email protected]" description="general default 
'fromEmailAddress' can be overridden in: EmailTemplateSetting"/>
+    <SystemProperty systemResourceId="general" 
systemPropertyId="mail.notifications.enabled" systemPropertyValue="N" 
description="mail notifications enabled (Y|N)"/>
+    <SystemProperty systemResourceId="general" 
systemPropertyId="mail.smtp.relay.host" systemPropertyValue="" description="the 
default mail server to use"/>
+    <SystemProperty systemResourceId="general" 
systemPropertyId="mail.smtp.auth.user" systemPropertyValue="" description="SMTP 
Auth username setting"/>
+    <SystemProperty systemResourceId="general" 
systemPropertyId="mail.smtp.auth.password" systemPropertyValue="" 
description="SMTP Auth password setting"/>
+    <SystemProperty systemResourceId="general" 
systemPropertyId="mail.smtp.port" systemPropertyValue="465" description="Gmail 
smtp port can be either 465 or 587"/>
+    <SystemProperty systemResourceId="general" 
systemPropertyId="mail.smtp.starttls.enable" systemPropertyValue="true" 
description="Gmail requires StartTLS"/>
+    <SystemProperty systemResourceId="general" 
systemPropertyId="mail.smtp.socketFactory.port" systemPropertyValue="465" 
description="Port needs to be the same as mail.smtp.port"/>
+    <SystemProperty systemResourceId="general" 
systemPropertyId="mail.smtp.socketFactory.class" 
systemPropertyValue="javax.net.ssl.SSLSocketFactory" description="JSSE socket 
factory"/>
+    <SystemProperty systemResourceId="general" 
systemPropertyId="mail.smtp.socketFactory.fallback" systemPropertyValue="false" 
description="Fallback [true|false] determines whether you will allow a non 
secure connection if you are unable to get a secure one"/>
+    <SystemProperty systemResourceId="general" 
systemPropertyId="mail.smtp.sendpartial" systemPropertyValue="true" 
description="setting this property to false will abort sending the message"/>
 </entity-engine-xml>

Modified: 
ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityUtilProperties.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityUtilProperties.java?rev=1748560&r1=1748559&r2=1748560&view=diff
==============================================================================
--- 
ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityUtilProperties.java
 (original)
+++ 
ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityUtilProperties.java
 Wed Jun 15 11:23:54 2016
@@ -24,7 +24,9 @@ import java.io.Serializable;
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.net.URL;
+import java.util.HashMap;
 import java.util.InvalidPropertiesFormatException;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
@@ -33,63 +35,75 @@ import java.util.ResourceBundle;
 import java.util.Set;
 
 import org.ofbiz.base.util.Debug;
+import org.ofbiz.base.util.UtilMisc;
 import org.ofbiz.base.util.UtilProperties;
 import org.ofbiz.base.util.UtilValidate;
 import org.ofbiz.base.util.collections.ResourceBundleMapWrapper;
 import org.ofbiz.entity.Delegator;
+import org.ofbiz.entity.GenericEntityException;
 import org.ofbiz.entity.GenericValue;
 
 @SuppressWarnings("serial")
 public class EntityUtilProperties implements Serializable {
 
     public final static String module = EntityUtilProperties.class.getName();
-    
-    protected static String getSystemPropertyValue(String resource, String 
name, Delegator delegator) {
-        if (resource == null || resource.length() <= 0) {
-            return null;
+
+    protected static Map<String, String> getSystemPropertyValue(String 
resource, String name, Delegator delegator) {
+        Map<String, String> results = new HashMap<>();
+        results.put("isExistInDb", "N");
+        results.put("value", "");
+
+        if (UtilValidate.isEmpty(resource) || UtilValidate.isEmpty(name)) {
+            results.put("isExistInDb", "N");
+            results.put("value", "");
+            return results;
         }
-        if (name == null || name.length() <= 0) return null;
-        
         resource = resource.replace(".properties", "");
-        
-        // find system property
         try {
             GenericValue systemProperty = EntityQuery.use(delegator)
-                                                     .from("SystemProperty")
-                                                     
.where("systemResourceId", resource, "systemPropertyId", name)
-                                                     .cache()
-                                                     .queryOne();
+                    .from("SystemProperty")
+                    .where("systemResourceId", resource, "systemPropertyId", 
name)
+                    .cache()
+                    .queryOne();
             if (systemProperty != null) {
-                String systemPropertyValue = 
systemProperty.getString("systemPropertyValue");
-                if (UtilValidate.isNotEmpty(systemPropertyValue)) {
-                    return systemPropertyValue;
-                }
+                //property exists in database
+                results.put("isExistInDb", "Y");
+                results.put("value", 
(systemProperty.getString("systemPropertyValue") != null) ? 
systemProperty.getString("systemPropertyValue") : "");
+                return results;
+            } else {
+                //property does not exists in database
+                results.put("isExistInDb", "N");
+                results.put("value", "");
+                return results;
             }
         } catch (Exception e) {
             Debug.logWarning("Could not get a system property for " + name + " 
: " + e.getMessage(), module);
         }
-        return null;
+        return results;
     }
-    
+
     public static boolean propertyValueEquals(String resource, String name, 
String compareString) {
         return UtilProperties.propertyValueEquals(resource, name, 
compareString);
     }
 
     public static boolean propertyValueEqualsIgnoreCase(String resource, 
String name, String compareString, Delegator delegator) {
-        String value = getSystemPropertyValue(resource, name, delegator);
-        if (UtilValidate.isNotEmpty(value)) {
-            return value.trim().equalsIgnoreCase(compareString);
+        Map<String, String> propMap = getSystemPropertyValue(resource, name, 
delegator);
+        if ("Y".equals(propMap.get("isExistInDb"))) {
+            compareString = (compareString == null) ? "" : compareString;
+            return propMap.get("value").equalsIgnoreCase(compareString);
         } else {
             return UtilProperties.propertyValueEqualsIgnoreCase(resource, 
name, compareString);
         }
     }
 
     public static String getPropertyValue(String resource, String name, String 
defaultValue, Delegator delegator) {
-        String value = getSystemPropertyValue(resource, name, delegator);
-        if (UtilValidate.isEmpty(value)) {
-            value = UtilProperties.getPropertyValue(resource, name, 
defaultValue);
+        Map<String, String> propMap = getSystemPropertyValue(resource, name, 
delegator);
+        if ("Y".equals(propMap.get("isExistInDb"))) {
+            String s = propMap.get("value");
+            return (UtilValidate.isEmpty(s)) ? defaultValue : s;
+        } else {
+            return UtilProperties.getPropertyValue(resource, name, 
defaultValue);
         }
-        return value;
     }
 
     public static double getPropertyNumber(String resource, String name, 
double defaultValue) {
@@ -129,11 +143,12 @@ public class EntityUtilProperties implem
     }
 
     public static String getPropertyValue(String resource, String name, 
Delegator delegator) {
-        String value = getSystemPropertyValue(resource, name, delegator);
-        if (UtilValidate.isEmpty(value)) {
-            value = UtilProperties.getPropertyValue(resource, name);
+        Map<String, String> propMap = getSystemPropertyValue(resource, name, 
delegator);
+        if ("Y".equals(propMap.get("isExistInDb"))) {
+            return propMap.get("value");
+        } else {
+            return UtilProperties.getPropertyValue(resource, name);
         }
-        return value;
     }
 
     public static Properties getProperties(String resource) {
@@ -144,10 +159,32 @@ public class EntityUtilProperties implem
         return UtilProperties.getProperties(url);
     }
 
+    public static Properties getProperties(Delegator delegator, String 
resourceName) {
+        Properties properties = UtilProperties.getProperties(resourceName);
+        List<GenericValue> gvList;
+        try {
+            gvList = EntityQuery.use(delegator)
+                    .from("SystemProperty")
+                    .where("systemResourceId", resourceName)
+                    .queryList();
+            if (UtilValidate.isNotEmpty(gvList)) {
+                for (Iterator<GenericValue> i = gvList.iterator(); 
i.hasNext();) {
+                    GenericValue gv = i.next();
+                    if 
(UtilValidate.isNotEmpty(gv.getString("systemPropertyValue"))) {
+                        
properties.setProperty(gv.getString("systemPropertyId"), 
gv.getString("systemPropertyValue"));
+                    }
+                }
+            }
+        } catch (GenericEntityException e) {
+            Debug.logError(e.getMessage(), module);
+        }
+        return properties;
+    }
+
     public static boolean propertyValueEquals(URL url, String name, String 
compareString) {
         return UtilProperties.propertyValueEquals(url, name, compareString);
     }
-    
+
     public static boolean propertyValueEqualsIgnoreCase(URL url, String name, 
String compareString) {
         return UtilProperties.propertyValueEqualsIgnoreCase(url, name, 
compareString);
     }
@@ -180,12 +217,34 @@ public class EntityUtilProperties implem
           UtilProperties.setPropertyValueInMemory(resource, name, value);
       }
 
+    public static String setPropertyValue(Delegator delegator, String 
resourceName, String name, String value) {
+        GenericValue gv = null;
+        String prevValue = null;
+        try {
+            gv = EntityQuery.use(delegator)
+                    .from("SystemProperty")
+                    .where("systemResourceId", resourceName, 
"systemPropertyId", name)
+                    .queryOne();
+            if (UtilValidate.isNotEmpty(gv)) {
+                prevValue = gv.getString("systemPropertyValue");
+                gv.set("systemPropertyValue", value);
+            } else {
+                gv = delegator.makeValue("SystemProperty", 
UtilMisc.toMap("systemResourceId", resourceName, "systemPropertyId", name, 
"systemPropertyValue", value, "description", null));
+            }
+            gv.store();
+        } catch (GenericEntityException e) {
+            Debug.logError(String.format("tenantId=%s, exception=%s, 
message=%s", delegator.getDelegatorTenantId(), e.getClass().getName(), 
e.getMessage()), module);
+        }
+        return prevValue;
+    }
+
     public static String getMessage(String resource, String name, Locale 
locale, Delegator delegator) {
-        String value = getSystemPropertyValue(resource, name, delegator);
-        if (UtilValidate.isEmpty(value)) {
-            value = UtilProperties.getMessage(resource, name, locale);
+        Map<String, String> propMap = getSystemPropertyValue(resource, name, 
delegator);
+        if ("Y".equals(propMap.get("isExistInDb"))) {
+            return propMap.get("value");
+        } else {
+            return UtilProperties.getMessage(resource, name, locale);
         }
-        return value;
     }
 
     public static String getMessage(String resource, String name, Object[] 
arguments, Locale locale) {


Reply via email to