You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by ta...@apache.org on 2020/05/22 15:05:04 UTC

[unomi] 01/01: UNOMI-338 avoid update date with invalid custom timestamp

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

taybou pushed a commit to branch UNOMI-338-profile-update
in repository https://gitbox.apache.org/repos/asf/unomi.git

commit 100013c46deb42e58f696cb6586e3ce04fe5516e
Author: Taybou <be...@gmail.com>
AuthorDate: Fri May 22 17:04:52 2020 +0200

    UNOMI-338 avoid update date with invalid custom timestamp
---
 .../baseplugin/actions/SetPropertyAction.java      | 23 +++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/SetPropertyAction.java b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/SetPropertyAction.java
index 4134cec..d65e649 100644
--- a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/SetPropertyAction.java
+++ b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/SetPropertyAction.java
@@ -22,7 +22,10 @@ import org.apache.unomi.api.actions.Action;
 import org.apache.unomi.api.actions.ActionExecutor;
 import org.apache.unomi.api.services.EventService;
 import org.apache.unomi.persistence.spi.PropertyHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
+import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.HashMap;
@@ -30,6 +33,7 @@ import java.util.Map;
 import java.util.TimeZone;
 
 public class SetPropertyAction implements ActionExecutor {
+    private static final Logger logger = LoggerFactory.getLogger(SetPropertyAction.class.getName());
 
     private EventService eventService;
 
@@ -69,7 +73,24 @@ public class SetPropertyAction implements ActionExecutor {
         if (propertyValue != null && propertyValue.equals("now")) {
             SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
             format.setTimeZone(TimeZone.getTimeZone("UTC"));
-            propertyValue = format.format(event.getTimeStamp());
+
+            Date date = new Date();
+            Date firstVisit = new Date();
+
+            try {
+                String propertyFirstVisit = (String) event.getProfile().getProperties().get("firstVisit");
+                if (propertyFirstVisit != null) {
+                    firstVisit = format.parse(propertyFirstVisit);
+                }
+
+                if (event.getTimeStamp().after(firstVisit)) {
+                    date = event.getTimeStamp();
+                }
+            } catch (ParseException e) {
+                logger.error("Error to parse date", e);
+            }
+
+            propertyValue = format.format(date);
         }
 
         if (storeInSession) {