Search in sources :

Example 6 with EntityInstance

use of org.qi4j.runtime.entity.EntityInstance in project qi4j-sdk by Qi4j.

the class UnitOfWorkInstance method get.

public <T> T get(EntityReference identity, ModuleUnitOfWork uow, Iterable<ModelModule<EntityModel>> potentialModels, Class<T> mixinType) throws EntityTypeNotFoundException, NoSuchEntityException {
    checkOpen();
    EntityInstance entityInstance = instanceCache.get(identity);
    if (entityInstance == null) {
        // Not yet in cache
        // Check if this is a root UoW, or if no parent UoW knows about this entity
        EntityState entityState = null;
        EntityModel model = null;
        ModuleInstance module = null;
        // Figure out what EntityStore to use
        for (ModelModule<EntityModel> potentialModel : potentialModels) {
            EntityStore store = potentialModel.module().entityStore();
            EntityStoreUnitOfWork storeUow = getEntityStoreUnitOfWork(store, potentialModel.module());
            try {
                entityState = storeUow.entityStateOf(identity);
            } catch (EntityNotFoundException e) {
                continue;
            }
            // Get the selected model
            model = (EntityModel) entityState.entityDescriptor();
            module = potentialModel.module();
        }
        // Check if model was found
        if (model == null) {
            // Check if state was found
            if (entityState == null) {
                throw new NoSuchEntityException(identity, mixinType);
            } else {
                throw new EntityTypeNotFoundException(mixinType.getName());
            }
        }
        // Create instance
        entityInstance = new EntityInstance(uow, module, model, entityState);
        instanceCache.put(identity, entityInstance);
    } else {
        // Check if it has been removed
        if (entityInstance.status() == EntityStatus.REMOVED) {
            throw new NoSuchEntityException(identity, mixinType);
        }
    }
    return entityInstance.proxy();
}
Also used : EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) EntityStoreUnitOfWork(org.qi4j.spi.entitystore.EntityStoreUnitOfWork) EntityModel(org.qi4j.runtime.entity.EntityModel) EntityInstance(org.qi4j.runtime.entity.EntityInstance) EntityState(org.qi4j.spi.entity.EntityState) EntityStore(org.qi4j.spi.entitystore.EntityStore) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) ModuleInstance(org.qi4j.runtime.structure.ModuleInstance) NoSuchEntityException(org.qi4j.api.unitofwork.NoSuchEntityException)

Example 7 with EntityInstance

use of org.qi4j.runtime.entity.EntityInstance in project qi4j-sdk by Qi4j.

the class AbstractAssociationInstance method getEntityReference.

protected EntityReference getEntityReference(Object composite) {
    if (composite == null) {
        return null;
    }
    InvocationHandler handler = Proxy.getInvocationHandler(composite);
    if (handler instanceof ProxyReferenceInvocationHandler) {
        handler = Proxy.getInvocationHandler(((ProxyReferenceInvocationHandler) handler).proxy());
    }
    EntityInstance instance = (EntityInstance) handler;
    return instance.identity();
}
Also used : ProxyReferenceInvocationHandler(org.qi4j.runtime.composite.ProxyReferenceInvocationHandler) EntityInstance(org.qi4j.runtime.entity.EntityInstance) ProxyReferenceInvocationHandler(org.qi4j.runtime.composite.ProxyReferenceInvocationHandler) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 8 with EntityInstance

use of org.qi4j.runtime.entity.EntityInstance in project qi4j-sdk by Qi4j.

the class UnitOfWorkInstance method notifyBeforeCompletion.

private void notifyBeforeCompletion(List<UnitOfWorkCallback> callbacks) throws UnitOfWorkCompletionException {
    // Notify explicitly registered callbacks
    if (callbacks != null) {
        for (UnitOfWorkCallback callback : callbacks) {
            callback.beforeCompletion();
        }
    }
    // Notify entities
    try {
        for (EntityInstance instance : instanceCache.values()) {
            boolean isCallback = instance.proxy() instanceof UnitOfWorkCallback;
            boolean isNotRemoved = !instance.status().equals(EntityStatus.REMOVED);
            if (isCallback && isNotRemoved) {
                UnitOfWorkCallback callback = UnitOfWorkCallback.class.cast(instance.proxy());
                callback.beforeCompletion();
            }
        }
    } catch (UnitOfWorkCompletionException e) {
        throw e;
    } catch (Exception e) {
        throw new UnitOfWorkCompletionException(e);
    }
}
Also used : UnitOfWorkCallback(org.qi4j.api.unitofwork.UnitOfWorkCallback) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) 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)

Example 9 with EntityInstance

use of org.qi4j.runtime.entity.EntityInstance 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)

Aggregations

EntityInstance (org.qi4j.runtime.entity.EntityInstance)9 NoSuchEntityException (org.qi4j.api.unitofwork.NoSuchEntityException)5 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)4 UnitOfWorkException (org.qi4j.api.unitofwork.UnitOfWorkException)4 EntityNotFoundException (org.qi4j.spi.entitystore.EntityNotFoundException)4 EntityComposite (org.qi4j.api.entity.EntityComposite)3 ConcurrentEntityModificationException (org.qi4j.api.unitofwork.ConcurrentEntityModificationException)3 UnitOfWorkCompletionException (org.qi4j.api.unitofwork.UnitOfWorkCompletionException)3 ConcurrentEntityStateModificationException (org.qi4j.spi.entitystore.ConcurrentEntityStateModificationException)3 EntityReference (org.qi4j.api.entity.EntityReference)2 UnitOfWorkCallback (org.qi4j.api.unitofwork.UnitOfWorkCallback)2 EntityModel (org.qi4j.runtime.entity.EntityModel)2 EntityState (org.qi4j.spi.entity.EntityState)2 EntityStoreUnitOfWork (org.qi4j.spi.entitystore.EntityStoreUnitOfWork)2 InvocationHandler (java.lang.reflect.InvocationHandler)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 UnitOfWorkOptions (org.qi4j.api.unitofwork.UnitOfWorkOptions)1 ProxyReferenceInvocationHandler (org.qi4j.runtime.composite.ProxyReferenceInvocationHandler)1 ModuleInstance (org.qi4j.runtime.structure.ModuleInstance)1