001/*
002 * Copyright 2010-2014 Ning, Inc.
003 * Copyright 2014-2015 The Billing Project, LLC
004 *
005 * The Billing Project licenses this file to you under the Apache License, version 2.0
006 * (the "License"); you may not use this file except in compliance with the
007 * License.  You may obtain a copy of the License at:
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
014 * License for the specific language governing permissions and limitations
015 * under the License.
016 */
017
018package com.ning.billing.recurly.model;
019
020import java.util.ArrayList;
021
022import javax.xml.bind.annotation.XmlTransient;
023
024import com.ning.billing.recurly.RecurlyClient;
025
026import com.fasterxml.jackson.annotation.JsonFormat;
027import com.fasterxml.jackson.annotation.JsonIgnore;
028import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
029import com.fasterxml.jackson.annotation.JsonSetter;
030
031/**
032 * Container for a collection of objects (e.g. accounts, coupons, plans, ...)
033 */
034@JsonIgnoreProperties(ignoreUnknown = true)
035@JsonFormat(shape = JsonFormat.Shape.OBJECT)
036public abstract class RecurlyObjects<T extends RecurlyObject> extends ArrayList<T> {
037
038    // See https://github.com/FasterXML/jackson-dataformat-xml/issues/76 and https://github.com/killbilling/recurly-java-library/issues/21
039    @JsonSetter
040    public void setRecurlyObject(final T value) {
041        add(value);
042    }
043
044    @XmlTransient
045    private RecurlyClient recurlyClient;
046
047    @XmlTransient
048    private String startUrl;
049
050    @XmlTransient
051    private String nextUrl;
052
053
054    @JsonIgnore
055    <U extends RecurlyObjects> U getStart(final Class<U> clazz) {
056        if (recurlyClient == null || startUrl == null) {
057            return null;
058        }
059        return recurlyClient.doGETWithFullURL(clazz, startUrl);
060    }
061
062    public abstract RecurlyObjects<T> getStart();
063
064    @JsonIgnore
065    <U extends RecurlyObjects> U getNext(final Class<U> clazz) {
066        if (recurlyClient == null || nextUrl == null) {
067            return null;
068        }
069        return recurlyClient.doGETWithFullURL(clazz, nextUrl);
070    }
071
072    public abstract RecurlyObjects<T> getNext();
073
074    @JsonIgnore
075    public void setRecurlyClient(final RecurlyClient recurlyClient) {
076        this.recurlyClient = recurlyClient;
077    }
078
079    @JsonIgnore
080    public String getStartUrl() {
081        return startUrl;
082    }
083
084    @JsonIgnore
085    public void setStartUrl(final String startUrl) {
086        this.startUrl = startUrl;
087    }
088
089    @JsonIgnore
090    public String getNextUrl() {
091        return nextUrl;
092    }
093
094    @JsonIgnore
095    public void setNextUrl(final String nextUrl) {
096        this.nextUrl = nextUrl;
097    }
098
099    @Override
100    @JsonIgnore // To avoid printing an <empty> tag in the XML
101    public boolean isEmpty() {
102        return super.isEmpty();
103    }
104}