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