Search in sources :

Example 11 with UnitOfWorkCompletionException

use of org.qi4j.api.unitofwork.UnitOfWorkCompletionException in project qi4j-sdk by Qi4j.

the class UnitOfWorkInstance method applyChanges.

private List<StateCommitter> applyChanges() throws UnitOfWorkCompletionException {
    List<StateCommitter> committers = new ArrayList<>();
    for (EntityStoreUnitOfWork entityStoreUnitOfWork : storeUnitOfWork.values()) {
        try {
            StateCommitter committer = entityStoreUnitOfWork.applyChanges();
            committers.add(committer);
        } catch (Exception e) {
            // Cancel all previously prepared stores
            for (StateCommitter committer : committers) {
                committer.cancel();
            }
            if (e instanceof ConcurrentEntityStateModificationException) {
                // If we cancelled due to concurrent modification, then create the proper exception for it!
                ConcurrentEntityStateModificationException mee = (ConcurrentEntityStateModificationException) e;
                Collection<EntityReference> modifiedEntityIdentities = mee.modifiedEntities();
                Collection<EntityComposite> modifiedEntities = new ArrayList<>();
                for (EntityReference modifiedEntityIdentity : modifiedEntityIdentities) {
                    Collection<EntityInstance> instances = instanceCache.values();
                    for (EntityInstance instance : instances) {
                        if (instance.identity().equals(modifiedEntityIdentity)) {
                            modifiedEntities.add(instance.<EntityComposite>proxy());
                        }
                    }
                }
                throw new ConcurrentEntityModificationException(modifiedEntities);
            } else {
                throw new UnitOfWorkCompletionException(e);
            }
        }
    }
    return committers;
}
Also used : EntityComposite(org.qi4j.api.entity.EntityComposite) EntityStoreUnitOfWork(org.qi4j.spi.entitystore.EntityStoreUnitOfWork) ArrayList(java.util.ArrayList) EntityInstance(org.qi4j.runtime.entity.EntityInstance) ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) UnitOfWorkException(org.qi4j.api.unitofwork.UnitOfWorkException) EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) NoSuchEntityException(org.qi4j.api.unitofwork.NoSuchEntityException) ConcurrentEntityStateModificationException(org.qi4j.spi.entitystore.ConcurrentEntityStateModificationException) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) ConcurrentEntityStateModificationException(org.qi4j.spi.entitystore.ConcurrentEntityStateModificationException) EntityReference(org.qi4j.api.entity.EntityReference) Collection(java.util.Collection) StateCommitter(org.qi4j.spi.entitystore.StateCommitter)

Example 12 with UnitOfWorkCompletionException

use of org.qi4j.api.unitofwork.UnitOfWorkCompletionException in project qi4j-sdk by Qi4j.

the class TransactionNotificationConcern method createEvent.

@Override
public ApplicationEvent createEvent(String name, Object[] args) {
    final UnitOfWork unitOfWork = uowf.currentUnitOfWork();
    ApplicationEvent event = next.createEvent(name, args);
    // Add event to list in UoW
    UnitOfWorkApplicationEvents events = unitOfWork.metaInfo(UnitOfWorkApplicationEvents.class);
    if (events == null) {
        events = new UnitOfWorkApplicationEvents();
        unitOfWork.setMetaInfo(events);
        unitOfWork.addUnitOfWorkCallback(new UnitOfWorkCallback() {

            @Override
            public void beforeCompletion() throws UnitOfWorkCompletionException {
            }

            @Override
            public void afterCompletion(UnitOfWorkStatus status) {
                if (status.equals(UnitOfWorkStatus.COMPLETED)) {
                    UnitOfWorkApplicationEvents events = unitOfWork.metaInfo(UnitOfWorkApplicationEvents.class);
                    try {
                        eventStore.storeEvents(events.getEvents());
                    } catch (IOException e) {
                        logger.error("Could not store events", e);
                    // How do we handle this? This is a major error!
                    }
                }
            }
        });
    }
    events.add(event);
    return event;
}
Also used : UnitOfWorkCallback(org.qi4j.api.unitofwork.UnitOfWorkCallback) UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) ApplicationEvent(org.qi4j.library.eventsourcing.application.api.ApplicationEvent) IOException(java.io.IOException)

Example 13 with UnitOfWorkCompletionException

