Search in sources :

Example 1 with BulkBaseSubscriptionAndAddOnsJson

use of org.killbill.billing.jaxrs.json.BulkBaseSubscriptionAndAddOnsJson in project killbill by killbill.

the class SubscriptionResource method createEntitlementsWithAddOns.

@TimedResource
@POST
@Path("/createEntitlementsWithAddOns")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Create multiple entitlements with addOn products")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid entitlements supplied") })
public Response createEntitlementsWithAddOns(final List<BulkBaseSubscriptionAndAddOnsJson> entitlementsWithAddOns, @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_MIGRATED) @DefaultValue("false") final Boolean isMigrated, @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(entitlementsWithAddOns) > 0, "Subscription bulk list mustn't be null or empty.");
    logDeprecationParameterWarningIfNeeded(QUERY_REQUESTED_DT, QUERY_ENTITLEMENT_REQUESTED_DT, QUERY_BILLING_REQUESTED_DT);
    final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString);
    final CallContext callContext = context.createContext(createdBy, reason, comment, request);
    final Account account = accountUserApi.getAccountById(UUID.fromString(entitlementsWithAddOns.get(0).getBaseEntitlementAndAddOns().get(0).getAccountId()), callContext);
    final List<BaseEntitlementWithAddOnsSpecifier> baseEntitlementWithAddOnsSpecifierList = new ArrayList<BaseEntitlementWithAddOnsSpecifier>();
    for (BulkBaseSubscriptionAndAddOnsJson bulkBaseEntitlementWithAddOns : entitlementsWithAddOns) {
        final Iterable<SubscriptionJson> baseEntitlements = Iterables.filter(bulkBaseEntitlementWithAddOns.getBaseEntitlementAndAddOns(), new Predicate<SubscriptionJson>() {

            @Override
            public boolean apply(final SubscriptionJson subscription) {
                return ProductCategory.BASE.toString().equalsIgnoreCase(subscription.getProductCategory());
            }
        });
        Preconditions.checkArgument(Iterables.size(baseEntitlements) > 0, "SubscriptionJson Base Entitlement needs to be provided");
        verifyNumberOfElements(Iterables.size(baseEntitlements), 1, "Only one BASE product is allowed per bundle.");
        final Iterable<SubscriptionJson> entitlementsWithBundleSpecified = Iterables.filter(bulkBaseEntitlementWithAddOns.getBaseEntitlementAndAddOns(), new Predicate<SubscriptionJson>() {

            @Override
            public boolean apply(final SubscriptionJson subscription) {
                return subscription.getBundleId() != null;
            }
        });
        Preconditions.checkArgument(Iterables.size(entitlementsWithBundleSpecified) == 0, "BundleId must not be specified when creating new bulks");
        SubscriptionJson baseEntitlement = baseEntitlements.iterator().next();
        final int addOnSubscriptionsSize = Iterables.size(Iterables.filter(bulkBaseEntitlementWithAddOns.getBaseEntitlementAndAddOns(), new Predicate<SubscriptionJson>() {

            @Override
            public boolean apply(final SubscriptionJson subscription) {
                return ProductCategory.ADD_ON.toString().equals(subscription.getProductCategory());
            }
        }));
        verifyNumberOfElements(addOnSubscriptionsSize, bulkBaseEntitlementWithAddOns.getBaseEntitlementAndAddOns().size() - 1, "It should be " + (bulkBaseEntitlementWithAddOns.getBaseEntitlementAndAddOns().size() - 1) + " ADD_ON products.");
        final List<EntitlementSpecifier> entitlementSpecifierList = buildEntitlementSpecifierList(bulkBaseEntitlementWithAddOns.getBaseEntitlementAndAddOns(), account.getCurrency());
        // create the baseEntitlementSpecifierWithAddOns
        final LocalDate resolvedEntitlementDate = requestedDate != null ? toLocalDate(requestedDate) : toLocalDate(entitlementDate);
        final LocalDate resolvedBillingDate = requestedDate != null ? toLocalDate(requestedDate) : toLocalDate(billingDate);
        BaseEntitlementWithAddOnsSpecifier baseEntitlementSpecifierWithAddOns = buildBaseEntitlementWithAddOnsSpecifier(entitlementSpecifierList, resolvedEntitlementDate, resolvedBillingDate, null, baseEntitlement, isMigrated);
        baseEntitlementWithAddOnsSpecifierList.add(baseEntitlementSpecifierWithAddOns);
    }
    final EntitlementCallCompletionCallback<List<Entitlement>> callback = new EntitlementCallCompletionCallback<List<Entitlement>>() {

        @Override
        public List<Entitlement> doOperation(final CallContext ctx) throws InterruptedException, TimeoutException, EntitlementApiException, SubscriptionApiException, AccountApiException {
            return entitlementApi.createBaseEntitlementsWithAddOns(account.getId(), baseEntitlementWithAddOnsSpecifierList, pluginProperties, callContext);
        }

        @Override
        public boolean isImmOperation() {
            return true;
        }

        @Override
        public Response doResponseOk(final List<Entitlement> entitlements) {
            return uriBuilder.buildResponse(uriInfo, AccountResource.class, "getAccountBundles", entitlements.get(0).getAccountId(), buildQueryParams(buildBundleIdList(entitlements)), request);
        }
    };
    final EntitlementCallCompletion<List<Entitlement>> callCompletionCreation = new EntitlementCallCompletion<List<Entitlement>>();
    return callCompletionCreation.withSynchronization(callback, timeoutSec, callCompletion, callContext);
}
Also used : Account(org.killbill.billing.account.api.Account) SubscriptionJson(org.killbill.billing.jaxrs.json.SubscriptionJson) ArrayList(java.util.ArrayList) BulkBaseSubscriptionAndAddOnsJson(org.killbill.billing.jaxrs.json.BulkBaseSubscriptionAndAddOnsJson) CallContext(org.killbill.billing.util.callcontext.CallContext) LocalDate(org.joda.time.LocalDate) Predicate(com.google.common.base.Predicate) EntitlementSpecifier(org.killbill.billing.entitlement.api.EntitlementSpecifier) PluginProperty(org.killbill.billing.payment.api.PluginProperty) BaseEntitlementWithAddOnsSpecifier(org.killbill.billing.entitlement.api.BaseEntitlementWithAddOnsSpecifier) List(java.util.List) ArrayList(java.util.ArrayList) Entitlement(org.killbill.billing.entitlement.api.Entitlement) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) Path(javax.ws.rs.Path) TimedResource(org.killbill.commons.metrics.TimedResource) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

Predicate (com.google.common.base.Predicate)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 LocalDate (org.joda.time.LocalDate)1 Account (org.killbill.billing.account.api.Account)1 PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)1 BaseEntitlementWithAddOnsSpecifier (org.killbill.billing.entitlement.api.BaseEntitlementWithAddOnsSpecifier)1 Entitlement (org.killbill.billing.entitlement.api.Entitlement)1 EntitlementSpecifier (org.killbill.billing.entitlement.api.EntitlementSpecifier)1 BulkBaseSubscriptionAndAddOnsJson (org.killbill.billing.jaxrs.json.BulkBaseSubscriptionAndAddOnsJson)1 SubscriptionJson (org.killbill.billing.jaxrs.json.SubscriptionJson)1 PluginProperty (org.killbill.billing.payment.api.PluginProperty)1 CallContext (org.killbill.billing.util.callcontext.CallContext)1 TimedResource (org.killbill.commons.metrics.TimedResource)1