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;
}
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
}
}
}
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();
}
}
Aggregations