SubscriptionUsageRecordJson.java

124 lines | 4.138 kB Blame History Raw Download
/*
 * Copyright 2014 Groupon, Inc
 * Copyright 2014 The Billing Project, LLC
 *
 * The Billing Project licenses this file to you under the Apache License, version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at:
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package org.killbill.billing.jaxrs.json;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.annotation.Nullable;

import org.joda.time.LocalDate;
import org.killbill.billing.usage.api.SubscriptionUsageRecord;
import org.killbill.billing.usage.api.UnitUsageRecord;
import org.killbill.billing.usage.api.UsageRecord;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;

public class SubscriptionUsageRecordJson {

    private final String subscriptionId;
    private final List<UnitUsageRecordJson> unitUsageRecords;

    @JsonCreator
    public SubscriptionUsageRecordJson(@JsonProperty("subscriptionId") final String subscriptionId,
                                   @JsonProperty("unitUsageRecords") final List<UnitUsageRecordJson> unitUsageRecords) {
        this.subscriptionId = subscriptionId;
        this.unitUsageRecords = unitUsageRecords;
    }

    public String getSubscriptionId() {
        return subscriptionId;
    }

    public List<UnitUsageRecordJson> getUnitUsageRecords() {
        return unitUsageRecords;
    }

    public static class UnitUsageRecordJson {

        private final String unitType;
        private final List<UsageRecordJson> usageRecords;

        @JsonCreator
        public UnitUsageRecordJson(@JsonProperty("unitType") final String unitType,
                                    @JsonProperty("usageRecords") final List<UsageRecordJson> usageRecords) {
            this.unitType = unitType;
            this.usageRecords = usageRecords;
        }

        public String getUnitType() {
            return unitType;
        }

        public List<UsageRecordJson> getUsageRecords() {
            return usageRecords;
        }

        public UnitUsageRecord toUnitUsageRecord() {
            final List<UsageRecord> tmp = ImmutableList.copyOf(Iterables.transform(usageRecords, new Function<UsageRecordJson, UsageRecord>() {
                @Override
                public UsageRecord apply(final UsageRecordJson input) {
                    return input.toUsageRecord();
                }
            }));
            return new UnitUsageRecord(unitType, tmp);
        }
    }

    public static class UsageRecordJson {

        private final LocalDate recordDate;
        private final Long amount;

        @JsonCreator
        public UsageRecordJson(@JsonProperty("recordDate") final LocalDate recordDate,
                               @JsonProperty("amount") final Long amount) {
            this.recordDate = recordDate;
            this.amount = amount;
        }

        public LocalDate getRecordDate() {
            return recordDate;
        }

        public Long getAmount() {
            return amount;
        }

        public UsageRecord toUsageRecord() {
            return new UsageRecord(recordDate, amount);
        }
    }

    public SubscriptionUsageRecord toSubscriptionUsageRecord() {
        final List<UnitUsageRecord> tmp = ImmutableList.copyOf(Iterables.transform(unitUsageRecords, new Function<UnitUsageRecordJson, UnitUsageRecord>() {
            @Override
            public UnitUsageRecord apply(final UnitUsageRecordJson input) {
                return input.toUnitUsageRecord();
            }
        }));
        final SubscriptionUsageRecord result = new SubscriptionUsageRecord(UUID.fromString(subscriptionId), tmp);
        return result;
    }
}