use of com.adobe.cq.wcm.core.components.models.ListItem in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class SearchResultServlet method getResults.
/**
* Gets the search results.
*
* @param request The search request.
* @param searchComponent The search component.
* @param pageManager A PageManager.
* @return List of search results.
*/
@NotNull
private List<ListItem> getResults(@NotNull final SlingHttpServletRequest request, @NotNull final Search searchComponent, @NotNull final PageManager pageManager) {
List<ListItem> results = new ArrayList<>();
String fulltext = request.getParameter(PARAM_FULLTEXT);
if (fulltext == null || fulltext.length() < searchComponent.getSearchTermMinimumLength()) {
return results;
}
long resultsOffset = Optional.ofNullable(request.getParameter(PARAM_RESULTS_OFFSET)).map(Long::parseLong).orElse(0L);
Map<String, String> predicatesMap = new HashMap<>();
predicatesMap.put(FulltextPredicateEvaluator.FULLTEXT, fulltext);
predicatesMap.put(PathPredicateEvaluator.PATH, searchComponent.getSearchRootPagePath());
predicatesMap.put(TypePredicateEvaluator.TYPE, NameConstants.NT_PAGE);
PredicateGroup predicates = PredicateConverter.createPredicates(predicatesMap);
ResourceResolver resourceResolver = request.getResource().getResourceResolver();
Query query = queryBuilder.createQuery(predicates, resourceResolver.adaptTo(Session.class));
if (searchComponent.getResultsSize() != 0) {
query.setHitsPerPage(searchComponent.getResultsSize());
}
if (resultsOffset != 0) {
query.setStart(resultsOffset);
}
SearchResult searchResult = query.getResult();
LinkHandler linkHandler = request.adaptTo(LinkHandler.class);
// Query builder has a leaking resource resolver, so the following work around is required.
ResourceResolver leakingResourceResolver = null;
try {
Iterator<Resource> resourceIterator = searchResult.getResources();
while (resourceIterator.hasNext()) {
Resource resource = resourceIterator.next();
// Get a reference to QB's leaking resource resolver
if (leakingResourceResolver == null) {
leakingResourceResolver = resource.getResourceResolver();
}
Optional.of(resource).map(res -> resourceResolver.getResource(res.getPath())).map(pageManager::getContainingPage).map(page -> new PageListItemImpl(linkHandler, page, searchComponent.getId(), null)).ifPresent(results::add);
}
} finally {
if (leakingResourceResolver != null) {
leakingResourceResolver.close();
}
}
return results;
}
use of com.adobe.cq.wcm.core.components.models.ListItem in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class CarouselImpl method getDataLayerShownItems.
/*
* DataLayerProvider implementation of field getters
*/
@Override
public String[] getDataLayerShownItems() {
String[] shownItems = new String[0];
List<ListItem> items = getItems();
if (!items.isEmpty()) {
ComponentData componentData = items.get(0).getData();
if (componentData != null) {
shownItems = new String[] { componentData.getId() };
}
}
return shownItems;
}
use of com.adobe.cq.wcm.core.components.models.ListItem in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class AbstractContainerImplTest method verifyContainerItems.
private void verifyContainerItems(Object[][] expectedItems, List<ListItem> items) {
assertEquals(expectedItems.length, items.size(), "The container has a different number of items than expected.");
int index = 0;
for (ListItem item : items) {
assertEquals(expectedItems[index][0], item.getDescription(), "The container item's description is not what was expected: " + item.getDescription());
assertEquals(expectedItems[index][1], item.getName(), "The container item's name is not what was expected: " + item.getName());
assertEquals(expectedItems[index][2], item.getPath(), "The container item's path is not what was expected: " + item.getPath());
assertEquals(expectedItems[index][3], item.getTitle(), "The container item's title is not what was expected: " + item.getTitle());
index++;
}
}
use of com.adobe.cq.wcm.core.components.models.ListItem in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class AbstractContainerImplTest method testEmptyContainer.
@Test
public void testEmptyContainer() {
Container container = new ContainerImpl();
List<ListItem> items = container.getItems();
assertEquals(0, items.size());
}
use of com.adobe.cq.wcm.core.components.models.ListItem in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class SearchResultServletTest method validateResponse.
@SuppressWarnings("deprecation")
private void validateResponse(MockSlingHttpServletResponse response, List<Map<String, String>> expected) throws IOException {
ObjectMapper mapper = new ObjectMapper();
SimpleAbstractTypeResolver resolver = new SimpleAbstractTypeResolver();
resolver.addMapping(ListItem.class, Item.class);
SimpleModule module = new SimpleModule();
module.setAbstractTypes(resolver);
mapper.registerModule(module);
ListItem[] listItems = mapper.readValue(response.getOutputAsString(), ListItem[].class);
List<Map<String, String>> actual = new LinkedList<>();
for (ListItem item : listItems) {
actual.add(ImmutableMap.of("url", item.getURL(), "title", item.getTitle(), "id", item.getId()));
}
assertEquals(expected, actual);
}
Aggregations