Skip to content
This repository has been archived by the owner on Dec 14, 2021. It is now read-only.

Commit

Permalink
Streamline by and sell trade saga
Browse files Browse the repository at this point in the history
Streamline by and sell trade saga

#28
  • Loading branch information
smcvb committed Jun 1, 2018
1 parent 23dae93 commit 01b24fc
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 268 deletions.
Expand Up @@ -18,7 +18,6 @@

import org.axonframework.commandhandling.CommandCallback;
import org.axonframework.commandhandling.CommandMessage;
import org.axonframework.commandhandling.GenericCommandMessage;
import org.axonframework.eventhandling.saga.EndSaga;
import org.axonframework.eventhandling.saga.SagaEventHandler;
import org.axonframework.eventhandling.saga.StartSaga;
Expand All @@ -32,161 +31,143 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author Jettro Coenradie
*/
@Saga
public class BuyTradeManagerSaga extends TradeManagerSaga {

private static final long serialVersionUID = 5948996680443725871L;
private final static Logger logger = LoggerFactory.getLogger(BuyTradeManagerSaga.class);
private static final Logger logger = LoggerFactory.getLogger(BuyTradeManagerSaga.class);

@StartSaga
@SagaEventHandler(associationProperty = "transactionIdentifier")
@SagaEventHandler(associationProperty = "transactionId")
public void handle(BuyTransactionStartedEvent event) {
if (logger.isDebugEnabled()) {
logger.debug(
"A new buy transaction is started with identifier {}, for portfolio with identifier {} and orderbook with identifier {}",
event.getTransactionId(),
event.getPortfolioId(),
event.getOrderBookId());
logger.debug("The new buy transaction with identifier {} is for buying {} items for the price of {}",
event.getTransactionId(),
event.getTotalItems(),
event.getPricePerItem());
}
setTransactionIdentifier(event.getTransactionId());
setOrderbookIdentifier(event.getOrderBookId());
setPortfolioIdentifier(event.getPortfolioId());
setPricePerItem(event.getPricePerItem());
setTotalItems(event.getTotalItems());

ReserveCashCommand command = new ReserveCashCommand(getPortfolioIdentifier(),
getTransactionIdentifier(),
getTotalItems()
* getPricePerItem());
getCommandBus().dispatch(new GenericCommandMessage<>(command));
transactionId = event.getTransactionId();
orderBookId = event.getOrderBookId();
portfolioId = event.getPortfolioId();
pricePerItem = event.getPricePerItem();
totalItems = event.getTotalItems();


logger.debug(
"A new buy transaction is started with identifier {}, for portfolio with identifier {} and order book with identifier {}",
transactionId,
portfolioId,
orderBookId
);
logger.debug(
"The new buy transaction with identifier {} is for buying {} items for the price of {}",
transactionId, totalItems, pricePerItem
);


commandGateway.send(new ReserveCashCommand(portfolioId, transactionId, totalItems * pricePerItem));
}

@SagaEventHandler(associationProperty = "transactionIdentifier")
@SuppressWarnings("unused")
@SagaEventHandler(associationProperty = "transactionId")
public void handle(CashReservedEvent event) {
logger.debug("Money for transaction with identifier {} is reserved", getTransactionIdentifier());
ConfirmTransactionCommand command = new ConfirmTransactionCommand(getTransactionIdentifier());
getCommandBus().dispatch(new GenericCommandMessage<>(command),
new CommandCallback<ConfirmTransactionCommand, Void>() {

@Override
public void onSuccess(CommandMessage commandMessage, Void result) {
// TODO jettro : Do we really need this?
logger.debug("Confirm transaction is dispatched successfully!");
}

@Override
public void onFailure(CommandMessage commandMessage, Throwable cause) {
logger.error("********* WOW!!!", cause);
}
});
logger.debug("Money for transaction with identifier {} is reserved", transactionId);

commandGateway.send(new ConfirmTransactionCommand(transactionId),
new CommandCallback<ConfirmTransactionCommand, Void>() {
@Override
public void onSuccess(CommandMessage commandMessage, Void result) {
// TODO jettro : Do we really need this? TODO #28 discuss this with Allard
logger.debug("Confirm transaction is dispatched successfully!");
}

@Override
public void onFailure(CommandMessage commandMessage, Throwable cause) {
logger.error("********* WOW!!!", cause);
}
});
}

@SagaEventHandler(associationProperty = "transactionIdentifier")
@EndSaga
@SagaEventHandler(associationProperty = "transactionId")
public void handle(CashReservationRejectedEvent event) {
logger.debug(
"Not enough cash was available to make reservation in transaction {} for portfolio {}. Required: {}",
getTransactionIdentifier(),
event.getPortfolioId(),
event.getAmountToPayInCents());
transactionId, event.getPortfolioId(), event.getAmountToPayInCents()
);
}

