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; 021import javax.xml.bind.annotation.XmlTransient; 022 023import org.joda.time.DateTime; 024 025/** 026 * Class that represents the Concept of a Coupon within the Recurly API. 027 */ 028@XmlRootElement(name = "coupon") 029public class Coupon extends RecurlyObject { 030 031 @XmlTransient 032 public static final String COUPON_RESOURCE = "/coupons"; 033 034 @XmlElement(name = "name") 035 private String name; 036 037 @XmlElement(name = "coupon_code") 038 private String couponCode; 039 040 /** 041 * Last date to redeem the coupon, defaults to no date 042 */ 043 @XmlElement(name = "redeem_by_date") 044 private DateTime redeemByDate; 045 046 /** 047 * Number of months after redemption that the coupon is valid, defaults to no date 048 */ 049 @XmlElement(name = "applies_for_months") 050 private Integer appliesForMonths; 051 052 /** 053 * Maximum number of accounts that may use the coupon before it can no longer be redeemed 054 */ 055 @XmlElement(name = "max_redemptions") 056 private Integer maxRedemptions; 057 058 /** 059 * The coupon is valid for all plans if true, defaults to true 060 */ 061 @XmlElement(name = "applies_to_all_plans") 062 private Boolean appliesToAllPlans; 063 064 /** 065 * If true, the coupon applies to the first invoice only 066 */ 067 @XmlElement(name = "single_use") 068 private Boolean singleUse; 069 070 /** 071 * "percent" or "dollars" 072 */ 073 @XmlElement(name = "discount_type") 074 private String discountType; 075 076 /** 077 * Discount percentage if discount_type is "percent" 078 */ 079 @XmlElement(name = "discount_percent") 080 private Integer discountPercent; 081 082 @XmlElement(name = "discount_in_cents") 083 private Integer discountInCents; 084 085 @XmlElement(name = "state") 086 private String state; 087 088 public String getState() { 089 return state; 090 } 091 092 public void setState(final Object state) { 093 this.state = stringOrNull(state); 094 } 095 096 /** 097 * Gets the name of the {@link Coupon} 098 * 099 * @return The {@link Coupon} name 100 */ 101 public String getName() { 102 return name; 103 } 104 105 /** 106 * Sets the name of the {@link Coupon} 107 * 108 * @param name The Name that is to be given to the {@link Coupon} 109 */ 110 public void setName(final Object name) { 111 this.name = stringOrNull(name); 112 } 113 114 /** 115 * Gets the coupon code for a {@link Coupon} 116 * 117 * @return The coupon code for the {@link Coupon} 118 */ 119 public String getCouponCode() { 120 return couponCode; 121 } 122 123 /** 124 * Sets the coupon code for the {@link Coupon} 125 * 126 * @param couponCode The coupon code 127 */ 128 public void setCouponCode(final Object couponCode) { 129 this.couponCode = stringOrNull(couponCode); 130 } 131 132 /** 133 * Sets the discount type for a {@link Coupon} 134 * 135 * @param discountType A String of: 'percent'; 'dollars'; 136 */ 137 public void setDiscountType(final Object discountType) { 138 this.discountType = stringOrNull(discountType); 139 } 140 141 /** 142 * Gets the discount type associated with the {@link Coupon} 143 * 144 * @return A String defining the discount type: 'percent' or 'dollars'. 145 */ 146 public String getDiscountType() { 147 return discountType; 148 } 149 150 /** 151 * Gets the percentage discount for a coupon 152 * 153 * @return The percentage 154 */ 155 public Integer getDiscountPercent() { 156 return discountPercent; 157 } 158 159 public void setDiscountPercent(final Object discountPercent) { 160 this.discountPercent = integerOrNull(discountPercent); 161 } 162 163 public DateTime getRedeemByDate() { 164 return redeemByDate; 165 } 166 167 public void setRedeemByDate(final Object redeemByDate) { 168 this.redeemByDate = dateTimeOrNull(redeemByDate); 169 } 170 171 public Integer getAppliesForMonths() { 172 return appliesForMonths; 173 } 174 175 public void setAppliesForMonths(final Object appliesForMonths) { 176 this.appliesForMonths = integerOrNull(appliesForMonths); 177 } 178 179 public Integer getMaxRedemptions() { 180 return maxRedemptions; 181 } 182 183 public void setMaxRedemptions(final Object maxRedemptions) { 184 this.maxRedemptions = integerOrNull(maxRedemptions); 185 } 186 187 public Boolean getSingleUse() { 188 return singleUse; 189 } 190 191 public void setSingleUse(final Object singleUse) { 192 this.singleUse = booleanOrNull(singleUse); 193 } 194 195 public Integer getDiscountInCents() { 196 return discountInCents; 197 } 198 199 public void setDiscountInCents(final Object discountInCents) { 200 this.discountInCents = integerOrNull(discountInCents); 201 } 202 203 public Boolean getAppliesToAllPlans() { 204 return appliesToAllPlans; 205 } 206 207 public void setAppliesToAllPlans(final Object appliesToAllPlans) { 208 this.appliesToAllPlans = booleanOrNull(appliesToAllPlans); 209 } 210 211 @Override 212 public String toString() { 213 final StringBuilder sb = new StringBuilder(); 214 sb.append("Coupon"); 215 sb.append("{name='").append(name).append('\''); 216 sb.append(", couponCode='").append(couponCode).append('\''); 217 sb.append(", discountType='").append(discountType).append('\''); 218 sb.append(", discountPercent='").append(discountPercent).append('\''); 219 sb.append('}'); 220 return sb.toString(); 221 } 222 223 @Override 224 public boolean equals(final Object o) { 225 if (this == o) { 226 return true; 227 } 228 if (o == null || getClass() != o.getClass()) { 229 return false; 230 } 231 232 final Coupon coupon = (Coupon) o; 233 234 if (couponCode != null ? !couponCode.equals(coupon.couponCode) : coupon.couponCode != null) { 235 return false; 236 } 237 if (discountPercent != null ? !discountPercent.equals(coupon.discountPercent) : coupon.discountPercent != null) { 238 return false; 239 } 240 if (discountType != null ? !discountType.equals(coupon.discountType) : coupon.discountType != null) { 241 return false; 242 } 243 if (name != null ? !name.equals(coupon.name) : coupon.name != null) { 244 return false; 245 } 246 247 return true; 248 } 249 250 @Override 251 public int hashCode() { 252 int result = name != null ? name.hashCode() : 0; 253 result = 31 * result + (couponCode != null ? couponCode.hashCode() : 0); 254 result = 31 * result + (discountType != null ? discountType.hashCode() : 0); 255 result = 31 * result + (discountPercent != null ? discountPercent.hashCode() : 0); 256 return result; 257 } 258}