Search in sources :

Example 1 with UnitOfWorkEventsVisitor

use of org.qi4j.library.eventsourcing.domain.source.UnitOfWorkEventsVisitor in project qi4j-sdk by Qi4j.

the class UnitOfWorkNotificationConcern method createEvent.

@Override
public DomainEventValue createEvent(EntityComposite entity, String name, Object[] args) {
    final UnitOfWork unitOfWork = uowf.currentUnitOfWork();
    DomainEventValue eventValue = next.createEvent(api.dereference(entity), name, args);
    // Add eventValue to list in UoW
    UnitOfWorkEvents events = unitOfWork.metaInfo(UnitOfWorkEvents.class);
    if (events == null) {
        events = new UnitOfWorkEvents();
        unitOfWork.setMetaInfo(events);
        unitOfWork.addUnitOfWorkCallback(new UnitOfWorkCallback() {

            String user;

            @Override
            public void beforeCompletion() throws UnitOfWorkCompletionException {
                user = currentUser.getCurrentUser();
            }

            @Override
            public void afterCompletion(UnitOfWorkStatus status) {
                if (status.equals(UnitOfWorkStatus.COMPLETED)) {
                    UnitOfWorkEvents events = unitOfWork.metaInfo(UnitOfWorkEvents.class);
                    ValueBuilder<UnitOfWorkDomainEventsValue> builder = vbf.newValueBuilder(UnitOfWorkDomainEventsValue.class);
                    builder.prototype().user().set(user);
                    builder.prototype().timestamp().set(System.currentTimeMillis());
                    builder.prototype().usecase().set(unitOfWork.usecase().name());
                    builder.prototype().version().set(version);
                    builder.prototype().events().get().addAll(events.getEventValues());
                    try {
                        final UnitOfWorkDomainEventsValue unitOfWorkDomainValue = builder.newInstance();
                        Inputs.iterable(Iterables.iterable(unitOfWorkDomainValue)).transferTo(eventOutput);
                        for (UnitOfWorkEventsVisitor unitOfWorkEventsVisitor : transactionVisitors) {
                            try {
                                unitOfWorkEventsVisitor.visit(unitOfWorkDomainValue);
                            } catch (Exception e) {
                                logger.warn("Could not deliver events", e);
                            }
                        }
                    } catch (IOException e) {
                        logger.error("Could not store events", e);
                    // How do we handle this? This is a major error!
                    }
                }
            }
        });
    }
    events.add(eventValue);
    return eventValue;
}
Also used : ValueBuilder(org.qi4j.api.value.ValueBuilder) UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) IOException(java.io.IOException) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) IOException(java.io.IOException) UnitOfWorkDomainEventsValue(org.qi4j.library.eventsourcing.domain.api.UnitOfWorkDomainEventsValue) UnitOfWorkEventsVisitor(org.qi4j.library.eventsourcing.domain.source.UnitOfWorkEventsVisitor) UnitOfWorkCallback(org.qi4j.api.unitofwork.UnitOfWorkCallback) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) DomainEventValue(org.qi4j.library.eventsourcing.domain.api.DomainEventValue)

Aggregations

IOException (java.io.IOException)1 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)1 UnitOfWorkCallback (org.qi4j.api.unitofwork.UnitOfWorkCallback)1 UnitOfWorkCompletionException (org.qi4j.api.unitofwork.UnitOfWorkCompletionException)1 ValueBuilder (org.qi4j.api.value.ValueBuilder)1 DomainEventValue (org.qi4j.library.eventsourcing.domain.api.DomainEventValue)1 UnitOfWorkDomainEventsValue (org.qi4j.library.eventsourcing.domain.api.UnitOfWorkDomainEventsValue)1 UnitOfWorkEventsVisitor (org.qi4j.library.eventsourcing.domain.source.UnitOfWorkEventsVisitor)1