killbill-aplcache

Merge branch 'integration' of github.com:ning/killbill

6/6/2012 4:43:37 PM

Details

diff --git a/api/src/main/java/com/ning/billing/catalog/api/Listing.java b/api/src/main/java/com/ning/billing/catalog/api/Listing.java
new file mode 100644
index 0000000..3fa0229
--- /dev/null
+++ b/api/src/main/java/com/ning/billing/catalog/api/Listing.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2010-2011 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 com.ning.billing.catalog.api;
+
+public interface Listing {
+    
+    Plan getPlan();
+    
+    PriceList getPriceList();
+}
diff --git a/api/src/main/java/com/ning/billing/catalog/api/StaticCatalog.java b/api/src/main/java/com/ning/billing/catalog/api/StaticCatalog.java
index 17ea132..0b2ef57 100644
--- a/api/src/main/java/com/ning/billing/catalog/api/StaticCatalog.java
+++ b/api/src/main/java/com/ning/billing/catalog/api/StaticCatalog.java
@@ -17,6 +17,7 @@
 package com.ning.billing.catalog.api;
 
 import java.util.Date;
+import java.util.List;
 
 
 public interface StaticCatalog {
@@ -76,4 +77,6 @@ public interface StaticCatalog {
 
     public abstract boolean canCreatePlan(PlanSpecifier specifier) throws CatalogApiException;
 
+    public abstract List<Listing> getAvailableAddonListings(String baseProductName) throws CatalogApiException;
+
 }
\ No newline at end of file
diff --git a/catalog/src/main/java/com/ning/billing/catalog/DefaultListing.java b/catalog/src/main/java/com/ning/billing/catalog/DefaultListing.java
new file mode 100644
index 0000000..bede9f0
--- /dev/null
+++ b/catalog/src/main/java/com/ning/billing/catalog/DefaultListing.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2010-2011 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 com.ning.billing.catalog;
+
+import com.ning.billing.catalog.api.Listing;
+import com.ning.billing.catalog.api.Plan;
+import com.ning.billing.catalog.api.PriceList;
+
+public class DefaultListing implements Listing {
+    private final Plan plan;
+    private final PriceList priceList;
+
+    public DefaultListing(Plan plan, PriceList priceList) {
+        super();
+        this.plan = plan;
+        this.priceList = priceList;
+    }
+
+    @Override
+    public Plan getPlan() {
+        return plan;
+    }
+
+    @Override
+    public PriceList getPriceList() {
+        return priceList;
+    }
+
+}
diff --git a/catalog/src/main/java/com/ning/billing/catalog/DefaultPriceListSet.java b/catalog/src/main/java/com/ning/billing/catalog/DefaultPriceListSet.java
index 83d810b..b8a8695 100644
--- a/catalog/src/main/java/com/ning/billing/catalog/DefaultPriceListSet.java
+++ b/catalog/src/main/java/com/ning/billing/catalog/DefaultPriceListSet.java
@@ -16,6 +16,9 @@
 
 package com.ning.billing.catalog;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
@@ -23,6 +26,7 @@ import javax.xml.bind.annotation.XmlElement;
 import com.ning.billing.ErrorCode;
 import com.ning.billing.catalog.api.BillingPeriod;
 import com.ning.billing.catalog.api.CatalogApiException;
+import com.ning.billing.catalog.api.PriceList;
 import com.ning.billing.catalog.api.PriceListSet;
 import com.ning.billing.catalog.api.Product;
 import com.ning.billing.util.config.ValidatingConfig;
@@ -99,6 +103,15 @@ public class DefaultPriceListSet extends ValidatingConfig<StandaloneCatalog> {
 		return childPriceLists;
 	}
 
+    public List<PriceList> getAllPriceLists() {
+        List<PriceList> result = new ArrayList<PriceList> (childPriceLists.length + 1);
+        result.add(getDefaultPricelist());
+        for(PriceList list : getChildPriceLists()) {
+            result.add(list);
+        }
+        return result;
+    }
+
 
 	
 }
