|
12 | 12 | */ |
13 | 13 | package com.labs64.netlicensing.service; |
14 | 14 |
|
| 15 | +import java.math.BigDecimal; |
15 | 16 | import java.util.HashMap; |
16 | 17 | import java.util.Map; |
17 | 18 |
|
18 | 19 | import org.apache.commons.lang3.StringUtils; |
19 | 20 |
|
20 | 21 | import com.labs64.netlicensing.domain.Constants; |
| 22 | +import com.labs64.netlicensing.domain.entity.LicenseTemplate; |
21 | 23 | import com.labs64.netlicensing.domain.entity.Product; |
22 | 24 | import com.labs64.netlicensing.domain.vo.Context; |
23 | 25 | import com.labs64.netlicensing.domain.vo.Page; |
24 | 26 | import com.labs64.netlicensing.exception.NetLicensingException; |
| 27 | +import com.labs64.netlicensing.exception.ServiceException; |
| 28 | +import com.labs64.netlicensing.provider.Form; |
25 | 29 | import com.labs64.netlicensing.util.CheckUtils; |
26 | 30 | import com.labs64.netlicensing.util.ConvertUtils; |
27 | 31 |
|
|
38 | 42 | */ |
39 | 43 | public class ProductService { |
40 | 44 |
|
| 45 | + private static final int HTTP_STATUS_NOT_FOUND = 404; |
| 46 | + private static final String NOT_FOUND_EXCEPTION_PREFIX = "NotFoundException:"; |
| 47 | + |
41 | 48 | /** |
42 | 49 | * Creates new product with given properties. |
43 | 50 | * |
@@ -140,4 +147,69 @@ public static void delete(final Context context, final String number, final bool |
140 | 147 | NetLicensingService.getInstance().delete(context, Constants.Product.ENDPOINT_PATH + "/" + number, params); |
141 | 148 | } |
142 | 149 |
|
| 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 | + |
143 | 215 | } |
0 commit comments