killbill-memoizeit

Fix tag manipulation to not interoret the tag but instead rely

1/4/2013 7:54:55 AM

Details

diff --git a/api/src/main/java/com/ning/billing/util/tag/ControlTagType.java b/api/src/main/java/com/ning/billing/util/tag/ControlTagType.java
index dc32253..d150f5a 100644
--- a/api/src/main/java/com/ning/billing/util/tag/ControlTagType.java
+++ b/api/src/main/java/com/ning/billing/util/tag/ControlTagType.java
@@ -16,6 +16,7 @@
 
 package com.ning.billing.util.tag;
 
+import java.util.Collection;
 import java.util.List;
 import java.util.UUID;
 
@@ -74,4 +75,40 @@ public enum ControlTagType {
         }
         return null;
     }
+
+    /**
+     * Convenience method to return whether an account is configured with auto_pay_off
+     * (information of which control tag is configured with which behavior stays here)
+     *
+     * @param input tagDefinitionIds for all the tags associated to a given account
+     * @return whether that account is set with auto_pay_off
+     */
+    public static boolean isAutoPayOff(final Collection<UUID> input) {
+        for (UUID cur : input) {
+           for (ControlTagType controlTag : values()) {
+               if (controlTag.getId().equals(cur) && controlTag.getAutoPaymentOff()) {
+                   return true;
+               }
+           }
+        }
+        return false;
+    }
+
+    /**
+     * Convenience method to return whether an account is configured with auto_invoicing_off
+     * (information of which control tag is configured with which behavior stays here)
+     *
+     * @param input tagDefinitionIds for all the tags associated to a given account
+     * @return whether that account is set with auto_invoicing_off
+     */
+    public static boolean isAutoInvoicingOff(final Collection<UUID> input) {
+        for (UUID cur : input) {
+            for (ControlTagType controlTag : values()) {
+                if (controlTag.getId().equals(cur) && controlTag.getAutoInvoicingOff()) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
 }
diff --git a/junction/src/main/java/com/ning/billing/junction/plumbing/billing/DefaultInternalBillingApi.java b/junction/src/main/java/com/ning/billing/junction/plumbing/billing/DefaultInternalBillingApi.java
index 8fecc53..86096b4 100644
--- a/junction/src/main/java/com/ning/billing/junction/plumbing/billing/DefaultInternalBillingApi.java
+++ b/junction/src/main/java/com/ning/billing/junction/plumbing/billing/DefaultInternalBillingApi.java
@@ -20,6 +20,8 @@ import java.util.List;
 import java.util.SortedSet;
 import java.util.UUID;
 
+import javax.annotation.Nullable;
+
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -43,6 +45,8 @@ import com.ning.billing.util.svcapi.tag.TagInternalApi;
 import com.ning.billing.util.tag.ControlTagType;
 import com.ning.billing.util.tag.Tag;
 
+import com.google.common.base.Function;
+import com.google.common.collect.Collections2;
 import com.google.inject.Inject;
 
 public class DefaultInternalBillingApi implements BillingInternalApi {
@@ -79,11 +83,10 @@ public class DefaultInternalBillingApi implements BillingInternalApi {
 
             // Check to see if billing is off for the account
             final List<Tag> accountTags = tagApi.getTags(accountId, ObjectType.ACCOUNT, context);
-            for (final Tag cur : accountTags) {
-                if (ControlTagType.AUTO_INVOICING_OFF.getId().equals(cur.getTagDefinitionId())) {
-                    result.setAccountAutoInvoiceIsOff(true);
-                    return result; // billing is off, we are done
-                }
+            final boolean found_AUTO_INVOICING_OFF = is_AUTO_INVOICING_OFF(accountTags);
+            if (found_AUTO_INVOICING_OFF) {
+                result.setAccountAutoInvoiceIsOff(true);
+                return result; // billing is off, we are done
             }
 
             addBillingEventsForBundles(bundles, account, context, result);
@@ -115,14 +118,7 @@ public class DefaultInternalBillingApi implements BillingInternalApi {
 
             //Check if billing is off for the bundle
             final List<Tag> bundleTags = tagApi.getTags(bundle.getId(), ObjectType.BUNDLE, context);
-
-            boolean found_AUTO_INVOICING_OFF = false;
-            for (final Tag cur : bundleTags) {
-                if (ControlTagType.AUTO_INVOICING_OFF.getId().equals(cur.getTagDefinitionId())) {
-                    found_AUTO_INVOICING_OFF = true;
-                    break;
-                }
-            }
+            boolean found_AUTO_INVOICING_OFF = is_AUTO_INVOICING_OFF(bundleTags);
             if (found_AUTO_INVOICING_OFF) {
                 for (final Subscription subscription : subscriptions) { // billing is off so list sub ids in set to be excluded
                     result.getSubscriptionIdsWithAutoInvoiceOff().add(subscription.getId());
@@ -156,4 +152,14 @@ public class DefaultInternalBillingApi implements BillingInternalApi {
             }
         }
     }
+
+    private final boolean is_AUTO_INVOICING_OFF(final List<Tag> tags) {
+        return ControlTagType.isAutoInvoicingOff(Collections2.transform(tags, new Function<Tag, UUID>() {
+            @Nullable
+            @Override
+            public UUID apply(@Nullable final Tag tag) {
+                return tag.getTagDefinitionId();
+            }
+        }));
+    }
 }
diff --git a/overdue/src/main/java/com/ning/billing/ovedue/notification/DefaultOverdueCheckNotifier.java b/overdue/src/main/java/com/ning/billing/ovedue/notification/DefaultOverdueCheckNotifier.java
index e0fd846..07e10db 100644
--- a/overdue/src/main/java/com/ning/billing/ovedue/notification/DefaultOverdueCheckNotifier.java
+++ b/overdue/src/main/java/com/ning/billing/ovedue/notification/DefaultOverdueCheckNotifier.java
@@ -35,6 +35,7 @@ import com.ning.billing.util.notificationq.NotificationQueueService.Notification
 import com.google.inject.Inject;
 
 public class DefaultOverdueCheckNotifier implements OverdueCheckNotifier {
+
     private static final Logger log = LoggerFactory.getLogger(DefaultOverdueCheckNotifier.class);
 
     public static final String OVERDUE_CHECK_NOTIFIER_QUEUE = "overdue-check-queue";
diff --git a/overdue/src/main/java/com/ning/billing/overdue/listener/OverdueDispatcher.java b/overdue/src/main/java/com/ning/billing/overdue/listener/OverdueDispatcher.java
index 95450ea..3340a49 100644
--- a/overdue/src/main/java/com/ning/billing/overdue/listener/OverdueDispatcher.java
+++ b/overdue/src/main/java/com/ning/billing/overdue/listener/OverdueDispatcher.java
@@ -50,24 +50,7 @@ public class OverdueDispatcher {
     public void processOverdueForAccount(final UUID accountId, final InternalCallContext context) {
         final List<SubscriptionBundle> bundles = entitlementApi.getBundlesForAccount(accountId, context);
         for (final SubscriptionBundle bundle : bundles) {
-            processOverdue(Type.SUBSCRIPTION_BUNDLE, bundle, context);
-        }
-    }
-
-    public void processOverdueForBundle(final UUID bundleId, final InternalCallContext context) {
-        try {
-            final SubscriptionBundle bundle = entitlementApi.getBundleFromId(bundleId, context);
-            processOverdue(Type.SUBSCRIPTION_BUNDLE, bundle, context);
-        } catch (EntitlementUserApiException e) {
-            log.error("Error processing Overdue for bundle with id: " + bundleId.toString(), e);
-        }
-    }
-
-    public void processOverdue(final Blockable.Type type, final Blockable blockable, final InternalCallContext context) {
-        try {
-            factory.createOverdueWrapperFor(blockable).refresh(context);
-        } catch (BillingExceptionBase e) {
-            log.error(String.format("Error processing Overdue for blockable %s (type %s)", blockable.getId(), type), e);
+            processOverdue(Type.SUBSCRIPTION_BUNDLE, bundle.getId(), context);
         }
     }
 
diff --git a/overdue/src/main/java/com/ning/billing/overdue/wrapper/OverdueWrapperFactory.java b/overdue/src/main/java/com/ning/billing/overdue/wrapper/OverdueWrapperFactory.java
index aebee68..49e2122 100644
--- a/overdue/src/main/java/com/ning/billing/overdue/wrapper/OverdueWrapperFactory.java
+++ b/overdue/src/main/java/com/ning/billing/overdue/wrapper/OverdueWrapperFactory.java
@@ -62,13 +62,13 @@ public class OverdueWrapperFactory {
     }
 
     @SuppressWarnings("unchecked")
-    public <T extends Blockable> OverdueWrapper<T> createOverdueWrapperFor(final T bloackable) throws OverdueException {
+    public <T extends Blockable> OverdueWrapper<T> createOverdueWrapperFor(final T blockable) throws OverdueException {
 
-        if (bloackable instanceof SubscriptionBundle) {
-            return (OverdueWrapper<T>) new OverdueWrapper<SubscriptionBundle>((SubscriptionBundle) bloackable, api, getOverdueStateSetBundle(),
+        if (blockable instanceof SubscriptionBundle) {
+            return (OverdueWrapper<T>) new OverdueWrapper<SubscriptionBundle>((SubscriptionBundle) blockable, api, getOverdueStateSetBundle(),
                                                                               clock, billingStateCalcuatorBundle, overdueStateApplicatorBundle);
         } else {
-            throw new OverdueException(ErrorCode.OVERDUE_TYPE_NOT_SUPPORTED, bloackable.getId(), bloackable.getClass());
+            throw new OverdueException(ErrorCode.OVERDUE_TYPE_NOT_SUPPORTED, blockable.getId(), blockable.getClass());
         }
     }
 
diff --git a/payment/src/main/java/com/ning/billing/payment/core/PaymentMethodProcessor.java b/payment/src/main/java/com/ning/billing/payment/core/PaymentMethodProcessor.java
index ecf7318..bd6c5ee 100644
--- a/payment/src/main/java/com/ning/billing/payment/core/PaymentMethodProcessor.java
+++ b/payment/src/main/java/com/ning/billing/payment/core/PaymentMethodProcessor.java
@@ -299,8 +299,8 @@ public class PaymentMethodProcessor extends ProcessorBase {
                         if (!deleteDefaultPaymentMethodWithAutoPayOff) {
                             throw new PaymentApiException(ErrorCode.PAYMENT_DEL_DEFAULT_PAYMENT_METHOD, account.getId());
                         } else {
-                            final boolean isAccountAutoPayOoff = isAccountAutoPayOff(account.getId(), context);
-                            if (!isAccountAutoPayOoff) {
+                            final boolean isAccountAutoPayOff = isAccountAutoPayOff(account.getId(), context);
+                            if (!isAccountAutoPayOff) {
                                 log.info("Setting account {} to AUTO_PAY_OFF because of default payment method deletion");
                                 setAccountAutoPayOff(account.getId(), context);
                             }
diff --git a/payment/src/main/java/com/ning/billing/payment/core/ProcessorBase.java b/payment/src/main/java/com/ning/billing/payment/core/ProcessorBase.java
index 1282a03..a8f5866 100644
--- a/payment/src/main/java/com/ning/billing/payment/core/ProcessorBase.java
+++ b/payment/src/main/java/com/ning/billing/payment/core/ProcessorBase.java
@@ -22,6 +22,8 @@ import java.util.UUID;
 import java.util.concurrent.Callable;
 import java.util.concurrent.ExecutorService;
 
+import javax.annotation.Nullable;
+
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -49,6 +51,9 @@ import com.ning.billing.util.svcsapi.bus.InternalBus.EventBusException;
 import com.ning.billing.util.tag.ControlTagType;
 import com.ning.billing.util.tag.Tag;
 
+import com.google.common.base.Function;
+import com.google.common.collect.Collections2;
+
 public abstract class ProcessorBase {
 
     private static final int NB_LOCK_TRY = 5;
@@ -81,12 +86,14 @@ public abstract class ProcessorBase {
 
     protected boolean isAccountAutoPayOff(final UUID accountId, final InternalTenantContext context) {
         final List<Tag> accountTags = tagInternalApi.getTags(accountId, ObjectType.ACCOUNT, context);
-        for (final Tag cur : accountTags) {
-            if (ControlTagType.AUTO_PAY_OFF.getId().equals(cur.getTagDefinitionId())) {
-                return true;
+
+        return ControlTagType.isAutoPayOff(Collections2.transform(accountTags, new Function<Tag, UUID>() {
+            @Nullable
+            @Override
+            public UUID apply(@Nullable final Tag tag) {
+                return tag.getTagDefinitionId();
             }
-        }
-        return false;
+        }));
     }
 
     protected void setAccountAutoPayOff(final UUID accountId, final InternalCallContext context) throws PaymentApiException {