-
Notifications
You must be signed in to change notification settings - Fork 5
NLIC-2515 Add DiscountService #61
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,16 +12,20 @@ | |
| */ | ||
| package com.labs64.netlicensing.service; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import org.apache.commons.lang3.StringUtils; | ||
|
|
||
| import com.labs64.netlicensing.domain.Constants; | ||
| import com.labs64.netlicensing.domain.entity.LicenseTemplate; | ||
| import com.labs64.netlicensing.domain.entity.Product; | ||
| import com.labs64.netlicensing.domain.vo.Context; | ||
| import com.labs64.netlicensing.domain.vo.Page; | ||
| import com.labs64.netlicensing.exception.NetLicensingException; | ||
| import com.labs64.netlicensing.exception.ServiceException; | ||
| import com.labs64.netlicensing.provider.Form; | ||
| import com.labs64.netlicensing.util.CheckUtils; | ||
| import com.labs64.netlicensing.util.ConvertUtils; | ||
|
|
||
|
|
@@ -38,6 +42,9 @@ | |
| */ | ||
| public class ProductService { | ||
|
|
||
| private static final int HTTP_STATUS_NOT_FOUND = 404; | ||
| private static final String NOT_FOUND_EXCEPTION_PREFIX = "NotFoundException:"; | ||
|
|
||
| /** | ||
| * Creates new product with given properties. | ||
| * | ||
|
|
@@ -140,4 +147,69 @@ public static void delete(final Context context, final String number, final bool | |
| NetLicensingService.getInstance().delete(context, Constants.Product.ENDPOINT_PATH + "/" + number, params); | ||
| } | ||
|
|
||
| /** | ||
| * Resolves a discount license template by product number and promo code. | ||
| * | ||
| * @param context | ||
| * determines the vendor on whose behalf the call is performed | ||
| * @param productNumber | ||
| * product number that defines promo code uniqueness scope | ||
| * @param promoCode | ||
| * promo code to resolve | ||
| * @return resolved discount license template or null if discount was not found | ||
| * @throws NetLicensingException | ||
| * any non-not-found service error | ||
| */ | ||
| public static LicenseTemplate resolveDiscount(final Context context, final String productNumber, | ||
| final String promoCode) throws NetLicensingException { | ||
| return resolveDiscount(context, productNumber, promoCode, null); | ||
| } | ||
|
|
||
| /** | ||
| * Resolves a discount license template by product number and promo code. | ||
| * | ||
| * @param context | ||
| * determines the vendor on whose behalf the call is performed | ||
| * @param productNumber | ||
| * product number that defines promo code uniqueness scope | ||
| * @param promoCode | ||
| * promo code to resolve | ||
| * @param cartTotal | ||
| * optional cart total used by the API to validate minimum cart total | ||
| * @return resolved discount license template or null if discount was not found | ||
| * @throws NetLicensingException | ||
| * any non-not-found service error | ||
| */ | ||
| public static LicenseTemplate resolveDiscount(final Context context, final String productNumber, | ||
| final String promoCode, final BigDecimal cartTotal) throws NetLicensingException { | ||
| CheckUtils.paramNotEmpty(productNumber, "productNumber"); | ||
| CheckUtils.paramNotEmpty(promoCode, "promoCode"); | ||
|
|
||
| final Form form = new Form(); | ||
| if (cartTotal != null) { | ||
| form.param(Constants.Product.Discount.CART_TOTAL, cartTotal.toString()); | ||
| } | ||
|
|
||
| try { | ||
| return NetLicensingService.getInstance().post(context, resolveDiscountEndpoint(productNumber, promoCode), | ||
| form, LicenseTemplate.class); | ||
| } catch (final ServiceException e) { | ||
| if (isNotFound(e)) { | ||
| return null; | ||
| } | ||
|
Comment on lines
+197
to
+199
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This belongs to the business logic. If null is a legitimate response, the REST interface should handle it gracefully as well. Otherwise, the contract diverges between the Java binding and the REST interface. |
||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| private static String resolveDiscountEndpoint(final String productNumber, final String promoCode) { | ||
| return Constants.Product.ENDPOINT_PATH + "/" + productNumber + "/" | ||
| + Constants.Product.Discount.ENDPOINT_PATH + "/" + promoCode + "/" | ||
| + Constants.Product.Discount.ENDPOINT_RESOLVE_PATH; | ||
| } | ||
|
|
||
| private static boolean isNotFound(final ServiceException e) { | ||
| return (e.getStatusCode() == HTTP_STATUS_NOT_FOUND) | ||
| || StringUtils.startsWith(e.getMessage(), NOT_FOUND_EXCEPTION_PREFIX); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deprecated method: StringUtils.startsWith |
||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this intended for direct use by vendors?