Repository: incubator-unomi Updated Branches: refs/heads/master 3e3c8cce0 -> fd1c9fba8
UNOMI-129 : support persona and systemProperties Project: http://git-wip-us.apache.org/repos/asf/incubator-unomi/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-unomi/commit/fd1c9fba Tree: http://git-wip-us.apache.org/repos/asf/incubator-unomi/tree/fd1c9fba Diff: http://git-wip-us.apache.org/repos/asf/incubator-unomi/diff/fd1c9fba Branch: refs/heads/master Commit: fd1c9fba8dde36a747992e1226aca77909707f64 Parents: 3e3c8cc Author: Abdelkader Midani <[email protected]> Authored: Mon Oct 30 04:56:32 2017 +0100 Committer: Abdelkader Midani <[email protected]> Committed: Mon Oct 30 04:56:53 2017 +0100 ---------------------------------------------------------------------- .../router/api/ImportExportConfiguration.java | 20 --- .../src/main/feature/feature.xml | 1 - .../java/org/apache/unomi/itests/AllITs.java | 2 +- .../itests/ProfilePropertiesUpdateActionIT.java | 114 -------------- .../unomi/itests/PropertiesUpdateActionIT.java | 114 ++++++++++++++ .../baseplugin/actions/SetPropertyAction.java | 8 +- .../actions/UpdateProfilePropertiesAction.java | 125 --------------- .../actions/UpdatePropertiesAction.java | 155 +++++++++++++++++++ .../actions/updateProfilePropertiesAction.json | 16 -- .../cxs/actions/updatePropertiesAction.json | 16 ++ .../updateProfilePropertiesEventCondition.json | 6 +- .../cxs/rules/updateProfileProperties.json | 23 --- .../META-INF/cxs/rules/updateProperties.json | 23 +++ .../resources/OSGI-INF/blueprint/blueprint.xml | 4 +- 14 files changed, 318 insertions(+), 309 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/fd1c9fba/extensions/router/router-api/src/main/java/org/apache/unomi/router/api/ImportExportConfiguration.java ---------------------------------------------------------------------- diff --git a/extensions/router/router-api/src/main/java/org/apache/unomi/router/api/ImportExportConfiguration.java b/extensions/router/router-api/src/main/java/org/apache/unomi/router/api/ImportExportConfiguration.java index d048a2e..10209bd 100644 --- a/extensions/router/router-api/src/main/java/org/apache/unomi/router/api/ImportExportConfiguration.java +++ b/extensions/router/router-api/src/main/java/org/apache/unomi/router/api/ImportExportConfiguration.java @@ -37,7 +37,6 @@ public class ImportExportConfiguration extends Item { private String multiValueSeparator = ";"; private String multiValueDelimiter = ""; private boolean active; - private boolean delayedUpdate; private String status; private List<Map<String, Object>> executions = new ArrayList(); @@ -134,25 +133,6 @@ public class ImportExportConfiguration extends Item { } /** - * Retrieves the import configuration delayedUpdate flag. - * - * @return true if the running import configuration update was delayed (Running config when trying to update) - */ - public boolean isDelayedUpdate() { - return this.delayedUpdate; - } - - /** - * Sets the delayedUpdate flag true/false. - * - * @param delayedUpdate a boolean to set to delay or not the running import configuration update - */ - public void setDelayedUpdate(boolean delayedUpdate) { - this.delayedUpdate = delayedUpdate; - } - - - /** * Retrieves the import configuration status for last execution. * * @return status of the last execution http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/fd1c9fba/extensions/router/router-karaf-feature/src/main/feature/feature.xml ---------------------------------------------------------------------- diff --git a/extensions/router/router-karaf-feature/src/main/feature/feature.xml b/extensions/router/router-karaf-feature/src/main/feature/feature.xml index 0d9e122..ff01a02 100644 --- a/extensions/router/router-karaf-feature/src/main/feature/feature.xml +++ b/extensions/router/router-karaf-feature/src/main/feature/feature.xml @@ -27,7 +27,6 @@ <bundle start-level="90">mvn:org.apache.camel/camel-blueprint/2.19.1</bundle> <bundle start-level="90">mvn:org.apache.camel/camel-jackson/2.19.1</bundle> <bundle start-level="90">mvn:org.apache.camel/camel-http-common/2.19.1</bundle> - <bundle start-level="90">mvn:org.apache.camel/camel-servlet/2.19.1</bundle> <bundle start-level="90">mvn:org.apache.camel/camel-ftp/2.19.1</bundle> <bundle start-level="90">mvn:org.apache.camel/camel-stream/2.19.1</bundle> <bundle start-level="90">mvn:org.apache.camel/camel-kafka/2.19.1</bundle> http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/fd1c9fba/itests/src/test/java/org/apache/unomi/itests/AllITs.java ---------------------------------------------------------------------- diff --git a/itests/src/test/java/org/apache/unomi/itests/AllITs.java b/itests/src/test/java/org/apache/unomi/itests/AllITs.java index 38e57e0..a919483 100644 --- a/itests/src/test/java/org/apache/unomi/itests/AllITs.java +++ b/itests/src/test/java/org/apache/unomi/itests/AllITs.java @@ -40,7 +40,7 @@ import org.junit.runners.Suite.SuiteClasses; ProfileImportRankingIT.class, ProfileImportActorsIT.class, ProfileExportIT.class, - ProfilePropertiesUpdateActionIT.class + PropertiesUpdateActionIT.class }) public class AllITs { } http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/fd1c9fba/itests/src/test/java/org/apache/unomi/itests/ProfilePropertiesUpdateActionIT.java ---------------------------------------------------------------------- diff --git a/itests/src/test/java/org/apache/unomi/itests/ProfilePropertiesUpdateActionIT.java b/itests/src/test/java/org/apache/unomi/itests/ProfilePropertiesUpdateActionIT.java deleted file mode 100644 index b89666a..0000000 --- a/itests/src/test/java/org/apache/unomi/itests/ProfilePropertiesUpdateActionIT.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * 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.itests; - -import org.apache.unomi.api.Event; -import org.apache.unomi.api.Profile; -import org.apache.unomi.api.services.EventService; -import org.apache.unomi.api.services.ProfileService; -import org.apache.unomi.plugins.baseplugin.actions.UpdateProfilePropertiesAction; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.ops4j.pax.exam.junit.PaxExam; -import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy; -import org.ops4j.pax.exam.spi.reactors.PerSuite; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.inject.Inject; -import java.io.IOException; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; - -/** - * Created by amidani on 12/10/2017. - */ - -@RunWith(PaxExam.class) -@ExamReactorStrategy(PerSuite.class) -public class ProfilePropertiesUpdateActionIT extends BaseIT { - - private final static Logger LOGGER = LoggerFactory.getLogger(ProfilePropertiesUpdateActionIT.class); - private final static String PROFILE_TARGET_TEST_ID = "profile-target-event"; - private final static String PROFILE_TEST_ID = "profile-to-update-by-event"; - - @Inject - protected ProfileService profileService; - - @Inject - protected EventService eventService; - - @Before - public void setUp() throws IOException { - Profile profile = new Profile(); - profile.setItemId(PROFILE_TEST_ID); - profileService.save(profile); - LOGGER.info("Profile saved with ID [{}].", profile.getItemId()); - - Profile profileTarget = new Profile(); - profileTarget.setItemId(PROFILE_TARGET_TEST_ID); - profileService.save(profileTarget); - LOGGER.info("Profile saved with ID [{}].", profileTarget.getItemId()); - } - - @Test - public void testUpdateProperties_CurrentProfile() { - Profile profile = profileService.load(PROFILE_TARGET_TEST_ID); - Assert.assertNull(profile.getProperty("firstName")); - - Event updateProfileProperties = new Event("updateProfileProperties", null, profile, null, null, profile, new Date()); - updateProfileProperties.setPersistent(false); - - Map<String, Object> propertyToUpdate = new HashMap<>(); - propertyToUpdate.put("properties.firstName", "UPDATED FIRST NAME CURRENT PROFILE"); - - updateProfileProperties.setProperty(UpdateProfilePropertiesAction.PROPS_TO_UPDATE, propertyToUpdate); - int changes = eventService.send(updateProfileProperties); - - LOGGER.info("Changes of the event : {}", changes); - - Assert.assertTrue(changes > 0); - } - - @Test - public void testUpdateProperties_NotCurrentProfile() { - - Profile profile = profileService.load(PROFILE_TARGET_TEST_ID); - Profile profileToUpdate = profileService.load(PROFILE_TEST_ID); - Assert.assertNull(profileToUpdate.getProperty("firstName")); - - Event updateProfileProperties = new Event("updateProfileProperties", null, profile, null, null, profile, new Date()); - updateProfileProperties.setPersistent(false); - - Map<String, Object> propertyToUpdate = new HashMap<>(); - propertyToUpdate.put("properties.firstName", "UPDATED FIRST NAME"); - - updateProfileProperties.setProperty(UpdateProfilePropertiesAction.PROPS_TO_UPDATE, propertyToUpdate); - updateProfileProperties.setProperty(UpdateProfilePropertiesAction.PROFILE_TARGET_ID_KEY, PROFILE_TEST_ID); - int changes = eventService.send(updateProfileProperties); - - LOGGER.info("Changes of the event : {}", changes); - - profileToUpdate = profileService.load(PROFILE_TEST_ID); - Assert.assertEquals(profileToUpdate.getProperty("firstName"), "UPDATED FIRST NAME"); - - } -} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/fd1c9fba/itests/src/test/java/org/apache/unomi/itests/PropertiesUpdateActionIT.java ---------------------------------------------------------------------- diff --git a/itests/src/test/java/org/apache/unomi/itests/PropertiesUpdateActionIT.java b/itests/src/test/java/org/apache/unomi/itests/PropertiesUpdateActionIT.java new file mode 100644 index 0000000..e92aff8 --- /dev/null +++ b/itests/src/test/java/org/apache/unomi/itests/PropertiesUpdateActionIT.java @@ -0,0 +1,114 @@ +/* + * 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.itests; + +import org.apache.unomi.api.Event; +import org.apache.unomi.api.Profile; +import org.apache.unomi.api.services.EventService; +import org.apache.unomi.api.services.ProfileService; +import org.apache.unomi.plugins.baseplugin.actions.UpdatePropertiesAction; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.ops4j.pax.exam.junit.PaxExam; +import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy; +import org.ops4j.pax.exam.spi.reactors.PerSuite; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.inject.Inject; +import java.io.IOException; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by amidani on 12/10/2017. + */ + +@RunWith(PaxExam.class) +@ExamReactorStrategy(PerSuite.class) +public class PropertiesUpdateActionIT extends BaseIT { + + private final static Logger LOGGER = LoggerFactory.getLogger(PropertiesUpdateActionIT.class); + private final static String PROFILE_TARGET_TEST_ID = "profile-target-event"; + private final static String PROFILE_TEST_ID = "profile-to-update-by-event"; + + @Inject + protected ProfileService profileService; + + @Inject + protected EventService eventService; + + @Before + public void setUp() throws IOException { + Profile profile = new Profile(); + profile.setItemId(PROFILE_TEST_ID); + profileService.save(profile); + LOGGER.info("Profile saved with ID [{}].", profile.getItemId()); + + Profile profileTarget = new Profile(); + profileTarget.setItemId(PROFILE_TARGET_TEST_ID); + profileService.save(profileTarget); + LOGGER.info("Profile saved with ID [{}].", profileTarget.getItemId()); + } + + @Test + public void testUpdateProperties_CurrentProfile() { + Profile profile = profileService.load(PROFILE_TARGET_TEST_ID); + Assert.assertNull(profile.getProperty("firstName")); + + Event updateProperties = new Event("updateProperties", null, profile, null, null, profile, new Date()); + updateProperties.setPersistent(false); + + Map<String, Object> propertyToUpdate = new HashMap<>(); + propertyToUpdate.put("properties.firstName", "UPDATED FIRST NAME CURRENT PROFILE"); + + updateProperties.setProperty(UpdatePropertiesAction.PROPS_TO_UPDATE, propertyToUpdate); + int changes = eventService.send(updateProperties); + + LOGGER.info("Changes of the event : {}", changes); + + Assert.assertTrue(changes > 0); + } + + @Test + public void testUpdateProperties_NotCurrentProfile() { + + Profile profile = profileService.load(PROFILE_TARGET_TEST_ID); + Profile profileToUpdate = profileService.load(PROFILE_TEST_ID); + Assert.assertNull(profileToUpdate.getProperty("firstName")); + + Event updateProperties = new Event("updateProperties", null, profile, null, null, profile, new Date()); + updateProperties.setPersistent(false); + + Map<String, Object> propertyToUpdate = new HashMap<>(); + propertyToUpdate.put("properties.firstName", "UPDATED FIRST NAME"); + + updateProperties.setProperty(UpdatePropertiesAction.PROPS_TO_UPDATE, propertyToUpdate); + updateProperties.setProperty(UpdatePropertiesAction.PROFILE_TARGET_ID_KEY, PROFILE_TEST_ID); + int changes = eventService.send(updateProperties); + + LOGGER.info("Changes of the event : {}", changes); + + profileToUpdate = profileService.load(PROFILE_TEST_ID); + Assert.assertEquals(profileToUpdate.getProperty("firstName"), "UPDATED FIRST NAME"); + + } +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/fd1c9fba/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/SetPropertyAction.java ---------------------------------------------------------------------- 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 ed857bd..2952a7e 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 @@ -46,8 +46,8 @@ public class SetPropertyAction implements ActionExecutor { Object setPropertyValueMultiple = action.getParameterValues().get("setPropertyValueMultiple"); Object setPropertyValueBoolean = action.getParameterValues().get("setPropertyValueBoolean"); - Event updateProfileProperties = new Event("updateProfileProperties", event.getSession(), event.getProfile(), event.getScope(), null, event.getProfile(), new Date()); - updateProfileProperties.setPersistent(false); + Event updateProperties = new Event("updateProperties", event.getSession(), event.getProfile(), event.getScope(), null, event.getProfile(), new Date()); + updateProperties.setPersistent(false); if (propertyValue == null) { if (propertyValueInteger != null) { @@ -78,8 +78,8 @@ public class SetPropertyAction implements ActionExecutor { Map<String, Object> propertyToUpdate = new HashMap<>(); propertyToUpdate.put(propertyName, propertyValue); - updateProfileProperties.setProperty(UpdateProfilePropertiesAction.PROPS_TO_UPDATE, propertyToUpdate); - int changes = eventService.send(updateProfileProperties); + updateProperties.setProperty(UpdatePropertiesAction.PROPS_TO_UPDATE, propertyToUpdate); + int changes = eventService.send(updateProperties); if ((changes & EventService.PROFILE_UPDATED) == EventService.PROFILE_UPDATED) { return EventService.PROFILE_UPDATED; http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/fd1c9fba/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 deleted file mode 100644 index 4a68a3b..0000000 --- a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/UpdateProfilePropertiesAction.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * 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.commons.lang3.StringUtils; -import org.apache.unomi.api.Event; -import org.apache.unomi.api.Profile; -import org.apache.unomi.api.PropertyType; -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.api.services.ProfileService; -import org.apache.unomi.persistence.spi.PropertyHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class UpdateProfilePropertiesAction implements ActionExecutor { - - public static final String PROPS_TO_ADD = "propertiesToAdd"; - public static final String PROPS_TO_UPDATE = "propertiesToUpdate"; - public static final String PROPS_TO_DELETE = "propertiesToDelete"; - public static final String PROFILE_TARGET_ID_KEY = "targetProfileId"; - Logger logger = LoggerFactory.getLogger(UpdateProfilePropertiesAction.class.getName()); - - private ProfileService profileService; - private EventService eventService; - - public int execute(Action action, Event event) { - - Profile target = event.getProfile(); - - String targetProfileId = (String) event.getProperty(PROFILE_TARGET_ID_KEY); - if (StringUtils.isNotBlank(targetProfileId) && event.getProfile() != null && !targetProfileId.equals(event.getProfile().getItemId())) { - target = profileService.load(targetProfileId); - if (target == null) { - logger.warn("No profile found with Id : {}. Update skipped.", targetProfileId); - return EventService.NO_CHANGE; - } - } - - boolean isProfileUpdated = false; - - Map<String, Object> propsToAdd = (HashMap<String, Object>) event.getProperties().get(PROPS_TO_ADD); - if (propsToAdd != null) { - for (String prop : propsToAdd.keySet()) { - PropertyType propType = null; - if (prop.startsWith("properties.")) { - propType = profileService.getPropertyType(prop.substring("properties.".length())); - } - if (propType != null) { - isProfileUpdated |= PropertyHelper.setProperty(target, prop, PropertyHelper.getValueByTypeId(propsToAdd.get(prop), propType.getValueTypeId()), "setIfMissing"); - } else { - isProfileUpdated |= PropertyHelper.setProperty(target, prop, propsToAdd.get(prop), "setIfMissing"); - } - } - } - - Map<String, Object> propsToUpdate = (HashMap<String, Object>) event.getProperties().get(PROPS_TO_UPDATE); - if (propsToUpdate != null) { - for (String prop : propsToUpdate.keySet()) { - PropertyType propType = null; - if (prop.startsWith("properties.")) { - propType = profileService.getPropertyType(prop.substring("properties.".length())); - } - if (propType != null) { - isProfileUpdated |= PropertyHelper.setProperty(target, prop, PropertyHelper.getValueByTypeId(propsToUpdate.get(prop), propType.getValueTypeId()), "alwaysSet"); - } else { - isProfileUpdated |= PropertyHelper.setProperty(target, prop, propsToUpdate.get(prop), "alwaysSet"); - } - } - } - - List<String> propsToDelete = (List<String>) event.getProperties().get(PROPS_TO_DELETE); - if (propsToDelete != null) { - for (String prop : propsToDelete) { - isProfileUpdated |= PropertyHelper.setProperty(target, prop, null, "remove"); - } - } - - 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); - int changes = eventService.send(profileUpdated); - if ((changes & EventService.PROFILE_UPDATED) == EventService.PROFILE_UPDATED) { - profileService.save(target); - } - return EventService.NO_CHANGE; - - } - - return isProfileUpdated ? EventService.PROFILE_UPDATED : EventService.NO_CHANGE; - - } - - public void setProfileService(ProfileService profileService) { - this.profileService = profileService; - } - - public void setEventService(EventService eventService) { - this.eventService = eventService; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/fd1c9fba/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/UpdatePropertiesAction.java ---------------------------------------------------------------------- diff --git a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/UpdatePropertiesAction.java b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/UpdatePropertiesAction.java new file mode 100644 index 0000000..4c7e796 --- /dev/null +++ b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/UpdatePropertiesAction.java @@ -0,0 +1,155 @@ +/* + * 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.commons.lang3.StringUtils; +import org.apache.unomi.api.Event; +import org.apache.unomi.api.Persona; +import org.apache.unomi.api.Profile; +import org.apache.unomi.api.PropertyType; +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.api.services.ProfileService; +import org.apache.unomi.persistence.spi.PropertyHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class UpdatePropertiesAction implements ActionExecutor { + + public static final String PROPS_TO_ADD = "propertiesToAdd"; + public static final String PROPS_TO_UPDATE = "propertiesToUpdate"; + public static final String PROPS_TO_DELETE = "propertiesToDelete"; + + public static final String SYS_PROPS_TO_ADD = "systemPropertiesToAdd"; + public static final String SYS_PROPS_TO_UPDATE = "systemPropertiesToUpdate"; + public static final String SYS_PROPS_TO_DELETE = "systemPropertiesToDelete"; + + public static final String TARGET_ID_KEY = "targetId"; + public static final String TARGET_TYPE_KEY = "targetType"; + + public static final String TARGET_TYPE_PROFILE = "profile"; + + Logger logger = LoggerFactory.getLogger(UpdatePropertiesAction.class.getName()); + + private ProfileService profileService; + private EventService eventService; + + public int execute(Action action, Event event) { + + Profile target = event.getProfile(); + + String targetId = (String) event.getProperty(TARGET_ID_KEY); + String targetType = (String) event.getProperty(TARGET_TYPE_KEY); + + if (StringUtils.isNotBlank(targetId) && event.getProfile() != null && !targetId.equals(event.getProfile().getItemId())) { + target = TARGET_TYPE_PROFILE.equals(targetType)?profileService.load(targetId) : profileService.loadPersona(targetId); + if (target == null) { + logger.warn("No profile found with Id : {}. Update skipped.", targetId); + return EventService.NO_CHANGE; + } + } + + boolean isProfileOrPersonaUpdated = false; + + Map<String, Object> propsToAdd = (HashMap<String, Object>) event.getProperties().get(PROPS_TO_ADD); + HashMap<String, Object> sysPropsToAdd = (HashMap<String, Object>) event.getProperties().get(SYS_PROPS_TO_ADD); + + if (propsToAdd != null) { + if(sysPropsToAdd != null) { + propsToAdd.putAll(sysPropsToAdd); + } + for (String prop : propsToAdd.keySet()) { + PropertyType propType = null; + if (prop.startsWith("properties.")) { + propType = profileService.getPropertyType(prop.substring("properties.".length())); + } + if (propType != null) { + isProfileOrPersonaUpdated |= PropertyHelper.setProperty(target, prop, PropertyHelper.getValueByTypeId(propsToAdd.get(prop), propType.getValueTypeId()), "setIfMissing"); + } else { + isProfileOrPersonaUpdated |= PropertyHelper.setProperty(target, prop, propsToAdd.get(prop), "setIfMissing"); + } + } + } + + Map<String, Object> propsToUpdate = (HashMap<String, Object>) event.getProperties().get(PROPS_TO_UPDATE); + Map<String, Object> sysPropsToUpdate = (HashMap<String, Object>) event.getProperties().get(SYS_PROPS_TO_UPDATE); + if (propsToUpdate != null) { + if (sysPropsToUpdate != null) { + propsToUpdate.putAll(sysPropsToUpdate); + } + for (String prop : propsToUpdate.keySet()) { + PropertyType propType = null; + if (prop.startsWith("properties.")) { + propType = profileService.getPropertyType(prop.substring("properties.".length())); + } + if (propType != null) { + isProfileOrPersonaUpdated |= PropertyHelper.setProperty(target, prop, PropertyHelper.getValueByTypeId(propsToUpdate.get(prop), propType.getValueTypeId()), "alwaysSet"); + } else { + isProfileOrPersonaUpdated |= PropertyHelper.setProperty(target, prop, propsToUpdate.get(prop), "alwaysSet"); + } + } + } + + List<String> propsToDelete = (List<String>) event.getProperties().get(PROPS_TO_DELETE); + List<String> sysPropsToDelete = (List<String>) event.getProperties().get(SYS_PROPS_TO_DELETE); + if (propsToDelete != null) { + if (sysPropsToDelete != null) { + propsToDelete.addAll(sysPropsToDelete); + } + for (String prop : propsToDelete) { + isProfileOrPersonaUpdated |= PropertyHelper.setProperty(target, prop, null, "remove"); + } + } + + if (StringUtils.isNotBlank(targetId) && isProfileOrPersonaUpdated && + event.getProfile() != null && !targetId.equals(event.getProfile().getItemId())) { + if(TARGET_TYPE_PROFILE.equals(event.getProfile().getItemType())) { + profileService.save(target); + Event profileUpdated = new Event("profileUpdated", null, target, null, null, target, new Date()); + profileUpdated.setPersistent(false); + int changes = eventService.send(profileUpdated); + if ((changes & EventService.PROFILE_UPDATED) == EventService.PROFILE_UPDATED) { + profileService.save(target); + } + } else { + profileService.savePersona((Persona) target); + } + + return EventService.NO_CHANGE; + + } + + return isProfileOrPersonaUpdated ? EventService.PROFILE_UPDATED : EventService.NO_CHANGE; + + } + + public void setProfileService(ProfileService profileService) { + this.profileService = profileService; + } + + public void setEventService(EventService eventService) { + this.eventService = eventService; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/fd1c9fba/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 deleted file mode 100644 index bd076ab..0000000 --- a/plugins/baseplugin/src/main/resources/META-INF/cxs/actions/updateProfilePropertiesAction.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "metadata": { - "id": "updateProfilePropertiesAction", - "name": "updateProfilePropertiesAction", - "description": "Update multiple profile properties", - "systemTags": [ - "profileTags", - "demographic" - ], - "readOnly": true - }, - "actionExecutor": "updateProfileProperties", - "parameters": [ - - ] -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/fd1c9fba/plugins/baseplugin/src/main/resources/META-INF/cxs/actions/updatePropertiesAction.json ---------------------------------------------------------------------- diff --git a/plugins/baseplugin/src/main/resources/META-INF/cxs/actions/updatePropertiesAction.json b/plugins/baseplugin/src/main/resources/META-INF/cxs/actions/updatePropertiesAction.json new file mode 100644 index 0000000..ab8f36f --- /dev/null +++ b/plugins/baseplugin/src/main/resources/META-INF/cxs/actions/updatePropertiesAction.json @@ -0,0 +1,16 @@ +{ + "metadata": { + "id": "updatePropertiesAction", + "name": "updatePropertiesAction", + "description": "Update multiple properties in profile/persona", + "systemTags": [ + "profileTags", + "demographic" + ], + "readOnly": true + }, + "actionExecutor": "updateProperties", + "parameters": [ + + ] +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/fd1c9fba/plugins/baseplugin/src/main/resources/META-INF/cxs/conditions/updateProfilePropertiesEventCondition.json ---------------------------------------------------------------------- diff --git a/plugins/baseplugin/src/main/resources/META-INF/cxs/conditions/updateProfilePropertiesEventCondition.json b/plugins/baseplugin/src/main/resources/META-INF/cxs/conditions/updateProfilePropertiesEventCondition.json index 14204a9..611e622 100644 --- a/plugins/baseplugin/src/main/resources/META-INF/cxs/conditions/updateProfilePropertiesEventCondition.json +++ b/plugins/baseplugin/src/main/resources/META-INF/cxs/conditions/updateProfilePropertiesEventCondition.json @@ -1,7 +1,7 @@ { "metadata": { - "id": "updateProfilePropertiesEventCondition", - "name": "updateProfilePropertiesEventCondition", + "id": "updatePropertiesEventCondition", + "name": "updatePropertiesEventCondition", "description": "", "systemTags": [ "profileTags", @@ -14,7 +14,7 @@ "parentCondition": { "type": "eventTypeCondition", "parameterValues": { - "eventTypeId": "updateProfileProperties" + "eventTypeId": "updateProperties" } }, http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/fd1c9fba/plugins/baseplugin/src/main/resources/META-INF/cxs/rules/updateProfileProperties.json ---------------------------------------------------------------------- diff --git a/plugins/baseplugin/src/main/resources/META-INF/cxs/rules/updateProfileProperties.json b/plugins/baseplugin/src/main/resources/META-INF/cxs/rules/updateProfileProperties.json deleted file mode 100644 index 4b5cd3e..0000000 --- a/plugins/baseplugin/src/main/resources/META-INF/cxs/rules/updateProfileProperties.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "metadata" : { - "id": "updateProfileProperties", - "name": "Update profile properties", - "description" : "Update profile properties", - "readOnly":true - }, - - "condition" : { - "type": "updateProfilePropertiesEventCondition", - "parameterValues": { - } - }, - - "actions" : [ - { - "type": "updateProfilePropertiesAction", - "parameterValues": { - } - } - ] - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/fd1c9fba/plugins/baseplugin/src/main/resources/META-INF/cxs/rules/updateProperties.json ---------------------------------------------------------------------- diff --git a/plugins/baseplugin/src/main/resources/META-INF/cxs/rules/updateProperties.json b/plugins/baseplugin/src/main/resources/META-INF/cxs/rules/updateProperties.json new file mode 100644 index 0000000..65a5435 --- /dev/null +++ b/plugins/baseplugin/src/main/resources/META-INF/cxs/rules/updateProperties.json @@ -0,0 +1,23 @@ +{ + "metadata" : { + "id": "updateProperties", + "name": "Update profile/persona properties", + "description" : "Update profile/persona properties", + "readOnly":true + }, + + "condition" : { + "type": "updatePropertiesEventCondition", + "parameterValues": { + } + }, + + "actions" : [ + { + "type": "updatePropertiesAction", + "parameterValues": { + } + } + ] + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/fd1c9fba/plugins/baseplugin/src/main/resources/OSGI-INF/blueprint/blueprint.xml ---------------------------------------------------------------------- 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 bbd8bc1..ac245ac 100644 --- a/plugins/baseplugin/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/plugins/baseplugin/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -172,9 +172,9 @@ <service auto-export="interfaces"> <service-properties> - <entry key="actionExecutorId" value="updateProfileProperties"/> + <entry key="actionExecutorId" value="updateProperties"/> </service-properties> - <bean class="org.apache.unomi.plugins.baseplugin.actions.UpdateProfilePropertiesAction"> + <bean class="org.apache.unomi.plugins.baseplugin.actions.UpdatePropertiesAction"> <property name="profileService" ref="profileService"/> <property name="eventService" ref="eventService"/> </bean>
