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 javax.xml.bind.annotation.XmlElement;
020import javax.xml.bind.annotation.XmlRootElement;
021
022import org.joda.time.DateTime;
023
024@XmlRootElement(name = "subscription")
025public class Subscription extends AbstractSubscription {
026
027    @XmlElement(name = "account")
028    private Account account;
029
030    @XmlElement(name = "plan")
031    private Plan plan;
032
033    @XmlElement(name = "uuid")
034    private String uuid;
035
036    @XmlElement(name = "state", required = false)
037    private String state;
038
039    @XmlElement(name = "currency")
040    private String currency;
041
042    @XmlElement(name = "activated_at")
043    private DateTime activatedAt;
044
045    @XmlElement(name = "canceled_at")
046    private DateTime canceledAt;
047
048    @XmlElement(name = "expires_at")
049    private DateTime expiresAt;
050
051    @XmlElement(name = "current_period_started_at")
052    private DateTime currentPeriodStartedAt;
053
054    @XmlElement(name = "current_period_ends_at")
055    private DateTime currentPeriodEndsAt;
056
057    @XmlElement(name = "trial_started_at")
058    private DateTime trialStartedAt;
059
060    @XmlElement(name = "trial_ends_at")
061    private DateTime trialEndsAt;
062
063    @XmlElement(name = "pending_subscription")
064    private Subscription pendingSubscription;
065
066    @XmlElement(name = "starts_at")
067    private DateTime startsAt;
068
069    @XmlElement(name = "collection_method")
070    private String collectionMethod;
071
072    @XmlElement(name = "net_terms")
073    private Integer netTerms;
074
075    //Purchase Order Number
076    @XmlElement(name = "po_number")
077    private String poNumber;
078
079    public Account getAccount() {
080        if (account != null && account.getCreatedAt() == null) {
081            account = fetch(account, Account.class);
082        }
083        return account;
084    }
085
086    public void setAccount(final Account account) {
087        this.account = account;
088    }
089
090    public Plan getPlan() {
091        return plan;
092    }
093
094    public void setPlan(final Plan plan) {
095        this.plan = plan;
096    }
097
098    public String getUuid() {
099        return uuid;
100    }
101
102    public void setUuid(final Object uuid) {
103        this.uuid = stringOrNull(uuid);
104    }
105
106    public String getState() {
107        return state;
108    }
109
110    public void setState(final Object state) {
111        this.state = stringOrNull(state);
112    }
113
114    public String getCurrency() {
115        return currency;
116    }
117
118    public void setCurrency(final Object currency) {
119        this.currency = stringOrNull(currency);
120    }
121
122    public DateTime getActivatedAt() {
123        return activatedAt;
124    }
125
126    public void setActivatedAt(final Object activatedAt) {
127        this.activatedAt = dateTimeOrNull(activatedAt);
128    }
129
130    public DateTime getCanceledAt() {
131        return canceledAt;
132    }
133
134    public void setCanceledAt(final Object canceledAt) {
135        this.canceledAt = dateTimeOrNull(canceledAt);
136    }
137
138    public DateTime getExpiresAt() {
139        return expiresAt;
140    }
141
142    public void setExpiresAt(final Object expiresAt) {
143        this.expiresAt = dateTimeOrNull(expiresAt);
144    }
145
146    public DateTime getCurrentPeriodStartedAt() {
147        return currentPeriodStartedAt;
148    }
149
150    public void setCurrentPeriodStartedAt(final Object currentPeriodStartedAt) {
151        this.currentPeriodStartedAt = dateTimeOrNull(currentPeriodStartedAt);
152    }
153
154    public DateTime getCurrentPeriodEndsAt() {
155        return currentPeriodEndsAt;
156    }
157
158    public void setCurrentPeriodEndsAt(final Object currentPeriodEndsAt) {
159        this.currentPeriodEndsAt = dateTimeOrNull(currentPeriodEndsAt);
160    }
161
162    public DateTime getTrialStartedAt() {
163        return trialStartedAt;
164    }
165
166    public void setTrialStartedAt(final Object trialStartedAt) {
167        this.trialStartedAt = dateTimeOrNull(trialStartedAt);
168    }
169
170    public DateTime getTrialEndsAt() {
171        return trialEndsAt;
172    }
173
174    public void setTrialEndsAt(final Object trialEndsAt) {
175        this.trialEndsAt = dateTimeOrNull(trialEndsAt);
176    }
177
178    public Subscription getPendingSubscription() {
179        return pendingSubscription;
180    }
181
182    public void setPendingSubscription(final Subscription pendingSubscription) {
183        this.pendingSubscription = pendingSubscription;
184    }
185
186    public DateTime getStartsAt() {
187        return startsAt;
188    }
189
190    public void setStartsAt(final Object startsAt) {
191        this.startsAt = dateTimeOrNull(startsAt);
192    }
193
194
195    public String getCollectionMethod() {
196        return collectionMethod;
197    }
198
199    public void setCollectionMethod(Object collectionMethod) {
200        this.collectionMethod = stringOrNull(collectionMethod);
201    }
202
203    public Integer getNetTerms() {
204        return netTerms;
205    }
206
207    public void setNetTerms(final Object netTerms) {
208        this.netTerms = integerOrNull(netTerms);
209    }
210
211    public String getPoNumber() {
212        return poNumber;
213    }
214
215    public void setPoNumber(Object poNumber) {
216        this.poNumber = stringOrNull(poNumber);
217    }
218
219
220    @Override
221    public String toString() {
222        final StringBuilder sb = new StringBuilder();
223        sb.append("Subscription");
224        sb.append("{account=").append(account);
225        sb.append(", plan=").append(plan);
226        sb.append(", uuid='").append(uuid).append('\'');
227        sb.append(", state='").append(state).append('\'');
228        sb.append(", unitAmountInCents=").append(unitAmountInCents);
229        sb.append(", currency='").append(currency).append('\'');
230        sb.append(", quantity=").append(quantity);
231        sb.append(", activatedAt=").append(activatedAt);
232        sb.append(", canceledAt=").append(canceledAt);
233        sb.append(", expiresAt=").append(expiresAt);
234        sb.append(", currentPeriodStartedAt=").append(currentPeriodStartedAt);
235        sb.append(", currentPeriodEndsAt=").append(currentPeriodEndsAt);
236        sb.append(", trialStartedAt=").append(trialStartedAt);
237        sb.append(", trialEndsAt=").append(trialEndsAt);
238        sb.append(", startsAt=").append(startsAt);
239        sb.append(", addOns=").append(addOns);
240        sb.append(", pendingSubscription=").append(pendingSubscription);
241        sb.append('}');
242        return sb.toString();
243    }
244
245    @Override
246    public boolean equals(final Object o) {
247        if (this == o) {
248            return true;
249        }
250        if (o == null || getClass() != o.getClass()) {
251            return false;
252        }
253
254        final Subscription that = (Subscription) o;
255
256        if (account != null ? !account.equals(that.account) : that.account != null) {
257            return false;
258        }
259        if (activatedAt != null ? !activatedAt.equals(that.activatedAt) : that.activatedAt != null) {
260            return false;
261        }
262        if (addOns != null ? !addOns.equals(that.addOns) : that.addOns != null) {
263            return false;
264        }
265        if (canceledAt != null ? !canceledAt.equals(that.canceledAt) : that.canceledAt != null) {
266            return false;
267        }
268        if (currency != null ? !currency.equals(that.currency) : that.currency != null) {
269            return false;
270        }
271        if (currentPeriodEndsAt != null ? !currentPeriodEndsAt.equals(that.currentPeriodEndsAt) : that.currentPeriodEndsAt != null) {
272            return false;
273        }
274        if (currentPeriodStartedAt != null ? !currentPeriodStartedAt.equals(that.currentPeriodStartedAt) : that.currentPeriodStartedAt != null) {
275            return false;
276        }
277        if (expiresAt != null ? !expiresAt.equals(that.expiresAt) : that.expiresAt != null) {
278            return false;
279        }
280        if (plan != null ? !plan.equals(that.plan) : that.plan != null) {
281            return false;
282        }
283        if (quantity != null ? !quantity.equals(that.quantity) : that.quantity != null) {
284            return false;
285        }
286
287        if (state != null ? !state.equals(that.state) : that.state != null) {
288            return false;
289        }
290        if (trialEndsAt != null ? !trialEndsAt.equals(that.trialEndsAt) : that.trialEndsAt != null) {
291            return false;
292        }
293        if (trialStartedAt != null ? !trialStartedAt.equals(that.trialStartedAt) : that.trialStartedAt != null) {
294            return false;
295        }
296        if (unitAmountInCents != null ? !unitAmountInCents.equals(that.unitAmountInCents) : that.unitAmountInCents != null) {
297            return false;
298        }
299        if (uuid != null ? !uuid.equals(that.uuid) : that.uuid != null) {
300            return false;
301        }
302        if (startsAt != null ? !startsAt.equals(that.startsAt) : that.startsAt != null) {
303            return false;
304        }
305        if (pendingSubscription != null ? !pendingSubscription.equals(that.pendingSubscription) : that.pendingSubscription != null) {
306            return false;
307        }
308        if (collectionMethod != null ? !collectionMethod.equals(that.collectionMethod) : that.collectionMethod != null) {
309            return false;
310        }
311
312        if (netTerms != null ? !netTerms.equals(that.netTerms) : that.netTerms != null) {
313            return false;
314        }
315
316        if (poNumber != null ? !poNumber.equals(that.poNumber) : that.poNumber != null) {
317            return false;
318        }
319
320        return true;
321    }
322
323    @Override
324    public int hashCode() {
325        int result = account != null ? account.hashCode() : 0;
326        result = 31 * result + (plan != null ? plan.hashCode() : 0);
327        result = 31 * result + (uuid != null ? uuid.hashCode() : 0);
328        //result = 31 * result + (state != null ? state.hashCode() : 0);
329        result = 31 * result + (unitAmountInCents != null ? unitAmountInCents.hashCode() : 0);
330        result = 31 * result + (currency != null ? currency.hashCode() : 0);
331        result = 31 * result + (quantity != null ? quantity.hashCode() : 0);
332        result = 31 * result + (activatedAt != null ? activatedAt.hashCode() : 0);
333        result = 31 * result + (canceledAt != null ? canceledAt.hashCode() : 0);
334        result = 31 * result + (expiresAt != null ? expiresAt.hashCode() : 0);
335        result = 31 * result + (currentPeriodStartedAt != null ? currentPeriodStartedAt.hashCode() : 0);
336        result = 31 * result + (currentPeriodEndsAt != null ? currentPeriodEndsAt.hashCode() : 0);
337        result = 31 * result + (trialStartedAt != null ? trialStartedAt.hashCode() : 0);
338        result = 31 * result + (trialEndsAt != null ? trialEndsAt.hashCode() : 0);
339        result = 31 * result + (addOns != null ? addOns.hashCode() : 0);
340        result = 31 * result + (pendingSubscription != null ? pendingSubscription.hashCode() : 0);
341        result = 31 * result + (startsAt != null ? startsAt.hashCode() : 0);
342        result = 31 * result + (collectionMethod != null ? collectionMethod.hashCode() : 0);
343        result = 31 * result + (netTerms != null ? netTerms.hashCode() : 0);
344        result = 31 * result + (poNumber != null ? poNumber.hashCode() : 0);
345        return result;
346    }
347}