killbill-aplcache

Details

diff --git a/account/src/main/java/com/ning/billing/account/api/Account.java b/account/src/main/java/com/ning/billing/account/api/Account.java
index d437ee7..9fcc93e 100644
--- a/account/src/main/java/com/ning/billing/account/api/Account.java
+++ b/account/src/main/java/com/ning/billing/account/api/Account.java
@@ -19,9 +19,8 @@ package com.ning.billing.account.api;
 import com.ning.billing.account.dao.IAccountDao;
 import com.ning.billing.account.glue.InjectorMagic;
 import com.ning.billing.catalog.api.Currency;
+import com.ning.billing.util.eventbus.IEventBusType;
 
-import java.util.ArrayList;
-import java.util.List;
 import java.util.UUID;
 
 public class Account extends CustomizableEntityBase implements IAccount {
@@ -155,6 +154,16 @@ public class Account extends CustomizableEntityBase implements IAccount {
     }
 
     @Override
+    protected IEventBusType getCreateEvent() {
+        return new AccountCreation(id, this);
+    }
+
+    @Override
+    protected IEventBusType getUpdateEvent() {
+        return new AccountChange(id, originalData, this);
+    }
+
+    @Override
     protected void loadObject() {
         this.originalData = dao.getAccountById(id);
         this.externalKey = originalData.getExternalKey();
@@ -164,30 +173,4 @@ public class Account extends CustomizableEntityBase implements IAccount {
         this.currency = originalData.getCurrency();
         this.billCycleDay = originalData.getBillCycleDay();
     }
-
-    private List<ChangedField> getChangedFields() {
-        List<ChangedField> changedFields = new ArrayList<ChangedField>();
-
-        if (!this.externalKey.equals(originalData.getExternalKey())) {
-            changedFields.add(new ChangedField("externalKey", this.externalKey, originalData.getExternalKey()));
-        }
-        if (!this.email.equals(originalData.getEmail())) {
-            changedFields.add(new ChangedField("email", this.email, originalData.getEmail()));
-        }
-        if (!this.name.equals(originalData.getName())) {
-            changedFields.add(new ChangedField("name", this.name, originalData.getName()));
-        }
-        if (!this.phone.equals(originalData.getPhone())) {
-            changedFields.add(new ChangedField("phone", this.phone, originalData.getPhone()));
-        }
-        if (!this.currency.equals(originalData.getCurrency())) {
-            changedFields.add(new ChangedField("currency", this.currency.toString(), originalData.getCurrency().toString()));
-        }
-        if (this.billCycleDay != originalData.getBillCycleDay()) {
-            changedFields.add(new ChangedField("billCycleDay", Integer.toString(this.billCycleDay),
-                                                               Integer.toString(originalData.getBillCycleDay())));
-        }
-
-        return changedFields;
-    }
 }
diff --git a/account/src/main/java/com/ning/billing/account/api/AccountChange.java b/account/src/main/java/com/ning/billing/account/api/AccountChange.java
new file mode 100644
index 0000000..b13a851
--- /dev/null
+++ b/account/src/main/java/com/ning/billing/account/api/AccountChange.java
@@ -0,0 +1,72 @@
+/*
+ * 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.account.api;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+
+public class AccountChange implements IAccountChange {
+    private final List<IChangedField> changedFields;
+    private final UUID id;
+
+    public AccountChange(UUID id, IAccountData oldData, IAccountData newData) {
+        this.id = id;
+        this.changedFields = calculateChangedFields(oldData, newData);
+    }
+
+    @Override
+    public UUID getAccountId() {
+        return id;
+    }
+
+    @Override
+    public List<IChangedField> getChangedFields() {
+        return changedFields;
+    }
+
+    @Override
+    public boolean hasChanges() {
+        return (changedFields.size() > 0);
+    }
+
+    private List<IChangedField> calculateChangedFields(IAccountData oldData, IAccountData newData) {
+        List<IChangedField> changedFields = new ArrayList<IChangedField>();
+
+        if (!newData.getExternalKey().equals(oldData.getExternalKey())) {
+            changedFields.add(new ChangedField("externalKey", newData.getExternalKey(), oldData.getExternalKey()));
+        }
+        if (!newData.getEmail().equals(oldData.getEmail())) {
+            changedFields.add(new ChangedField("email", newData.getEmail(), oldData.getEmail()));
+        }
+        if (!newData.getName().equals(oldData.getName())) {
+            changedFields.add(new ChangedField("name", newData.getName(), oldData.getName()));
+        }
+        if (!newData.getPhone().equals(oldData.getPhone())) {
+            changedFields.add(new ChangedField("phone", newData.getPhone(), oldData.getPhone()));
+        }
+        if (!newData.getCurrency().equals(oldData.getCurrency())) {
+            changedFields.add(new ChangedField("currency", newData.getCurrency().toString(), oldData.getCurrency().toString()));
+        }
+        if (newData.getBillCycleDay() != oldData.getBillCycleDay()) {
+            changedFields.add(new ChangedField("billCycleDay", Integer.toString(newData.getBillCycleDay()),
+                                                               Integer.toString(oldData.getBillCycleDay())));
+        }
+
+        return changedFields;
+    }
+}
diff --git a/account/src/main/java/com/ning/billing/account/api/AccountCreation.java b/account/src/main/java/com/ning/billing/account/api/AccountCreation.java
new file mode 100644
index 0000000..c22f92e
--- /dev/null
+++ b/account/src/main/java/com/ning/billing/account/api/AccountCreation.java
@@ -0,0 +1,39 @@
+/*
+ * 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.account.api;
+
+import java.util.UUID;
+
+public class AccountCreation implements IAccountCreation {
+    private final UUID id;
+    private final IAccountData data;
+
+    public AccountCreation(UUID id, IAccountData data) {
+        this.id = id;
+        this.data = data;
+    }
+
+    @Override
+    public UUID getId() {
+        return id;
+    }
+
+    @Override
+    public IAccountData getData() {
+        return data;
+    }
+}
diff --git a/account/src/main/java/com/ning/billing/account/api/ChangedField.java b/account/src/main/java/com/ning/billing/account/api/ChangedField.java
index c770106..ac4220e 100644
--- a/account/src/main/java/com/ning/billing/account/api/ChangedField.java
+++ b/account/src/main/java/com/ning/billing/account/api/ChangedField.java
@@ -19,7 +19,7 @@ package com.ning.billing.account.api;
 import com.ning.billing.util.clock.Clock;
 import org.joda.time.DateTime;
 
-public class ChangedField {
+public class ChangedField implements IChangedField {
     private final String fieldName;
     private final String oldValue;
     private final String newValue;
@@ -33,4 +33,24 @@ public class ChangedField {
         this.oldValue = oldValue;
         this.newValue = newValue;
     }
+
+    @Override
+    public String getFieldName() {
+        return fieldName;
+    }
+
+    @Override
+    public String getOldValue() {
+        return oldValue;
+    }
+
+    @Override
+    public String getNewValue() {
+        return newValue;
+    }
+
+    @Override
+    public DateTime getChangeDate() {
+        return changeDate;
+    }
 }
diff --git a/account/src/main/java/com/ning/billing/account/api/CustomizableEntityBase.java b/account/src/main/java/com/ning/billing/account/api/CustomizableEntityBase.java
index 516d58c..e266428 100644
--- a/account/src/main/java/com/ning/billing/account/api/CustomizableEntityBase.java
+++ b/account/src/main/java/com/ning/billing/account/api/CustomizableEntityBase.java
@@ -38,7 +38,7 @@ public abstract class CustomizableEntityBase extends EntityBase implements ICust
 
     @Override
     public void save() {
-        saveObject();
+        super.save();
         fields.save();
     }
 
diff --git a/account/src/main/java/com/ning/billing/account/api/EntityBase.java b/account/src/main/java/com/ning/billing/account/api/EntityBase.java
index 710bd13..da67bc8 100644
--- a/account/src/main/java/com/ning/billing/account/api/EntityBase.java
+++ b/account/src/main/java/com/ning/billing/account/api/EntityBase.java
@@ -16,13 +16,23 @@
 
 package com.ning.billing.account.api;
 
+import com.ning.billing.account.glue.InjectorMagic;
+import com.ning.billing.util.eventbus.IEventBus;
+import com.ning.billing.util.eventbus.IEventBusType;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 import java.util.UUID;
 
 public abstract class EntityBase implements IEntity, IPersistable {
+    protected final Logger log = LoggerFactory.getLogger(this.getClass());
+    private static IEventBus eventBus;
+
     protected final UUID id;
     protected boolean isNew;
 
     public EntityBase(UUID id) {
+        eventBus = InjectorMagic.getEventBus();
         this.id = id;
         this.isNew = false;
     }
@@ -54,16 +64,36 @@ public abstract class EntityBase implements IEntity, IPersistable {
 
     @Override
     public void save() {
+        IEventBusType event;
+
         if (isNew) {
+            event = getCreateEvent();
             saveObject();
         } else {
+            event = getUpdateEvent();
             updateObject();
         }
+
+        if (event != null) {
+            try {
+                eventBus.post(event);
+            } catch (IEventBus.EventBusException evbe) {
+                log.error("Failed to post account change to event bus during save.", evbe);
+            }
+        }
     }
 
     protected abstract void saveObject();
     protected abstract void updateObject();
 
+    protected IEventBusType getCreateEvent() {
+        return null;
+    }
+
+    protected IEventBusType getUpdateEvent() {
+        return null;
+    }
+
     @Override
     public abstract void load();
 }
diff --git a/account/src/main/java/com/ning/billing/account/core/Engine.java b/account/src/main/java/com/ning/billing/account/core/Engine.java
new file mode 100644
index 0000000..82fcf29
--- /dev/null
+++ b/account/src/main/java/com/ning/billing/account/core/Engine.java
@@ -0,0 +1,72 @@
+/*
+ * 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.account.core;
+
+import com.ning.billing.account.api.IAccountChange;
+import com.ning.billing.account.api.IAccountService;
+import com.ning.billing.account.api.IAccountUserApi;
+import com.ning.billing.lifecycle.LyfecycleHandlerType;
+import com.ning.billing.util.eventbus.IEventBus;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.inject.Inject;
+
+public class Engine implements IAccountService, IAccountChangeListener {
+    private final static Logger log = LoggerFactory.getLogger(Engine.class);
+
+    private static final String ACCOUNT_SERVICE_NAME = "account-service";
+    private final IEventBus eventBus;
+    private final IAccountUserApi userApi;
+
+    @Inject
+    public Engine(IEventBus eventBus, IAccountUserApi userApi) {
+        this.eventBus = eventBus;
+        this.userApi = userApi;
+    }
+
+    @LyfecycleHandlerType(LyfecycleHandlerType.LyfecycleLevel.INIT_SERVICE)
+    public void initialize() {
+    }
+
+    @LyfecycleHandlerType(LyfecycleHandlerType.LyfecycleLevel.START_SERVICE)
+    public void start() {
+    }
+
+    @LyfecycleHandlerType(LyfecycleHandlerType.LyfecycleLevel.STOP_SERVICE)
+    public void stop() {
+    }
+
+    @Override
+    public IAccountUserApi getAccountUserApi() {
+        return userApi;
+    }
+
+    @Override
+    public String getName() {
+        return ACCOUNT_SERVICE_NAME;
+    }
+
+    @Override
+    public void processAccountChange(IAccountChange change) {
+        try {
+            eventBus.post(change);
+        } catch (IEventBus.EventBusException e) {
+            log.warn("Failed to post account change event.");
+        }
+    }
+}
diff --git a/account/src/main/java/com/ning/billing/account/core/IAccountChangeListener.java b/account/src/main/java/com/ning/billing/account/core/IAccountChangeListener.java
new file mode 100644
index 0000000..6abd970
--- /dev/null
+++ b/account/src/main/java/com/ning/billing/account/core/IAccountChangeListener.java
@@ -0,0 +1,23 @@
+/*
+ * 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.account.core;
+
+import com.ning.billing.account.api.IAccountChange;
+
+public interface IAccountChangeListener {
+    public void processAccountChange(IAccountChange change);
+}
diff --git a/account/src/main/java/com/ning/billing/account/glue/AccountModule.java b/account/src/main/java/com/ning/billing/account/glue/AccountModule.java
index e0b4022..62b66e1 100644
--- a/account/src/main/java/com/ning/billing/account/glue/AccountModule.java
+++ b/account/src/main/java/com/ning/billing/account/glue/AccountModule.java
@@ -21,6 +21,7 @@ import com.ning.billing.account.api.AccountService;
 import com.ning.billing.account.api.AccountUserApi;
 import com.ning.billing.account.api.IAccountService;
 import com.ning.billing.account.api.IAccountUserApi;
+import com.ning.billing.account.core.Engine;
 import com.ning.billing.account.dao.AccountDao;
 import com.ning.billing.account.dao.FieldStoreDao;
 import com.ning.billing.account.dao.IAccountDao;
@@ -29,30 +30,36 @@ import org.skife.config.ConfigurationObjectFactory;
 
 public class AccountModule extends AbstractModule {
 
-    protected void installConfig() {
+    private void installConfig() {
         final IAccountConfig config = new ConfigurationObjectFactory(System.getProperties()).build(IAccountConfig.class);
         bind(IAccountConfig.class).toInstance(config);
     }
 
-    protected void installAccountDao() {
+    private void installAccountCore() {
+        bind(IAccountService.class).to(Engine.class).asEagerSingleton();
+        bind(Engine.class).asEagerSingleton();
+    }
+
+    private void installAccountDao() {
         bind(IAccountDao.class).to(AccountDao.class).asEagerSingleton();
     }
 
-    protected void installAccountUserApi() {
+    private void installAccountUserApi() {
         bind(IAccountUserApi.class).to(AccountUserApi.class).asEagerSingleton();
     }
 
-    protected void installAccountService() {
+    private void installAccountService() {
         bind(IAccountService.class).to(AccountService.class).asEagerSingleton();
     }
 
-    protected void installFieldStore() {
+    private void installFieldStore() {
         bind(IFieldStoreDao.class).to(FieldStoreDao.class).asEagerSingleton();
     }
 
     @Override
     protected void configure() {
         installConfig();
+        installAccountCore();
         installAccountDao();
         installAccountUserApi();
         installAccountService();
diff --git a/account/src/main/java/com/ning/billing/account/glue/InjectorMagic.java b/account/src/main/java/com/ning/billing/account/glue/InjectorMagic.java
index d27a20b..d6cdd07 100644
--- a/account/src/main/java/com/ning/billing/account/glue/InjectorMagic.java
+++ b/account/src/main/java/com/ning/billing/account/glue/InjectorMagic.java
@@ -21,6 +21,7 @@ import com.google.inject.Injector;
 import com.ning.billing.account.dao.IAccountDao;
 import com.ning.billing.account.dao.IFieldStoreDao;
 import com.ning.billing.account.dao.ITagStoreDao;
+import com.ning.billing.util.eventbus.IEventBus;
 
 public class InjectorMagic {
     public static InjectorMagic instance;
@@ -59,4 +60,8 @@ public class InjectorMagic {
     public static ITagStoreDao getTagStoreDao() {
         return InjectorMagic.get().getInstance(ITagStoreDao.class);
     }
+
+    public static IEventBus getEventBus() {
+        return InjectorMagic.get().getInstance(IEventBus.class);
+    }
 }
diff --git a/api/src/main/java/com/ning/billing/account/api/IAccountChange.java b/api/src/main/java/com/ning/billing/account/api/IAccountChange.java
new file mode 100644
index 0000000..0eb2789
--- /dev/null
+++ b/api/src/main/java/com/ning/billing/account/api/IAccountChange.java
@@ -0,0 +1,30 @@
+/*
+ * 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.account.api;
+
+import com.ning.billing.util.eventbus.IEventBusType;
+
+import java.util.List;
+import java.util.UUID;
+
+public interface IAccountChange extends IEventBusType {
+    public UUID getAccountId();
+
+    public List<IChangedField> getChangedFields();
+
+    public boolean hasChanges();
+}
diff --git a/api/src/main/java/com/ning/billing/account/api/IAccountCreation.java b/api/src/main/java/com/ning/billing/account/api/IAccountCreation.java
new file mode 100644
index 0000000..69d0739
--- /dev/null
+++ b/api/src/main/java/com/ning/billing/account/api/IAccountCreation.java
@@ -0,0 +1,27 @@
+/*
+ * 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.account.api;
+
+import com.ning.billing.util.eventbus.IEventBusType;
+
+import java.util.UUID;
+
+public interface IAccountCreation extends IEventBusType {
+    public UUID getId();
+
+    public IAccountData getData();
+}
diff --git a/api/src/main/java/com/ning/billing/account/api/IChangedField.java b/api/src/main/java/com/ning/billing/account/api/IChangedField.java
new file mode 100644
index 0000000..2bb037b
--- /dev/null
+++ b/api/src/main/java/com/ning/billing/account/api/IChangedField.java
@@ -0,0 +1,26 @@
+/*
+ * 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.account.api;
+
+import org.joda.time.DateTime;
+
+public interface IChangedField {
+    public DateTime getChangeDate();
+    public String getFieldName();
+    public String getOldValue();
+    public String getNewValue();
+}
diff --git a/api/src/main/java/com/ning/billing/invoice/api/IInvoiceService.java b/api/src/main/java/com/ning/billing/invoice/api/IInvoiceService.java
index fc32e32..6f00a68 100644
--- a/api/src/main/java/com/ning/billing/invoice/api/IInvoiceService.java
+++ b/api/src/main/java/com/ning/billing/invoice/api/IInvoiceService.java
@@ -19,6 +19,6 @@ package com.ning.billing.invoice.api;
 import com.ning.billing.lifecycle.IService;
 
 public interface IInvoiceService extends IService {
-    public IInvoiceUserApi getInvoiceUserApi();
+    public IInvoiceUserApi getUserApi();
     public IInvoicePaymentApi getPaymentApi();
 }
diff --git a/invoice/src/main/java/com/ning/billing/invoice/api/InvoiceService.java b/invoice/src/main/java/com/ning/billing/invoice/api/InvoiceService.java
index 247a3c6..1d4ca79 100644
--- a/invoice/src/main/java/com/ning/billing/invoice/api/InvoiceService.java
+++ b/invoice/src/main/java/com/ning/billing/invoice/api/InvoiceService.java
@@ -21,11 +21,13 @@ import com.ning.billing.lifecycle.LyfecycleHandlerType;
 
 public class InvoiceService implements IInvoiceService {
     private static final String INVOICE_SERVICE_NAME = "invoice-service";
-    private final IInvoiceUserApi invoiceApi;
+    private final IInvoiceUserApi userApi;
+    private final IInvoicePaymentApi paymentApi;
 
     @Inject
-    public InvoiceService(IInvoiceUserApi api) {
-        this.invoiceApi = api;
+    public InvoiceService(IInvoiceUserApi userApi, IInvoicePaymentApi paymentApi) {
+        this.userApi = userApi;
+        this.paymentApi = paymentApi;
     }
 
     @Override
@@ -34,8 +36,13 @@ public class InvoiceService implements IInvoiceService {
     }
 
     @Override
-    public IInvoiceUserApi getInvoiceUserApi() {
-        return invoiceApi;
+    public IInvoiceUserApi getUserApi() {
+        return userApi;
+    }
+
+    @Override
+    public IInvoicePaymentApi getPaymentApi() {
+        return paymentApi;
     }
 
     @LyfecycleHandlerType(LyfecycleHandlerType.LyfecycleLevel.INIT_SERVICE)