Skip to content

Commit

Permalink
LPD-34676 Added Commerce Order FDSAPIURLResolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
gianmarcobrunialti committed Sep 23, 2024
1 parent 9e190c2 commit 6d0a45f
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.liferay.commerce.product.service.CommerceChannelLocalService;
import com.liferay.frontend.data.set.resolver.FDSAPIURLResolver;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.util.Validator;

import javax.servlet.http.HttpServletRequest;

Expand Down Expand Up @@ -42,29 +43,47 @@ public String resolve(String baseURL, HttpServletRequest httpServletRequest)
(CommerceContext)httpServletRequest.getAttribute(
CommerceWebKeys.COMMERCE_CONTEXT);

AccountEntry accountEntry = commerceContext.getAccountEntry();
CommerceChannel commerceChannel =
_commerceChannelLocalService.getCommerceChannel(
commerceContext.getCommerceChannelId());
CommerceOrder commerceOrder = commerceContext.getCommerceOrder();

return baseURL.replaceAll(
"\\{accountExternalReferenceCode\\}",
accountEntry.getExternalReferenceCode()
).replaceAll(
"\\{accountId\\}", String.valueOf(accountEntry.getAccountEntryId())
).replaceAll(
"\\{cartId\\}", String.valueOf(commerceOrder.getCommerceOrderId())
).replaceAll(
"\\{channelExternalReferenceCode\\}",
String.valueOf(commerceChannel.getExternalReferenceCode())
).replaceAll(
"\\{channelId\\}",
String.valueOf(commerceContext.getCommerceChannelId())
).replaceAll(
"\\{externalReferenceCode\\}",
commerceOrder.getExternalReferenceCode()
);
if (Validator.isNotNull(commerceContext)) {
AccountEntry accountEntry = commerceContext.getAccountEntry();

if (Validator.isNotNull(accountEntry)) {
baseURL = baseURL.replaceAll(
"\\{accountExternalReferenceCode\\}",
accountEntry.getExternalReferenceCode()
).replaceAll(
"\\{accountId\\}",
String.valueOf(accountEntry.getAccountEntryId())
);
}

CommerceChannel commerceChannel =
_commerceChannelLocalService.getCommerceChannel(
commerceContext.getCommerceChannelId());

if (Validator.isNotNull(commerceChannel)) {
baseURL = baseURL.replaceAll(
"\\{channelExternalReferenceCode\\}",
String.valueOf(commerceChannel.getExternalReferenceCode())
).replaceAll(
"\\{channelId\\}",
String.valueOf(commerceContext.getCommerceChannelId())
);
}

CommerceOrder commerceOrder = commerceContext.getCommerceOrder();

if (Validator.isNotNull(commerceOrder)) {
baseURL = baseURL.replaceAll(
"\\{cartId\\}",
String.valueOf(commerceOrder.getCommerceOrderId())
).replaceAll(
"\\{externalReferenceCode\\}",
commerceOrder.getExternalReferenceCode()
);
}
}

return baseURL;
}

@Reference
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* SPDX-FileCopyrightText: (c) 2024 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/

package com.liferay.commerce.order.content.web.internal.frontend.data.set.resolver;

import com.liferay.commerce.constants.CommerceWebKeys;
import com.liferay.commerce.context.CommerceContext;
import com.liferay.commerce.model.CommerceOrder;
import com.liferay.frontend.data.set.resolver.FDSAPIURLResolver;
import com.liferay.portal.kernel.util.Validator;

import javax.servlet.http.HttpServletRequest;

import org.osgi.service.component.annotations.Component;

