All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zuora.sdk.catalog.ChargeDefinitionsService Maven / Gradle / Ivy

package com.zuora.sdk.catalog;

import static com.zuora.sdk.catalog.Catalog.getUserAgent;
import static com.zuora.sdk.json.PropertyNameScheme.LowerCamelCase;

import com.zuora.sdk.error.ErrorMappers;
import com.zuora.sdk.error.Validations;
import com.zuora.sdk.http.HttpMethod;
import com.zuora.sdk.http.HttpSupport;
import com.zuora.zevolve.api.model.CreateProductChargeDefinitionRequest;
import com.zuora.zevolve.api.model.EffectiveDatingQueryOption;
import com.zuora.zevolve.api.model.GetPlanChargesByContextRequest;
import com.zuora.zevolve.api.model.GetPlanChargesByContextRequestv2;
import com.zuora.zevolve.api.model.GetProductChargeDefinitionRequest;
import com.zuora.zevolve.api.model.GetProductChargeDefinitionsRequest;
import com.zuora.zevolve.api.model.MigrateToAbpV2Request;
import com.zuora.zevolve.api.model.ProductChargeDefinition;
import com.zuora.zevolve.api.model.ProductChargeDefinitionWithRevisions;
import com.zuora.zevolve.api.model.ProductChargeDefinitions;
import com.zuora.zevolve.api.model.UpdateProductChargeDefinitionRequest;
import com.zuora.zevolve.api.model.Value;

import com.fasterxml.jackson.core.type.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;
import java.util.Optional;

public class ChargeDefinitionsService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ChargesService.class);
    private final HttpSupport httpSupport;

    public ChargeDefinitionsService(HttpSupport httpSupport) {
        this.httpSupport = httpSupport;
    }

    public ProductChargeDefinitionWithRevisions createChargeDefinition(CreateProductChargeDefinitionRequest request) {
        Validations.requireNonNull(request, "request is required");
        return httpSupport.call(
                HttpMethod.POST,
                Catalog.getPathPrefix() + "/charge-definitions",
                Optional.of(request),
                new TypeReference<>() {},
                ErrorMappers::appErrorMapper,
                LowerCamelCase,
                getUserAgent());
    }

    public ProductChargeDefinitionWithRevisions getChargeDefinition(String chargeDefinitionKey,
            boolean hideInheritedValues,
            EffectiveDatingQueryOption effectiveDatingQueryOption,
            String time) {
        Validations.requireNotEmpty(chargeDefinitionKey, "chargeDefinitionKey required");
        return httpSupport.call(
                HttpMethod.POST,
                Catalog.getPathPrefix() + "/charge-definitions/singleton-query",
                Optional.of(GetProductChargeDefinitionRequest.builder()
                        .hideInheritedValues(hideInheritedValues)
                        .chargeDefinitionKey(chargeDefinitionKey)
                        .time(time)
                        .effectiveDatingQueryOption(effectiveDatingQueryOption)
                        .build()),
                new TypeReference<>() {},
                ErrorMappers::appErrorMapper,
                LowerCamelCase,
                getUserAgent());
    }

    public List getChargeDefinitions(String chargeKey,
            int page,
            int pageSize,
            boolean hideInheritedValues) {
        ProductChargeDefinitions response = httpSupport.call(
                HttpMethod.POST,
                Catalog.getPathPrefix() + "/charge-definitions/query",
                Optional.of(GetProductChargeDefinitionsRequest.builder()
                        .charge(chargeKey)
                        .page(page)
                        .pageSize(pageSize)
                        .hideInheritedValues(hideInheritedValues)
                        .build()),
                new TypeReference<>() {},
                ErrorMappers::appErrorMapper,
                LowerCamelCase,
                getUserAgent());
        return response.getChargeDefinitions();
    }

    public List getChargeDefinitions(
            String contextName,
            Map contextMap) {
        ProductChargeDefinitions response = httpSupport.call(
                HttpMethod.POST,
                Catalog.getPathPrefix() + "/plans/charge-definitions/context-query-v2",
                Optional.of(GetPlanChargesByContextRequestv2.builder()
                        .contextName(contextName)
                        .contextmap(contextMap)
                        .build()),
                new TypeReference<>() {},
                ErrorMappers::appErrorMapper,
                LowerCamelCase,
                getUserAgent());
        return response.getChargeDefinitions();
    }


    public ProductChargeDefinitionWithRevisions updateChargeDefinition(UpdateProductChargeDefinitionRequest request) {
        Validations.requireNonNull(request, "request is required");
        return httpSupport.call(
                HttpMethod.PUT,
                Catalog.getPathPrefix() + "/charge-definitions",
                Optional.of(request),
                new TypeReference<>() {},
                ErrorMappers::appErrorMapper,
                LowerCamelCase,
                getUserAgent());
    }

    public Boolean deleteChargeDefinition(String chargeDefinitionKey) {
        Validations.requireNotEmpty(chargeDefinitionKey, "chargeDefinitionKey required");
        return "ok".equalsIgnoreCase(httpSupport.call(
                HttpMethod.DELETE,
                Catalog.getPathPrefix() + "/charge-definitions/" + chargeDefinitionKey,
                Optional.empty(),
                new TypeReference<>() {},
                ErrorMappers::appErrorMapper,
                LowerCamelCase,
                getUserAgent()));
    }

    public List getPlanChargeDefinitions(String ratePlanId,
            String contextName,
            Map contextMap) {
        Validations.requireNotEmpty(ratePlanId, "ratePlanId required");
        ProductChargeDefinitions response = httpSupport.call(
                HttpMethod.POST,
                Catalog.getPathPrefix() + "/plans/charge-definitions/context-query",
                Optional.of(GetPlanChargesByContextRequest.builder()
                        .ratePlanId(ratePlanId)
                        .contextName(contextName)
                        .contextmap(contextMap)
                        .build()),
                new TypeReference<>() {},
                ErrorMappers::appErrorMapper,
                LowerCamelCase,
                getUserAgent());
        return response.getChargeDefinitions();
    }

    public Boolean migrateToDynamicPricing(String ratePlanNumber) {
        Validations.requireNotEmpty(ratePlanNumber, "ratePlanNumber required");
        return "ok".equalsIgnoreCase(httpSupport.call(
                HttpMethod.POST,
                Catalog.getPathPrefix() + "/charge-definitions/migrations",
                Optional.of(MigrateToAbpV2Request.builder()
                        .productRatePlanNumber(ratePlanNumber)
                        .build()),
                new TypeReference<>() {},
                ErrorMappers::appErrorMapper,
                LowerCamelCase,
                getUserAgent()));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy