Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*******************************************************************************
*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*
******************************************************************************/
package com.adobe.cq.commerce.core.components.internal.models.v1.productcollection;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Nonnull;
import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
import org.apache.sling.models.annotations.injectorspecific.Self;

import com.adobe.cq.commerce.core.components.models.common.ProductListItem;
import com.adobe.cq.commerce.core.components.models.productcollection.ProductCollection;
import com.adobe.cq.commerce.core.components.services.UrlProvider;
import com.adobe.cq.commerce.core.components.utils.SiteNavigation;
import com.adobe.cq.commerce.core.search.internal.models.SearchOptionsImpl;
import com.adobe.cq.commerce.core.search.internal.models.SearchResultsSetImpl;
import com.adobe.cq.commerce.core.search.models.SearchResultsSet;
import com.adobe.cq.commerce.core.search.services.SearchResultsService;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.designer.Style;

import static com.adobe.cq.commerce.core.search.internal.models.SearchOptionsImpl.PAGE_SIZE_DEFAULT;

@Model(
adaptables = SlingHttpServletRequest.class,
adapters = ProductCollection.class,
resourceType = ProductCollectionImpl.RESOURCE_TYPE)
public class ProductCollectionImpl implements ProductCollection {
protected static final String RESOURCE_TYPE = "core/cif/components/commerce/productcollection/v1/productcollection";
protected static final boolean LOAD_CLIENT_PRICE_DEFAULT = true;
protected Page productPage;
protected boolean loadClientPrice;
protected int navPageSize;
@Self
protected SlingHttpServletRequest request;
@ScriptVariable
protected ValueMap properties;
@ScriptVariable
protected Style currentStyle;
@Inject
protected Resource resource;
@Inject
protected Page currentPage;
@Inject
protected SearchResultsService searchResultsService;
@Inject
protected UrlProvider urlProvider;
protected SearchOptionsImpl searchOptions;
protected SearchResultsSet searchResultsSet;

@PostConstruct
private void baseInitModel() {
navPageSize = properties.get(PN_PAGE_SIZE, currentStyle.get(PN_PAGE_SIZE, PAGE_SIZE_DEFAULT));
loadClientPrice = properties.get(PN_LOAD_CLIENT_PRICE, currentStyle.get(PN_LOAD_CLIENT_PRICE, LOAD_CLIENT_PRICE_DEFAULT));
// get product template page
productPage = SiteNavigation.getProductPage(currentPage);
if (productPage == null) {
productPage = currentPage;
}
}

public Integer calculateCurrentPageCursor(final String currentPageIndexCandidate) {
// make sure the current page from the query string is reasonable i.e. numeric and over 0
try {
int i = Integer.parseInt(currentPageIndexCandidate);
if (i < 1) {
i = 1;
}
return i;
} catch (NumberFormatException x) {
return 1;
}
}

@Override
public boolean loadClientPrice() {
return loadClientPrice;
}

protected Map<String, String> createFilterMap(final Map<String, String[]> parameterMap) {
Map<String, String> filters = new HashMap<>();
parameterMap.forEach((code, value) -> {
// we'll make sure there is a value defined for the key
if (value.length != 1) {
return;
}

filters.put(code, value[0]);
});

return filters;
}

@Nonnull
@Override
public Collection<ProductListItem> getProducts() {
return getSearchResultsSet().getProductListItems();
}

@Nonnull
@Override
public SearchResultsSet getSearchResultsSet() {
if (searchResultsSet == null) {
searchResultsSet = new SearchResultsSetImpl();
}
return searchResultsSet;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,39 @@

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.cq.commerce.core.components.client.MagentoGraphqlClient;
import com.adobe.cq.commerce.core.components.internal.models.v1.productcollection.ProductCollectionImpl;
import com.adobe.cq.commerce.core.components.models.common.ProductListItem;
import com.adobe.cq.commerce.core.components.models.productlist.ProductList;
import com.adobe.cq.commerce.core.components.models.retriever.AbstractCategoryRetriever;
import com.adobe.cq.commerce.core.components.services.UrlProvider;
import com.adobe.cq.commerce.core.components.services.UrlProvider.CategoryIdentifierType;
import com.adobe.cq.commerce.core.components.utils.SiteNavigation;
import com.adobe.cq.commerce.core.search.internal.converters.ProductToProductListItemConverter;
import com.adobe.cq.commerce.core.search.internal.models.SearchOptionsImpl;
import com.adobe.cq.commerce.core.search.internal.models.SearchResultsSetImpl;
import com.adobe.cq.commerce.core.search.models.SearchResultsSet;
import com.adobe.cq.commerce.core.search.services.SearchResultsService;
import com.adobe.cq.commerce.magento.graphql.CategoryProducts;
import com.adobe.cq.commerce.magento.graphql.ProductInterfaceQuery;
import com.adobe.cq.sightly.SightlyWCMMode;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.designer.Style;

@Model(adaptables = SlingHttpServletRequest.class, adapters = ProductList.class, resourceType = ProductListImpl.RESOURCE_TYPE)
public class ProductListImpl implements ProductList {
public class ProductListImpl extends ProductCollectionImpl implements ProductList {

protected static final String RESOURCE_TYPE = "core/cif/components/commerce/productlist/v1/productlist";
protected static final String PLACEHOLDER_DATA = "productlist-component-placeholder-data.json";
Expand All @@ -65,63 +57,28 @@ public class ProductListImpl implements ProductList {

private static final boolean SHOW_TITLE_DEFAULT = true;
private static final boolean SHOW_IMAGE_DEFAULT = true;
private static final boolean LOAD_CLIENT_PRICE_DEFAULT = true;

private Page productPage;
private boolean showTitle;
private boolean showImage;
private boolean loadClientPrice;
private int navPageSize;

@Self
private SlingHttpServletRequest request;

@ScriptVariable
private ValueMap properties;

@ScriptVariable
private Style currentStyle;

@ScriptVariable(name = "wcmmode")
private SightlyWCMMode wcmMode;

@Inject
private Resource resource;

@Inject
private Page currentPage;

@Inject
private SearchResultsService searchResultsService;

@Inject
private UrlProvider urlProvider;
private SightlyWCMMode wcmMode = null;

private AbstractCategoryRetriever categoryRetriever;
private SearchOptionsImpl searchOptions;
private SearchResultsSet searchResultsSet;
private boolean usePlaceholderData;

@PostConstruct
private void initModel() {
// read properties
showTitle = properties.get(PN_SHOW_TITLE, currentStyle.get(PN_SHOW_TITLE, SHOW_TITLE_DEFAULT));
showImage = properties.get(PN_SHOW_IMAGE, currentStyle.get(PN_SHOW_IMAGE, SHOW_IMAGE_DEFAULT));
navPageSize = properties.get(PN_PAGE_SIZE, currentStyle.get(PN_PAGE_SIZE, SearchOptionsImpl.PAGE_SIZE_DEFAULT));
loadClientPrice = properties.get(PN_LOAD_CLIENT_PRICE, currentStyle.get(PN_LOAD_CLIENT_PRICE, LOAD_CLIENT_PRICE_DEFAULT));

String currentPageIndexCandidate = request.getParameter(SearchOptionsImpl.CURRENT_PAGE_PARAMETER_ID);
// make sure the current page from the query string is reasonable i.e. numeric and over 0
Integer currentPageIndex = calculateCurrentPageCursor(currentPageIndexCandidate);

Map<String, String> searchFilters = createFilterMap(request.getParameterMap());

// get product template page
productPage = SiteNavigation.getProductPage(currentPage);
if (productPage == null) {
productPage = currentPage;
}

MagentoGraphqlClient magentoGraphqlClient = MagentoGraphqlClient.create(resource);

// Parse category identifier from URL
Expand Down Expand Up @@ -183,11 +140,6 @@ public boolean showImage() {
return showImage;
}

@Override
public boolean loadClientPrice() {
return loadClientPrice;
}

@Nonnull
@Override
public Collection<ProductListItem> getProducts() {
Expand All @@ -196,30 +148,14 @@ public Collection<ProductListItem> getProducts() {
ProductToProductListItemConverter converter = new ProductToProductListItemConverter(productPage, request, urlProvider);
return categoryProducts.getItems().stream()
.map(converter)
.filter(p -> p != null) // the converter returns null if the conversion fails
.filter(Objects::nonNull) // the converter returns null if the conversion fails
.collect(Collectors.toList());
} else {
return getSearchResultsSet().getProductListItems();
}
}

protected Map<String, String> createFilterMap(final Map<String, String[]> parameterMap) {
Map<String, String> filters = new HashMap<>();
parameterMap.entrySet().forEach(filterCandidate -> {
String code = filterCandidate.getKey();
String[] value = filterCandidate.getValue();

// we'll make sure there is a value defined for the key
if (value.length != 1) {
return;
}

filters.put(code, value[0]);
});

return filters;
}

@Nonnull
@Override
public SearchResultsSet getSearchResultsSet() {
if (searchResultsSet == null) {
Expand All @@ -229,16 +165,8 @@ public SearchResultsSet getSearchResultsSet() {
return searchResultsSet;
}

protected Integer calculateCurrentPageCursor(final String currentPageIndexCandidate) {
// make sure the current page from the query string is reasonable i.e. numeric and over 0
return StringUtils.isNumeric(currentPageIndexCandidate) && Integer.valueOf(currentPageIndexCandidate) > 0
? Integer.parseInt(currentPageIndexCandidate)
: 1;
}

@Override
public AbstractCategoryRetriever getCategoryRetriever() {
return categoryRetriever;
}

}
Loading