/** * Apply a {@link DomainEventMessage} with given payload and metadata (metadata from interceptors will be combined * with the provided metadata). Applying events means they are immediately applied (published) to the aggregate and * scheduled for publication to other event handlers. * <p/> * The event is applied on all entities part of this aggregate. If the event is applied from an event handler of the * aggregate and additional events need to be applied that depends on state changes brought about by the first event * use the returned {@link ApplyMore} instance. * * @param payload the payload of the event to apply * @param metaData any meta-data that must be registered with the Event * @return a gizmo to apply additional events after the given event has been processed by the entire aggregate * @see ApplyMore */ public static ApplyMore apply(Object payload, MetaData metaData) { return AggregateLifecycle.getInstance().doApply(payload, metaData); }
/** * Creates a new aggregate instance. In order for new aggregate to be created, a {@link Repository} should be * available to the current aggregate. {@link Repository} of an aggregate to be created is exposed to the current * aggregate via {@link RepositoryProvider}. * * @param <T> type of new aggregate to be created * @param aggregateType type of new aggregate to be created * @param factoryMethod factory method which creates new aggregate * @return a new aggregate instance * @throws Exception thrown if something goes wrong during instantiation of new aggregate */ public static <T> Aggregate<T> createNew(Class<T> aggregateType, Callable<T> factoryMethod) throws Exception { if (!isLive()) { throw new UnsupportedOperationException( "Aggregate is still initializing its state, creation of new aggregates is not possible"); } return getInstance().doCreateNew(aggregateType, factoryMethod); }
/** * Marks this aggregate as deleted, instructing a repository to remove that aggregate at an appropriate time. * <p/> * Note that different repository implementations may react differently to aggregates marked for deletion. * Typically, Event Sourced Repositories will ignore the marking and expect deletion to be provided as part of Event * information. */ public static void markDeleted() { getInstance().doMarkDeleted(); }
public void credit(long amount, String bankTransferId) { apply(new DestinationBankAccountCreditedEvent(id, amount, bankTransferId)); }
/** * Apply a {@link DomainEventMessage} with given payload without metadata (though interceptors can also be used to * provide metadata). Applying events means they are immediately applied (published) to the aggregate and scheduled * for publication to other event handlers. * <p/> * The event is applied on all entities part of this aggregate. If the event is applied from an event handler of the * aggregate and additional events need to be applied that depends on state changes brought about by the first event * use the returned {@link ApplyMore} instance. * * @param payload the payload of the event to apply * @return a gizmo to apply additional events after the given event has been processed by the entire aggregate * @see ApplyMore */ public static ApplyMore apply(Object payload) { return AggregateLifecycle.getInstance().doApply(payload, MetaData.emptyInstance()); }
@SuppressWarnings("unused") @CommandHandler public void handle(CancelTransactionCommand cmd) { if (type == TransactionType.BUY) { apply(new BuyTransactionCancelledEvent(transactionId, amountOfItems, amountOfExecutedItems)); } else if (type == TransactionType.SELL) { apply(new SellTransactionCancelledEvent(transactionId, amountOfItems, amountOfExecutedItems)); } }
@SuppressWarnings("unused") @CommandHandler public void handle(CancelTransactionCommand cmd) { if (type == TransactionType.BUY) { apply(new BuyTransactionCancelledEvent(transactionId, amountOfItems, amountOfExecutedItems)); } else if (type == TransactionType.SELL) { apply(new SellTransactionCancelledEvent(transactionId, amountOfItems, amountOfExecutedItems)); } }
@CommandHandler public void handle(RemoveMenuItemCommand command) { apply(new MenuItemRemovedEvent(id)); }
@CommandHandler public void handle(ReserveCashCommand cmd) { TransactionId transactionId = cmd.getTransactionId(); long amountToReserve = cmd.getAmountOfMoneyToReserve(); if (amountOfMoney >= amountToReserve) { apply(new CashReservedEvent(portfolioId, transactionId, amountToReserve)); } else { apply(new CashReservationRejectedEvent(portfolioId, transactionId, amountToReserve)); } }
@CommandHandler public void handle(ReserveCashCommand cmd) { TransactionId transactionId = cmd.getTransactionId(); long amountToReserve = cmd.getAmountOfMoneyToReserve(); if (amountOfMoney >= amountToReserve) { apply(new CashReservedEvent(portfolioId, transactionId, amountToReserve)); } else { apply(new CashReservationRejectedEvent(portfolioId, transactionId, amountToReserve)); } }
@CommandHandler public void handle(final UnregisterEntranceCommand cmd) { if (!registered) throw new IllegalStateException(format("entrance %s not registered.", cmd.getEntranceId())); apply(new EntranceUnregisteredEvent(cmd.getEntranceId())); }
@CommandHandler public void handle(final ActivateGuestCommand cmd) { if (!expireAt.isAfter(now())) throw new IllegalStateException(format("guest access %s expired.", cmd.getGiestId())); apply(new GuestActivatedEvent(cmd.getGiestId())); }
@CommandHandler public void handle(final UnlockEntranceCommand cmd) { if (!registered) throw new IllegalStateException(format("entrance %s not registered.", cmd.getEntranceId())); apply(new EntranceUnlockedEvent(cmd.getEntranceId())); }
/** * Completes a Task. * * @param command complete Task */ @CommandHandler void on(CompleteTaskCommand command) { apply(new TaskCompletedEvent(command.getId())); }
/** * Unstars a Task. * * @param command unstar Task */ @CommandHandler void on(UnstarTaskCommand command) { apply(new TaskUnstarredEvent(command.getId())); }
/** * Stars a Task. * * @param command star Task */ @CommandHandler void on(StarTaskCommand command) { apply(new TaskStarredEvent(command.getId())); }
@CommandHandler public User(CreateUserCommand cmd) { String name = cmd.getName(); if (StringUtils.isBlank(name)) { throw new IllegalArgumentException("The user's name should be non empty"); } String username = cmd.getUsername(); if (StringUtils.isBlank(username)) { throw new IllegalArgumentException("The username should be non empty"); } String password = cmd.getPassword(); if (StringUtils.isBlank(password)) { throw new IllegalArgumentException("The password should be non empty"); } apply(new UserCreatedEvent(cmd.getUserId(), name, username, hashOf(password.toCharArray()))); }