Code formating

This commit is contained in:
Serdar Hamzaoğulları
2017-08-13 00:08:35 +03:00
parent 64824d65aa
commit 4b3435c550
22 changed files with 1396 additions and 448 deletions

View File

@ -0,0 +1,184 @@
@startuml
package com.iluwatar.event.sourcing.journal {
class JsonFileJournal {
- aFile : File
- events : List<String>
- index : int
+ JsonFileJournal()
+ readNext() : DomainEvent
+ reset()
+ write(domainEvent : DomainEvent)
}
}
package com.iluwatar.event.sourcing.processor {
class DomainEventProcessor {
- precessorJournal : ProcessorJournal
+ DomainEventProcessor()
+ process(domainEvent : DomainEvent)
+ recover()
+ setPrecessorJournal(precessorJournal : ProcessorJournal)
}
}
package com.iluwatar.event.sourcing.service {
class AccountService {
- eventProcessor : EventProcessor
+ AccountService(eventProcessor : EventProcessor)
+ createAccount(accountNo : int, owner : String)
}
class MoneyTransactionService {
- eventProcessor : EventProcessor
+ MoneyTransactionService(eventProcessor : EventProcessor)
+ depositMoney(accountNo : int, money : BigDecimal)
+ transferMoney(accountNoFrom : int, accountNoTo : int, money : BigDecimal)
+ withdrawalMoney(accountNo : int, money : BigDecimal)
}
class SequenceIdGenerator {
- sequenceId : long {static}
+ SequenceIdGenerator()
+ nextSequenceId() : long {static}
}
}
package com.iluwatar.event.sourcing.event {
class AccountCreateEvent {
- accountNo : int
- owner : String
+ AccountCreateEvent(sequenceId : long, createdTime : long, accountNo : int, owner : String)
+ getAccountNo() : int
+ getOwner() : String
+ process()
}
class MoneyDepositEvent {
- accountNo : int
- money : BigDecimal
+ MoneyDepositEvent(sequenceId : long, createdTime : long, accountNo : int, money : BigDecimal)
+ getAccountNo() : int
+ getMoney() : BigDecimal
+ process()
}
class MoneyTransferEvent {
- accountNoFrom : int
- accountNoTo : int
- money : BigDecimal
+ MoneyTransferEvent(sequenceId : long, createdTime : long, money : BigDecimal, accountNoFrom : int, accountNoTo : int)
+ getAccountNoFrom() : int
+ getAccountNoTo() : int
+ getMoney() : BigDecimal
+ process()
}
class MoneyWithdrawalEvent {
- accountNo : int
- money : BigDecimal
+ MoneyWithdrawalEvent(sequenceId : long, createdTime : long, accountNo : int, money : BigDecimal)
+ getAccountNo() : int
+ getMoney() : BigDecimal
+ process()
}
}
package com.iluwatar.event.sourcing.gateway {
class AccountCreateContractSender {
+ AccountCreateContractSender()
+ sendContractInfo(account : Account)
}
class Gateways {
- accountCreateContractSender : AccountCreateContractSender {static}
- transactionLogger : TransactionLogger {static}
+ Gateways()
+ getAccountCreateContractSender() : AccountCreateContractSender {static}
+ getTransactionLogger() : TransactionLogger {static}
}
class TransactionLogger {
+ TransactionLogger()
+ log(transaction : Transaction)
}
}
package com.iluwatar.event.sourcing.app {
class App {
+ App()
+ main(args : String[]) {static}
}
}
package com.iluwatar.event.sourcing.state {
class AccountAggregate {
- accounts : Map<Integer, Account> {static}
+ AccountAggregate()
+ getAccount(accountNo : int) : Account {static}
+ putAccount(account : Account) {static}
+ resetState() {static}
}
}
package com.iluwatar.event.sourcing.domain {
class Account {
- accountNo : int
- money : BigDecimal
- owner : String
- transactions : List<Transaction>
+ Account(accountNo : int, owner : String)
+ copy() : Account
- depositMoney(money : BigDecimal) : Transaction
+ getAccountNo() : int
+ getMoney() : BigDecimal
+ getOwner() : String
+ getTransactions() : List<Transaction>
- handleDeposit(money : BigDecimal, realTime : boolean)
+ handleEvent(accountCreateEvent : AccountCreateEvent)
+ handleEvent(moneyDepositEvent : MoneyDepositEvent)
+ handleEvent(moneyWithdrawalEvent : MoneyWithdrawalEvent)
+ handleTransferFromEvent(moneyTransferEvent : MoneyTransferEvent)
+ handleTransferToEvent(moneyTransferEvent : MoneyTransferEvent)
- handleWithdrawal(money : BigDecimal, realTime : boolean)
+ setMoney(money : BigDecimal)
+ setTransactions(transactions : List<Transaction>)
+ toString() : String
- withdrawMoney(money : BigDecimal) : Transaction
}
class Transaction {
- accountNo : int
- lastBalance : BigDecimal
- moneyIn : BigDecimal
- moneyOut : BigDecimal
+ Transaction(accountNo : int, moneyIn : BigDecimal, moneyOut : BigDecimal, lastBalance : BigDecimal)
+ getAccountNo() : int
+ getLastBalance() : BigDecimal
+ getMoneyIn() : BigDecimal
+ getMoneyOut() : BigDecimal
+ toString() : String
}
}
package com.iluwatar.event.sourcing.api {
abstract class DomainEvent {
- createdTime : long
- eventClassName : String
- realTime : boolean
- sequenceId : long
+ DomainEvent(sequenceId : long, createdTime : long, eventClassName : String)
+ getCreatedTime() : long
+ getEventClassName() : String
+ getSequenceId() : long
+ isRealTime() : boolean
+ process() {abstract}
+ setRealTime(realTime : boolean)
}
interface EventProcessor {
+ process(DomainEvent) {abstract}
+ recover() {abstract}
+ setPrecessorJournal(ProcessorJournal) {abstract}
}
interface ProcessorJournal {
+ readNext() : DomainEvent {abstract}
+ reset() {abstract}
+ write(DomainEvent) {abstract}
}
}
Gateways --> "-accountCreateContractSender" AccountCreateContractSender
DomainEventProcessor --> "-precessorJournal" ProcessorJournal
Account --> "-transactions" Transaction
Gateways --> "-transactionLogger" TransactionLogger
AccountService --> "-eventProcessor" EventProcessor
MoneyTransactionService --> "-eventProcessor" EventProcessor
AccountCreateEvent --|> DomainEvent
MoneyDepositEvent --|> DomainEvent
MoneyTransferEvent --|> DomainEvent
MoneyWithdrawalEvent --|> DomainEvent
JsonFileJournal ..|> ProcessorJournal
DomainEventProcessor ..|> EventProcessor
@enduml