@SagaEventHandler(associationProperty = "transactionIdentifier")
@SuppressWarnings("unused")
@SagaEventHandler(associationProperty = "transactionId")
public void handle(BuyTransactionConfirmedEvent event) {
logger.debug("Buy Transaction {} is approved to make the buy order", event.getTransactionId());
CreateBuyOrderCommand command = new CreateBuyOrderCommand(new OrderId(), getPortfolioIdentifier(),
getOrderbookIdentifier(),
getTransactionIdentifier(),
getTotalItems(),
getPricePerItem());
getCommandBus().dispatch(new GenericCommandMessage<>(command));
logger.debug("Buy Transaction {} is approved to make the buy order", transactionId);

commandGateway.send(new CreateBuyOrderCommand(new OrderId(),
portfolioId,
orderBookId,
transactionId,
totalItems,
pricePerItem));
}

@SagaEventHandler(associationProperty = "transactionIdentifier")
@SagaEventHandler(associationProperty = "transactionId")
public void handle(BuyTransactionCancelledEvent event) {
long amountToCancel = (event.getTotalAmountOfItems() - event.getAmountOfExecutedItems()) * getPricePerItem();
logger.debug("Buy Transaction {} is cancelled, amount of cash reserved to cancel is {}",
event.getTransactionId(),
amountToCancel);
CancelCashReservationCommand command = new CancelCashReservationCommand(
getPortfolioIdentifier(),
getTransactionIdentifier(),
amountToCancel);
getCommandBus().dispatch(new GenericCommandMessage<>(command));
long amountToCancel = (event.getTotalAmountOfItems() - event.getAmountOfExecutedItems()) * pricePerItem;

logger.debug(
"Buy Transaction {} is cancelled, amount of cash reserved to cancel is {}",
transactionId, amountToCancel
);

commandGateway.send(new CancelCashReservationCommand(portfolioId, transactionId, amountToCancel));
}

@SagaEventHandler(associationProperty = "buyTransactionId", keyName = "transactionIdentifier")
@SagaEventHandler(associationProperty = "buyTransactionId", keyName = "transactionId")
public void handle(TradeExecutedEvent event) {
logger.debug("Buy Transaction {} is executed, items for transaction are {} for a price of {}",
getTransactionIdentifier(), event.getTradeCount(), event.getTradePrice());
ExecutedTransactionCommand command = new ExecutedTransactionCommand(getTransactionIdentifier(),
event.getTradeCount(),
event.getTradePrice());
getCommandBus().dispatch(new GenericCommandMessage<>(command));
long tradeCount = event.getTradeCount();
long tradePrice = event.getTradePrice();

logger.debug(
"Buy Transaction {} is executed, items for transaction are {} for a price of {}",
transactionId, tradeCount, tradePrice
);

commandGateway.send(new ExecutedTransactionCommand(transactionId, tradeCount, tradePrice));
}

