Search in sources :

Example 6 with EntityStoreUnitOfWork

use of org.qi4j.spi.entitystore.EntityStoreUnitOfWork in project qi4j-sdk by Qi4j.

the class UnitOfWorkInstance method discard.

public void discard() {
    if (!isOpen()) {
        return;
    }
    close();
    // Copy list so that it cannot be modified during completion
    List<UnitOfWorkCallback> currentCallbacks = callbacks == null ? null : new ArrayList<UnitOfWorkCallback>(callbacks);
    // Call callbacks
    notifyAfterCompletion(currentCallbacks, DISCARDED);
    for (EntityStoreUnitOfWork entityStoreUnitOfWork : storeUnitOfWork.values()) {
        entityStoreUnitOfWork.discard();
    }
    callbacks = currentCallbacks;
}
Also used : UnitOfWorkCallback(org.qi4j.api.unitofwork.UnitOfWorkCallback) EntityStoreUnitOfWork(org.qi4j.spi.entitystore.EntityStoreUnitOfWork)

Example 7 with EntityStoreUnitOfWork

use of org.qi4j.spi.entitystore.EntityStoreUnitOfWork in project qi4j-sdk by Qi4j.

the class UnitOfWorkInstance method applyChanges.

private List<StateCommitter> applyChanges() throws UnitOfWorkCompletionException {
    List<StateCommitter> committers = new ArrayList<StateCommitter>();
    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<EntityComposite>();
                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)

Aggregations

EntityStoreUnitOfWork (org.qi4j.spi.entitystore.EntityStoreUnitOfWork)7 EntityNotFoundException (org.qi4j.spi.entitystore.EntityNotFoundException)4 EntityReference (org.qi4j.api.entity.EntityReference)3 EntityState (org.qi4j.spi.entity.EntityState)3 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)2 NoSuchEntityException (org.qi4j.api.unitofwork.NoSuchEntityException)2 Usecase (org.qi4j.api.usecase.Usecase)2 EntityInstance (org.qi4j.runtime.entity.EntityInstance)2 ConcurrentEntityStateModificationException (org.qi4j.spi.entitystore.ConcurrentEntityStateModificationException)2 JSONEntityState (org.qi4j.spi.entitystore.helpers.JSONEntityState)2 ResourceException (org.restlet.resource.ResourceException)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Date (java.util.Date)1 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)1 EntityComposite (org.qi4j.api.entity.EntityComposite)1 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)1 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)1 ConcurrentEntityModificationException (org.qi4j.api.unitofwork.ConcurrentEntityModificationException)1 UnitOfWorkCallback (org.qi4j.api.unitofwork.UnitOfWorkCallback)1