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