/
StandaloneCatalog.java
404 lines (338 loc) · 14 KB
/
StandaloneCatalog.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
* Copyright 2010-2013 Ning, Inc.
*
* Ning licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package org.killbill.billing.catalog;
import java.net.URI;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Nullable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import org.killbill.billing.ErrorCode;
import org.killbill.billing.catalog.api.BillingActionPolicy;
import org.killbill.billing.catalog.api.BillingAlignment;
import org.killbill.billing.catalog.api.BillingMode;
import org.killbill.billing.catalog.api.BillingPeriod;
import org.killbill.billing.catalog.api.CatalogApiException;
import org.killbill.billing.catalog.api.Currency;
import org.killbill.billing.catalog.api.Listing;
import org.killbill.billing.catalog.api.Plan;
import org.killbill.billing.catalog.api.PlanAlignmentChange;
import org.killbill.billing.catalog.api.PlanAlignmentCreate;
import org.killbill.billing.catalog.api.PlanChangeResult;
import org.killbill.billing.catalog.api.PlanPhase;
import org.killbill.billing.catalog.api.PlanPhasePriceOverride;
import org.killbill.billing.catalog.api.PlanPhaseSpecifier;
import org.killbill.billing.catalog.api.PlanSpecifier;
import org.killbill.billing.catalog.api.PriceList;
import org.killbill.billing.catalog.api.Product;
import org.killbill.billing.catalog.api.ProductCategory;
import org.killbill.billing.catalog.api.StaticCatalog;
import org.killbill.billing.catalog.rules.PlanRules;
import org.killbill.xmlloader.ValidatingConfig;
import org.killbill.xmlloader.ValidationErrors;
import com.google.common.collect.ImmutableList;
@XmlRootElement(name = "catalog")
@XmlAccessorType(XmlAccessType.NONE)
public class StandaloneCatalog extends ValidatingConfig<StandaloneCatalog> implements StaticCatalog {
@XmlElement(required = true)
private Date effectiveDate;
@XmlElement(required = true)
private String catalogName;
@XmlElement(required = true)
private BillingMode recurringBillingMode;
private URI catalogURI;
@XmlElementWrapper(name = "currencies", required = true)
@XmlElement(name = "currency", required = true)
private Currency[] supportedCurrencies;
@XmlElementWrapper(name = "units", required = false)
@XmlElement(name = "unit", required = true)
private DefaultUnit[] units;
@XmlElementWrapper(name = "products", required = true)
@XmlElement(name = "product", required = true)
private DefaultProduct[] products;
@XmlElement(name = "rules", required = true)
private PlanRules planRules;
@XmlElementWrapper(name = "plans", required = true)
@XmlElement(name = "plan", required = true)
private DefaultPlan[] plans;
@XmlElement(name = "priceLists", required = true)
private DefaultPriceListSet priceLists;
public StandaloneCatalog() {
}
protected StandaloneCatalog(final Date effectiveDate) {
this.effectiveDate = effectiveDate;
}
/* (non-Javadoc)
* @see org.killbill.billing.catalog.ICatalog#getCalalogName()
*/
@Override
public String getCatalogName() {
return catalogName;
}
@Override
public Date getEffectiveDate() {
return effectiveDate;
}
@Override
public BillingMode getRecurringBillingMode() {
return recurringBillingMode;
}
/* (non-Javadoc)
* @see org.killbill.billing.catalog.ICatalog#getProducts()
*/
@Override
public DefaultProduct[] getCurrentProducts() {
return products;
}
/* (non-Javadoc)
* @see org.killbill.billing.catalog.ICatalog#getProducts()
*/
@Override
public DefaultUnit[] getCurrentUnits() {
return units;
}
@Override
public Currency[] getCurrentSupportedCurrencies() {
return supportedCurrencies;
}
@Override
public DefaultPlan[] getCurrentPlans() {
return plans;
}
public URI getCatalogURI() {
return catalogURI;
}
public PlanRules getPlanRules() {
return planRules;
}
public DefaultPriceList findCurrentPriceList(final String priceListName) throws CatalogApiException {
return priceLists.findPriceListFrom(priceListName);
}
public DefaultPriceListSet getPriceLists() {
return this.priceLists;
}
/* (non-Javadoc)
* @see org.killbill.billing.catalog.ICatalog#getPlan(java.lang.String, java.lang.String)
*/
@Override
public DefaultPlan findCurrentPlan(final String productName, final BillingPeriod period, final String priceListName, List<PlanPhasePriceOverride> overrides) throws CatalogApiException {
if (productName == null) {
throw new CatalogApiException(ErrorCode.CAT_NULL_PRODUCT_NAME);
}
if (priceLists == null) {
throw new CatalogApiException(ErrorCode.CAT_PRICE_LIST_NOT_FOUND, priceListName);
}
final Product product = findCurrentProduct(productName);
final DefaultPlan result = priceLists.getPlanFrom(priceListName, product, period);
if (result == null) {
final String periodString = (period == null) ? "NULL" : period.toString();
throw new CatalogApiException(ErrorCode.CAT_PLAN_NOT_FOUND, productName, periodString, priceListName);
}
return result;
}
@Override
public DefaultPlan findCurrentPlan(final String name) throws CatalogApiException {
if (name == null || plans == null) {
throw new CatalogApiException(ErrorCode.CAT_NO_SUCH_PLAN, name);
}
for (final DefaultPlan p : plans) {
if (p.getName().equals(name)) {
return p;
}
}
throw new CatalogApiException(ErrorCode.CAT_NO_SUCH_PLAN, name);
}
@Override
public Product findCurrentProduct(final String name) throws CatalogApiException {
if (name == null || products == null) {
throw new CatalogApiException(ErrorCode.CAT_NO_SUCH_PRODUCT, name);
}
for (final DefaultProduct p : products) {
if (p.getName().equals(name)) {
return p;
}
}
throw new CatalogApiException(ErrorCode.CAT_NO_SUCH_PRODUCT, name);
}
@Override
public PlanPhase findCurrentPhase(final String name) throws CatalogApiException {
if (name == null || plans == null) {
throw new CatalogApiException(ErrorCode.CAT_NO_SUCH_PHASE, name);
}
final String planName = DefaultPlanPhase.planName(name);
final Plan plan = findCurrentPlan(planName);
return plan.findPhase(name);
}
@Override
public PriceList findCurrentPricelist(final String name)
throws CatalogApiException {
if (name == null || priceLists == null) {
throw new CatalogApiException(ErrorCode.CAT_PRICE_LIST_NOT_FOUND, name);
}
return priceLists.findPriceListFrom(name);
}
//////////////////////////////////////////////////////////////////////////////
//
// RULES
//
//////////////////////////////////////////////////////////////////////////////
@Override
public BillingActionPolicy planChangePolicy(final PlanPhaseSpecifier from, final PlanSpecifier to) throws CatalogApiException {
return planRules.getPlanChangePolicy(from, to, this);
}
@Override
public PlanAlignmentChange planChangeAlignment(final PlanPhaseSpecifier from, final PlanSpecifier to) throws CatalogApiException {
return planRules.getPlanChangeAlignment(from, to, this);
}
@Override
public BillingActionPolicy planCancelPolicy(final PlanPhaseSpecifier planPhase) throws CatalogApiException {
return planRules.getPlanCancelPolicy(planPhase, this);
}
@Override
public PlanAlignmentCreate planCreateAlignment(final PlanSpecifier specifier) throws CatalogApiException {
return planRules.getPlanCreateAlignment(specifier, this);
}
@Override
public BillingAlignment billingAlignment(final PlanPhaseSpecifier planPhase) throws CatalogApiException {
return planRules.getBillingAlignment(planPhase, this);
}
@Override
public PlanChangeResult planChange(final PlanPhaseSpecifier from, final PlanSpecifier to)
throws CatalogApiException {
return planRules.planChange(from, to, this);
}
@Override
public ValidationErrors validate(final StandaloneCatalog catalog, final ValidationErrors errors) {
validateCollection(catalog, errors, products);
validateCollection(catalog, errors, plans);
priceLists.validate(catalog, errors);
planRules.validate(catalog, errors);
return errors;
}
@Override
public void initialize(final StandaloneCatalog catalog, final URI sourceURI) {
catalogURI = sourceURI;
super.initialize(catalog, sourceURI);
planRules.initialize(catalog, sourceURI);
priceLists.initialize(catalog, sourceURI);
for (final DefaultProduct p : products) {
p.initialize(catalog, sourceURI);
}
for (final DefaultPlan p : plans) {
p.initialize(catalog, sourceURI);
}
}
//////////////////////////////////////////////////////////////////////////////
//
// UNIT LIMIT
//
//////////////////////////////////////////////////////////////////////////////
@Override
public boolean compliesWithLimits(final String phaseName, final String unit, final double value) throws CatalogApiException {
PlanPhase phase = findCurrentPhase(phaseName);
return phase.compliesWithLimits(unit, value);
}
protected StandaloneCatalog setProducts(final DefaultProduct[] products) {
this.products = products;
return this;
}
protected StandaloneCatalog setSupportedCurrencies(final Currency[] supportedCurrencies) {
this.supportedCurrencies = supportedCurrencies;
return this;
}
protected StandaloneCatalog setPlanChangeRules(final PlanRules planChangeRules) {
this.planRules = planChangeRules;
return this;
}
protected StandaloneCatalog setPlans(final DefaultPlan[] plans) {
this.plans = plans;
return this;
}
public StandaloneCatalog setCatalogName(final String catalogName) {
this.catalogName = catalogName;
return this;
}
protected StandaloneCatalog setEffectiveDate(final Date effectiveDate) {
this.effectiveDate = effectiveDate;
return this;
}
public StandaloneCatalog setRecurringBillingMode(final BillingMode recurringBillingMode) {
this.recurringBillingMode = recurringBillingMode;
return this;
}
protected StandaloneCatalog setPlanRules(final PlanRules planRules) {
this.planRules = planRules;
return this;
}
protected StandaloneCatalog setPriceLists(final DefaultPriceListSet priceLists) {
this.priceLists = priceLists;
return this;
}
@Override
public boolean canCreatePlan(final PlanSpecifier specifier) throws CatalogApiException {
final Product product = findCurrentProduct(specifier.getProductName());
final Plan plan = findCurrentPlan(specifier.getProductName(), specifier.getBillingPeriod(), specifier.getPriceListName(), ImmutableList.<PlanPhasePriceOverride>of());
final DefaultPriceList priceList = findCurrentPriceList(specifier.getPriceListName());
return (!product.isRetired()) &&
(!plan.isRetired()) &&
(!priceList.isRetired());
}
@Override
public List<Listing> getAvailableAddOnListings(final String baseProductName, @Nullable final String priceListName) {
final List<Listing> availAddons = new ArrayList<Listing>();
try {
Product product = findCurrentProduct(baseProductName);
if ( product != null ) {
for ( Product availAddon : product.getAvailable() ) {
for ( BillingPeriod billingPeriod : BillingPeriod.values()) {
for( PriceList priceList : getPriceLists().getAllPriceLists()) {
if (priceListName == null || priceListName.equals(priceList.getName())) {
Plan addonInList = priceList.findPlan(availAddon, billingPeriod);
if ( (addonInList != null) ) {
availAddons.add(new DefaultListing(addonInList, priceList));
}
}
}
}
}
}
} catch (CatalogApiException e) {
// No such product - just return an empty list
}
return availAddons;
}
@Override
public List<Listing> getAvailableBasePlanListings() {
final List<Listing> availBasePlans = new ArrayList<Listing>();
for (Plan plan : getCurrentPlans()) {
if (plan.getProduct().getCategory().equals(ProductCategory.BASE)) {
for (PriceList priceList : getPriceLists().getAllPriceLists()) {
for (Plan priceListPlan : priceList.getPlans()) {
if (priceListPlan.getName().equals(plan.getName()) &&
priceListPlan.getProduct().getName().equals(plan.getProduct().getName())) {
availBasePlans.add(new DefaultListing(priceListPlan, priceList));
}
}
}
}
}
return availBasePlans;
}
}