use of org.qi4j.api.unitofwork.UnitOfWorkCallback 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;
}
Aggregations