From 478bd5bcd17ea4f67dba4e37bd751d9f7710e5a7 Mon Sep 17 00:00:00 2001 From: Helmi Akermi <70575401+hakermi@users.noreply.github.com> Date: Thu, 16 Jan 2025 15:23:47 +0100 Subject: [PATCH] feat: Index dropdown property choose option with its related translations - MEED-8008 - Meeds-io/MIPs#171 (#4345) Index dropdown property choose option with its related translations --- .../ProfileIndexingServiceConnector.java | 67 ++++++++++++++---- .../ProfileIndexingServiceConnectorTest.java | 69 ++++++++++++++++++- 2 files changed, 119 insertions(+), 17 deletions(-) diff --git a/component/core/src/main/java/org/exoplatform/social/core/jpa/search/ProfileIndexingServiceConnector.java b/component/core/src/main/java/org/exoplatform/social/core/jpa/search/ProfileIndexingServiceConnector.java index 7f825b44896..2bbf526bf46 100644 --- a/component/core/src/main/java/org/exoplatform/social/core/jpa/search/ProfileIndexingServiceConnector.java +++ b/component/core/src/main/java/org/exoplatform/social/core/jpa/search/ProfileIndexingServiceConnector.java @@ -25,6 +25,8 @@ import java.util.Set; import java.util.stream.Collectors; +import io.meeds.social.translation.model.TranslationField; +import io.meeds.social.translation.service.TranslationService; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -40,6 +42,7 @@ import org.exoplatform.social.core.jpa.storage.dao.IdentityDAO; import org.exoplatform.social.core.manager.IdentityManager; import org.exoplatform.social.core.profileproperty.ProfilePropertyService; +import org.exoplatform.social.core.profileproperty.model.ProfilePropertyOption; import org.exoplatform.social.core.profileproperty.model.ProfilePropertySetting; import org.exoplatform.social.core.relationship.model.Relationship; @@ -61,18 +64,26 @@ public class ProfileIndexingServiceConnector extends ElasticIndexingServiceConne private final ProfilePropertyService profilePropertyService; - private static final String HIDDEN_VALUE = "hidden"; + private final TranslationService translationService; + + private static final String HIDDEN_VALUE = "hidden"; + + private static final String PROFILE_PROPERTY_FIELD_NAME = "optionValue"; + + private static final String PROFILE_PROPERTY_OBJECT_TYPE = "propertySettingOption"; public ProfileIndexingServiceConnector(InitParams initParams, IdentityManager identityManager, IdentityDAO identityDAO, ConnectionDAO connectionDAO, - ProfilePropertyService profilePropertyService) { + ProfilePropertyService profilePropertyService, + TranslationService translationService) { super(initParams); this.identityManager = identityManager; this.identityDAO = identityDAO; this.connectionDAO = connectionDAO; this.profilePropertyService = profilePropertyService; + this.translationService = translationService; } @Override @@ -256,21 +267,25 @@ private Document getDocument(String id) { for (ProfilePropertySetting profilePropertySetting : profilePropertyService.getPropertySettings()) { if (profilePropertySetting.isVisible() && !fields.containsKey(profilePropertySetting.getPropertyName())) { - // Avoid indexing invisible and not editable properties - if (profile.getProperty(profilePropertySetting.getPropertyName()) != null && profile.getProperty(profilePropertySetting.getPropertyName()) instanceof String value) { - if (StringUtils.isNotEmpty(value)) { - // Avoid having dots in field names in ES, otherwise properties with String values may be converted in Objects in some cases - addPropertyToDocumentFields(fields, profilePropertySetting.getPropertyName(), value, Long.parseLong(id)); - } - } else { - List> multiValues = (List>) profile.getProperty(profilePropertySetting.getPropertyName()); + Object propertyValue = profile.getProperty(profilePropertySetting.getPropertyName()); + if (propertyValue instanceof String value && StringUtils.isNotEmpty(value)) { + addPropertyToDocumentFields(fields, + profilePropertySetting.getPropertyName(), + parseValue(profilePropertySetting, value), + Long.parseLong(id)); + } else if (propertyValue instanceof List) { + List> multiValues = (List>) propertyValue; if (CollectionUtils.isNotEmpty(multiValues)) { String value = multiValues.stream() - .filter(property -> property.get("value") != null) - .map(property -> property.get("value")) - .collect(Collectors.joining(",", "", "")); + .filter(property -> property.get("value") != null) + .map(property -> parseValue(profilePropertySetting, property.get("value"))) + .collect(Collectors.joining(",", "", "")); + if (StringUtils.isNotEmpty(value)) { - addPropertyToDocumentFields(fields, profilePropertySetting.getPropertyName(), removeAccents(value), Long.parseLong(id)); + addPropertyToDocumentFields(fields, + profilePropertySetting.getPropertyName(), + removeAccents(value), + Long.parseLong(id)); } } } @@ -315,4 +330,28 @@ private static String removeAccents(String string) { } return string; } + + private String parseValue(ProfilePropertySetting profilePropertySetting, String value) { + if (!profilePropertySetting.isDropdownList()) { + return value; + } + String optionValue = profilePropertySetting.getPropertyOptions() + .stream() + .filter(option -> option.getId() == Long.parseLong(value)) + .findFirst() + .map(ProfilePropertyOption::getValue) + .orElse(value); + try { + TranslationField translationField = translationService.getTranslationField(PROFILE_PROPERTY_OBJECT_TYPE, + Long.parseLong(value), + PROFILE_PROPERTY_FIELD_NAME); + if (translationField != null && !translationField.getLabels().isEmpty()) { + String translations = String.join("-", translationField.getLabels().values()); + return String.join("-", optionValue, translations); + } + } catch (Exception e) { + LOG.error("Error parsing profile property value translations", e); + } + return optionValue; + } } diff --git a/component/core/src/test/java/org/exoplatform/social/core/jpa/search/ProfileIndexingServiceConnectorTest.java b/component/core/src/test/java/org/exoplatform/social/core/jpa/search/ProfileIndexingServiceConnectorTest.java index 90a8777608c..ff1a123a321 100644 --- a/component/core/src/test/java/org/exoplatform/social/core/jpa/search/ProfileIndexingServiceConnectorTest.java +++ b/component/core/src/test/java/org/exoplatform/social/core/jpa/search/ProfileIndexingServiceConnectorTest.java @@ -1,5 +1,8 @@ package org.exoplatform.social.core.jpa.search; +import io.meeds.social.translation.service.TranslationService; +import org.exoplatform.social.core.profileproperty.model.ProfilePropertyOption; +import org.exoplatform.social.core.profileproperty.model.ProfilePropertySetting; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -21,6 +24,12 @@ import io.meeds.social.core.profileproperty.storage.CachedProfileSettingStorage; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.stream.Stream; + @RunWith(MockitoJUnitRunner.class) public class ProfileIndexingServiceConnectorTest extends AbstractCoreTest { @@ -37,6 +46,8 @@ public class ProfileIndexingServiceConnectorTest extends AbstractCoreTest { private ProfilePropertyService profilePropertyService; private ProfileIndexingServiceConnector profileIndexingServiceConnector; + + private TranslationService translationService; @Override @Before @@ -44,6 +55,7 @@ public void setUp() throws Exception { super.setUp(); identityManager = getService(IdentityManagerImpl.class); profilePropertyService = getService(ProfilePropertyService.class); + translationService = getService(TranslationService.class); getService(CachedProfileSettingStorage.class).clearCaches(); InitParams initParams = new InitParams(); @@ -55,21 +67,72 @@ public void setUp() throws Exception { userIdentity = identityManager.getOrCreateIdentity(OrganizationIdentityProvider.NAME, "root"); Profile profile = userIdentity.getProfile(); profile.setProperty("profession", "Developer"); + identityManager.updateProfile(profile, true); this.profileIndexingServiceConnector = new ProfileIndexingServiceConnector(initParams, identityManager, identityDAO, connectionDAO, - profilePropertyService); + profilePropertyService, + translationService); } @Test public void testUpdate() { Document document = profileIndexingServiceConnector.update(userIdentity.getId()); - assertEquals("Developer",document.getFields().get("profession")); + assertEquals("Developer", document.getFields().get("profession")); profilePropertyService.hidePropertySetting(Long.parseLong(userIdentity.getId()), profilePropertyService.getProfileSettingByName("profession").getId()); document = profileIndexingServiceConnector.update(userIdentity.getId()); - assertEquals("hidden",document.getFields().get("profession")); + assertEquals("hidden", document.getFields().get("profession")); + } + + @Test + public void testIndexDropdownPropertyOptionValue() throws Exception { + ProfilePropertySetting dropdownListPropertySetting = createProfileSettingDropdownInstance("propDropdownTest"); + ProfilePropertySetting dropdownPropertySetting = profilePropertyService.createPropertySetting(dropdownListPropertySetting); + + Map labels = new HashMap<>(); + labels.put(Locale.US, "option en"); + labels.put(Locale.FRANCE, "option fr"); + + + userIdentity = identityManager.getOrCreateIdentity(OrganizationIdentityProvider.NAME, "root"); + Profile profile = userIdentity.getProfile(); + profile.setProperty("propDropdownTest", dropdownPropertySetting.getPropertyOptions().getFirst().getId()); + identityManager.updateProfile(profile, true); + + Document document = profileIndexingServiceConnector.update(userIdentity.getId()); + assertEquals("option", document.getFields().get("propDropdownTest")); + + translationService.saveTranslationLabels("propertySettingOption", + dropdownPropertySetting.getPropertyOptions().getFirst().getId(), + "optionValue", + labels); + + document = profileIndexingServiceConnector.update(userIdentity.getId()); + assertEquals("option-option fr-option en", document.getFields().get("propDropdownTest")); + } + + + private ProfilePropertySetting createProfileSettingDropdownInstance(String propertyName) { + ProfilePropertySetting profilePropertySetting = new ProfilePropertySetting(); + profilePropertySetting.setActive(true); + profilePropertySetting.setEditable(true); + profilePropertySetting.setVisible(true); + profilePropertySetting.setPropertyName(propertyName); + profilePropertySetting.setGroupSynchronized(false); + profilePropertySetting.setMultiValued(false); + profilePropertySetting.setPropertyType("text"); + profilePropertySetting.setParentId(0L); + profilePropertySetting.setOrder(0L); + profilePropertySetting.setDropdownList(true); + + List profilePropertyOptions = Stream.generate(ProfilePropertyOption::new) + .limit(2) + .peek(option -> option.setValue("option")) + .toList(); + profilePropertySetting.setPropertyOptions(profilePropertyOptions); + return profilePropertySetting; } }