use of org.qi4j.runtime.entity.EntityModel 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.EntityModel in project qi4j-sdk by Qi4j.
the class EntityAssemblyImpl method newEntityModel.
EntityModel newEntityModel(StateDeclarations stateDeclarations, AssociationDeclarations associationDecs, ManyAssociationDeclarations manyAssociationDecs, NamedAssociationDeclarations namedAssociationDecs, AssemblyHelper helper) {
this.associationDeclarations = associationDecs;
this.manyAssociationDeclarations = manyAssociationDecs;
this.namedAssociationDeclarations = namedAssociationDecs;
try {
associationsModel = new AssociationsModel();
manyAssociationsModel = new ManyAssociationsModel();
namedAssociationsModel = new NamedAssociationsModel();
buildComposite(helper, stateDeclarations);
EntityModel entityModel = new EntityModel(types, visibility, metaInfo, (EntityMixinsModel) mixinsModel, (EntityStateModel) stateModel, compositeMethodsModel);
return entityModel;
} catch (Exception e) {
throw new InvalidApplicationException("Could not register " + types, e);
}
}
use of org.qi4j.runtime.entity.EntityModel 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<>(model, this, uow.getEntityStoreUnitOfWork(entityStore, moduleInstance), identity);
return builder;
}
use of org.qi4j.runtime.entity.EntityModel 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.EntityModel in project qi4j-sdk by Qi4j.
the class ModuleAssemblyImpl method assembleModule.
ModuleModel assembleModule(AssemblyHelper helper) throws AssemblyException {
List<TransientModel> transientModels = new ArrayList<>();
List<ObjectModel> objectModels = new ArrayList<>();
List<ValueModel> valueModels = new ArrayList<>();
List<ServiceModel> serviceModels = new ArrayList<>();
List<ImportedServiceModel> importedServiceModels = new ArrayList<>();
if (name == null) {
throw new AssemblyException("Module must have name set");
}
for (TransientAssemblyImpl compositeDeclaration : transientAssemblies.values()) {
transientModels.add(compositeDeclaration.newTransientModel(metaInfoDeclaration, helper));
}
for (ValueAssemblyImpl valueDeclaration : valueAssemblies.values()) {
valueModels.add(valueDeclaration.newValueModel(metaInfoDeclaration, helper));
}
List<EntityModel> entityModels = new ArrayList<>();
for (EntityAssemblyImpl entityDeclaration : entityAssemblies.values()) {
entityModels.add(entityDeclaration.newEntityModel(metaInfoDeclaration, metaInfoDeclaration, metaInfoDeclaration, metaInfoDeclaration, helper));
}
for (ObjectAssemblyImpl objectDeclaration : objectAssemblies.values()) {
objectDeclaration.addObjectModel(objectModels);
}
for (ServiceAssemblyImpl serviceDeclaration : serviceAssemblies) {
if (serviceDeclaration.identity == null) {
serviceDeclaration.identity = generateId(serviceDeclaration.types());
}
serviceModels.add(serviceDeclaration.newServiceModel(metaInfoDeclaration, helper));
}
for (ImportedServiceAssemblyImpl importedServiceDeclaration : importedServiceAssemblies.values()) {
importedServiceDeclaration.addImportedServiceModel(importedServiceModels);
}
ModuleModel moduleModel = new ModuleModel(name, metaInfo, new ActivatorsModel<>(activators), new TransientsModel(transientModels), new EntitiesModel(entityModels), new ObjectsModel(objectModels), new ValuesModel(valueModels), new ServicesModel(serviceModels), new ImportedServicesModel(importedServiceModels));
// Check for duplicate service identities
Set<String> identities = new HashSet<>();
for (ServiceModel serviceModel : serviceModels) {
String identity = serviceModel.identity();
if (identities.contains(identity)) {
throw new DuplicateServiceIdentityException("Duplicated service identity: " + identity + " in module " + moduleModel.name());
}
identities.add(identity);
}
for (ImportedServiceModel serviceModel : importedServiceModels) {
String identity = serviceModel.identity();
if (identities.contains(identity)) {
throw new DuplicateServiceIdentityException("Duplicated service identity: " + identity + " in module " + moduleModel.name());
}
identities.add(identity);
}
for (ImportedServiceModel importedServiceModel : importedServiceModels) {
boolean found = false;
for (ObjectModel objectModel : objectModels) {
if (first(objectModel.types()).equals(importedServiceModel.serviceImporter())) {
found = true;
break;
}
}
if (!found) {
@SuppressWarnings("raw") Class<? extends ServiceImporter> serviceFactoryType = importedServiceModel.serviceImporter();
ObjectModel objectModel = new ObjectModel(serviceFactoryType, Visibility.module, new MetaInfo());
objectModels.add(objectModel);
}
}
return moduleModel;
}
Aggregations