Skip to content

Commit

Permalink
Issue 29709 make personalization work with keytag (#29715)
Browse files Browse the repository at this point in the history
### Proposed Changes

This change allows personas to be resolved by either Id or by keytag. It
is important because it makes the personas actually usable from the
front end without having to go into the backend to get a personas
identifier
  • Loading branch information
wezell authored Aug 23, 2024
1 parent 7c092e1 commit c628743
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,13 @@ private Persona find(final String identifier, final User user, final boolean res
final Contentlet contentlet = APILocator.getContentletAPI().findContentletByIdentifier(identifier, live,
APILocator.getLanguageAPI().getDefaultLanguage().getId(), user, respectFrontEndRoles);

return UtilMethods.isSet(contentlet) ? fromContentlet(contentlet) : null;

if(contentlet != null) {
return fromContentlet(contentlet);
}


return findPersonaByTag(identifier, user, respectFrontEndRoles).orElseGet(()->null);
}

@Override
Expand Down Expand Up @@ -502,11 +508,15 @@ public Structure getDefaultPersonaStructure() throws DotSecurityException, DotDa
public Optional<Persona> findPersonaByTag(final String personaTag, final User user, final boolean respectFrontEndRoles)
throws DotSecurityException, DotDataException {

if(UtilMethods.isEmpty(personaTag)){
return Optional.empty();
}

final StringBuilder query = new StringBuilder(" +baseType:").append(BaseContentType.PERSONA.getType())
.append(" +").append(PERSONA_KEY_TAG).append(":").append(personaTag);

final List<Contentlet> contentlets =
APILocator.getContentletAPI().search(query.toString(), -1, 0, StringPool.BLANK, user, respectFrontEndRoles);
APILocator.getContentletAPI().search(query.toString(), 1, 0, StringPool.BLANK, user, respectFrontEndRoles);
final Optional<Contentlet> persona = null != contentlets ? contentlets.stream().findFirst() : Optional.empty();
return persona.isPresent() ? Optional.ofNullable(fromContentlet(persona.get())) : Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
import com.dotcms.repackage.org.apache.struts.Globals;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.util.UtilMethods;
import java.util.List;

import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.awaitility.Awaitility;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -289,6 +292,45 @@ public void testgetPersonasIncludingDefaultPersona_filterNewPersonaContentType_S
}
}
}




/**
* Method to test: {@link PersonaAPI#find(String identifier,com.liferay.portal.model.User user, boolean respectFrontendRoles)}
* When: finding a persona
* Should: the persona should be resolved by id or by keytag
*/
@Test
public void test_personas_test_resolve_by_keytag() throws Exception {

ContentType customPersonaType = new ContentTypeDataGen()
.host(host)
.baseContentType(BaseContentType.PERSONA).nextPersisted();

final Contentlet newPersona = new ContentletDataGen(customPersonaType.id())
.host(host)
.setProperty("name", "AnotherPersona" + System.currentTimeMillis())
.setProperty("keyTag", "AnotherPersona" + System.currentTimeMillis())
.nextPersisted();

Contentlet personaById = personaAPI.find(newPersona.getIdentifier(), APILocator.systemUser(), true);


assertTrue(UtilMethods.isSet(()->personaById.getIdentifier()));


Awaitility.await().atMost(15, TimeUnit.SECONDS).pollInterval(2, TimeUnit.SECONDS).until(() -> {
try {
Contentlet personaByKeyTag = personaAPI.find(newPersona.getStringProperty("keyTag"), APILocator.systemUser(), true);
assert(personaById.getIdentifier().equals(personaByKeyTag.getIdentifier()));
return true;
} catch (Exception e) {
// ignore
}
return false;
});

APILocator.getContentletAPI().destroy(newPersona, APILocator.systemUser(), false);


}
}

0 comments on commit c628743

Please sign in to comment.