You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by jk...@apache.org on 2021/07/09 15:41:43 UTC

[unomi] 01/01: test action

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

jkevan pushed a commit to branch testAction
in repository https://gitbox.apache.org/repos/asf/unomi.git

commit 9bc17e5ce2a2360193a17830a0585ed3b952869a
Author: Kevan <ke...@jahia.com>
AuthorDate: Fri Jul 9 17:41:31 2021 +0200

    test action
---
 .../baseplugin/actions/IncrementOfferAction.java   | 61 ++++++++++++++++++++++
 .../META-INF/cxs/actions/incrementOfferAction.json | 14 +++++
 .../resources/OSGI-INF/blueprint/blueprint.xml     |  6 +++
 3 files changed, 81 insertions(+)

diff --git a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/IncrementOfferAction.java b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/IncrementOfferAction.java
new file mode 100644
index 0000000..1b70bb7
--- /dev/null
+++ b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/IncrementOfferAction.java
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.unomi.plugins.baseplugin.actions;
+
+import org.apache.unomi.api.CustomItem;
+import org.apache.unomi.api.Event;
+import org.apache.unomi.api.Profile;
+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 java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class IncrementOfferAction implements ActionExecutor {
+
+    public int execute(Action action, Event event) {
+        if (event.getEventType().equals("offer")) {
+            Profile profile = event.getProfile();
+            CustomItem offer = (CustomItem) event.getTarget();
+            List<Map<String, Object>> offers = profile.getProperties().containsKey("offers") ?
+                    (List<Map<String, Object>>) profile.getProperties().get("offers") :
+                    new ArrayList<>();
+
+            // check existing offer
+            for (Map<String, Object> offerEntry : offers) {
+                if (offerEntry.get("offerId").equals(offer.getItemId())) {
+                    offerEntry.put("offerScore", ((Integer) offerEntry.get("offerScore")) + ((Integer) offer.getProperties().get("weight")));
+                    PropertyHelper.setProperty(event.getProfile(), "properties.offers", offers, null);
+                    return EventService.PROFILE_UPDATED;
+                }
+            }
+
+            // new offer
+            Map<String, Object> newOffer = new HashMap<>();
+            newOffer.put("offerId", offer.getItemId());
+            newOffer.put("offerScore", offer.getProperties().get("weight"));
+            offers.add(newOffer);
+            PropertyHelper.setProperty(event.getProfile(), "properties.offers", offers, null);
+            return EventService.PROFILE_UPDATED;
+        }
+        return EventService.NO_CHANGE;
+    }
+}
\ No newline at end of file
diff --git a/plugins/baseplugin/src/main/resources/META-INF/cxs/actions/incrementOfferAction.json b/plugins/baseplugin/src/main/resources/META-INF/cxs/actions/incrementOfferAction.json
new file mode 100644
index 0000000..c907e65
--- /dev/null
+++ b/plugins/baseplugin/src/main/resources/META-INF/cxs/actions/incrementOfferAction.json
@@ -0,0 +1,14 @@
+{
+  "metadata": {
+    "id": "incrementOfferAction",
+    "name": "incrementOfferAction",
+    "description": "",
+    "systemTags": [
+      "profileTags",
+      "event"
+    ],
+    "readOnly": true
+  },
+  "actionExecutor": "incrementOfferAction",
+  "parameters": []
+}
diff --git a/plugins/baseplugin/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/plugins/baseplugin/src/main/resources/OSGI-INF/blueprint/blueprint.xml
index 53fb4e6..765eb64 100644
--- a/plugins/baseplugin/src/main/resources/OSGI-INF/blueprint/blueprint.xml
+++ b/plugins/baseplugin/src/main/resources/OSGI-INF/blueprint/blueprint.xml
@@ -174,6 +174,12 @@
 
 
     <!-- Action executors -->
+    <service interface="org.apache.unomi.api.actions.ActionExecutor">
+        <service-properties>
+            <entry key="actionExecutorId" value="incrementOfferAction"/>
+        </service-properties>
+        <bean class="org.apache.unomi.plugins.baseplugin.actions.IncrementOfferAction"/>
+    </service>
 
     <service interface="org.apache.unomi.api.actions.ActionExecutor">
         <service-properties>