diff --git a/catalog/src/main/java/com/ning/billing/catalog/StandaloneCatalog.java b/catalog/src/main/java/com/ning/billing/catalog/StandaloneCatalog.java
index a63645c..32324f9 100644
--- a/catalog/src/main/java/com/ning/billing/catalog/StandaloneCatalog.java
+++ b/catalog/src/main/java/com/ning/billing/catalog/StandaloneCatalog.java
@@ -17,8 +17,10 @@
 package com.ning.billing.catalog;
 
 import java.net.URI;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Date;
+import java.util.List;
 
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
@@ -32,6 +34,7 @@ import com.ning.billing.catalog.api.BillingAlignment;
 import com.ning.billing.catalog.api.BillingPeriod;
 import com.ning.billing.catalog.api.CatalogApiException;
 import com.ning.billing.catalog.api.Currency;
+import com.ning.billing.catalog.api.Listing;
 import com.ning.billing.catalog.api.Plan;
 import com.ning.billing.catalog.api.PlanAlignmentChange;
 import com.ning.billing.catalog.api.PlanAlignmentCreate;
@@ -303,15 +306,41 @@ public class StandaloneCatalog extends ValidatingConfig<StandaloneCatalog> imple
         return this;
     }
 
-	@Override
-	public boolean canCreatePlan(PlanSpecifier specifier) throws CatalogApiException {
-		Product product = findCurrentProduct(specifier.getProductName());
-		Plan plan = findCurrentPlan(specifier.getProductName(), specifier.getBillingPeriod(), specifier.getPriceListName());
-		DefaultPriceList priceList = findCurrentPriceList(specifier.getPriceListName());
-		
-		return (!product.isRetired()) &&
-				(!plan.isRetired()) &&
-				(!priceList.isRetired());
-	}
+    @Override
+    public boolean canCreatePlan(PlanSpecifier specifier) throws CatalogApiException {
+        Product product = findCurrentProduct(specifier.getProductName());
+        Plan plan = findCurrentPlan(specifier.getProductName(), specifier.getBillingPeriod(), specifier.getPriceListName());
+        DefaultPriceList priceList = findCurrentPriceList(specifier.getPriceListName());
+        
+        return (!product.isRetired()) &&
+                (!plan.isRetired()) &&
+                (!priceList.isRetired());
+    }
+
+    @Override
+    public List<Listing> getAvailableAddonListings(String baseProductName) {
+        List<Listing> availAddons = new ArrayList<Listing>();
+
+        try {
+            Product product = findCurrentProduct(baseProductName);
+            if ( product != null ) {
+                for ( Product availAddon : product.getAvailable() ) {
+                    for ( Plan plan : getCurrentPlans()) {
+                        if( plan.getProduct().equals(availAddon)) {
+                            for( PriceList priceList : getPriceLists().getAllPriceLists()) {
+                                if ( priceList.findPlan(availAddon, plan.getBillingPeriod()) != null &&
+                                     priceList.findPlan(product, plan.getBillingPeriod()).equals(plan)) {
+                                    availAddons.add(new DefaultListing(plan, priceList));
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        } catch (CatalogApiException e) {
+            // No such product - just return an empty list
+        }
+
+        return availAddons;    }
 
 }
diff --git a/catalog/src/main/java/com/ning/billing/catalog/VersionedCatalog.java b/catalog/src/main/java/com/ning/billing/catalog/VersionedCatalog.java
index 215b142..3bd370a 100644
--- a/catalog/src/main/java/com/ning/billing/catalog/VersionedCatalog.java
+++ b/catalog/src/main/java/com/ning/billing/catalog/VersionedCatalog.java
@@ -39,6 +39,7 @@ import com.ning.billing.catalog.api.BillingPeriod;
 import com.ning.billing.catalog.api.Catalog;
 import com.ning.billing.catalog.api.CatalogApiException;
 import com.ning.billing.catalog.api.Currency;
+import com.ning.billing.catalog.api.Listing;
 import com.ning.billing.catalog.api.Plan;
 import com.ning.billing.catalog.api.PlanAlignmentChange;
 import com.ning.billing.catalog.api.PlanAlignmentCreate;
@@ -445,4 +446,9 @@ public class VersionedCatalog extends ValidatingConfig<StandaloneCatalog> implem
 		return versionForDate(clock.getUTCNow()).canCreatePlan(specifier);
 	}
 
+    @Override
+    public List<Listing> getAvailableAddonListings(String baseProductName) throws CatalogApiException {
+        return versionForDate(clock.getUTCNow()).getAvailableAddonListings(baseProductName);
+    }
+
 }
diff --git a/jaxrs/src/main/java/com/ning/billing/jaxrs/json/PlanDetailJason.java b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/PlanDetailJason.java
new file mode 100644
index 0000000..7474315
--- /dev/null
+++ b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/PlanDetailJason.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2010-2011 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 com.ning.billing.jaxrs.json;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.ning.billing.catalog.api.BillingPeriod;
+import com.ning.billing.catalog.api.InternationalPrice;
+import com.ning.billing.catalog.api.Listing;
+
+public class PlanDetailJason {
+
+    final String productName;
+    final  String planName;
+    final  BillingPeriod billingPeriod;
+    final  String priceListName;
+    final  InternationalPrice finalPhasePrice;
+    public PlanDetailJason(
+            @JsonProperty("product") String productName,
+            @JsonProperty("plan") String planName,
+            @JsonProperty("final_phase_billing_period") BillingPeriod billingPeriod,
+            @JsonProperty("priceList") String priceListName,
+            @JsonProperty("final_phase_recurring_price") InternationalPrice finalPhasePrice
+                ) {
+        this.productName = productName;
+        this.planName = planName;
+        this.billingPeriod = billingPeriod;
+        this.priceListName = priceListName;
+        this.finalPhasePrice = finalPhasePrice;
+    }
+    
+    public PlanDetailJason(Listing listing) {
+        this.productName = listing.getPlan().getProduct().getName();
+        this.planName = listing.getPlan().getName();
+        this.billingPeriod = listing.getPlan().getBillingPeriod();
+        this.priceListName = listing.getPriceList().getName();
+        this.finalPhasePrice = listing.getPlan().getFinalPhase().getRecurringPrice();
+    }
+
+    public String getProductName() {
+        return productName;
+    }
+
+    public String getPlanName() {
+        return planName;
+    }
+
+    public BillingPeriod getBillingPeriod() {
+        return billingPeriod;
+    }
+
+    public String getPriceListName() {
+        return priceListName;
+    }
+
+    public InternationalPrice getFinalPhasePrice() {
+        return finalPhasePrice;
+    }
+    
+ 
+}
diff --git a/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/CatalogResource.java b/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/CatalogResource.java
new file mode 100644
index 0000000..ec6e5c0
--- /dev/null
+++ b/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/CatalogResource.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2010-2011 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 com.ning.billing.jaxrs.resources;
+
+import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
+import static javax.ws.rs.core.MediaType.APPLICATION_XML;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
+
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+import com.ning.billing.catalog.api.CatalogApiException;
+import com.ning.billing.catalog.api.CatalogService;
+import com.ning.billing.catalog.api.Listing;
+import com.ning.billing.catalog.api.StaticCatalog;
+import com.ning.billing.jaxrs.json.PlanDetailJason;
+import com.ning.billing.util.config.XMLWriter;
+
+@Singleton
+@Path(JaxrsResource.CATALOG_PATH)
+public class CatalogResource implements JaxrsResource {
+
+    private CatalogService catalogService;
+
+    @Inject
+    public CatalogResource(final CatalogService catalogService)
+    {
+        this.catalogService = catalogService;
+    }
+
+    @GET
+    @Produces(APPLICATION_XML)
+    public Response getCatalog() throws Exception {
+        return Response.status(Status.OK).entity(XMLWriter.writeXML((StaticCatalog) catalogService.getCurrentCatalog(), StaticCatalog.class)).build();
+    }
+
+    // Need to figure out dependency on StandaloneCatalog
+    //    @GET
+    //    @Path("/xsd")
+    //    @Produces(APPLICATION_XML)
+    //    public String getCatalogXsd() throws Exception
+    //    {
+    //        InputStream stream = XMLSchemaGenerator.xmlSchema(StandaloneCatalog.class);
+    //        StringWriter writer = new StringWriter();
+    //        IOUtils.copy(stream, writer);
+    //        String result = writer.toString();
+    //
+    //        return result;
+    //    }
+
+
+
+    @GET
+    @Path("/availableAddons")
+    @Produces(APPLICATION_JSON)
+    public Response getAvailableAddons(@QueryParam("baseProductName") final String baseProductName) throws CatalogApiException {
+        StaticCatalog catalog = catalogService.getCurrentCatalog();
+        List<Listing> listings = catalog.getAvailableAddonListings(baseProductName);
+        List<PlanDetailJason> details = new ArrayList<PlanDetailJason>();
+        for(Listing listing : listings) {
+            details.add(new PlanDetailJason(listing));
+        }
+        return Response.status(Status.OK).entity(details).build();
+    }
+}
diff --git a/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/JaxrsResource.java b/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/JaxrsResource.java
index b35b2bf..1c4a588 100644
--- a/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/JaxrsResource.java
+++ b/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/JaxrsResource.java
@@ -15,12 +15,6 @@
  */
 package com.ning.billing.jaxrs.resources;
 
-import com.ning.billing.jaxrs.json.CustomFieldJson;
-import com.ning.billing.util.callcontext.CallContext;
-
-import javax.ws.rs.core.Response;
-import java.util.List;
-import java.util.UUID;
 
 public interface JaxrsResource {
 	
@@ -82,4 +76,8 @@ public interface JaxrsResource {
 
     public static final String TAGS = "tags";
     public static final String CUSTOM_FIELDS = "custom_fields";
+    
+    public static final String CATALOG = "catalog";
+    public static final String CATALOG_PATH = PREFIX + "/" + CATALOG;
+
 }
diff --git a/server/src/main/java/com/ning/billing/server/modules/KillbillServerModule.java b/server/src/main/java/com/ning/billing/server/modules/KillbillServerModule.java
index fd9d2a2..32041a2 100644
--- a/server/src/main/java/com/ning/billing/server/modules/KillbillServerModule.java
+++ b/server/src/main/java/com/ning/billing/server/modules/KillbillServerModule.java
@@ -16,9 +16,6 @@
 
 package com.ning.billing.server.modules;
 
-import com.ning.billing.util.email.EmailModule;
-import com.ning.billing.util.email.templates.TemplateModule;
-import com.ning.billing.util.glue.GlobalLockerModule;
 import org.skife.jdbi.v2.DBI;
 import org.skife.jdbi.v2.IDBI;
 
@@ -31,6 +28,7 @@ import com.ning.billing.entitlement.glue.DefaultEntitlementModule;
 import com.ning.billing.invoice.glue.DefaultInvoiceModule;
 import com.ning.billing.jaxrs.resources.AccountResource;
 import com.ning.billing.jaxrs.resources.BundleResource;
+import com.ning.billing.jaxrs.resources.CatalogResource;
 import com.ning.billing.jaxrs.resources.InvoiceResource;
 import com.ning.billing.jaxrs.resources.PaymentResource;
 import com.ning.billing.jaxrs.resources.SubscriptionResource;
@@ -39,10 +37,13 @@ import com.ning.billing.jaxrs.util.KillbillEventHandler;
 import com.ning.billing.jaxrs.util.TagHelper;
 import com.ning.billing.junction.glue.DefaultJunctionModule;
 import com.ning.billing.payment.setup.PaymentModule;
+import com.ning.billing.util.email.EmailModule;
+import com.ning.billing.util.email.templates.TemplateModule;
 import com.ning.billing.util.glue.BusModule;
 import com.ning.billing.util.glue.CallContextModule;
 import com.ning.billing.util.glue.ClockModule;
 import com.ning.billing.util.glue.CustomFieldModule;
+import com.ning.billing.util.glue.GlobalLockerModule;
 import com.ning.billing.util.glue.NotificationQueueModule;
 import com.ning.billing.util.glue.TagStoreModule;
 import com.ning.jetty.jdbi.guice.providers.DBIProvider;
@@ -69,6 +70,7 @@ public class KillbillServerModule extends AbstractModule
         bind(InvoiceResource.class).asEagerSingleton();
         bind(PaymentResource.class).asEagerSingleton();
         bind(TagResource.class).asEagerSingleton();
+        bind(CatalogResource.class).asEagerSingleton();
         bind(KillbillEventHandler.class).asEagerSingleton();
     }