You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by sh...@apache.org on 2020/11/11 09:21:01 UTC

[unomi] 15/17: UNOMI-379_support_addValues (#207)

This is an automated email from the ASF dual-hosted git repository.

shuber pushed a commit to branch unomi-1.5.x
in repository https://gitbox.apache.org/repos/asf/unomi.git

commit 9d89baae0488358ade562b42d62454efcc1eddba
Author: amitco1 <am...@yotpo.com>
AuthorDate: Tue Nov 10 11:56:23 2020 +0200

    UNOMI-379_support_addValues (#207)
    
    Co-authored-by: Gilad Weinbach <gw...@yotpo.com>
    (cherry picked from commit 353a4b74dcecceb27951fa0bad12736f0ec37377)
---
 .../unomi/persistence/spi/PropertyHelper.java      | 33 ++++++++++++++++++----
 1 file changed, 28 insertions(+), 5 deletions(-)

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 98ec311..38a09e0 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
@@ -25,10 +25,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.lang.reflect.InvocationTargetException;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.LinkedHashMap;
-import java.util.List;
+import java.util.*;
 
 /**
  * Helper method for properties
@@ -85,7 +82,23 @@ public class PropertyHelper {
                     BeanUtils.setProperty(target, propertyName, values);
                     return true;
                 }
-            } else if (propertyValue != null && !compareValues(propertyValue, BeanUtils.getProperty(target, propertyName))) {
+            }
+            if (setPropertyStrategy != null && setPropertyStrategy.equals("addValues")) {
+                Object newValues = propertyValue;
+                List<Object> newValuesList = convertToList(newValues);
+
+                Object previousValue = PropertyUtils.getProperty(target, propertyName);
+                List<Object> previousValueList = convertToList(previousValue);
+
+                newValuesList.addAll(previousValueList);
+                Set<Object> propertiesSet = new HashSet<>(newValuesList);
+                List<Object> propertiesList = Arrays.asList(propertiesSet.toArray());
+
+                BeanUtils.setProperty(target, propertyName, propertiesList);
+                return true;
+
+            }
+            else if (propertyValue != null && !compareValues(propertyValue, BeanUtils.getProperty(target, propertyName))) {
                 if (setPropertyStrategy == null ||
                         setPropertyStrategy.equals("alwaysSet") ||
                         (setPropertyStrategy.equals("setIfMissing") && BeanUtils.getProperty(target, propertyName) == null)) {
@@ -99,6 +112,16 @@ public class PropertyHelper {
         return false;
     }
 
+    public static List<Object> convertToList(Object value) {
+        List<Object> convertedList = new ArrayList<>();
+        if (value != null && value instanceof List) {
+            convertedList.addAll((List) value);
+        } else if (value != null) {
+            convertedList.add(value);
+        }
+        return convertedList;
+    }
+
     public static Integer getInteger(Object value) {
         if (value instanceof Number) {
             return ((Number) value).intValue();