@Component(
property = "fds.rest.application.key=/headless-commerce-delivery-cart/v1.0/CartItem",
service = FDSAPIURLResolver.class
)
public class PendingCommerceOrderItemFDSAPIURLResolver
implements FDSAPIURLResolver {

@Override
public String getSchema() {
return "CartItem";
}

@Override
public String resolve(
String baseURL, HttpServletRequest httpServletRequest) {

CommerceContext commerceContext =
(CommerceContext)httpServletRequest.getAttribute(
CommerceWebKeys.COMMERCE_CONTEXT);

if (Validator.isNotNull(commerceContext)) {
CommerceOrder commerceOrder = commerceContext.getCommerceOrder();

if (Validator.isNotNull(commerceOrder)) {
baseURL = baseURL.replaceAll(
"\\{cartId\\}",
String.valueOf(commerceOrder.getCommerceOrderId())
).replaceAll(
"\\{externalReferenceCode\\}",
commerceOrder.getExternalReferenceCode()
);
}
}

return baseURL;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* SPDX-FileCopyrightText: (c) 2024 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/

package com.liferay.commerce.order.content.web.internal.frontend.data.set.resolver;

import com.liferay.account.model.AccountEntry;
import com.liferay.commerce.constants.CommerceWebKeys;
import com.liferay.commerce.context.CommerceContext;
import com.liferay.commerce.model.CommerceOrder;
import com.liferay.commerce.product.model.CommerceChannel;
import com.liferay.commerce.product.service.CommerceChannelLocalService;
import com.liferay.frontend.data.set.resolver.FDSAPIURLResolver;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.util.Validator;

import javax.servlet.http.HttpServletRequest;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
* @author Gianmarco Brunialti Masera
*/
@Component(
property = "fds.rest.application.key=/headless-commerce-delivery-order/v1.0/PlacedOrder",
service = FDSAPIURLResolver.class
)
public class PlacedCommerceOrderFDSAPIURLResolver implements FDSAPIURLResolver {

@Override
public String getSchema() {
return "PlacedOrder";
}

@Override
public String resolve(String baseURL, HttpServletRequest httpServletRequest)
throws PortalException {

CommerceContext commerceContext =
(CommerceContext)httpServletRequest.getAttribute(
CommerceWebKeys.COMMERCE_CONTEXT);

if (Validator.isNotNull(commerceContext)) {
AccountEntry accountEntry = commerceContext.getAccountEntry();

if (Validator.isNotNull(accountEntry)) {
baseURL = baseURL.replaceAll(
"\\{accountExternalReferenceCode\\}",
accountEntry.getExternalReferenceCode()
).replaceAll(
"\\{accountId\\}",
String.valueOf(accountEntry.getAccountEntryId())
);
}

CommerceChannel commerceChannel =
_commerceChannelLocalService.getCommerceChannel(
commerceContext.getCommerceChannelId());

if (Validator.isNotNull(commerceChannel)) {
baseURL = baseURL.replaceAll(
"\\{channelExternalReferenceCode\\}",
String.valueOf(commerceChannel.getExternalReferenceCode())
).replaceAll(
"\\{channelId\\}",
String.valueOf(commerceContext.getCommerceChannelId())
);
}

CommerceOrder commerceOrder = commerceContext.getCommerceOrder();

if (Validator.isNotNull(commerceOrder)) {
baseURL = baseURL.replaceAll(
"\\{placedOrderId\\}",
String.valueOf(commerceOrder.getCommerceOrderId())
).replaceAll(
"\\{externalReferenceCode\\}",
commerceOrder.getExternalReferenceCode()
);
}
}

return baseURL;
}

@Reference
private CommerceChannelLocalService _commerceChannelLocalService;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* SPDX-FileCopyrightText: (c) 2024 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/

package com.liferay.commerce.order.content.web.internal.frontend.data.set.resolver;

import com.liferay.commerce.constants.CommerceWebKeys;
import com.liferay.commerce.context.CommerceContext;
import com.liferay.commerce.model.CommerceOrder;
import com.liferay.frontend.data.set.resolver.FDSAPIURLResolver;
import com.liferay.portal.kernel.util.Validator;

import javax.servlet.http.HttpServletRequest;

import org.osgi.service.component.annotations.Component;

/**
* @author Gianmarco Brunialti Masera
*/
@Component(
property = "fds.rest.application.key=/headless-commerce-delivery-order/v1.0/PlacedOrderItem",
service = FDSAPIURLResolver.class
)
public class PlacedCommerceOrderItemFDSAPIURLResolver
implements FDSAPIURLResolver {

@Override
public String getSchema() {
return "PlacedOrderItem";
}

@Override
public String resolve(
String baseURL, HttpServletRequest httpServletRequest) {

CommerceContext commerceContext =
(CommerceContext)httpServletRequest.getAttribute(
CommerceWebKeys.COMMERCE_CONTEXT);

if (Validator.isNotNull(commerceContext)) {
CommerceOrder commerceOrder = commerceContext.getCommerceOrder();

if (Validator.isNotNull(commerceOrder)) {
baseURL = baseURL.replaceAll(
"\\{placedOrderId\\}",
String.valueOf(commerceOrder.getCommerceOrderId())
).replaceAll(
"\\{externalReferenceCode\\}",
commerceOrder.getExternalReferenceCode()
);
}
}

return baseURL;
}

}

0 comments on commit 6d0a45f

Please sign in to comment.