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

io.github.isagroup.services.serializer.PricingManagerSerializer Maven / Gradle / Ivy

package io.github.isagroup.services.serializer;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;

import io.github.isagroup.exceptions.SerializerException;
import io.github.isagroup.models.AddOn;
import io.github.isagroup.models.Feature;
import io.github.isagroup.models.Plan;
import io.github.isagroup.models.PricingManager;
import io.github.isagroup.models.UsageLimit;
import io.github.isagroup.services.updaters.Version;

public class PricingManagerSerializer {

    private Map serializedPricingManager;

    public PricingManagerSerializer() {
        this.serializedPricingManager = new LinkedHashMap<>();
    }

    public Map serialize(PricingManager pricingManager) throws SerializerException {

        serializedPricingManager.put("version", Version.LATEST.toString());
        serializedPricingManager.put("saasName", pricingManager.getSaasName());
        serializedPricingManager.put("createdAt", pricingManager.getCreatedAt().toString());
        serializedPricingManager.put("url", pricingManager.getUrl());
        serializedPricingManager.put("tags", pricingManager.getTags());
        serializedPricingManager.put("billing", pricingManager.getBilling());
        serializedPricingManager.put("variables", pricingManager.getVariables());
        serializedPricingManager.put("currency", pricingManager.getCurrency());

        if (pricingManager.getFeatures() == null) {
            throw new SerializerException("Features are null. Filling the pricing with features is mandatory.");
        }

        if (pricingManager.getPlans() == null && pricingManager.getAddOns() == null) {
            throw new SerializerException(
                    "Plans and AddOns are null. You have to set at least one of them.");
        }

        serializedPricingManager.put("features", serializeFeatures(pricingManager));
        serializedPricingManager.put("usageLimits", serializeUsageLimits(pricingManager));
        serializedPricingManager.put("plans", serializePlans(pricingManager));
        serializedPricingManager.put("addOns", serializeAddOns(pricingManager).orElse(null));

        return serializedPricingManager;
    }

    private Map serializeFeatures(PricingManager pricingManager) {

        Map serializedFeatures = new LinkedHashMap<>();
        for (Feature feature : pricingManager.getFeatures().values()) {
            serializedFeatures.put(feature.getName(), feature.serializeFeature());

        }
        return serializedFeatures;
    }

    private Map serializeUsageLimits(PricingManager pricingManager) {
        Map serializedUsageLimits = new LinkedHashMap<>();

        if (pricingManager.getUsageLimits() == null) {
            return null;
        }

        for (UsageLimit usageLimit : pricingManager.getUsageLimits().values()) {
            serializedUsageLimits.put(usageLimit.getName(), usageLimit.serialize());

        }

        return serializedUsageLimits;

    }

    private Map serializePlans(PricingManager pricingManager) {
        Map serializedPlans = new LinkedHashMap<>();
        for (Plan plan : pricingManager.getPlans().values()) {
            serializedPlans.put(plan.getName(), plan.serializePlan());
        }
        return serializedPlans;
    }

    public Optional> serializeAddOns(PricingManager pricingManager) {

        if (pricingManager.getAddOns() == null) {
            return Optional.empty();
        }

        Map serializedAddOns = new LinkedHashMap<>();
        for (AddOn addOn : pricingManager.getAddOns().values()) {
            serializedAddOns.put(addOn.getName(), addOn.serializeAddOn());
        }
        return Optional.of(serializedAddOns);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy