Repository: incubator-unomi
Updated Branches:
  refs/heads/master 97df6d1cd -> 64d109ef1


UNOMI-129 : Allow multiple profile properties Add/Update/Delete through an event


Project: http://git-wip-us.apache.org/repos/asf/incubator-unomi/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-unomi/commit/64d109ef
Tree: http://git-wip-us.apache.org/repos/asf/incubator-unomi/tree/64d109ef
Diff: http://git-wip-us.apache.org/repos/asf/incubator-unomi/diff/64d109ef

Branch: refs/heads/master
Commit: 64d109ef160626b819e3c29f58a0941d1dd13f47
Parents: 97df6d1
Author: Abdelkader Midani <[email protected]>
Authored: Thu Oct 12 14:46:29 2017 +0200
Committer: Abdelkader Midani <[email protected]>
Committed: Thu Oct 12 14:46:36 2017 +0200

----------------------------------------------------------------------
 .../unomi/persistence/spi/PropertyHelper.java   | 59 +++++++++++++-------
 .../actions/UpdateProfilePropertiesAction.java  | 22 +++++---
 .../actions/updateProfilePropertiesAction.json  |  4 +-
 3 files changed, 55 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/64d109ef/persistence-spi/src/main/java/org/apache/unomi/persistence/spi/PropertyHelper.java
----------------------------------------------------------------------
diff --git 
a/persistence-spi/src/main/java/org/apache/unomi/persistence/spi/PropertyHelper.java
 
b/persistence-spi/src/main/java/org/apache/unomi/persistence/spi/PropertyHelper.java
index 39b7580..d6e5be8 100644
--- 
a/persistence-spi/src/main/java/org/apache/unomi/persistence/spi/PropertyHelper.java
+++ 
b/persistence-spi/src/main/java/org/apache/unomi/persistence/spi/PropertyHelper.java
@@ -41,17 +41,21 @@ public class PropertyHelper {
     public static boolean setProperty(Object target, String propertyName, 
Object propertyValue, String setPropertyStrategy) {
         try {
             String parentPropertyName;
-            if(setPropertyStrategy!=null && 
setPropertyStrategy.equals("remove")){
-                if(resolver.hasNested(propertyName)) {
+            if (setPropertyStrategy != null && 
setPropertyStrategy.equals("remove")) {
+                if (resolver.hasNested(propertyName)) {
                     parentPropertyName = propertyName.substring(0, 
propertyName.lastIndexOf('.'));
-                    try{
+                    try {
                         Object parentPropertyValue = 
PropertyUtils.getNestedProperty(target, parentPropertyName);
-                        if(parentPropertyValue instanceof HashMap){
-                            
((HashMap)parentPropertyValue).remove(propertyName.substring(propertyName.lastIndexOf('.')+1));
-                            PropertyUtils.setNestedProperty(target, 
parentPropertyName, parentPropertyValue);
-                            return true;
+                        if (parentPropertyValue instanceof HashMap) {
+                            if (((HashMap) 
parentPropertyValue).keySet().contains(propertyName.substring(propertyName.lastIndexOf('.')
 + 1))) {
+                                ((HashMap) 
parentPropertyValue).remove(propertyName.substring(propertyName.lastIndexOf('.')
 + 1));
+                                PropertyUtils.setNestedProperty(target, 
parentPropertyName, parentPropertyValue);
+                                return true;
+                            } else {
+                                return false;
+                            }
                         }
-                    } catch(NestedNullException ex){
+                    } catch (NestedNullException ex) {
                         return false;
                     }
 
@@ -81,7 +85,7 @@ public class PropertyHelper {
                     BeanUtils.setProperty(target, propertyName, values);
                     return true;
                 }
-            } else if (propertyValue != null && 
!propertyValue.equals(BeanUtils.getProperty(target, propertyName))) {
+            } else if (propertyValue != null && !compareValues(propertyValue, 
BeanUtils.getProperty(target, propertyName))) {
                 if (setPropertyStrategy == null ||
                         setPropertyStrategy.equals("alwaysSet") ||
                         (setPropertyStrategy.equals("setIfMissing") && 
BeanUtils.getProperty(target, propertyName) == null)) {
@@ -97,7 +101,7 @@ public class PropertyHelper {
 
     public static Integer getInteger(Object value) {
         if (value instanceof Number) {
-            return ((Number)value).intValue();
+            return ((Number) value).intValue();
         } else {
             try {
                 return Integer.parseInt(value.toString());
@@ -111,33 +115,48 @@ public class PropertyHelper {
     public static Boolean getBooleanValue(Object setPropertyValueBoolean) {
 
         if (setPropertyValueBoolean instanceof Boolean) {
-            return((Boolean)setPropertyValueBoolean);
+            return ((Boolean) setPropertyValueBoolean);
         } else if (setPropertyValueBoolean instanceof Number) {
-            if (((Number)setPropertyValueBoolean).intValue() >= 1) {
-                return  new Boolean(true);
+            if (((Number) setPropertyValueBoolean).intValue() >= 1) {
+                return new Boolean(true);
             } else {
-                return  new Boolean(false);
+                return new Boolean(false);
             }
         } else {
-            if (((String)setPropertyValueBoolean).equalsIgnoreCase("true") || 
((String)setPropertyValueBoolean).equalsIgnoreCase("on") ||
-                    ((String)setPropertyValueBoolean).equalsIgnoreCase("yes") 
|| ((String)setPropertyValueBoolean).equalsIgnoreCase("1")) {
-                return  new Boolean(true);
+            if (((String) setPropertyValueBoolean).equalsIgnoreCase("true") || 
((String) setPropertyValueBoolean).equalsIgnoreCase("on") ||
+                    ((String) setPropertyValueBoolean).equalsIgnoreCase("yes") 
|| ((String) setPropertyValueBoolean).equalsIgnoreCase("1")) {
+                return new Boolean(true);
             } else {
-                return  new Boolean(false);
+                return new Boolean(false);
             }
         }
 
     }
 
     public static Object getValueByTypeId(Object propertyValue, String 
valueTypeId) {
-        if(("boolean".equals(valueTypeId)) ) {
+        if (("boolean".equals(valueTypeId))) {
             return getBooleanValue(propertyValue);
-        } else if("integer".equals(valueTypeId)) {
+        } else if ("integer".equals(valueTypeId)) {
             return getInteger(propertyValue);
         } else {
             return propertyValue.toString();
         }
     }
 
+    public static boolean compareValues(Object propertyValue, Object 
beanPropertyValue) {
+        if (propertyValue == null) {
+            return true;
+        } else if (beanPropertyValue == null) {
+            return false;
+        }
+        if (propertyValue instanceof Integer) {
+            return propertyValue.equals(getInteger(beanPropertyValue));
+        } else if (propertyValue instanceof Boolean) {
+            return propertyValue.equals(getBooleanValue(beanPropertyValue));
+        } else {
+            return propertyValue.equals(beanPropertyValue);
+        }
+    }
+
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/64d109ef/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/UpdateProfilePropertiesAction.java
----------------------------------------------------------------------
diff --git 
a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/UpdateProfilePropertiesAction.java
 
b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/UpdateProfilePropertiesAction.java
index b4b6c7b..0a16434 100644
--- 
a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/UpdateProfilePropertiesAction.java
+++ 
b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/UpdateProfilePropertiesAction.java
@@ -33,6 +33,7 @@ import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.regex.Pattern;
 
 public class UpdateProfilePropertiesAction implements ActionExecutor {
 
@@ -61,27 +62,31 @@ public class UpdateProfilePropertiesAction implements 
ActionExecutor {
 
         Map<String, Object> propsToAdd = (HashMap<String, Object>) 
event.getProperties().get(PROPS_TO_ADD);
         for (String prop : propsToAdd.keySet()) {
-            PropertyType propType = profileService.getPropertyType(prop);
+            String[] splitPropName = prop.split(Pattern.quote("."));
+            PropertyType propType = 
profileService.getPropertyType(splitPropName[splitPropName.length - 1]);
             if (propType != null) {
-                isProfileUpdated = PropertyHelper.setProperty(target, 
"properties." + prop, PropertyHelper.getValueByTypeId(propsToAdd.get(prop), 
propType.getValueTypeId()), "setIfMissing") || isProfileUpdated;
+                isProfileUpdated |= PropertyHelper.setProperty(target, prop, 
PropertyHelper.getValueByTypeId(propsToAdd.get(prop), 
propType.getValueTypeId()), "setIfMissing");
             }
         }
         Map<String, Object> propsToUpdate = (HashMap<String, Object>) 
event.getProperties().get(PROPS_TO_UPDATE);
         for (String prop : propsToUpdate.keySet()) {
-            PropertyType propType = profileService.getPropertyType(prop);
+            String[] splitPropName = prop.split(Pattern.quote("."));
+            PropertyType propType = 
profileService.getPropertyType(splitPropName[splitPropName.length - 1]);
             if (propType != null) {
-                isProfileUpdated = PropertyHelper.setProperty(target, 
"properties." + prop, PropertyHelper.getValueByTypeId(propsToUpdate.get(prop), 
propType.getValueTypeId()), "alwaysSet") || isProfileUpdated;
+                isProfileUpdated |= PropertyHelper.setProperty(target, prop, 
PropertyHelper.getValueByTypeId(propsToUpdate.get(prop), 
propType.getValueTypeId()), "alwaysSet");
             }
         }
         List<String> propsToDelete = (List<String>) 
event.getProperties().get(PROPS_TO_DELETE);
         for (String prop : propsToDelete) {
-            PropertyType propType = profileService.getPropertyType(prop);
+            String[] splitPropName = prop.split(Pattern.quote("."));
+            PropertyType propType = 
profileService.getPropertyType(splitPropName[splitPropName.length - 1]);
             if (propType != null) {
-                isProfileUpdated = PropertyHelper.setProperty(target, 
"properties." + prop, null, "remove") || isProfileUpdated;
+                isProfileUpdated |= PropertyHelper.setProperty(target, prop, 
null, "remove");
             }
         }
 
-        if ((StringUtils.isNotBlank(targetProfileId) || (event.getProfile() != 
null && StringUtils.isNotBlank(event.getProfile().getItemId()))) && 
isProfileUpdated) {
+        if (StringUtils.isNotBlank(targetProfileId) && isProfileUpdated &&
+                event.getProfile() != null && 
!targetProfileId.equals(event.getProfile().getItemId())) {
             profileService.save(target);
             Event profileUpdated = new Event("profileUpdated", null, target, 
null, null, target, new Date());
             profileUpdated.setPersistent(false);
@@ -89,7 +94,10 @@ public class UpdateProfilePropertiesAction implements 
ActionExecutor {
             if ((changes & EventService.PROFILE_UPDATED) == 
EventService.PROFILE_UPDATED) {
                 profileService.save(target);
             }
+            return EventService.NO_CHANGE;
+
         }
+
         return isProfileUpdated ? EventService.PROFILE_UPDATED : 
EventService.NO_CHANGE;
 
     }

http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/64d109ef/plugins/baseplugin/src/main/resources/META-INF/cxs/actions/updateProfilePropertiesAction.json
----------------------------------------------------------------------
diff --git 
a/plugins/baseplugin/src/main/resources/META-INF/cxs/actions/updateProfilePropertiesAction.json
 
b/plugins/baseplugin/src/main/resources/META-INF/cxs/actions/updateProfilePropertiesAction.json
index 21a46d1..bd076ab 100644
--- 
a/plugins/baseplugin/src/main/resources/META-INF/cxs/actions/updateProfilePropertiesAction.json
+++ 
b/plugins/baseplugin/src/main/resources/META-INF/cxs/actions/updateProfilePropertiesAction.json
@@ -5,9 +5,7 @@
     "description": "Update multiple profile properties",
     "systemTags": [
       "profileTags",
-      "demographic",
-      "availableToEndUser",
-      "allowMultipleInstances"
+      "demographic"
     ],
     "readOnly": true
   },

Reply via email to