View File

@ -1,3 +1,25 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.event.sourcing.api; package com.iluwatar.event.sourcing.api;
import java.io.Serializable; import java.io.Serializable;
@ -6,35 +28,71 @@ import java.io.Serializable;
* Created by serdarh on 06.08.2017. * Created by serdarh on 06.08.2017.
*/ */
public abstract class DomainEvent implements Serializable { public abstract class DomainEvent implements Serializable {
private final long sequenceId; private final long sequenceId;
private final long createdTime; private final long createdTime;
private boolean realTime = true;
private final String eventClassName; private final String eventClassName;
private boolean realTime = true;
/**
* Instantiates a new Domain event.
*
* @param sequenceId the sequence id
* @param createdTime the created time
* @param eventClassName the event class name
*/
public DomainEvent(long sequenceId, long createdTime, String eventClassName) { public DomainEvent(long sequenceId, long createdTime, String eventClassName) {
this.sequenceId = sequenceId; this.sequenceId = sequenceId;
this.createdTime = createdTime; this.createdTime = createdTime;
this.eventClassName = eventClassName; this.eventClassName = eventClassName;
} }
/**
* Gets sequence id.
*
* @return the sequence id
*/
public long getSequenceId() { public long getSequenceId() {
return sequenceId; return sequenceId;
} }
/**
* Gets created time.
*
* @return the created time
*/
public long getCreatedTime() { public long getCreatedTime() {
return createdTime; return createdTime;
} }
/**
* Is real time boolean.
*
* @return the boolean
*/
public boolean isRealTime() { public boolean isRealTime() {
return realTime; return realTime;
} }
/**
* Sets real time.
*
* @param realTime the real time
*/
public void setRealTime(boolean realTime) { public void setRealTime(boolean realTime) {
this.realTime = realTime; this.realTime = realTime;
} }
/**
* Process.
*/
public abstract void process(); public abstract void process();
/**
* Gets event class name.
*
* @return the event class name
*/
public String getEventClassName() { public String getEventClassName() {
return eventClassName; return eventClassName;
} }

View File

@ -1,10 +1,48 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.event.sourcing.api; package com.iluwatar.event.sourcing.api;
/** /**
* Created by serdarh on 06.08.2017. * Created by serdarh on 06.08.2017.
*/ */
public interface EventProcessor { public interface EventProcessor {
/**
* Process.
*
* @param domainEvent the domain event
*/
void process(DomainEvent domainEvent); void process(DomainEvent domainEvent);
/**
* Sets precessor journal.
*
* @param precessorJournal the precessor journal
*/
void setPrecessorJournal(ProcessorJournal precessorJournal); void setPrecessorJournal(ProcessorJournal precessorJournal);
/**
* Recover.
*/
void recover(); void recover();
} }

View File

@ -1,10 +1,48 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.event.sourcing.api; package com.iluwatar.event.sourcing.api;
/** /**
* Created by serdarh on 06.08.2017. * Created by serdarh on 06.08.2017.
*/ */
public interface ProcessorJournal { public interface ProcessorJournal {
/**
* Write.
*
* @param domainEvent the domain event
*/
void write(DomainEvent domainEvent); void write(DomainEvent domainEvent);
/**
* Reset.
*/
void reset(); void reset();
/**
* Read next domain event.
*
* @return the domain event
*/
DomainEvent readNext(); DomainEvent readNext();
} }

View File

@ -1,3 +1,25 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.event.sourcing.app; package com.iluwatar.event.sourcing.app;
import com.iluwatar.event.sourcing.journal.JsonFileJournal; import com.iluwatar.event.sourcing.journal.JsonFileJournal;
@ -5,7 +27,6 @@ import com.iluwatar.event.sourcing.processor.DomainEventProcessor;
import com.iluwatar.event.sourcing.service.AccountService; import com.iluwatar.event.sourcing.service.AccountService;
import com.iluwatar.event.sourcing.service.MoneyTransactionService; import com.iluwatar.event.sourcing.service.MoneyTransactionService;
import com.iluwatar.event.sourcing.state.AccountAggregate; import com.iluwatar.event.sourcing.state.AccountAggregate;
import java.math.BigDecimal; import java.math.BigDecimal;
/** /**
@ -13,6 +34,11 @@ import java.math.BigDecimal;
*/ */
public class App { public class App {
/**
* The entry point of application.
*
* @param args the input arguments
*/
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Running the system first time............"); System.out.println("Running the system first time............");
@ -24,14 +50,15 @@ public class App {
System.out.println("Creating th accounts............"); System.out.println("Creating th accounts............");
AccountService accountService = new AccountService(domainEventProcessor); AccountService accountService = new AccountService(domainEventProcessor);
MoneyTransactionService moneyTransactionService = new MoneyTransactionService(domainEventProcessor); MoneyTransactionService moneyTransactionService = new MoneyTransactionService(
domainEventProcessor);
accountService.createAccount(1, "Daenerys Targaryen"); accountService.createAccount(1, "Daenerys Targaryen");
accountService.createAccount(2, "Jon Snow"); accountService.createAccount(2, "Jon Snow");
System.out.println("Do some money operations............"); System.out.println("Do some money operations............");
moneyTransactionService.depositMoney(1, new BigDecimal("100000")); moneyTransactionService.depositMoney(1, new BigDecimal("100000"));
moneyTransactionService.depositMoney(2,new BigDecimal("10")); moneyTransactionService.depositMoney(2, new BigDecimal("100"));
moneyTransactionService.transferMoney(1, 2, new BigDecimal("10000")); moneyTransactionService.transferMoney(1, 2, new BigDecimal("10000"));
moneyTransactionService.withdrawalMoney(2, new BigDecimal("1000")); moneyTransactionService.withdrawalMoney(2, new BigDecimal("1000"));

View File

@ -1,3 +1,25 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.event.sourcing.domain; package com.iluwatar.event.sourcing.domain;
import com.iluwatar.event.sourcing.event.AccountCreateEvent; import com.iluwatar.event.sourcing.event.AccountCreateEvent;
@ -6,7 +28,6 @@ import com.iluwatar.event.sourcing.event.MoneyTransferEvent;
import com.iluwatar.event.sourcing.event.MoneyWithdrawalEvent; import com.iluwatar.event.sourcing.event.MoneyWithdrawalEvent;
import com.iluwatar.event.sourcing.gateway.Gateways; import com.iluwatar.event.sourcing.gateway.Gateways;
import com.iluwatar.event.sourcing.state.AccountAggregate; import com.iluwatar.event.sourcing.state.AccountAggregate;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -15,11 +36,18 @@ import java.util.List;
* Created by serdarh on 06.08.2017. * Created by serdarh on 06.08.2017.
*/ */
public class Account { public class Account {
private final int accountNo; private final int accountNo;
private final String owner; private final String owner;
private BigDecimal money; private BigDecimal money;
private List<Transaction> transactions; private List<Transaction> transactions;
/**
* Instantiates a new Account.
*
* @param accountNo the account no
* @param owner the owner
*/
public Account(int accountNo, String owner) { public Account(int accountNo, String owner) {
this.accountNo = accountNo; this.accountNo = accountNo;
this.owner = owner; this.owner = owner;
@ -27,30 +55,65 @@ public class Account {
transactions = new ArrayList<>(); transactions = new ArrayList<>();
} }
/**
* Gets account no.
*
* @return the account no
*/
public int getAccountNo() { public int getAccountNo() {
return accountNo; return accountNo;
} }
/**
* Gets owner.
*
* @return the owner
*/
public String getOwner() { public String getOwner() {
return owner; return owner;
} }
/**
* Gets money.
*
* @return the money
*/
public BigDecimal getMoney() { public BigDecimal getMoney() {
return money; return money;
} }
public List<Transaction> getTransactions() { /**
return transactions; * Sets money.
} *
* @param money the money
*/
public void setMoney(BigDecimal money) { public void setMoney(BigDecimal money) {
this.money = money; this.money = money;
} }
/**
* Gets transactions.
*
* @return the transactions
*/
public List<Transaction> getTransactions() {
return transactions;
}
/**
* Sets transactions.
*
* @param transactions the transactions
*/
public void setTransactions(List<Transaction> transactions) { public void setTransactions(List<Transaction> transactions) {
this.transactions = transactions; this.transactions = transactions;
} }
/**
* Copy account.
*
* @return the account
*/
public Account copy() { public Account copy() {
Account account = new Account(accountNo, owner); Account account = new Account(accountNo, owner);
account.setMoney(money); account.setMoney(money);
@ -60,12 +123,12 @@ public class Account {
@Override @Override
public String toString() { public String toString() {
return "Account{" + return "Account{"
"accountNo=" + accountNo + + "accountNo=" + accountNo
", owner='" + owner + '\'' + + ", owner='" + owner + '\''
", money=" + money + + ", money=" + money
", transactions=" + transactions + + ", transactions=" + transactions
'}'; + '}';
} }
private Transaction depositMoney(BigDecimal money) { private Transaction depositMoney(BigDecimal money) {
@ -102,24 +165,30 @@ public class Account {
} }
} }
/**
* Handle event.
*
* @param moneyDepositEvent the money deposit event
*/
public void handleEvent(MoneyDepositEvent moneyDepositEvent) { public void handleEvent(MoneyDepositEvent moneyDepositEvent) {
handleDeposit(moneyDepositEvent.getMoney(), moneyDepositEvent.isRealTime()); handleDeposit(moneyDepositEvent.getMoney(), moneyDepositEvent.isRealTime());
} }
/**
* Handle event.
*
* @param moneyWithdrawalEvent the money withdrawal event
*/
public void handleEvent(MoneyWithdrawalEvent moneyWithdrawalEvent) { public void handleEvent(MoneyWithdrawalEvent moneyWithdrawalEvent) {
handleWithdrawal(moneyWithdrawalEvent.getMoney(), moneyWithdrawalEvent.isRealTime()); handleWithdrawal(moneyWithdrawalEvent.getMoney(), moneyWithdrawalEvent.isRealTime());
} }
/**
public void handleTransferFromEvent(MoneyTransferEvent moneyTransferEvent) { * Handle event.
handleWithdrawal(moneyTransferEvent.getMoney(),moneyTransferEvent.isRealTime()); *
} * @param accountCreateEvent the account create event
*/
public void handleTransferToEvent(MoneyTransferEvent moneyTransferEvent) {
handleDeposit(moneyTransferEvent.getMoney(),moneyTransferEvent.isRealTime());
}
public void handleEvent(AccountCreateEvent accountCreateEvent) { public void handleEvent(AccountCreateEvent accountCreateEvent) {
AccountAggregate.putAccount(this); AccountAggregate.putAccount(this);
// check if this event is replicated from journal before calling an external gateway function // check if this event is replicated from journal before calling an external gateway function
@ -127,4 +196,24 @@ public class Account {
Gateways.getAccountCreateContractSender().sendContractInfo(this); Gateways.getAccountCreateContractSender().sendContractInfo(this);
} }
} }
/**
* Handle transfer from event.
*
* @param moneyTransferEvent the money transfer event
*/
public void handleTransferFromEvent(MoneyTransferEvent moneyTransferEvent) {
handleWithdrawal(moneyTransferEvent.getMoney(), moneyTransferEvent.isRealTime());
}
/**
* Handle transfer to event.
*
* @param moneyTransferEvent the money transfer event
*/
public void handleTransferToEvent(MoneyTransferEvent moneyTransferEvent) {
handleDeposit(moneyTransferEvent.getMoney(), moneyTransferEvent.isRealTime());
}
} }

View File

@ -1,3 +1,25 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.event.sourcing.domain; package com.iluwatar.event.sourcing.domain;
import java.math.BigDecimal; import java.math.BigDecimal;
@ -6,41 +28,71 @@ import java.math.BigDecimal;
* Created by serdarh on 06.08.2017. * Created by serdarh on 06.08.2017.
*/ */
public class Transaction { public class Transaction {
private final int accountNo; private final int accountNo;
private final BigDecimal moneyIn; private final BigDecimal moneyIn;
private final BigDecimal moneyOut; private final BigDecimal moneyOut;
private final BigDecimal lastBalance; private final BigDecimal lastBalance;
public Transaction(int accountNo, BigDecimal moneyIn, BigDecimal moneyOut, BigDecimal lastBalance) { /**
* Instantiates a new Transaction.
*
* @param accountNo the account no
* @param moneyIn the money in
* @param moneyOut the money out
* @param lastBalance the last balance
*/
public Transaction(int accountNo, BigDecimal moneyIn, BigDecimal moneyOut,
BigDecimal lastBalance) {
this.accountNo = accountNo; this.accountNo = accountNo;
this.moneyIn = moneyIn; this.moneyIn = moneyIn;
this.moneyOut = moneyOut; this.moneyOut = moneyOut;
this.lastBalance = lastBalance; this.lastBalance = lastBalance;
} }
/**
* Gets account no.
*
* @return the account no
*/
public int getAccountNo() { public int getAccountNo() {
return accountNo; return accountNo;
} }
/**
* Gets money in.
*
* @return the money in
*/
public BigDecimal getMoneyIn() { public BigDecimal getMoneyIn() {
return moneyIn; return moneyIn;
} }
/**
* Gets money out.
*
* @return the money out
*/
public BigDecimal getMoneyOut() { public BigDecimal getMoneyOut() {
return moneyOut; return moneyOut;
} }
/**
* Gets last balance.
*
* @return the last balance
*/
public BigDecimal getLastBalance() { public BigDecimal getLastBalance() {
return lastBalance; return lastBalance;
} }
@Override @Override
public String toString() { public String toString() {
return "Transaction{" + return "Transaction{"
"accountNo=" + accountNo + + "accountNo=" + accountNo
", moneyIn=" + moneyIn + + ", moneyIn=" + moneyIn
", moneyOut=" + moneyOut + + ", moneyOut=" + moneyOut
", lastBalance=" + lastBalance + + ", lastBalance=" + lastBalance
'}'; + '}';
} }
} }

View File

@ -1,3 +1,25 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.event.sourcing.event; package com.iluwatar.event.sourcing.event;
import com.iluwatar.event.sourcing.api.DomainEvent; import com.iluwatar.event.sourcing.api.DomainEvent;
@ -8,19 +30,38 @@ import com.iluwatar.event.sourcing.state.AccountAggregate;
* Created by serdarh on 06.08.2017. * Created by serdarh on 06.08.2017.
*/ */
public class AccountCreateEvent extends DomainEvent { public class AccountCreateEvent extends DomainEvent {
private final int accountNo; private final int accountNo;
private final String owner; private final String owner;
/**
* Instantiates a new Account create event.
*
* @param sequenceId the sequence id
* @param createdTime the created time
* @param accountNo the account no
* @param owner the owner
*/
public AccountCreateEvent(long sequenceId, long createdTime, int accountNo, String owner) { public AccountCreateEvent(long sequenceId, long createdTime, int accountNo, String owner) {
super(sequenceId, createdTime, "AccountCreateEvent"); super(sequenceId, createdTime, "AccountCreateEvent");
this.accountNo = accountNo; this.accountNo = accountNo;
this.owner = owner; this.owner = owner;
} }
/**
* Gets account no.
*
* @return the account no
*/
public int getAccountNo() { public int getAccountNo() {
return accountNo; return accountNo;
} }
/**
* Gets owner.
*
* @return the owner
*/
public String getOwner() { public String getOwner() {
return owner; return owner;
} }

View File

@ -1,28 +1,68 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.event.sourcing.event; package com.iluwatar.event.sourcing.event;
import com.iluwatar.event.sourcing.api.DomainEvent; import com.iluwatar.event.sourcing.api.DomainEvent;
import com.iluwatar.event.sourcing.domain.Account; import com.iluwatar.event.sourcing.domain.Account;
import com.iluwatar.event.sourcing.state.AccountAggregate; import com.iluwatar.event.sourcing.state.AccountAggregate;
import java.math.BigDecimal; import java.math.BigDecimal;
/** /**
* Created by serdarh on 06.08.2017. * Created by serdarh on 06.08.2017.
*/ */
public class MoneyDepositEvent extends DomainEvent { public class MoneyDepositEvent extends DomainEvent {
private BigDecimal money; private BigDecimal money;
private int accountNo; private int accountNo;
/**
* Instantiates a new Money deposit event.
*
* @param sequenceId the sequence id
* @param createdTime the created time
* @param accountNo the account no
* @param money the money
*/
public MoneyDepositEvent(long sequenceId, long createdTime, int accountNo, BigDecimal money) { public MoneyDepositEvent(long sequenceId, long createdTime, int accountNo, BigDecimal money) {
super(sequenceId, createdTime, "MoneyDepositEvent"); super(sequenceId, createdTime, "MoneyDepositEvent");
this.money = money; this.money = money;
this.accountNo = accountNo; this.accountNo = accountNo;
} }
/**
* Gets money.
*
* @return the money
*/
public BigDecimal getMoney() { public BigDecimal getMoney() {
return money; return money;
} }
/**
* Gets account no.
*
* @return the account no
*/
public int getAccountNo() { public int getAccountNo() {
return accountNo; return accountNo;
} }

View File

@ -1,34 +1,81 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.event.sourcing.event; package com.iluwatar.event.sourcing.event;
import com.iluwatar.event.sourcing.api.DomainEvent; import com.iluwatar.event.sourcing.api.DomainEvent;
import com.iluwatar.event.sourcing.domain.Account; import com.iluwatar.event.sourcing.domain.Account;
import com.iluwatar.event.sourcing.state.AccountAggregate; import com.iluwatar.event.sourcing.state.AccountAggregate;
import java.math.BigDecimal; import java.math.BigDecimal;
/** /**
* Created by serdarh on 06.08.2017. * Created by serdarh on 06.08.2017.
*/ */
public class MoneyTransferEvent extends DomainEvent { public class MoneyTransferEvent extends DomainEvent {
private BigDecimal money; private BigDecimal money;
private int accountNoFrom; private int accountNoFrom;
private int accountNoTo; private int accountNoTo;
public MoneyTransferEvent(long sequenceId, long createdTime, BigDecimal money, int accountNoFrom, int accountNoTo) { /**
* Instantiates a new Money transfer event.
*
* @param sequenceId the sequence id
* @param createdTime the created time
* @param money the money
* @param accountNoFrom the account no from
* @param accountNoTo the account no to
*/
public MoneyTransferEvent(long sequenceId, long createdTime, BigDecimal money, int accountNoFrom,
int accountNoTo) {
super(sequenceId, createdTime, "MoneyTransferEvent"); super(sequenceId, createdTime, "MoneyTransferEvent");
this.money = money; this.money = money;
this.accountNoFrom = accountNoFrom; this.accountNoFrom = accountNoFrom;
this.accountNoTo = accountNoTo; this.accountNoTo = accountNoTo;
} }
/**
* Gets money.
*
* @return the money
*/
public BigDecimal getMoney() { public BigDecimal getMoney() {
return money; return money;
} }
/**
* Gets account no from.
*
* @return the account no from
*/
public int getAccountNoFrom() { public int getAccountNoFrom() {
return accountNoFrom; return accountNoFrom;
} }
/**
* Gets account no to.
*
* @return the account no to
*/
public int getAccountNoTo() { public int getAccountNoTo() {
return accountNoTo; return accountNoTo;
} }

View File

@ -1,28 +1,68 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.event.sourcing.event; package com.iluwatar.event.sourcing.event;
import com.iluwatar.event.sourcing.api.DomainEvent; import com.iluwatar.event.sourcing.api.DomainEvent;
import com.iluwatar.event.sourcing.domain.Account; import com.iluwatar.event.sourcing.domain.Account;
import com.iluwatar.event.sourcing.state.AccountAggregate; import com.iluwatar.event.sourcing.state.AccountAggregate;
import java.math.BigDecimal; import java.math.BigDecimal;
/** /**
* Created by serdarh on 06.08.2017. * Created by serdarh on 06.08.2017.
*/ */
public class MoneyWithdrawalEvent extends DomainEvent { public class MoneyWithdrawalEvent extends DomainEvent {
private BigDecimal money; private BigDecimal money;
private int accountNo; private int accountNo;
/**
* Instantiates a new Money withdrawal event.
*
* @param sequenceId the sequence id
* @param createdTime the created time
* @param accountNo the account no
* @param money the money
*/
public MoneyWithdrawalEvent(long sequenceId, long createdTime, int accountNo, BigDecimal money) { public MoneyWithdrawalEvent(long sequenceId, long createdTime, int accountNo, BigDecimal money) {
super(sequenceId, createdTime, "MoneyWithdrawalEvent"); super(sequenceId, createdTime, "MoneyWithdrawalEvent");
this.money = money; this.money = money;
this.accountNo = accountNo; this.accountNo = accountNo;
} }
/**
* Gets money.
*
* @return the money
*/
public BigDecimal getMoney() { public BigDecimal getMoney() {
return money; return money;
} }
/**
* Gets account no.
*
* @return the account no
*/
public int getAccountNo() { public int getAccountNo() {
return accountNo; return accountNo;
} }

View File

@ -1,3 +1,25 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.event.sourcing.gateway; package com.iluwatar.event.sourcing.gateway;
import com.iluwatar.event.sourcing.domain.Account; import com.iluwatar.event.sourcing.domain.Account;
@ -6,6 +28,12 @@ import com.iluwatar.event.sourcing.domain.Account;
* Created by serdarh on 06.08.2017. * Created by serdarh on 06.08.2017.
*/ */
public class AccountCreateContractSender { public class AccountCreateContractSender {
/**
* Send contract info.
*
* @param account the account
*/
public void sendContractInfo(Account account) { public void sendContractInfo(Account account) {
// an example imaginary funciton which sends account info to some external end point // an example imaginary funciton which sends account info to some external end point
} }

View File

@ -1,16 +1,49 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.event.sourcing.gateway; package com.iluwatar.event.sourcing.gateway;
/** /**
* Created by serdarh on 06.08.2017. * Created by serdarh on 06.08.2017.
*/ */
public class Gateways { public class Gateways {
private static AccountCreateContractSender accountCreateContractSender = new AccountCreateContractSender(); private static AccountCreateContractSender accountCreateContractSender = new AccountCreateContractSender();
private static TransactionLogger transactionLogger = new TransactionLogger(); private static TransactionLogger transactionLogger = new TransactionLogger();
/**
* Gets account create contract sender.
*
* @return the account create contract sender
*/
public static AccountCreateContractSender getAccountCreateContractSender() { public static AccountCreateContractSender getAccountCreateContractSender() {
return accountCreateContractSender; return accountCreateContractSender;
} }
/**
* Gets transaction logger.
*
* @return the transaction logger
*/
public static TransactionLogger getTransactionLogger() { public static TransactionLogger getTransactionLogger() {
return transactionLogger; return transactionLogger;
} }

View File

@ -1,3 +1,25 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.event.sourcing.gateway; package com.iluwatar.event.sourcing.gateway;
import com.iluwatar.event.sourcing.domain.Transaction; import com.iluwatar.event.sourcing.domain.Transaction;
@ -6,6 +28,12 @@ import com.iluwatar.event.sourcing.domain.Transaction;
* Created by serdarh on 06.08.2017. * Created by serdarh on 06.08.2017.
*/ */
public class TransactionLogger { public class TransactionLogger {
/**
* Log.
*
* @param transaction the transaction
*/
public void log(Transaction transaction) { public void log(Transaction transaction) {
// example imaginary function that logs the transaction to somewhere // example imaginary function that logs the transaction to somewhere
} }

View File

@ -1,3 +1,25 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.event.sourcing.journal; package com.iluwatar.event.sourcing.journal;
import com.google.gson.Gson; import com.google.gson.Gson;
@ -9,8 +31,15 @@ import com.iluwatar.event.sourcing.event.AccountCreateEvent;
import com.iluwatar.event.sourcing.event.MoneyDepositEvent; import com.iluwatar.event.sourcing.event.MoneyDepositEvent;
import com.iluwatar.event.sourcing.event.MoneyTransferEvent; import com.iluwatar.event.sourcing.event.MoneyTransferEvent;
import com.iluwatar.event.sourcing.event.MoneyWithdrawalEvent; import com.iluwatar.event.sourcing.event.MoneyWithdrawalEvent;
import java.io.BufferedReader;
import java.io.*; import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -22,10 +51,15 @@ public class JsonFileJournal implements ProcessorJournal{
private File aFile; private File aFile;
private List<String> events = new ArrayList<>(); private List<String> events = new ArrayList<>();
private int index = 0; private int index = 0;
/**
* Instantiates a new Json file journal.
*/
public JsonFileJournal() { public JsonFileJournal() {
aFile = new File("Journal.json"); aFile = new File("Journal.json");
if (aFile.exists()) { if (aFile.exists()) {
try (BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(aFile), "UTF-8"))) { try (BufferedReader input = new BufferedReader(
new InputStreamReader(new FileInputStream(aFile), "UTF-8"))) {
String line; String line;
while ((line = input.readLine()) != null) { while ((line = input.readLine()) != null) {
events.add(line); events.add(line);
@ -54,7 +88,8 @@ public class JsonFileJournal implements ProcessorJournal{
throw new RuntimeException("Journal Event not recegnized"); throw new RuntimeException("Journal Event not recegnized");
} }
try (Writer output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(aFile, true), "UTF-8"))) { try (Writer output = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(aFile, true), "UTF-8"))) {
String eventString = jsonElement.toString(); String eventString = jsonElement.toString();
output.write(eventString + "\r\n"); output.write(eventString + "\r\n");
} catch (IOException e) { } catch (IOException e) {

View File

@ -1,3 +1,25 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.event.sourcing.processor; package com.iluwatar.event.sourcing.processor;
import com.iluwatar.event.sourcing.api.DomainEvent; import com.iluwatar.event.sourcing.api.DomainEvent;

View File

@ -1,22 +1,56 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.event.sourcing.service; package com.iluwatar.event.sourcing.service;
import com.iluwatar.event.sourcing.api.EventProcessor; import com.iluwatar.event.sourcing.api.EventProcessor;
import com.iluwatar.event.sourcing.event.AccountCreateEvent; import com.iluwatar.event.sourcing.event.AccountCreateEvent;
import java.util.Date; import java.util.Date;
/** /**
* Created by serdarh on 06.08.2017. * Created by serdarh on 06.08.2017.
*/ */
public class AccountService { public class AccountService {
private EventProcessor eventProcessor; private EventProcessor eventProcessor;
/**
* Instantiates a new Account service.
*
* @param eventProcessor the event processor
*/
public AccountService(EventProcessor eventProcessor) { public AccountService(EventProcessor eventProcessor) {
this.eventProcessor = eventProcessor; this.eventProcessor = eventProcessor;
} }
/**
* Create account.
*
* @param accountNo the account no
* @param owner the owner
*/
public void createAccount(int accountNo, String owner) { public void createAccount(int accountNo, String owner) {
AccountCreateEvent accountCreateEvent = new AccountCreateEvent(SequenceIdGenerator.nextSequenceId(), new Date().getTime(),accountNo,owner); AccountCreateEvent accountCreateEvent = new AccountCreateEvent(
SequenceIdGenerator.nextSequenceId(), new Date().getTime(), accountNo, owner);
eventProcessor.process(accountCreateEvent); eventProcessor.process(accountCreateEvent);
} }
} }

View File

@ -1,10 +1,31 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.event.sourcing.service; package com.iluwatar.event.sourcing.service;
import com.iluwatar.event.sourcing.api.EventProcessor; import com.iluwatar.event.sourcing.api.EventProcessor;
import com.iluwatar.event.sourcing.event.MoneyDepositEvent; import com.iluwatar.event.sourcing.event.MoneyDepositEvent;
import com.iluwatar.event.sourcing.event.MoneyTransferEvent; import com.iluwatar.event.sourcing.event.MoneyTransferEvent;
import com.iluwatar.event.sourcing.event.MoneyWithdrawalEvent; import com.iluwatar.event.sourcing.event.MoneyWithdrawalEvent;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.Date; import java.util.Date;
@ -12,24 +33,53 @@ import java.util.Date;
* Created by serdarh on 06.08.2017. * Created by serdarh on 06.08.2017.
*/ */
public class MoneyTransactionService { public class MoneyTransactionService {
private EventProcessor eventProcessor; private EventProcessor eventProcessor;
/**
* Instantiates a new Money transaction service.
*
* @param eventProcessor the event processor
*/
public MoneyTransactionService(EventProcessor eventProcessor) { public MoneyTransactionService(EventProcessor eventProcessor) {
this.eventProcessor = eventProcessor; this.eventProcessor = eventProcessor;
} }
/**
* Deposit money.
*
* @param accountNo the account no
* @param money the money
*/
public void depositMoney(int accountNo, BigDecimal money) { public void depositMoney(int accountNo, BigDecimal money) {
MoneyDepositEvent moneyDepositEvent = new MoneyDepositEvent(SequenceIdGenerator.nextSequenceId(), new Date().getTime(), accountNo, money); MoneyDepositEvent moneyDepositEvent = new MoneyDepositEvent(
SequenceIdGenerator.nextSequenceId(), new Date().getTime(), accountNo, money);
eventProcessor.process(moneyDepositEvent); eventProcessor.process(moneyDepositEvent);
} }
/**
* Withdrawal money.
*
* @param accountNo the account no
* @param money the money
*/
public void withdrawalMoney(int accountNo, BigDecimal money) { public void withdrawalMoney(int accountNo, BigDecimal money) {
MoneyWithdrawalEvent moneyWithdrawalEvent = new MoneyWithdrawalEvent(SequenceIdGenerator.nextSequenceId(), new Date().getTime(), accountNo, money); MoneyWithdrawalEvent moneyWithdrawalEvent = new MoneyWithdrawalEvent(
SequenceIdGenerator.nextSequenceId(), new Date().getTime(), accountNo, money);
eventProcessor.process(moneyWithdrawalEvent); eventProcessor.process(moneyWithdrawalEvent);
} }
/**
* Transfer money.
*
* @param accountNoFrom the account no from
* @param accountNoTo the account no to
* @param money the money
*/
public void transferMoney(int accountNoFrom, int accountNoTo, BigDecimal money) { public void transferMoney(int accountNoFrom, int accountNoTo, BigDecimal money) {
MoneyTransferEvent moneyTransferEvent = new MoneyTransferEvent(SequenceIdGenerator.nextSequenceId(), new Date().getTime(), money, accountNoFrom, accountNoTo); MoneyTransferEvent moneyTransferEvent = new MoneyTransferEvent(
SequenceIdGenerator.nextSequenceId(), new Date().getTime(), money, accountNoFrom,
accountNoTo);
eventProcessor.process(moneyTransferEvent); eventProcessor.process(moneyTransferEvent);
} }
} }

View File

@ -1,11 +1,39 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.event.sourcing.service; package com.iluwatar.event.sourcing.service;
/** /**
* Created by serdarh on 06.08.2017. * Created by serdarh on 06.08.2017.
*/ */
public class SequenceIdGenerator { public class SequenceIdGenerator {
private static long sequenceId = 0L; private static long sequenceId = 0L;
/**
* Next sequence id long.
*
* @return the long
*/
public static long nextSequenceId() { public static long nextSequenceId() {
sequenceId++; sequenceId++;
return sequenceId; return sequenceId;

View File

@ -1,7 +1,28 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.event.sourcing.state; package com.iluwatar.event.sourcing.state;
import com.iluwatar.event.sourcing.domain.Account; import com.iluwatar.event.sourcing.domain.Account;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -9,12 +30,24 @@ import java.util.Map;
* Created by serdarh on 06.08.2017. * Created by serdarh on 06.08.2017.
*/ */
public class AccountAggregate { public class AccountAggregate {
private static Map<Integer, Account> accounts = new HashMap<>(); private static Map<Integer, Account> accounts = new HashMap<>();
/**
* Put account.
*
* @param account the account
*/
public static void putAccount(Account account) { public static void putAccount(Account account) {
accounts.put(account.getAccountNo(), account); accounts.put(account.getAccountNo(), account);
} }
/**
* Gets account.
*
* @param accountNo the account no
* @return the account
*/
public static Account getAccount(int accountNo) { public static Account getAccount(int accountNo) {
Account account = accounts.get(accountNo); Account account = accounts.get(accountNo);
if (account == null) { if (account == null) {
@ -23,6 +56,9 @@ public class AccountAggregate {
return account.copy(); return account.copy();
} }
/**
* Reset state.
*/
public static void resetState() { public static void resetState() {
accounts = new HashMap<>(); accounts = new HashMap<>();
} }