Skip to content

Commit

Permalink
jaxrs: See #492
Browse files Browse the repository at this point in the history
Add new query params to subscription resource to control both entitlement and billing date when creating subscriptions.
Backward compatible mode with param 'requestedDate' was preserved with a warning.
  • Loading branch information
sbrossie committed Feb 19, 2016
1 parent 0afd474 commit 3fde612
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
Expand Up @@ -30,6 +30,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -442,4 +443,9 @@ protected void verifyNumberOfElements(int actual, int expected, String errorMess
Preconditions.checkArgument(actual == expected, errorMessage);
}

protected void logDeprecationParameterWarningIfNeeded(@Nullable final String deprecatedParam, final String...replacementParams) {
if (deprecatedParam != null) {
log.warn(String.format("Parameter %s is being deprecated: Instead use parameters %s", deprecatedParam, Joiner.on(",").join(replacementParams)));
}
}
}
Expand Up @@ -67,6 +67,8 @@ public interface JaxrsResource {
public static final String QUERY_EXTERNAL_KEY = "externalKey";
public static final String QUERY_API_KEY = "apiKey";
public static final String QUERY_REQUESTED_DT = "requestedDate";
public static final String QUERY_ENTITLEMENT_REQUESTED_DT = "entitlementDate";
public static final String QUERY_BILLING_REQUESTED_DT = "billingDate";
public static final String QUERY_CALL_COMPLETION = "callCompletion";
public static final String QUERY_USE_REQUESTED_DATE_FOR_BILLING = "useRequestedDateForBilling";
public static final String QUERY_CALL_TIMEOUT = "callTimeoutSec";
Expand Down
Expand Up @@ -92,6 +92,7 @@

import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.inject.Inject;
import com.wordnik.swagger.annotations.Api;
Expand Down Expand Up @@ -152,7 +153,9 @@ public Response getEntitlement(@PathParam("subscriptionId") final String subscri
@ApiOperation(value = "Create an entitlement")
@ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid entitlement supplied")})
public Response createEntitlement(final SubscriptionJson entitlement,
@QueryParam(QUERY_REQUESTED_DT) final String requestedDate,
@QueryParam(QUERY_REQUESTED_DT) final String requestedDate, /* This is deprecated, only used for backward compatibility */
@QueryParam(QUERY_ENTITLEMENT_REQUESTED_DT) final String entitlementDate,
@QueryParam(QUERY_BILLING_REQUESTED_DT) final String billingDate,
@QueryParam(QUERY_CALL_COMPLETION) @DefaultValue("false") final Boolean callCompletion,
@QueryParam(QUERY_CALL_TIMEOUT) @DefaultValue("3") final long timeoutSec,
@QueryParam(QUERY_PLUGIN_PROPERTY) final List<String> pluginPropertiesString,
Expand All @@ -166,6 +169,9 @@ public Response createEntitlement(final SubscriptionJson entitlement,
entitlement.getProductCategory(), "SubscriptionJson productCategory needs to be set",
entitlement.getBillingPeriod(), "SubscriptionJson billingPeriod needs to be set",
entitlement.getPriceList(), "SubscriptionJson priceList needs to be set");

logDeprecationParameterWarningIfNeeded(QUERY_REQUESTED_DT, QUERY_ENTITLEMENT_REQUESTED_DT, QUERY_BILLING_REQUESTED_DT);

// For ADD_ON we can provide externalKey or the bundleId
final boolean createAddOnEntitlement = ProductCategory.ADD_ON.toString().equals(entitlement.getProductCategory());
if (createAddOnEntitlement) {
Expand All @@ -186,16 +192,16 @@ public Entitlement doOperation(final CallContext ctx) throws InterruptedExceptio
ProductCategory.valueOf(entitlement.getProductCategory()),
BillingPeriod.valueOf(entitlement.getBillingPeriod()), entitlement.getPriceList(), phaseType);

final LocalDate inputLocalDate = toLocalDate(requestedDate, callContext);
final LocalDate resolvedEntitlementDate = requestedDate != null ? toLocalDate(requestedDate, callContext) : toLocalDate(entitlementDate, callContext);
final LocalDate resolvedBillingDate = requestedDate != null ? toLocalDate(requestedDate, callContext) : toLocalDate(billingDate, callContext);
final PlanSpecifier planSpec = new PlanSpecifier(entitlement.getProductName(),
ProductCategory.valueOf(entitlement.getProductCategory()),
BillingPeriod.valueOf(entitlement.getBillingPeriod()), entitlement.getPriceList());

final List<PlanPhasePriceOverride> overrides = PhasePriceOverrideJson.toPlanPhasePriceOverrides(entitlement.getPriceOverrides(), planSpec, account.getCurrency());
return createAddOnEntitlement ?
entitlementApi.addEntitlement(getBundleIdForAddOnCreation(entitlement), spec, overrides, inputLocalDate, inputLocalDate, pluginProperties, callContext) :
// TODO billingDate
entitlementApi.createBaseEntitlement(account.getId(), spec, entitlement.getExternalKey(), overrides, inputLocalDate, inputLocalDate, pluginProperties, callContext);
entitlementApi.addEntitlement(getBundleIdForAddOnCreation(entitlement), spec, overrides, resolvedEntitlementDate, resolvedBillingDate, pluginProperties, callContext) :
entitlementApi.createBaseEntitlement(account.getId(), spec, entitlement.getExternalKey(), overrides, resolvedEntitlementDate, resolvedBillingDate, pluginProperties, callContext);
}

private UUID getBundleIdForAddOnCreation(final SubscriptionJson entitlement) throws SubscriptionApiException {
Expand Down Expand Up @@ -231,15 +237,17 @@ public Response doResponseOk(final Entitlement createdEntitlement) {
@ApiOperation(value = "Create an entitlement with addOn products")
@ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid entitlement supplied")})
public Response createEntitlementWithAddOns(final List<SubscriptionJson> entitlements,
@QueryParam(QUERY_REQUESTED_DT) final String requestedDate,
@QueryParam(QUERY_CALL_COMPLETION) @DefaultValue("false") final Boolean callCompletion,
@QueryParam(QUERY_CALL_TIMEOUT) @DefaultValue("3") final long timeoutSec,
@QueryParam(QUERY_PLUGIN_PROPERTY) final List<String> pluginPropertiesString,
@HeaderParam(HDR_CREATED_BY) final String createdBy,
@HeaderParam(HDR_REASON) final String reason,
@HeaderParam(HDR_COMMENT) final String comment,
@javax.ws.rs.core.Context final HttpServletRequest request,
@javax.ws.rs.core.Context final UriInfo uriInfo) throws EntitlementApiException, AccountApiException, SubscriptionApiException {
@QueryParam(QUERY_REQUESTED_DT) final String requestedDate, /* This is deprecated, only used for backward compatibility */
@QueryParam(QUERY_ENTITLEMENT_REQUESTED_DT) final String entitlementDate,
@QueryParam(QUERY_BILLING_REQUESTED_DT) final String billingDate,
@QueryParam(QUERY_CALL_COMPLETION) @DefaultValue("false") final Boolean callCompletion,
@QueryParam(QUERY_CALL_TIMEOUT) @DefaultValue("3") final long timeoutSec,
@QueryParam(QUERY_PLUGIN_PROPERTY) final List<String> pluginPropertiesString,
@HeaderParam(HDR_CREATED_BY) final String createdBy,
@HeaderParam(HDR_REASON) final String reason,
@HeaderParam(HDR_COMMENT) final String comment,
@javax.ws.rs.core.Context final HttpServletRequest request,
@javax.ws.rs.core.Context final UriInfo uriInfo) throws EntitlementApiException, AccountApiException, SubscriptionApiException {

Preconditions.checkArgument(Iterables.size(entitlements) > 0, "Subscription list mustn't be null or empty.");

Expand All @@ -251,6 +259,8 @@ public Response createEntitlementWithAddOns(final List<SubscriptionJson> entitle
entitlement.getPriceList(), "SubscriptionJson priceList needs to be set for each element");
}

logDeprecationParameterWarningIfNeeded(QUERY_REQUESTED_DT, QUERY_ENTITLEMENT_REQUESTED_DT, QUERY_BILLING_REQUESTED_DT);

final int baseSubscriptionsSize = Iterables.size(Iterables.filter(entitlements, new Predicate<SubscriptionJson>() {
@Override
public boolean apply(final SubscriptionJson subscription) {
Expand Down Expand Up @@ -314,9 +324,10 @@ public List<PlanPhasePriceOverride> getOverrides() {
entitlementSpecifierList.add(specifier);
}

final LocalDate inputLocalDate = toLocalDate(requestedDate, callContext);
final LocalDate resolvedEntitlementDate = requestedDate != null ? toLocalDate(requestedDate, callContext) : toLocalDate(entitlementDate, callContext);
final LocalDate resolvedBillingDate = requestedDate != null ? toLocalDate(requestedDate, callContext) : toLocalDate(billingDate, callContext);
return entitlementApi.createBaseEntitlementWithAddOns(account.getId(), baseEntitlement.getExternalKey(), entitlementSpecifierList,
inputLocalDate, inputLocalDate, pluginProperties, callContext);
resolvedEntitlementDate, resolvedBillingDate, pluginProperties, callContext);
}

@Override
Expand Down

0 comments on commit 3fde612

Please sign in to comment.