@SagaEventHandler(associationProperty = "transactionIdentifier")
@EndSaga
@SagaEventHandler(associationProperty = "transactionId")
public void handle(BuyTransactionExecutedEvent event) {
logger.debug("Buy Transaction {} is executed, last amount of executed items is {} for a price of {}",
event.getTransactionId(), event.getAmountOfItems(), event.getItemPrice());

returnDifferenceInBidPriceAndExecutedPrice(getPricePerItem(), event.getItemPrice(), event.getAmountOfItems());

ConfirmCashReservationCommand confirmCommand =
new ConfirmCashReservationCommand(getPortfolioIdentifier(),
getTransactionIdentifier(),
event.getAmountOfItems() * event.getItemPrice());
getCommandBus().dispatch(new GenericCommandMessage<>(confirmCommand));
AddItemsToPortfolioCommand addItemsCommand =
new AddItemsToPortfolioCommand(getPortfolioIdentifier(),
getOrderbookIdentifier(),
event.getAmountOfItems());
getCommandBus().dispatch(new GenericCommandMessage<>(addItemsCommand));
long amountOfItems = event.getAmountOfItems();
long itemPrice = event.getItemPrice();

logger.debug(
"Buy Transaction {} is executed, last amount of executed items is {} for a price of {}",
transactionId, amountOfItems, itemPrice
);

returnDifferenceInBidPriceAndExecutedPrice(pricePerItem, itemPrice, amountOfItems);

commandGateway.send(new ConfirmCashReservationCommand(portfolioId, transactionId, amountOfItems * itemPrice));
commandGateway.send(new AddItemsToPortfolioCommand(portfolioId, orderBookId, amountOfItems));
}

@SagaEventHandler(associationProperty = "transactionIdentifier")
@SagaEventHandler(associationProperty = "transactionId")
public void handle(BuyTransactionPartiallyExecutedEvent event) {
logger.debug("Buy Transaction {} is partially executed, amount of executed items is {} for a price of {}",
event.getTransactionId(),
event.getAmountOfExecutedItems(),
event.getItemPrice());

returnDifferenceInBidPriceAndExecutedPrice(getPricePerItem(),
event.getItemPrice(),
event.getAmountOfExecutedItems());

ConfirmCashReservationCommand confirmCommand =
new ConfirmCashReservationCommand(getPortfolioIdentifier(),
getTransactionIdentifier(),
event.getAmountOfExecutedItems() * event
.getItemPrice());
getCommandBus().dispatch(new GenericCommandMessage<>(confirmCommand));
AddItemsToPortfolioCommand addItemsCommand =
new AddItemsToPortfolioCommand(getPortfolioIdentifier(),
getOrderbookIdentifier(),
event.getAmountOfExecutedItems());
getCommandBus().dispatch(new GenericCommandMessage<>(addItemsCommand));
long amountOfExecutedItems = event.getAmountOfExecutedItems();
long itemPrice = event.getItemPrice();

logger.debug(
"Buy Transaction {} is partially executed, amount of executed items is {} for a price of {}",
transactionId, amountOfExecutedItems, itemPrice
);

returnDifferenceInBidPriceAndExecutedPrice(pricePerItem, itemPrice, amountOfExecutedItems);

commandGateway.send(new ConfirmCashReservationCommand(portfolioId,
transactionId,
amountOfExecutedItems * itemPrice));
commandGateway.send(new AddItemsToPortfolioCommand(portfolioId, orderBookId, amountOfExecutedItems));
}

private void returnDifferenceInBidPriceAndExecutedPrice(long bidPrice, long executedPrice,
long amountOfExecutedItems) {
long totalDifferenceInCents = amountOfExecutedItems * bidPrice - amountOfExecutedItems * executedPrice;

if (totalDifferenceInCents > 0) {
CancelCashReservationCommand cancelCashReservationCommand = new CancelCashReservationCommand(
getPortfolioIdentifier(),
getTransactionIdentifier(),
totalDifferenceInCents);
getCommandBus().dispatch(new GenericCommandMessage<>(
cancelCashReservationCommand));
commandGateway.send(new CancelCashReservationCommand(portfolioId, transactionId, totalDifferenceInCents));
}
}
}

0 comments on commit 01b24fc

Please sign in to comment.