killbill-aplcache

First pass at Catalog endpoints - tests still needed and output

6/6/2012 12:13:31 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/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/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();
+    }
+}