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