Search in sources :

Example 6 with EntityComposite

use of org.qi4j.api.entity.EntityComposite 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 7 with EntityComposite

use of org.qi4j.api.entity.EntityComposite in project qi4j-sdk by Qi4j.

the class UnitOfWorkInjectionTest method givenEntityInOneUnitOfWorkWhenCurrentUnitOfWorkHasChangedThenUnitOfWorkInjectionInEntityPointsToCorrectUow.

@Test
public void givenEntityInOneUnitOfWorkWhenCurrentUnitOfWorkHasChangedThenUnitOfWorkInjectionInEntityPointsToCorrectUow() throws Exception {
    Usecase usecase = UsecaseBuilder.newUsecase("usecase1");
    UnitOfWork uow = module.newUnitOfWork(usecase);
    try {
        Trial trial = uow.newEntity(Trial.class, "123");
        trial.doSomething();
        uow.complete();
        uow = module.newUnitOfWork(usecase);
        usecase = UsecaseBuilder.newUsecase("usecase2");
        UnitOfWork uow2 = module.newUnitOfWork(usecase);
        trial = uow.get(trial);
        trial.doSomething();
        assertEquals("123", ((EntityComposite) trial).identity().get());
        assertEquals("usecase1", trial.usecaseName());
        uow2.discard();
    } catch (Throwable ex) {
        ex.printStackTrace();
    } finally {
        try {
            while (module.isUnitOfWorkActive()) {
                uow = module.currentUnitOfWork();
                uow.discard();
            }
        } catch (IllegalStateException e) {
        // Continue
        }
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) EntityComposite(org.qi4j.api.entity.EntityComposite) Usecase(org.qi4j.api.usecase.Usecase) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 8 with EntityComposite

use of org.qi4j.api.entity.EntityComposite in project qi4j-sdk by Qi4j.

the class LoggingServiceMixin method createLogRecord.

private void createLogRecord(UnitOfWork uow, LogType type, Composite composite, String category, String message, List<Serializable> params) {
    if (composite instanceof ServiceComposite) {
        EntityBuilder<ServiceLogRecord> builder = uow.newEntityBuilder(ServiceLogRecord.class);
        ServiceLogRecord state = builder.instance();
        setStandardStuff(type, composite, category, message, state, params);
        state.source().set(((ServiceComposite) composite).identity().get());
        ServiceLogRecord slr = builder.newInstance();
    } else if (composite instanceof EntityComposite) {
        EntityBuilder<EntityLogRecord> builder = uow.newEntityBuilder(EntityLogRecord.class);
        EntityLogRecord state = builder.instance();
        setStandardStuff(type, composite, category, message, state, params);
        state.source().set((EntityComposite) composite);
        EntityLogRecord elr = builder.newInstance();
    } else {
        EntityBuilder<CompositeLogRecord> builder = uow.newEntityBuilder(CompositeLogRecord.class);
        CompositeLogRecord state = builder.instance();
        setStandardStuff(type, composite, category, message, state, params);
        state.source().set(composite);
        CompositeLogRecord clr = builder.newInstance();
    }
}
Also used : EntityComposite(org.qi4j.api.entity.EntityComposite) CompositeLogRecord(org.qi4j.logging.log.records.CompositeLogRecord) EntityLogRecord(org.qi4j.logging.log.records.EntityLogRecord) EntityBuilder(org.qi4j.api.entity.EntityBuilder) ServiceLogRecord(org.qi4j.logging.log.records.ServiceLogRecord) ServiceComposite(org.qi4j.api.service.ServiceComposite)

Aggregations

EntityComposite (org.qi4j.api.entity.EntityComposite)8 EntityBuilder (org.qi4j.api.entity.EntityBuilder)3 ServiceComposite (org.qi4j.api.service.ServiceComposite)3 EntityInstance (org.qi4j.runtime.entity.EntityInstance)3 NoSuchEntityException (org.qi4j.api.unitofwork.NoSuchEntityException)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Test (org.junit.Test)1 AssociationStateDescriptor (org.qi4j.api.association.AssociationStateDescriptor)1 AssociationStateHolder (org.qi4j.api.association.AssociationStateHolder)1 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)1 EntityReference (org.qi4j.api.entity.EntityReference)1 ConcurrentEntityModificationException (org.qi4j.api.unitofwork.ConcurrentEntityModificationException)1 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)1 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)1 UnitOfWorkCompletionException (org.qi4j.api.unitofwork.UnitOfWorkCompletionException)1 UnitOfWorkException (org.qi4j.api.unitofwork.UnitOfWorkException)1 Usecase (org.qi4j.api.usecase.Usecase)1 ValueComposite (org.qi4j.api.value.ValueComposite)1 ValueDescriptor (org.qi4j.api.value.ValueDescriptor)1