use of org.qi4j.spi.entitystore.EntityStore 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);
} 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);
}
}
return entityInstance.proxy();
}
use of org.qi4j.spi.entitystore.EntityStore in project qi4j-sdk by Qi4j.
the class ModuleUnitOfWork method newEntityBuilder.
@Override
public <T> EntityBuilder<T> newEntityBuilder(Class<T> type, String identity) throws EntityTypeNotFoundException {
ModelModule<EntityModel> model = moduleInstance.typeLookup().lookupEntityModel(type);
if (model == null) {
throw new EntityTypeNotFoundException(type.getName());
}
EntityStore entityStore = model.module().entityStore();
// Generate id if necessary
if (identity == null) {
IdentityGenerator idGen = model.module().identityGenerator();
if (idGen == null) {
throw new NoSuchServiceException(IdentityGenerator.class.getName(), model.module().name());
}
identity = idGen.generate(first(model.model().types()));
}
EntityBuilder<T> builder;
builder = new EntityBuilderInstance<T>(model, this, uow.getEntityStoreUnitOfWork(entityStore, moduleInstance), identity);
return builder;
}
use of org.qi4j.spi.entitystore.EntityStore in project qi4j-sdk by Qi4j.
the class DebuggingTest method whenCallingMethodThenExpectDebugEntityCreated.
@Test
public void whenCallingMethodThenExpectDebugEntityCreated() {
UnitOfWork uow = module.newUnitOfWork();
try {
// There is no Query capability available for Libraries, since that sits in Extensions.
// Obtaining the EntityStore directly is a very ugly hack to get around this problem, and only related
// to the test sitting in qi4j-libraries source repository.
// QueryBuilder<DebugRecord> builder = module.newQueryBuilder( DebugRecord.class );
// Query<DebugRecord> query = builder.newQuery( uow );
// assertEquals( 0, query.count() );
Some service = (Some) module.findService(Some.class).get();
String message = service.doSomething("World!", 10);
assertEquals(message, "Hello!");
EntityStore es = (EntityStore) module.findService(EntityStore.class).get();
final String[] result = new String[1];
es.entityStates(module).transferTo(Transforms.map(new Function<EntityState, EntityState>() {
public EntityState map(EntityState entityState) {
if (ServiceDebugRecordEntity.class.getName().equals(first(entityState.entityDescriptor().types()).getName())) {
result[0] = entityState.identity().identity();
}
return entityState;
}
}, Outputs.<EntityState>noop()));
ServiceDebugRecordEntity debugEntry = uow.get(ServiceDebugRecordEntity.class, result[0]);
String mess = debugEntry.message().get();
System.out.println(mess);
assertEquals("some message.", mess);
uow.complete();
} catch (ConcurrentEntityModificationException e) {
e.printStackTrace();
uow.discard();
} catch (UnitOfWorkCompletionException e) {
e.printStackTrace();
uow.discard();
} finally {
if (uow.isOpen()) {
uow.discard();
}
}
}
use of org.qi4j.spi.entitystore.EntityStore in project qi4j-sdk by Qi4j.
the class ModuleUnitOfWork method newEntityBuilderWithState.
@Override
public <T> EntityBuilder<T> newEntityBuilderWithState(Class<T> type, String identity, Function<PropertyDescriptor, Object> propertyFunction, Function<AssociationDescriptor, EntityReference> associationFunction, Function<AssociationDescriptor, Iterable<EntityReference>> manyAssociationFunction, Function<AssociationDescriptor, Map<String, EntityReference>> namedAssociationFunction) throws EntityTypeNotFoundException {
NullArgumentException.validateNotNull("propertyFunction", propertyFunction);
NullArgumentException.validateNotNull("associationFunction", associationFunction);
NullArgumentException.validateNotNull("manyAssociationFunction", manyAssociationFunction);
NullArgumentException.validateNotNull("namedAssociationFunction", namedAssociationFunction);
ModelModule<EntityModel> model = moduleInstance.typeLookup().lookupEntityModel(type);
if (model == null) {
throw new EntityTypeNotFoundException(type.getName());
}
EntityStore entityStore = model.module().entityStore();
StateResolver stateResolver = new FunctionStateResolver(propertyFunction, associationFunction, manyAssociationFunction, namedAssociationFunction);
if (identity == null) {
// Use identity from StateResolver if available
PropertyModel identityModel = model.model().state().findPropertyModelByQualifiedName(IDENTITY_STATE_NAME);
identity = (String) stateResolver.getPropertyState(identityModel);
if (identity == null) {
// Generate identity
IdentityGenerator idGen = model.module().identityGenerator();
if (idGen == null) {
throw new NoSuchServiceException(IdentityGenerator.class.getName(), model.module().name());
}
identity = idGen.generate(first(model.model().types()));
}
}
return new EntityBuilderInstance<>(model, this, uow.getEntityStoreUnitOfWork(entityStore, moduleInstance), identity, stateResolver);
}
Aggregations