Skip to content

Commit 24d5056

Browse files
committed
move discount resolve API to ProductService
1 parent 5dac09b commit 24d5056

4 files changed

Lines changed: 191 additions & 279 deletions

File tree

NetLicensingClient/src/main/java/com/labs64/netlicensing/service/DiscountService.java

Lines changed: 0 additions & 99 deletions
This file was deleted.

NetLicensingClient/src/main/java/com/labs64/netlicensing/service/ProductService.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@
1212
*/
1313
package com.labs64.netlicensing.service;
1414

15+
import java.math.BigDecimal;
1516
import java.util.HashMap;
1617
import java.util.Map;
1718

1819
import org.apache.commons.lang3.StringUtils;
1920

2021
import com.labs64.netlicensing.domain.Constants;
22+
import com.labs64.netlicensing.domain.entity.LicenseTemplate;
2123
import com.labs64.netlicensing.domain.entity.Product;
2224
import com.labs64.netlicensing.domain.vo.Context;
2325
import com.labs64.netlicensing.domain.vo.Page;
2426
import com.labs64.netlicensing.exception.NetLicensingException;
27+
import com.labs64.netlicensing.exception.ServiceException;
28+
import com.labs64.netlicensing.provider.Form;
2529
import com.labs64.netlicensing.util.CheckUtils;
2630
import com.labs64.netlicensing.util.ConvertUtils;
2731

@@ -38,6 +42,9 @@
3842
*/
3943
public class ProductService {
4044

45+
private static final int HTTP_STATUS_NOT_FOUND = 404;
46+
private static final String NOT_FOUND_EXCEPTION_PREFIX = "NotFoundException:";
47+
4148
/**
4249
* Creates new product with given properties.
4350
*
@@ -140,4 +147,69 @@ public static void delete(final Context context, final String number, final bool
140147
NetLicensingService.getInstance().delete(context, Constants.Product.ENDPOINT_PATH + "/" + number, params);
141148
}
142149

150+
/**
151+
* Resolves a discount license template by product number and promo code.
152+
*
153+
* @param context
154+
* determines the vendor on whose behalf the call is performed
155+
* @param productNumber
156+
* product number that defines promo code uniqueness scope
157+
* @param promoCode
158+
* promo code to resolve
159+
* @return resolved discount license template or null if discount was not found
160+
* @throws NetLicensingException
161+
* any non-not-found service error
162+
*/
163+
public static LicenseTemplate resolveDiscount(final Context context, final String productNumber,
164+
final String promoCode) throws NetLicensingException {
165+
return resolveDiscount(context, productNumber, promoCode, null);
166+
}
167+
168+
/**
169+
* Resolves a discount license template by product number and promo code.
170+
*
171+
* @param context
172+
* determines the vendor on whose behalf the call is performed
173+
* @param productNumber
174+
* product number that defines promo code uniqueness scope
175+
* @param promoCode
176+
* promo code to resolve
177+
* @param cartTotal
178+
* optional cart total used by the API to validate minimum cart total
179+
* @return resolved discount license template or null if discount was not found
180+
* @throws NetLicensingException
181+
* any non-not-found service error
182+
*/
183+
public static LicenseTemplate resolveDiscount(final Context context, final String productNumber,
184+
final String promoCode, final BigDecimal cartTotal) throws NetLicensingException {
185+
CheckUtils.paramNotEmpty(productNumber, "productNumber");
186+
CheckUtils.paramNotEmpty(promoCode, "promoCode");
187+
188+
final Form form = new Form();
189+
if (cartTotal != null) {
190+
form.param(Constants.Product.Discount.CART_TOTAL, cartTotal.toString());
191+
}
192+
193+
try {
194+
return NetLicensingService.getInstance().post(context, resolveDiscountEndpoint(productNumber, promoCode),
195+
form, LicenseTemplate.class);
196+
} catch (final ServiceException e) {
197+
if (isNotFound(e)) {
198+
return null;
199+
}
200+
throw e;
201+
}
202+
}
203+
204+
private static String resolveDiscountEndpoint(final String productNumber, final String promoCode) {
205+
return Constants.Product.ENDPOINT_PATH + "/" + productNumber + "/"
206+
+ Constants.Product.Discount.ENDPOINT_PATH + "/" + promoCode + "/"
207+
+ Constants.Product.Discount.ENDPOINT_RESOLVE_PATH;
208+
}
209+
210+
private static boolean isNotFound(final ServiceException e) {
211+
return (e.getStatusCode() == HTTP_STATUS_NOT_FOUND)
212+
|| StringUtils.startsWith(e.getMessage(), NOT_FOUND_EXCEPTION_PREFIX);
213+
}
214+
143215
}

NetLicensingClient/src/test/java/com/labs64/netlicensing/service/DiscountServiceTest.java

Lines changed: 0 additions & 180 deletions
This file was deleted.

0 commit comments

Comments
 (0)