
org.killbill.billing.jaxrs.json.AccountTimelineJson Maven / Gradle / Ivy
/*
* Copyright 2010-2013 Ning, Inc.
* 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.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.killbill.billing.account.api.Account;
import org.killbill.billing.catalog.api.CatalogApiException;
import org.killbill.billing.entitlement.api.SubscriptionBundle;
import org.killbill.billing.invoice.api.Invoice;
import org.killbill.billing.invoice.api.InvoiceItem;
import org.killbill.billing.invoice.api.InvoiceItemType;
import org.killbill.billing.invoice.api.InvoicePayment;
import org.killbill.billing.jaxrs.resources.JaxRsResourceBase;
import org.killbill.billing.payment.api.Payment;
import org.killbill.billing.util.audit.AccountAuditLogs;
import org.killbill.billing.util.audit.AuditLog;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
@ApiModel(value="AccountTimeline")
public class AccountTimelineJson {
private final AccountJson account;
private final List bundles;
private final List invoices;
private final List payments;
@JsonCreator
public AccountTimelineJson(@JsonProperty("account") final AccountJson account,
@JsonProperty("bundles") final List bundles,
@JsonProperty("invoices") final List invoices,
@JsonProperty("payments") final List payments) {
this.account = account;
this.bundles = bundles;
this.invoices = invoices;
this.payments = payments;
}
public AccountTimelineJson(final Account account,
final List invoices,
final List payments,
final List invoicePayments,
final List bundles,
final AccountAuditLogs accountAuditLogs) throws CatalogApiException {
this.account = new AccountJson(account, null, null, accountAuditLogs);
this.bundles = new LinkedList();
for (final SubscriptionBundle bundle : bundles) {
final BundleJson jsonWithSubscriptions = new BundleJson(bundle, account.getCurrency(), accountAuditLogs);
this.bundles.add(jsonWithSubscriptions);
}
this.invoices = new LinkedList();
// Extract the credits from the invoices first
final List credits = new ArrayList();
for (final Invoice invoice : invoices) {
for (final InvoiceItem invoiceItem : invoice.getInvoiceItems()) {
if (InvoiceItemType.CREDIT_ADJ.equals(invoiceItem.getInvoiceItemType())) {
final List auditLogs = accountAuditLogs.getAuditLogsForInvoiceItem(invoiceItem.getId());
credits.add(new InvoiceItemJson(invoiceItem, Collections.emptyList(), auditLogs));
}
}
}
// Create now the invoice json objects
for (final Invoice invoice : invoices) {
final List auditLogs = accountAuditLogs.getAuditLogsForInvoice(invoice.getId());
this.invoices.add(new InvoiceJson(invoice,
getBundleExternalKey(invoice, bundles),
credits,
auditLogs));
}
this.payments = new LinkedList();
for (final Payment payment : payments) {
final UUID invoiceId = JaxRsResourceBase.getInvoiceId(invoicePayments, payment);
this.payments.add(new InvoicePaymentJson(payment, invoiceId, accountAuditLogs));
}
}
public AccountJson getAccount() {
return account;
}
public List getBundles() {
return bundles;
}
public List getInvoices() {
return invoices;
}
public List getPayments() {
return payments;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("AccountTimelineJson");
sb.append("{account=").append(account);
sb.append(", bundles=").append(bundles);
sb.append(", invoices=").append(invoices);
sb.append(", payments=").append(payments);
sb.append('}');
return sb.toString();
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final AccountTimelineJson that = (AccountTimelineJson) o;
if (account != null ? !account.equals(that.account) : that.account != null) {
return false;
}
if (bundles != null ? !bundles.equals(that.bundles) : that.bundles != null) {
return false;
}
if (invoices != null ? !invoices.equals(that.invoices) : that.invoices != null) {
return false;
}
if (payments != null ? !payments.equals(that.payments) : that.payments != null) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = account != null ? account.hashCode() : 0;
result = 31 * result + (bundles != null ? bundles.hashCode() : 0);
result = 31 * result + (invoices != null ? invoices.hashCode() : 0);
result = 31 * result + (payments != null ? payments.hashCode() : 0);
return result;
}
private String getBundleExternalKey(final Invoice invoice, final List bundles) {
final Set b = new HashSet();
for (final InvoiceItem cur : invoice.getInvoiceItems()) {
b.add(cur.getBundleId());
}
boolean first = true;
final StringBuilder tmp = new StringBuilder();
for (final UUID cur : b) {
for (final SubscriptionBundle bt : bundles) {
if (bt.getId().equals(cur)) {
if (!first) {
tmp.append(",");
}
tmp.append(bt.getExternalKey());
first = false;
break;
}
}
}
return tmp.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy