killbill-memoizeit

overdue: add formatter for BillingState Prettify the amount

9/27/2012 6:19:31 PM

Details

diff --git a/api/src/main/java/com/ning/billing/overdue/applicator/formatters/BillingStateFormatter.java b/api/src/main/java/com/ning/billing/overdue/applicator/formatters/BillingStateFormatter.java
new file mode 100644
index 0000000..c856ba5
--- /dev/null
+++ b/api/src/main/java/com/ning/billing/overdue/applicator/formatters/BillingStateFormatter.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2010-2012 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.overdue.applicator.formatters;
+
+import com.ning.billing.overdue.config.api.BillingState;
+
+public abstract class BillingStateFormatter extends BillingState {
+
+    public BillingStateFormatter(final BillingState billingState) {
+        super(billingState.getObjectId(), billingState.getNumberOfUnpaidInvoices(), billingState.getBalanceOfUnpaidInvoices(),
+              billingState.getDateOfEarliestUnpaidInvoice(), billingState.getAccountTimeZone(), billingState.getIdOfEarliestUnpaidInvoice(),
+              billingState.getResponseForLastFailedPayment(), billingState.getTags());
+    }
+
+    public abstract String getFormattedBalanceOfUnpaidInvoices();
+}
diff --git a/api/src/main/java/com/ning/billing/overdue/applicator/formatters/OverdueEmailFormatterFactory.java b/api/src/main/java/com/ning/billing/overdue/applicator/formatters/OverdueEmailFormatterFactory.java
new file mode 100644
index 0000000..a5063c5
--- /dev/null
+++ b/api/src/main/java/com/ning/billing/overdue/applicator/formatters/OverdueEmailFormatterFactory.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2010-2012 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.overdue.applicator.formatters;
+
+import com.ning.billing.overdue.config.api.BillingState;
+
+public interface OverdueEmailFormatterFactory {
+
+    public BillingStateFormatter createBillingStateFormatter(BillingState billingState);
+}
diff --git a/invoice/src/main/java/com/ning/billing/invoice/template/formatters/DefaultInvoiceFormatter.java b/invoice/src/main/java/com/ning/billing/invoice/template/formatters/DefaultInvoiceFormatter.java
index 34ceac6..2eb05f7 100644
--- a/invoice/src/main/java/com/ning/billing/invoice/template/formatters/DefaultInvoiceFormatter.java
+++ b/invoice/src/main/java/com/ning/billing/invoice/template/formatters/DefaultInvoiceFormatter.java
@@ -57,7 +57,7 @@ import com.google.common.base.Objects;
 import com.google.common.base.Strings;
 import com.google.common.collect.ImmutableList;
 
-import static com.ning.billing.invoice.template.formatters.DefaultAmountFormatter.round;
+import static com.ning.billing.util.DefaultAmountFormatter.round;
 
 /**
  * Format invoice fields
diff --git a/invoice/src/main/java/com/ning/billing/invoice/template/formatters/DefaultInvoiceItemFormatter.java b/invoice/src/main/java/com/ning/billing/invoice/template/formatters/DefaultInvoiceItemFormatter.java
index b16a17e..9ea3a1f 100644
--- a/invoice/src/main/java/com/ning/billing/invoice/template/formatters/DefaultInvoiceItemFormatter.java
+++ b/invoice/src/main/java/com/ning/billing/invoice/template/formatters/DefaultInvoiceItemFormatter.java
@@ -35,7 +35,7 @@ import com.ning.billing.util.template.translation.TranslatorConfig;
 import com.google.common.base.Objects;
 import com.google.common.base.Strings;
 
-import static com.ning.billing.invoice.template.formatters.DefaultAmountFormatter.round;
+import static com.ning.billing.util.DefaultAmountFormatter.round;
 
 /**
  * Format invoice item fields
diff --git a/overdue/src/main/java/com/ning/billing/overdue/applicator/formatters/DefaultBillingStateFormatter.java b/overdue/src/main/java/com/ning/billing/overdue/applicator/formatters/DefaultBillingStateFormatter.java
new file mode 100644
index 0000000..74228a2
--- /dev/null
+++ b/overdue/src/main/java/com/ning/billing/overdue/applicator/formatters/DefaultBillingStateFormatter.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2010-2012 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.overdue.applicator.formatters;
+
+import java.math.BigDecimal;
+
+import com.ning.billing.overdue.config.api.BillingState;
+
+import com.google.common.base.Objects;
+
+import static com.ning.billing.util.DefaultAmountFormatter.round;
+
+public class DefaultBillingStateFormatter extends BillingStateFormatter {
+
+    public DefaultBillingStateFormatter(final BillingState billingState) {
+        super(billingState);
+    }
+
+    @Override
+    public String getFormattedBalanceOfUnpaidInvoices() {
+        return round(Objects.firstNonNull(getBalanceOfUnpaidInvoices(), BigDecimal.ZERO)).toString();
+    }
+}
diff --git a/overdue/src/main/java/com/ning/billing/overdue/applicator/formatters/DefaultOverdueEmailFormatterFactory.java b/overdue/src/main/java/com/ning/billing/overdue/applicator/formatters/DefaultOverdueEmailFormatterFactory.java
new file mode 100644
index 0000000..c9c1744
--- /dev/null
+++ b/overdue/src/main/java/com/ning/billing/overdue/applicator/formatters/DefaultOverdueEmailFormatterFactory.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2010-2012 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.overdue.applicator.formatters;
+
+import com.ning.billing.overdue.config.api.BillingState;
+
+public class DefaultOverdueEmailFormatterFactory implements OverdueEmailFormatterFactory {
+
+    @Override
+    public BillingStateFormatter createBillingStateFormatter(final BillingState billingState) {
+        return new DefaultBillingStateFormatter(billingState);
+    }
+}
diff --git a/overdue/src/main/java/com/ning/billing/overdue/applicator/OverdueEmailGenerator.java b/overdue/src/main/java/com/ning/billing/overdue/applicator/OverdueEmailGenerator.java
index ec2f971..9c95917 100644
--- a/overdue/src/main/java/com/ning/billing/overdue/applicator/OverdueEmailGenerator.java
+++ b/overdue/src/main/java/com/ning/billing/overdue/applicator/OverdueEmailGenerator.java
@@ -23,6 +23,7 @@ import java.util.Map;
 import com.ning.billing.account.api.Account;
 import com.ning.billing.junction.api.Blockable;
 import com.ning.billing.overdue.OverdueState;
+import com.ning.billing.overdue.applicator.formatters.OverdueEmailFormatterFactory;
 import com.ning.billing.overdue.config.api.BillingState;
 import com.ning.billing.util.email.templates.TemplateEngine;
 
@@ -31,10 +32,12 @@ import com.google.inject.Inject;
 public class OverdueEmailGenerator {
 
     private final TemplateEngine templateEngine;
+    private final OverdueEmailFormatterFactory overdueEmailFormatterFactory;
 
     @Inject
-    public OverdueEmailGenerator(final TemplateEngine templateEngine) {
+    public OverdueEmailGenerator(final TemplateEngine templateEngine, final OverdueEmailFormatterFactory overdueEmailFormatterFactory) {
         this.templateEngine = templateEngine;
+        this.overdueEmailFormatterFactory = overdueEmailFormatterFactory;
     }
 
     public <T extends Blockable> String generateEmail(final Account account, final BillingState<T> billingState,
@@ -43,7 +46,7 @@ public class OverdueEmailGenerator {
 
         // TODO raw objects for now. We eventually should respect the account locale and support translations
         data.put("account", account);
-        data.put("billingState", billingState);
+        data.put("billingState", overdueEmailFormatterFactory.createBillingStateFormatter(billingState));
         data.put("overdueable", overdueable);
         data.put("nextOverdueState", nextOverdueState);
 
diff --git a/overdue/src/main/java/com/ning/billing/overdue/glue/DefaultOverdueModule.java b/overdue/src/main/java/com/ning/billing/overdue/glue/DefaultOverdueModule.java
index baf8700..08920d8 100644
--- a/overdue/src/main/java/com/ning/billing/overdue/glue/DefaultOverdueModule.java
+++ b/overdue/src/main/java/com/ning/billing/overdue/glue/DefaultOverdueModule.java
@@ -28,6 +28,8 @@ import com.ning.billing.overdue.OverdueService;
 import com.ning.billing.overdue.OverdueUserApi;
 import com.ning.billing.overdue.api.DefaultOverdueUserApi;
 import com.ning.billing.overdue.applicator.OverdueEmailGenerator;
+import com.ning.billing.overdue.applicator.formatters.DefaultOverdueEmailFormatterFactory;
+import com.ning.billing.overdue.applicator.formatters.OverdueEmailFormatterFactory;
 import com.ning.billing.overdue.service.DefaultOverdueService;
 import com.ning.billing.overdue.service.ExtendedOverdueService;
 import com.ning.billing.overdue.wrapper.OverdueWrapperFactory;
@@ -61,6 +63,7 @@ public class DefaultOverdueModule extends AbstractModule implements OverdueModul
     }
 
     protected void installOverdueEmail() {
+        bind(OverdueEmailFormatterFactory.class).to(DefaultOverdueEmailFormatterFactory.class).asEagerSingleton();
         bind(OverdueEmailGenerator.class).asEagerSingleton();
     }
 
diff --git a/overdue/src/test/java/com/ning/billing/overdue/applicator/formatters/TestDefaultBillingStateFormatter.java b/overdue/src/test/java/com/ning/billing/overdue/applicator/formatters/TestDefaultBillingStateFormatter.java
new file mode 100644
index 0000000..b0e7f0f
--- /dev/null
+++ b/overdue/src/test/java/com/ning/billing/overdue/applicator/formatters/TestDefaultBillingStateFormatter.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2010-2012 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.overdue.applicator.formatters;
+
+import java.math.BigDecimal;
+import java.util.UUID;
+
+import org.joda.time.DateTimeZone;
+import org.joda.time.LocalDate;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import com.ning.billing.overdue.OverdueTestSuite;
+import com.ning.billing.overdue.config.api.BillingState;
+
+public class TestDefaultBillingStateFormatter extends OverdueTestSuite {
+
+    @Test(groups = "fast")
+    public void testBalanceFormatting() throws Exception {
+        final BillingState billingState = new BillingState(UUID.randomUUID(), 2, BigDecimal.TEN,
+                                                           new LocalDate(), DateTimeZone.UTC, UUID.randomUUID(),
+                                                           null, null);
+        final DefaultBillingStateFormatter formatter = new DefaultBillingStateFormatter(billingState);
+        Assert.assertEquals(formatter.getFormattedBalanceOfUnpaidInvoices(), "10.00");
+    }
+}