Search in sources :

Example 1 with EntityInstance

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

the class ModuleUnitOfWork method get.

@Override
@SuppressWarnings("unchecked")
public <T> T get(T entity) throws EntityTypeNotFoundException {
    EntityComposite entityComposite = (EntityComposite) entity;
    EntityInstance compositeInstance = EntityInstance.entityInstanceOf(entityComposite);
    ModelModule<EntityModel> model = new ModelModule<>(compositeInstance.module(), compositeInstance.entityModel());
    Class<T> type = (Class<T>) first(compositeInstance.types());
    return uow.get(compositeInstance.identity(), this, Collections.singletonList(model), type);
}
Also used : EntityComposite(org.qi4j.api.entity.EntityComposite) EntityModel(org.qi4j.runtime.entity.EntityModel) EntityInstance(org.qi4j.runtime.entity.EntityInstance)

Example 2 with EntityInstance

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

the class ModuleUnitOfWork method remove.

@Override
public void remove(Object entity) throws LifecycleException {
    uow.checkOpen();
    EntityComposite entityComposite = (EntityComposite) entity;
    EntityInstance compositeInstance = EntityInstance.entityInstanceOf(entityComposite);
    if (compositeInstance.status() == EntityStatus.NEW) {
        compositeInstance.remove(this);
        uow.remove(compositeInstance.identity());
    } else if (compositeInstance.status() == EntityStatus.LOADED || compositeInstance.status() == EntityStatus.UPDATED) {
        compositeInstance.remove(this);
    } else {
        throw new NoSuchEntityException(compositeInstance.identity(), compositeInstance.types());
    }
}
Also used : EntityComposite(org.qi4j.api.entity.EntityComposite) EntityInstance(org.qi4j.runtime.entity.EntityInstance) NoSuchEntityException(org.qi4j.api.unitofwork.NoSuchEntityException)

Example 3 with EntityInstance

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

the class EntityBuilderInstance method newInstance.

@Override
@SuppressWarnings("unchecked")
public T newInstance() throws LifecycleException {
    checkValid();
    String identity;
    // Figure out whether to use given or generated identity
    identity = (String) entityState.propertyValueOf(IDENTITY_STATE_NAME);
    EntityState newEntityState = model.model().newEntityState(store, EntityReference.parseEntityReference(identity));
    prototypeInstance.invokeCreate();
    // Check constraints
    prototypeInstance.checkConstraints();
    entityState.copyTo(newEntityState);
    EntityInstance instance = model.model().newInstance(uow, model.module(), newEntityState);
    Object proxy = instance.proxy();
    // Add entity in UOW
    uow.addEntity(instance);
    // Invalidate builder
    this.identity = null;
    return (T) proxy;
}
Also used : EntityInstance(org.qi4j.runtime.entity.EntityInstance) EntityState(org.qi4j.spi.entity.EntityState)

Example 4 with EntityInstance

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

the class UnitOfWorkInstance method pause.

public void pause() {
    if (!paused) {
        paused = true;
        getCurrent().pop();
        UnitOfWorkOptions unitOfWorkOptions = metaInfo().get(UnitOfWorkOptions.class);
        if (unitOfWorkOptions == null) {
            unitOfWorkOptions = usecase().metaInfo(UnitOfWorkOptions.class);
        }
        if (unitOfWorkOptions != null) {
            if (unitOfWorkOptions.isPruneOnPause()) {
                List<EntityReference> prunedInstances = null;
                for (EntityInstance entityInstance : instanceCache.values()) {
                    if (entityInstance.status() == EntityStatus.LOADED) {
                        if (prunedInstances == null) {
                            prunedInstances = new ArrayList<>();
                        }
                        prunedInstances.add(entityInstance.identity());
                    }
                }
                if (prunedInstances != null) {
                    for (EntityReference prunedInstance : prunedInstances) {
                        instanceCache.remove(prunedInstance);
                    }
                }
            }
        }
    } else {
        throw new UnitOfWorkException("Unit of work is not active");
    }
}
Also used : UnitOfWorkException(org.qi4j.api.unitofwork.UnitOfWorkException) EntityReference(org.qi4j.api.entity.EntityReference) EntityInstance(org.qi4j.runtime.entity.EntityInstance) UnitOfWorkOptions(org.qi4j.api.unitofwork.UnitOfWorkOptions)

Example 5 with EntityInstance

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

the class UnitOfWorkInstance method notifyAfterCompletion.

private void notifyAfterCompletion(List<UnitOfWorkCallback> callbacks, final UnitOfWorkCallback.UnitOfWorkStatus status) {
    if (callbacks != null) {
        for (UnitOfWorkCallback callback : callbacks) {
            try {
                callback.afterCompletion(status);
            } catch (Exception e) {
            // Ignore
            }
        }
    }
    // 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.afterCompletion(status);
            }
        }
    } catch (Exception e) {
    // Ignore
    }
}
Also used : UnitOfWorkCallback(org.qi4j.api.unitofwork.UnitOfWorkCallback) 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)

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