Some Object Orianted refactor
This commit is contained in:
parent
1474a50e5e
commit
64824d65aa
@ -8,7 +8,7 @@ import java.io.Serializable;
|
||||
public abstract class DomainEvent implements Serializable {
|
||||
private final long sequenceId;
|
||||
private final long createdTime;
|
||||
private boolean replica = false;
|
||||
private boolean realTime = true;
|
||||
private final String eventClassName;
|
||||
|
||||
public DomainEvent(long sequenceId, long createdTime, String eventClassName) {
|
||||
@ -25,12 +25,12 @@ public abstract class DomainEvent implements Serializable {
|
||||
return createdTime;
|
||||
}
|
||||
|
||||
public boolean isReplica() {
|
||||
return replica;
|
||||
public boolean isRealTime() {
|
||||
return realTime;
|
||||
}
|
||||
|
||||
public void setReplica(boolean replica) {
|
||||
this.replica = replica;
|
||||
public void setRealTime(boolean realTime) {
|
||||
this.realTime = realTime;
|
||||
}
|
||||
|
||||
public abstract void process();
|
||||
|
@ -6,6 +6,5 @@ package com.iluwatar.event.sourcing.api;
|
||||
public interface EventProcessor {
|
||||
void process(DomainEvent domainEvent);
|
||||
void setPrecessorJournal(ProcessorJournal precessorJournal);
|
||||
void addExternalEventListener(ExternalEventListener externalEventListener);
|
||||
void recover();
|
||||
}
|
||||
|
@ -1,8 +0,0 @@
|
||||
package com.iluwatar.event.sourcing.api;
|
||||
|
||||
/**
|
||||
* Created by serdarh on 06.08.2017.
|
||||
*/
|
||||
public interface ExternalEventListener {
|
||||
void notify(DomainEvent domainEvent);
|
||||
}
|
@ -1,5 +1,12 @@
|
||||
package com.iluwatar.event.sourcing.domain;
|
||||
|
||||
import com.iluwatar.event.sourcing.event.AccountCreateEvent;
|
||||
import com.iluwatar.event.sourcing.event.MoneyDepositEvent;
|
||||
import com.iluwatar.event.sourcing.event.MoneyTransferEvent;
|
||||
import com.iluwatar.event.sourcing.event.MoneyWithdrawalEvent;
|
||||
import com.iluwatar.event.sourcing.gateway.Gateways;
|
||||
import com.iluwatar.event.sourcing.state.AccountAggregate;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -60,4 +67,64 @@ public class Account {
|
||||
", transactions=" + transactions +
|
||||
'}';
|
||||
}
|
||||
|
||||
private Transaction depositMoney(BigDecimal money) {
|
||||
this.money = this.money.add(money);
|
||||
Transaction transaction = new Transaction(accountNo,money,BigDecimal.ZERO,this.money);
|
||||
transactions.add(transaction);
|
||||
return transaction;
|
||||
}
|
||||
|
||||
private Transaction withdrawMoney(BigDecimal money) {
|
||||
this.money = this.money.subtract(money);
|
||||
Transaction transaction = new Transaction(accountNo,BigDecimal.ZERO,money,this.money);
|
||||
transactions.add(transaction);
|
||||
return transaction;
|
||||
}
|
||||
|
||||
private void handleDeposit(BigDecimal money,boolean realTime) {
|
||||
Transaction transaction = depositMoney(money);
|
||||
AccountAggregate.putAccount(this);
|
||||
if(realTime) {
|
||||
Gateways.getTransactionLogger().log(transaction);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleWithdrawal(BigDecimal money, boolean realTime) {
|
||||
if(this.money.compareTo(money)==-1){
|
||||
throw new RuntimeException("Insufficient Account Balance");
|
||||
}
|
||||
|
||||
Transaction transaction = withdrawMoney(money);
|
||||
AccountAggregate.putAccount(this);
|
||||
if(realTime) {
|
||||
Gateways.getTransactionLogger().log(transaction);
|
||||
}
|
||||
}
|
||||
|
||||
public void handleEvent(MoneyDepositEvent moneyDepositEvent) {
|
||||
handleDeposit(moneyDepositEvent.getMoney(),moneyDepositEvent.isRealTime());
|
||||
}
|
||||
|
||||
|
||||
public void handleEvent(MoneyWithdrawalEvent moneyWithdrawalEvent) {
|
||||
handleWithdrawal(moneyWithdrawalEvent.getMoney(),moneyWithdrawalEvent.isRealTime());
|
||||
}
|
||||
|
||||
|
||||
public void handleTransferFromEvent(MoneyTransferEvent moneyTransferEvent) {
|
||||
handleWithdrawal(moneyTransferEvent.getMoney(),moneyTransferEvent.isRealTime());
|
||||
}
|
||||
|
||||
public void handleTransferToEvent(MoneyTransferEvent moneyTransferEvent) {
|
||||
handleDeposit(moneyTransferEvent.getMoney(),moneyTransferEvent.isRealTime());
|
||||
}
|
||||
|
||||
public void handleEvent(AccountCreateEvent accountCreateEvent) {
|
||||
AccountAggregate.putAccount(this);
|
||||
// check if this event is replicated from journal before calling an external gateway function
|
||||
if(accountCreateEvent.isRealTime()) {
|
||||
Gateways.getAccountCreateContractSender().sendContractInfo(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ package com.iluwatar.event.sourcing.event;
|
||||
|
||||
import com.iluwatar.event.sourcing.api.DomainEvent;
|
||||
import com.iluwatar.event.sourcing.domain.Account;
|
||||
import com.iluwatar.event.sourcing.gateway.Gateways;
|
||||
import com.iluwatar.event.sourcing.state.AccountAggregate;
|
||||
|
||||
/**
|
||||
@ -33,11 +32,6 @@ public class AccountCreateEvent extends DomainEvent {
|
||||
throw new RuntimeException("Account already exists");
|
||||
}
|
||||
account = new Account(accountNo,owner);
|
||||
AccountAggregate.putAccount(account);
|
||||
|
||||
// check if this event is replicated from journal before calling an external gateway function
|
||||
if(!isReplica()) {
|
||||
Gateways.getAccountCreateContractSender().sendContractInfo(account);
|
||||
}
|
||||
account.handleEvent(this);
|
||||
}
|
||||
}
|
||||
|
@ -2,8 +2,6 @@ package com.iluwatar.event.sourcing.event;
|
||||
|
||||
import com.iluwatar.event.sourcing.api.DomainEvent;
|
||||
import com.iluwatar.event.sourcing.domain.Account;
|
||||
import com.iluwatar.event.sourcing.domain.Transaction;
|
||||
import com.iluwatar.event.sourcing.gateway.Gateways;
|
||||
import com.iluwatar.event.sourcing.state.AccountAggregate;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
@ -21,18 +19,20 @@ public class MoneyDepositEvent extends DomainEvent {
|
||||
this.accountNo = accountNo;
|
||||
}
|
||||
|
||||
public BigDecimal getMoney() {
|
||||
return money;
|
||||
}
|
||||
|
||||
public int getAccountNo() {
|
||||
return accountNo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
Account account = AccountAggregate.getAccount(accountNo);
|
||||
if(account==null){
|
||||
throw new RuntimeException("Account not found");
|
||||
}
|
||||
account.setMoney(account.getMoney().add(money));
|
||||
Transaction transaction = new Transaction(accountNo,money,BigDecimal.ZERO,account.getMoney());
|
||||
account.getTransactions().add(transaction);
|
||||
AccountAggregate.putAccount(account);
|
||||
if(!isReplica()) {
|
||||
Gateways.getTransactionLogger().log(transaction);
|
||||
}
|
||||
account.handleEvent(this);
|
||||
}
|
||||
}
|
||||
|
@ -2,8 +2,6 @@ package com.iluwatar.event.sourcing.event;
|
||||
|
||||
import com.iluwatar.event.sourcing.api.DomainEvent;
|
||||
import com.iluwatar.event.sourcing.domain.Account;
|
||||
import com.iluwatar.event.sourcing.domain.Transaction;
|
||||
import com.iluwatar.event.sourcing.gateway.Gateways;
|
||||
import com.iluwatar.event.sourcing.state.AccountAggregate;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
@ -23,6 +21,18 @@ public class MoneyTransferEvent extends DomainEvent {
|
||||
this.accountNoTo = accountNoTo;
|
||||
}
|
||||
|
||||
public BigDecimal getMoney() {
|
||||
return money;
|
||||
}
|
||||
|
||||
public int getAccountNoFrom() {
|
||||
return accountNoFrom;
|
||||
}
|
||||
|
||||
public int getAccountNoTo() {
|
||||
return accountNoTo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
Account accountFrom = AccountAggregate.getAccount(accountNoFrom);
|
||||
@ -33,24 +43,8 @@ public class MoneyTransferEvent extends DomainEvent {
|
||||
if(accountTo==null){
|
||||
throw new RuntimeException("Account not found"+ accountTo);
|
||||
}
|
||||
if(accountFrom.getMoney().compareTo(money)==-1){
|
||||
throw new RuntimeException("Insufficient Account Balance");
|
||||
}
|
||||
accountFrom.setMoney(accountFrom.getMoney().subtract(money));
|
||||
accountTo.setMoney(accountTo.getMoney().add(money));
|
||||
|
||||
Transaction transactionFrom = new Transaction(accountNoFrom,BigDecimal.ZERO,money,accountFrom.getMoney());
|
||||
accountFrom.getTransactions().add(transactionFrom);
|
||||
|
||||
Transaction transactionTo = new Transaction(accountNoTo,money,BigDecimal.ZERO,accountTo.getMoney());
|
||||
accountTo.getTransactions().add(transactionTo);
|
||||
|
||||
AccountAggregate.putAccount(accountFrom);
|
||||
AccountAggregate.putAccount(accountTo);
|
||||
|
||||
if(!isReplica()) {
|
||||
Gateways.getTransactionLogger().log(transactionFrom);
|
||||
Gateways.getTransactionLogger().log(transactionTo);
|
||||
}
|
||||
accountFrom.handleTransferFromEvent(this);
|
||||
accountTo.handleTransferToEvent(this);
|
||||
}
|
||||
}
|
||||
|
@ -2,8 +2,6 @@ package com.iluwatar.event.sourcing.event;
|
||||
|
||||
import com.iluwatar.event.sourcing.api.DomainEvent;
|
||||
import com.iluwatar.event.sourcing.domain.Account;
|
||||
import com.iluwatar.event.sourcing.domain.Transaction;
|
||||
import com.iluwatar.event.sourcing.gateway.Gateways;
|
||||
import com.iluwatar.event.sourcing.state.AccountAggregate;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
@ -21,18 +19,20 @@ public class MoneyWithdrawalEvent extends DomainEvent {
|
||||
this.accountNo = accountNo;
|
||||
}
|
||||
|
||||
public BigDecimal getMoney() {
|
||||
return money;
|
||||
}
|
||||
|
||||
public int getAccountNo() {
|
||||
return accountNo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
Account account = AccountAggregate.getAccount(accountNo);
|
||||
if(account==null){
|
||||
throw new RuntimeException("Account not found");
|
||||
}
|
||||
account.setMoney(account.getMoney().subtract(money));
|
||||
Transaction transaction = new Transaction(accountNo,BigDecimal.ZERO,money,account.getMoney());
|
||||
account.getTransactions().add(transaction);
|
||||
AccountAggregate.putAccount(account);
|
||||
if(!isReplica()) {
|
||||
Gateways.getTransactionLogger().log(transaction);
|
||||
}
|
||||
account.handleEvent(this);
|
||||
}
|
||||
}
|
||||
|
@ -93,7 +93,7 @@ public class JsonFileJournal implements ProcessorJournal{
|
||||
throw new RuntimeException("Journal Event not recegnized");
|
||||
}
|
||||
|
||||
domainEvent.setReplica(true);
|
||||
domainEvent.setRealTime(false);
|
||||
return domainEvent;
|
||||
}
|
||||
}
|
||||
|
@ -2,23 +2,17 @@ package com.iluwatar.event.sourcing.processor;
|
||||
|
||||
import com.iluwatar.event.sourcing.api.DomainEvent;
|
||||
import com.iluwatar.event.sourcing.api.EventProcessor;
|
||||
import com.iluwatar.event.sourcing.api.ExternalEventListener;
|
||||
import com.iluwatar.event.sourcing.api.ProcessorJournal;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by serdarh on 06.08.2017.
|
||||
*/
|
||||
public class DomainEventProcessor implements EventProcessor {
|
||||
|
||||
private ProcessorJournal precessorJournal;
|
||||
private List<ExternalEventListener> externalEventListeners = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void process(DomainEvent domainEvent) {
|
||||
externalEventListeners.forEach(externalEventListener -> externalEventListener.notify(domainEvent));
|
||||
domainEvent.process();
|
||||
precessorJournal.write(domainEvent);
|
||||
}
|
||||
@ -28,11 +22,6 @@ public class DomainEventProcessor implements EventProcessor {
|
||||
this.precessorJournal = precessorJournal;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addExternalEventListener(ExternalEventListener externalEventListener) {
|
||||
externalEventListeners.add(externalEventListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recover() {
|
||||
DomainEvent domainEvent;
|
||||
|
Loading…
x
Reference in New Issue
Block a user