Skip to content

Commit

Permalink
feat: Index dropdown property choose option with its related translat…
Browse files Browse the repository at this point in the history
…ions - MEED-8008 - Meeds-io/MIPs#171 (#4345)

Index dropdown property choose option with its related translations
  • Loading branch information
hakermi committed Jan 16, 2025
1 parent b91f113 commit 7aad06b
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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<Map<String, String>> multiValues = (List<Map<String, String>>) 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<Map<String, String>> multiValues = (List<Map<String, String>>) 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));
}
}
}
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {

Expand All @@ -37,13 +46,16 @@ public class ProfileIndexingServiceConnectorTest extends AbstractCoreTest {
private ProfilePropertyService profilePropertyService;

private ProfileIndexingServiceConnector profileIndexingServiceConnector;

private TranslationService translationService;

@Override
@Before
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();
Expand All @@ -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<Locale, String> 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<ProfilePropertyOption> profilePropertyOptions = Stream.generate(ProfilePropertyOption::new)
.limit(2)
.peek(option -> option.setValue("option"))
.toList();
profilePropertySetting.setPropertyOptions(profilePropertyOptions);
return profilePropertySetting;
}
}

0 comments on commit 7aad06b

Please sign in to comment.