use of org.qi4j.api.unitofwork.UnitOfWorkCompletionException 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)

Example 14 with UnitOfWorkCompletionException

use of org.qi4j.api.unitofwork.UnitOfWorkCompletionException in project qi4j-sdk by Qi4j.

the class DebuggingServiceMixin method debug.

@Override
public void debug(Composite composite, String message) {
    UnitOfWork uow = uowf.newUnitOfWork();
    try {
        List<Serializable> paramsList = new ArrayList<Serializable>();
        createDebugRecord(uow, composite, message, paramsList);
        uow.complete();
    } catch (ConcurrentEntityModificationException e) {
    // ignore for now. Perhaps discard() and try again.
    } catch (UnitOfWorkCompletionException e) {
    // ignore for now. Perhaps discard() and try again.
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) Serializable(java.io.Serializable) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) ArrayList(java.util.ArrayList)

Example 15 with UnitOfWorkCompletionException

use of org.qi4j.api.unitofwork.UnitOfWorkCompletionException in project qi4j-sdk by Qi4j.

the class WicketQi4jApplication method handleUnitOfWork.

private void handleUnitOfWork() {
    getRequestCycleListeners().add(new AbstractRequestCycleListener() {

        @Override
        public void onBeginRequest(final RequestCycle requestCycle) {
            super.onBeginRequest(requestCycle);
            logger.debug("================================");
            logger.debug("REQUEST start");
            logger.debug(requestCycle.getRequest().toString());
            logger.debug(requestCycle.getRequest().getRequestParameters().toString());
            UnitOfWork uow = uowf.newUnitOfWork(UsecaseBuilder.newUsecase("REQUEST"));
            logger.debug("  ### NEW " + uow + "   ### MODULE: " + qi4jModule);
        }

        @Override
        public void onEndRequest(final RequestCycle requestCycle) {
            UnitOfWork uow = uowf.currentUnitOfWork();
            if (uow != null) {
                try {
                    if ("POST".equals(((HttpServletRequest) requestCycle.getRequest().getContainerRequest()).getMethod())) {
                        // "Save"
                        logger.debug("  ### COMPLETE " + uow + "   ### MODULE: " + qi4jModule);
                        uow.complete();
                    } else {
                        // GET requests
                        logger.debug("  ### DISCARD " + uow + "   ### MODULE: " + qi4jModule);
                        uow.discard();
                    }
                } catch (ConcurrentEntityModificationException e) {
                    logger.error("  ### DISCARD " + uow + "   ### MODULE: " + qi4jModule);
                    uow.discard();
                    e.printStackTrace();
                } catch (UnitOfWorkCompletionException e) {
                    logger.error("  ### DISCARD " + uow + "   ### MODULE: " + qi4jModule);
                    uow.discard();
                    e.printStackTrace();
                }
            }
            logger.debug("REQUEST end");
            logger.debug("------------------------------------");
        }
    });
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) AbstractRequestCycleListener(org.apache.wicket.request.cycle.AbstractRequestCycleListener)

Aggregations

UnitOfWorkCompletionException (org.qi4j.api.unitofwork.UnitOfWorkCompletionException)20 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)18 ConcurrentEntityModificationException (org.qi4j.api.unitofwork.ConcurrentEntityModificationException)12 ArrayList (java.util.ArrayList)7 IOException (java.io.IOException)5 Test (org.junit.Test)5 Serializable (java.io.Serializable)4 NoSuchEntityException (org.qi4j.api.unitofwork.NoSuchEntityException)3 UnitOfWorkCallback (org.qi4j.api.unitofwork.UnitOfWorkCallback)3 AssemblyException (org.qi4j.bootstrap.AssemblyException)3 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)3 File (java.io.File)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 AbstractRequestCycleListener (org.apache.wicket.request.cycle.AbstractRequestCycleListener)2 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)2 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)2 UnitOfWorkException (org.qi4j.api.unitofwork.UnitOfWorkException)2 ConcurrentUoWFileModificationException (org.qi4j.library.uowfile.internal.ConcurrentUoWFileModificationException)2 EntityInstance (org.qi4j.runtime.entity.EntityInstance)2 ConcurrentEntityStateModificationException (org.qi4j.spi.entitystore.ConcurrentEntityStateModificationException)2