Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SITES-22965: Add function to set category filter type #1011

Merged
merged 27 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
56a2268
SITES-21947: Add function to set category filter type
alwinjoseph02 Jun 19, 2024
3d53247
SITES-21947: Resolves the space issues & comment
alwinjoseph02 Jun 19, 2024
e9e084c
SITES-21947: Resolves the space issues & comment
alwinjoseph02 Jun 19, 2024
2bb1584
SITES-21947: Updates the comment
alwinjoseph02 Jun 19, 2024
8571af9
SITES-21947: Removes the spaces
alwinjoseph02 Jun 19, 2024
8cb6da3
SITES-21947: Adds proper formatting
alwinjoseph02 Jun 20, 2024
ae09b83
SITES-22965: Adds teaser,button & category carousel compoment compact…
alwinjoseph02 Jun 27, 2024
e68bac5
SITES-22965: Uses the void as return
alwinjoseph02 Jun 27, 2024
cbb153e
SITES-22965: Adds code coverage
alwinjoseph02 Jun 28, 2024
b0ef429
SITES-22965: resolves the code format issues
alwinjoseph02 Jun 28, 2024
2628e6c
SITES-22965: Adds validate method on url formaters
alwinjoseph02 Jul 2, 2024
9b43f44
SITES-22965: Optimize the code & adds new unit test
alwinjoseph02 Jul 2, 2024
2b8105c
SITES-22965: Adds test coverages
alwinjoseph02 Jul 2, 2024
eabbe62
SITES-22965: Adds test coverages for urlproviderimpl
alwinjoseph02 Jul 2, 2024
a2e01c5
Merge branch 'master' into feature/SITES-21947
alwinjoseph02 Jul 2, 2024
8f98dd3
SITES-22965: Adds test coverage
alwinjoseph02 Jul 3, 2024
2cbebf4
SITES-22965: Formats the code
alwinjoseph02 Jul 3, 2024
088b6a9
SITES-22965: Adds new method to generate the category url format
alwinjoseph02 Jul 4, 2024
673f47d
SITES-22965: Resolves unwanted imports
alwinjoseph02 Jul 4, 2024
5647675
SITES-22965: Reverts the unwanted changes
alwinjoseph02 Jul 4, 2024
5c3043a
SITES-22965: Adds new identifier
alwinjoseph02 Jul 4, 2024
8a7fd08
SITES-22965: Adds the PR comments changes
alwinjoseph02 Jul 10, 2024
9a1705b
SITES-22965: Updates the readme & comments
alwinjoseph02 Jul 10, 2024
5d500bb
SITES-22965: Adds more test coverage
alwinjoseph02 Jul 10, 2024
4ce0d82
SITES-22965: Makes tocategoryurl to deprecated and reomve its usages
alwinjoseph02 Jul 11, 2024
62bed42
Revert "SITES-22965: Makes tocategoryurl to deprecated and reomve its…
alwinjoseph02 Jul 12, 2024
7eac3d3
SITES-22965: Reverted deprecation changes and changes the method name
alwinjoseph02 Jul 12, 2024
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
Expand Up @@ -47,6 +47,7 @@
import com.adobe.cq.commerce.core.components.models.categorylist.FeaturedCategoryListItem;
import com.adobe.cq.commerce.core.components.models.common.CommerceIdentifier;
import com.adobe.cq.commerce.core.components.models.retriever.AbstractCategoriesRetriever;
import com.adobe.cq.commerce.core.components.models.retriever.AbstractCategoryRetriever;
import com.adobe.cq.commerce.core.components.services.urls.CategoryUrlFormat;
import com.adobe.cq.commerce.core.components.services.urls.UrlProvider;
import com.adobe.cq.commerce.magento.graphql.CategoryTree;
Expand Down Expand Up @@ -106,6 +107,7 @@ private void initModel() {
// After the identifier type has been determined, the specific list will be used further
List<String> categoryIdentifiers = new ArrayList<>();
assetOverride = new HashMap<>();
String categoryIdType = null;

// Iterate entries of composite multifield
Resource items = resource.getChild(ITEMS_PROP);
Expand All @@ -117,6 +119,8 @@ private void initModel() {
ValueMap props = item.getValueMap();

String categoryIdentifier = props.get(CATEGORY_IDENTIFIER, String.class);
categoryIdType = props.get(CATEGORY_IDENTIFIER_TYPE, String.class);

if (StringUtils.isEmpty(categoryIdentifier)) {
continue;
}
Expand All @@ -141,6 +145,9 @@ private void initModel() {
categoriesRetriever = new CategoriesRetriever(magentoGraphqlClient);
// Setting the identifiers list based on the determined identifier type
categoriesRetriever.setIdentifiers(categoryIdentifiers);
if (AbstractCategoryRetriever.CATEGORY_IDENTIFIER_URL_PATH.equals(categoryIdType)) {
categoriesRetriever.setCategoryIdType(categoryIdType);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.apache.sling.models.factory.ModelFactory;

import com.adobe.cq.commerce.core.components.client.MagentoGraphqlClient;
import com.adobe.cq.commerce.core.components.models.retriever.AbstractCategoryRetriever;
import com.adobe.cq.commerce.core.components.services.urls.CategoryUrlFormat;
import com.adobe.cq.commerce.core.components.services.urls.UrlProvider;
import com.adobe.cq.wcm.core.components.commons.link.Link;
import com.adobe.cq.wcm.core.components.models.Button;
Expand Down Expand Up @@ -71,6 +73,8 @@ public class ButtonImpl implements Button {
private String productIdentifier;
@ValueMapValue(name = "categoryId", injectionStrategy = InjectionStrategy.OPTIONAL)
private String categoryIdentifier;
@ValueMapValue(name = "categoryIdType", injectionStrategy = InjectionStrategy.OPTIONAL)
private String categoryIdType;
@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
private String externalLink;
@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
Expand Down Expand Up @@ -100,7 +104,13 @@ private void initModel() {
} else if (PRODUCT.equals(linkType) && productIdentifier != null) {
link = urlProvider.toProductUrl(request, currentPage, productIdentifier);
} else if (CATEGORY.equals(linkType) && categoryIdentifier != null) {
link = urlProvider.toCategoryUrl(request, currentPage, categoryIdentifier);
CategoryUrlFormat.Params params = new CategoryUrlFormat.Params();
if (AbstractCategoryRetriever.CATEGORY_IDENTIFIER_URL_PATH.equals(categoryIdType)) {
params.setUrlPath(categoryIdentifier);
} else {
params.setUid(categoryIdentifier);
}
link = urlProvider.formatCategoryUrl(request, currentPage, params);
} else if (StringUtils.isNotEmpty(linkTo)) {
link = linkTo + ".html";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@

import com.adobe.cq.commerce.core.components.internal.models.v1.common.CommerceIdentifierImpl;
import com.adobe.cq.commerce.core.components.models.common.CommerceIdentifier;
import com.adobe.cq.commerce.core.components.models.retriever.AbstractCategoryRetriever;
import com.adobe.cq.commerce.core.components.models.teaser.CommerceTeaser;
import com.adobe.cq.commerce.core.components.services.urls.CategoryUrlFormat;
import com.adobe.cq.commerce.core.components.services.urls.UrlProvider;
import com.adobe.cq.export.json.ComponentExporter;
import com.adobe.cq.export.json.ExporterConstants;
Expand Down Expand Up @@ -90,16 +92,25 @@ void populateActions() {
for (Resource action : configuredActions) {
ValueMap actionProperties = action.getValueMap();
String productSku = actionProperties.get(PN_ACTION_PRODUCT_SKU, String.class);
String categoryUid = actionProperties.get(PN_ACTION_CATEGORY_ID, String.class);
String categoryId = actionProperties.get(PN_ACTION_CATEGORY_ID, String.class);
String categoryIdType = actionProperties.get(PN_ACTION_CATEGORY_ID_TYPE, String.class);
String link = actionProperties.get(Teaser.PN_ACTION_LINK, String.class);

ListItem wcmAction = null;
String actionUrl;
CommerceIdentifier identifier = null;

if (StringUtils.isNotBlank(categoryUid)) {
actionUrl = urlProvider.toCategoryUrl(request, currentPage, categoryUid);
identifier = new CommerceIdentifierImpl(categoryUid, CommerceIdentifier.IdentifierType.UID,
if (StringUtils.isNotBlank(categoryId)) {
CommerceIdentifier.IdentifierType identifierType = CommerceIdentifier.IdentifierType.UID;
CategoryUrlFormat.Params params = new CategoryUrlFormat.Params();
if (AbstractCategoryRetriever.CATEGORY_IDENTIFIER_URL_PATH.equals(categoryIdType)) {
params.setUrlPath(categoryId);
identifierType = CommerceIdentifier.IdentifierType.URL_PATH;
} else {
params.setUid(categoryId);
}
actionUrl = urlProvider.formatCategoryUrl(request, currentPage, params);
identifier = new CommerceIdentifierImpl(categoryId, identifierType,
CommerceIdentifier.EntityType.CATEGORY);
} else if (StringUtils.isNotBlank(productSku)) {
actionUrl = urlProvider.toProductUrl(request, currentPage, productSku);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import com.adobe.cq.commerce.core.components.internal.services.urlformats.ProductPageWithUrlKey;
import com.adobe.cq.commerce.core.components.internal.services.urlformats.ProductPageWithUrlPath;
import com.adobe.cq.commerce.core.components.models.common.SiteStructure;
import com.adobe.cq.commerce.core.components.models.retriever.AbstractCategoryRetriever;
import com.adobe.cq.commerce.core.components.services.ComponentsConfiguration;
import com.adobe.cq.commerce.core.components.services.urls.CategoryUrlFormat;
import com.adobe.cq.commerce.core.components.services.urls.GenericUrlFormat;
Expand Down Expand Up @@ -365,40 +366,120 @@ public String toCategoryUrl(SlingHttpServletRequest request, Page page, String c
CategoryUrlFormat.Params params = new CategoryUrlFormat.Params();
params.setUid(categoryIdentifier);

CategoryInterface category = callCategoryApi(request, categoryIdentifier, null);
if (category != null) {
params.setUrlKey(category.getUrlKey());
params.setUrlPath(category.getUrlPath());
}
return toCategoryUrl(request, page, params);
}

/**
* Call the category Api whenever required
*
* @param request The current Sling HTTP request
* @param categoryIdentifier The category identifier
* @return CategoryInterface
*/
protected CategoryInterface callCategoryApi(
SlingHttpServletRequest request,
String categoryIdentifier,
String categoryIdType) {
CategoryInterface category = null;
MagentoGraphqlClient magentoGraphqlClient = request != null ? request.adaptTo(MagentoGraphqlClient.class)
: null;
if (magentoGraphqlClient != null && StringUtils.isNotBlank(categoryIdentifier)) {
CategoryUrlParameterRetriever retriever = new CategoryUrlParameterRetriever(magentoGraphqlClient);
retriever.setIdentifier(categoryIdentifier);
CategoryInterface category = retriever.fetchCategory();
if (category != null) {
params.setUrlKey(category.getUrlKey());
params.setUrlPath(category.getUrlPath());
} else {
if (AbstractCategoryRetriever.CATEGORY_IDENTIFIER_URL_PATH.equals(categoryIdType)) {
retriever.setCategoryIdType(AbstractCategoryRetriever.CATEGORY_IDENTIFIER_URL_PATH);
}

category = retriever.fetchCategory();
if (category == null) {
LOGGER.debug("Could not generate category page URL for {}.", categoryIdentifier);
}
}
return toCategoryUrl(request, page, params);
return category;
}

/**
* Set the page parameter for the category URL format
*
* @param request The current Sling HTTP request
* @param categoryUrlFormat The current category URL format
* @param givenPage This parameter can be null if the URL template does set a
* {{page}} parameter and a request is given.
* @param params The parameters used in the URL template.
* @return CategoryInterface
*/
protected Pair<CategoryUrlFormat, CategoryUrlFormat.Params> getSpecificPageFormatAndParams(SlingHttpServletRequest request,
CategoryUrlFormat categoryUrlFormat, @Nullable Page givenPage, CategoryUrlFormat.Params params) {
SiteStructure siteStructure = siteStructureFactory.getSiteStructure(request, givenPage);
Pair<Page, CategoryUrlFormat> pair = getSpecificPageAndFormat(siteStructure, params,
this::getCategoryUrlFormatFromContext, specificPageStrategy::getGenericPage, specificPageStrategy::getSpecificPage);

if (pair != null) {
Page specificPage = pair.getLeft();
categoryUrlFormat = pair.getRight();

if (!specificPage.getPath().equals(params.getPage())) {
params = new CategoryUrlFormat.Params(params);
params.setPage(specificPage.getPath());
}
}
return Pair.of(categoryUrlFormat, params);
}

@Override
public String toCategoryUrl(SlingHttpServletRequest request, @Nullable Page givenPage, CategoryUrlFormat.Params params) {
CategoryUrlFormat categoryUrlFormat = getCategoryUrlFormatFromContext(request, givenPage);

if (givenPage != null) {
SiteStructure siteStructure = siteStructureFactory.getSiteStructure(request, givenPage);
Pair<Page, CategoryUrlFormat> pair = getSpecificPageAndFormat(siteStructure, params,
this::getCategoryUrlFormatFromContext, specificPageStrategy::getGenericPage, specificPageStrategy::getSpecificPage);
Pair<CategoryUrlFormat, CategoryUrlFormat.Params> pair = getSpecificPageFormatAndParams(request, categoryUrlFormat, givenPage,
params);
categoryUrlFormat = pair.getLeft();
params = pair.getRight();
}

if (pair != null) {
Page specificPage = pair.getLeft();
categoryUrlFormat = pair.getRight();
return categoryUrlFormat.format(params);
}

if (!specificPage.getPath().equals(params.getPage())) {
params = new CategoryUrlFormat.Params(params);
params.setPage(specificPage.getPath());
@Override
public String formatCategoryUrl(SlingHttpServletRequest request, @Nullable Page givenPage, CategoryUrlFormat.Params params) {
CategoryUrlFormat categoryUrlFormat = getCategoryUrlFormatFromContext(request, givenPage);
String categoryIdentifier = StringUtils.isNotEmpty(params.getUid()) ? params.getUid() : params.getUrlKey();
boolean urlPathFlag = false;

// Checks all the required parameters for the category URL format
if (!categoryUrlFormat.validateRequiredParams(params)) {
// Ignore the urlKey checks as it is not unique
if (StringUtils.isNotEmpty(params.getUrlPath())) {
categoryIdentifier = params.getUrlPath();
urlPathFlag = true;
}

// Call the MagentoGraphqlClient to get the category details
CategoryInterface category = callCategoryApi(
request,
categoryIdentifier,
urlPathFlag ? AbstractCategoryRetriever.CATEGORY_IDENTIFIER_URL_PATH : null);
if (category != null) {
// Set the required parameters for the category URL format on the basis of the category details
if (urlPathFlag) {
params.setUid(category.get("uid").toString());
} else {
params.setUrlPath(category.getUrlPath());
}
params.setUrlKey(category.getUrlKey());
}
}
if (givenPage != null) {
Pair<CategoryUrlFormat, CategoryUrlFormat.Params> pair = getSpecificPageFormatAndParams(request, categoryUrlFormat, givenPage,
params);
categoryUrlFormat = pair.getLeft();
params = pair.getRight();
}

return categoryUrlFormat.format(params);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ private CategoryPageWithUrlKey() {
super();
}

@Override
public boolean validateRequiredParams(Params parameters) {
return StringUtils.isNotEmpty(parameters.getUrlPath()) || StringUtils.isNotEmpty(parameters.getUrlKey());
}

@Override
public String format(Params parameters) {
return StringUtils.defaultIfEmpty(parameters.getPage(), "{{page}}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ private CategoryPageWithUrlPath() {
super();
}

@Override
public boolean validateRequiredParams(Params parameters) {
return StringUtils.isNotEmpty(parameters.getUrlPath());
}

@Override
public String format(Params parameters) {
String urlKey = StringUtils.defaultIfEmpty(parameters.getUrlKey(), "{{url_path}}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
@ConsumerType
public interface FeaturedCategoryList extends Component {

/**
* Name of the resource property that stores category id type
* which is used to build call-to-action link for category page
*
*/
String CATEGORY_IDENTIFIER_TYPE = "categoryIdType";

/**
* Returns the categories data in a list from Magento depending on configurations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
@Version("3.1.0")
@Version("3.2.0")
package com.adobe.cq.commerce.core.components.models.categorylist;

import org.osgi.annotation.versioning.Version;
import org.osgi.annotation.versioning.Version;
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface CommerceIdentifier {
* The type of this identifier
*/
enum IdentifierType {
SKU, URL_KEY, UID
SKU, URL_KEY, UID, URL_PATH
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
@Version("2.4.0")
@Version("2.5.0")
package com.adobe.cq.commerce.core.components.models.common;

import org.osgi.annotation.versioning.Version;
import org.osgi.annotation.versioning.Version;
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
@Version("5.1.0")
@Version("5.2.0")
package com.adobe.cq.commerce.core.components.models.productlist;

import org.osgi.annotation.versioning.Version;
import org.osgi.annotation.versioning.Version;
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ public abstract class AbstractCategoriesRetriever extends AbstractRetriever {
*/
protected List<String> identifiers;

/**
* CategoryIdType that should be used when fetched. Usually uid but we can define it explicitly in the implementation
* specific and should be checked in subclass implementations.
*/
protected String categoryIdType;

public AbstractCategoriesRetriever(MagentoGraphqlClient client) {
super(client);
}
Expand Down Expand Up @@ -93,6 +99,16 @@ public void setIdentifiers(List<String> identifiers) {
this.identifiers = identifiers;
}

/**
* Set the type category identifier which will using during fetch. Categories are retrieved using
* this categoryIdType if not set then it will use UID.
*
* @param categoryIdType Type category identifier
*/
public void setCategoryIdType(String categoryIdType) {
this.categoryIdType = categoryIdType;
}

/**
* Extend the category query part of the category GraphQL query with a partial query provided by a lambda hook that sets additional
* fields.
Expand Down Expand Up @@ -163,7 +179,13 @@ public void extendCategoryFilterWith(Function<CategoryFilterInput, CategoryFilte
protected String generateQuery(List<String> identifiers) {
CategoryFilterInput filter = new CategoryFilterInput();
FilterEqualTypeInput identifiersFilter = new FilterEqualTypeInput().setIn(identifiers);
filter.setCategoryUid(identifiersFilter);

// Set the filter type
if (AbstractCategoryRetriever.CATEGORY_IDENTIFIER_URL_PATH.equals(this.categoryIdType)) {
filter.setUrlPath(identifiersFilter);
} else {
filter.setCategoryUid(identifiersFilter);
}

// Apply category filter hook
if (this.categoryFilterHook != null) {
Expand Down
Loading