UNOMI-413-mergeProfileOnPropertyAction: support take Events profile as master, fix persistence bug when new profile is merged and it was the event profile (#232)
diff --git a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/MergeProfilesOnPropertyAction.java b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/MergeProfilesOnPropertyAction.java
index ffdf626..b01990b 100644
--- a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/MergeProfilesOnPropertyAction.java
+++ b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/MergeProfilesOnPropertyAction.java
@@ -132,11 +132,18 @@
return StringUtils.isEmpty(mergeProfilePreviousPropertyValue) ? EventService.PROFILE_UPDATED : EventService.NO_CHANGE;
}
- // Use oldest profile for master profile
- final Profile masterProfile = profileService.mergeProfiles(profiles.get(0), profiles);
+ Profile markedMasterProfile;
+ boolean forceEventProfileAsMaster = action.getParameterValues().containsKey("forceEventProfileAsMaster") ?
+ (boolean) action.getParameterValues().get("forceEventProfileAsMaster") : false;
+ if (forceEventProfileAsMaster)
+ markedMasterProfile = event.getProfile();
+ else
+ markedMasterProfile = profiles.get(0);// Use oldest profile for master profile
+
+ final Profile masterProfile = profileService.mergeProfiles(markedMasterProfile, profiles);
// Profile has changed
- if (!masterProfile.getItemId().equals(profileId)) {
+ if (forceEventProfileAsMaster || !masterProfile.getItemId().equals(profileId)) {
HttpServletResponse httpServletResponse = (HttpServletResponse) event.getAttributes().get(Event.HTTP_RESPONSE_ATTRIBUTE);
// we still send back the current profile cookie. It will be changed on the next request to the ContextServlet.
// The current profile will be deleted only then because we cannot delete it right now (too soon)
@@ -192,7 +199,14 @@
sourceMap.put("mergedWith", masterProfileId);
profile.setSystemProperty("lastUpdated", new Date());
sourceMap.put("systemProperties", profile.getSystemProperties());
- persistenceService.update(profile, null, Profile.class, sourceMap);
+
+ boolean isExist = persistenceService.load(profile.getItemId(), Profile.class) != null;
+
+ if (isExist == false) //save the original event profile is it has been changed
+ persistenceService.save(profile);
+ else
+ persistenceService.update(profile, null, Profile.class, sourceMap,true);
+
}
}
} catch (Exception e) {
diff --git a/plugins/baseplugin/src/main/resources/META-INF/cxs/actions/mergeProfilesOnPropertyAction.json b/plugins/baseplugin/src/main/resources/META-INF/cxs/actions/mergeProfilesOnPropertyAction.json
index 6082f9f..07bf38c 100644
--- a/plugins/baseplugin/src/main/resources/META-INF/cxs/actions/mergeProfilesOnPropertyAction.json
+++ b/plugins/baseplugin/src/main/resources/META-INF/cxs/actions/mergeProfilesOnPropertyAction.json
@@ -15,6 +15,11 @@
"id": "mergeProfilePropertyName",
"type": "string",
"multivalued": false
+ },
+ {
+ "id": "forceEventProfileAsMaster",
+ "type": "boolean",
+ "multivalued": false
}
]
}
\ No newline at end of file