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.regex.Matcher;
020import java.util.regex.Pattern;
021
022import javax.xml.bind.annotation.XmlElement;
023import javax.xml.bind.annotation.XmlElementWrapper;
024import javax.xml.bind.annotation.XmlRootElement;
025import javax.xml.bind.annotation.XmlTransient;
026
027import org.joda.time.DateTime;
028
029@XmlRootElement(name = "account")
030public class Account extends RecurlyObject {
031
032    @XmlTransient
033    public static final String ACCOUNT_RESOURCE = "/accounts";
034
035    @XmlTransient
036    public static final Pattern ACCOUNT_CODE_PATTERN = Pattern.compile(ACCOUNT_RESOURCE + "/(.+)$");
037
038    @XmlElement(name = "address")
039    private Address address;
040
041    @XmlElementWrapper(name = "adjustments")
042    @XmlElement(name = "adjustment")
043    private Adjustments adjustments;
044
045    @XmlElementWrapper(name = "invoices")
046    @XmlElement(name = "invoice")
047    private Invoices invoices;
048
049    @XmlElementWrapper(name = "subscriptions")
050    @XmlElement(name = "subscription")
051    private Subscriptions subscriptions;
052
053    @XmlElementWrapper(name = "transactions")
054    @XmlElement(name = "transaction")
055    private Transactions transactions;
056
057    @XmlElement(name = "account_code")
058    private String accountCode;
059
060    @XmlElement(name = "state")
061    private String state;
062
063    @XmlElement(name = "username")
064    private String username;
065
066    @XmlElement(name = "email")
067    private String email;
068
069    @XmlElement(name = "first_name")
070    private String firstName;
071
072    @XmlElement(name = "last_name")
073    private String lastName;
074
075    @XmlElement(name = "company_name")
076    private String companyName;
077
078    @XmlElement(name = "accept_language")
079    private String acceptLanguage;
080
081    @XmlElement(name = "hosted_login_token")
082    private String hostedLoginToken;
083
084    @XmlElement(name = "created_at")
085    private DateTime createdAt;
086
087    @XmlElement(name = "billing_info")
088    private BillingInfo billingInfo;
089
090    @Override
091    public void setHref(final Object href) {
092        super.setHref(href);
093
094        // If there was an href try to parse out the account code since
095        // Recurly doesn't currently provide it elsewhere.
096        if (this.href != null) {
097            final Matcher m = ACCOUNT_CODE_PATTERN.matcher(this.href);
098            if (m.find()) {
099                setAccountCode(m.group(1));
100            }
101        }
102    }
103
104    public Address getAddress() {
105        return address;
106    }
107
108    public void setAddress(final Address address) {
109        this.address = address;
110    }
111
112    public Adjustments getAdjustments() {
113        return adjustments;
114    }
115
116    public void setAdjustments(final Adjustments adjustments) {
117        this.adjustments = adjustments;
118    }
119
120    public Invoices getInvoices() {
121        return invoices;
122    }
123
124    public void setInvoices(final Invoices invoices) {
125        this.invoices = invoices;
126    }
127
128    public Subscriptions getSubscriptions() {
129        return subscriptions;
130    }
131
132    public void setSubscriptions(final Subscriptions subscriptions) {
133        this.subscriptions = subscriptions;
134    }
135
136    public Transactions getTransactions() {
137        return transactions;
138    }
139
140    public void setTransactions(final Transactions transactions) {
141        this.transactions = transactions;
142    }
143
144    public String getAccountCode() {
145        return accountCode;
146    }
147
148    public void setAccountCode(final Object accountCode) {
149        this.accountCode = stringOrNull(accountCode);
150    }
151
152    public String getState() {
153        return state;
154    }
155
156    public void setState(final Object state) {
157        this.state = stringOrNull(state);
158    }
159
160    public String getUsername() {
161        return username;
162    }
163
164    public void setUsername(final Object username) {
165        this.username = stringOrNull(username);
166    }
167
168    public String getEmail() {
169        return email;
170    }
171
172    public void setEmail(final Object email) {
173        this.email = stringOrNull(email);
174    }
175
176    public String getFirstName() {
177        return firstName;
178    }
179
180    public void setFirstName(final Object firstName) {
181        this.firstName = stringOrNull(firstName);
182    }
183
184    public String getLastName() {
185        return lastName;
186    }
187
188    public void setLastName(final Object lastName) {
189        this.lastName = stringOrNull(lastName);
190    }
191
192    public String getCompanyName() {
193        return companyName;
194    }
195
196    public void setCompanyName(final Object companyName) {
197        this.companyName = stringOrNull(companyName);
198    }
199
200    public String getAcceptLanguage() {
201        return acceptLanguage;
202    }
203
204    public void setAcceptLanguage(final Object acceptLanguage) {
205        this.acceptLanguage = stringOrNull(acceptLanguage);
206    }
207
208    public String getHostedLoginToken() {
209        return hostedLoginToken;
210    }
211
212    public void setHostedLoginToken(final Object hostedLoginToken) {
213        this.hostedLoginToken = stringOrNull(hostedLoginToken);
214    }
215
216    public DateTime getCreatedAt() {
217        return createdAt;
218    }
219
220    public void setCreatedAt(final Object createdAt) {
221        this.createdAt = dateTimeOrNull(createdAt);
222    }
223
224    public BillingInfo getBillingInfo() {
225        return billingInfo;
226    }
227
228    public void setBillingInfo(final BillingInfo billingInfo) {
229        this.billingInfo = billingInfo;
230    }
231
232    @Override
233    public String toString() {
234        final StringBuilder sb = new StringBuilder("Account{");
235        sb.append("address=").append(address);
236        sb.append(", href=").append(href);
237        sb.append(", adjustments=").append(adjustments);
238        sb.append(", invoices=").append(invoices);
239        sb.append(", subscriptions=").append(subscriptions);
240        sb.append(", transactions=").append(transactions);
241        sb.append(", accountCode='").append(accountCode).append('\'');
242        sb.append(", state='").append(state).append('\'');
243        sb.append(", username='").append(username).append('\'');
244        sb.append(", email='").append(email).append('\'');
245        sb.append(", firstName='").append(firstName).append('\'');
246        sb.append(", lastName='").append(lastName).append('\'');
247        sb.append(", companyName='").append(companyName).append('\'');
248        sb.append(", acceptLanguage='").append(acceptLanguage).append('\'');
249        sb.append(", hostedLoginToken='").append(hostedLoginToken).append('\'');
250        sb.append(", createdAt=").append(createdAt);
251        sb.append(", billingInfo=").append(billingInfo);
252        sb.append('}');
253        return sb.toString();
254    }
255
256    @Override
257    public boolean equals(final Object o) {
258        if (this == o) {
259            return true;
260        }
261        if (o == null || getClass() != o.getClass()) {
262            return false;
263        }
264
265        final Account account = (Account) o;
266
267        if (acceptLanguage != null ? !acceptLanguage.equals(account.acceptLanguage) : account.acceptLanguage != null) {
268            return false;
269        }
270        if (accountCode != null ? !accountCode.equals(account.accountCode) : account.accountCode != null) {
271            return false;
272        }
273        if (address != null ? !address.equals(account.address) : account.address != null) {
274            return false;
275        }
276        if (adjustments != null ? !adjustments.equals(account.adjustments) : account.adjustments != null) {
277            return false;
278        }
279        if (billingInfo != null ? !billingInfo.equals(account.billingInfo) : account.billingInfo != null) {
280            return false;
281        }
282        if (companyName != null ? !companyName.equals(account.companyName) : account.companyName != null) {
283            return false;
284        }
285        if (createdAt != null ? !createdAt.equals(account.createdAt) : account.createdAt != null) {
286            return false;
287        }
288        if (email != null ? !email.equals(account.email) : account.email != null) {
289            return false;
290        }
291        if (firstName != null ? !firstName.equals(account.firstName) : account.firstName != null) {
292            return false;
293        }
294        if (href != null ? !href.equals(account.href) : account.href != null) {
295            return false;
296        }
297        if (hostedLoginToken != null ? !hostedLoginToken.equals(account.hostedLoginToken) : account.hostedLoginToken != null) {
298            return false;
299        }
300        if (invoices != null ? !invoices.equals(account.invoices) : account.invoices != null) {
301            return false;
302        }
303        if (lastName != null ? !lastName.equals(account.lastName) : account.lastName != null) {
304            return false;
305        }
306        if (state != null ? !state.equals(account.state) : account.state != null) {
307            return false;
308        }
309        if (subscriptions != null ? !subscriptions.equals(account.subscriptions) : account.subscriptions != null) {
310            return false;
311        }
312        if (transactions != null ? !transactions.equals(account.transactions) : account.transactions != null) {
313            return false;
314        }
315        if (username != null ? !username.equals(account.username) : account.username != null) {
316            return false;
317        }
318
319        return true;
320    }
321
322    @Override
323    public int hashCode() {
324        int result = address != null ? address.hashCode() : 0;
325        result = 31 * result + (href != null ? href.hashCode() : 0);
326        result = 31 * result + (adjustments != null ? adjustments.hashCode() : 0);
327        result = 31 * result + (invoices != null ? invoices.hashCode() : 0);
328        result = 31 * result + (subscriptions != null ? subscriptions.hashCode() : 0);
329        result = 31 * result + (transactions != null ? transactions.hashCode() : 0);
330        result = 31 * result + (accountCode != null ? accountCode.hashCode() : 0);
331        result = 31 * result + (state != null ? state.hashCode() : 0);
332        result = 31 * result + (username != null ? username.hashCode() : 0);
333        result = 31 * result + (email != null ? email.hashCode() : 0);
334        result = 31 * result + (firstName != null ? firstName.hashCode() : 0);
335        result = 31 * result + (lastName != null ? lastName.hashCode() : 0);
336        result = 31 * result + (companyName != null ? companyName.hashCode() : 0);
337        result = 31 * result + (acceptLanguage != null ? acceptLanguage.hashCode() : 0);
338        result = 31 * result + (hostedLoginToken != null ? hostedLoginToken.hashCode() : 0);
339        result = 31 * result + (createdAt != null ? createdAt.hashCode() : 0);
340        result = 31 * result + (billingInfo != null ? billingInfo.hashCode() : 0);
341        return result;
342    }
343}