Search in sources :

Example 1 with EntityModel

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);
}
Also used : EntityComposite(org.qi4j.api.entity.EntityComposite) EntityModel(org.qi4j.runtime.entity.EntityModel) EntityInstance(org.qi4j.runtime.entity.EntityInstance)

Example 2 with EntityModel

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);
    }
}
Also used : ManyAssociationsModel(org.qi4j.runtime.association.ManyAssociationsModel) NamedAssociationsModel(org.qi4j.runtime.association.NamedAssociationsModel) EntityModel(org.qi4j.runtime.entity.EntityModel) InvalidApplicationException(org.qi4j.api.common.InvalidApplicationException) ManyAssociationsModel(org.qi4j.runtime.association.ManyAssociationsModel) AssociationsModel(org.qi4j.runtime.association.AssociationsModel) NamedAssociationsModel(org.qi4j.runtime.association.NamedAssociationsModel) InvalidApplicationException(org.qi4j.api.common.InvalidApplicationException)

Example 3 with EntityModel

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;
}
Also used : NoSuchServiceException(org.qi4j.api.service.NoSuchServiceException) EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) EntityModel(org.qi4j.runtime.entity.EntityModel) EntityStore(org.qi4j.spi.entitystore.EntityStore) IdentityGenerator(org.qi4j.api.entity.IdentityGenerator)

Example 4 with EntityModel

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();
}
Also used : EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) EntityStoreUnitOfWork(org.qi4j.spi.entitystore.EntityStoreUnitOfWork) EntityModel(org.qi4j.runtime.entity.EntityModel) EntityInstance(org.qi4j.runtime.entity.EntityInstance) EntityState(org.qi4j.spi.entity.EntityState) EntityStore(org.qi4j.spi.entitystore.EntityStore) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) ModuleInstance(org.qi4j.runtime.structure.ModuleInstance) NoSuchEntityException(org.qi4j.api.unitofwork.NoSuchEntityException)

Example 5 with EntityModel

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;
}
Also used : ObjectModel(org.qi4j.runtime.object.ObjectModel) ValueModel(org.qi4j.runtime.value.ValueModel) ImportedServiceModel(org.qi4j.runtime.service.ImportedServiceModel) ArrayList(java.util.ArrayList) ImportedServicesModel(org.qi4j.runtime.service.ImportedServicesModel) MetaInfo(org.qi4j.api.common.MetaInfo) ObjectsModel(org.qi4j.runtime.object.ObjectsModel) ModuleModel(org.qi4j.runtime.structure.ModuleModel) EntitiesModel(org.qi4j.runtime.entity.EntitiesModel) AssemblyException(org.qi4j.bootstrap.AssemblyException) ServiceModel(org.qi4j.runtime.service.ServiceModel) ImportedServiceModel(org.qi4j.runtime.service.ImportedServiceModel) HashSet(java.util.HashSet) ImportedServicesModel(org.qi4j.runtime.service.ImportedServicesModel) ServicesModel(org.qi4j.runtime.service.ServicesModel) EntityModel(org.qi4j.runtime.entity.EntityModel) DuplicateServiceIdentityException(org.qi4j.api.service.DuplicateServiceIdentityException) ValuesModel(org.qi4j.runtime.value.ValuesModel) TransientModel(org.qi4j.runtime.composite.TransientModel) TransientsModel(org.qi4j.runtime.composite.TransientsModel)

Aggregations

EntityModel (org.qi4j.runtime.entity.EntityModel)6 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)3 EntityStore (org.qi4j.spi.entitystore.EntityStore)3 IdentityGenerator (org.qi4j.api.entity.IdentityGenerator)2 NoSuchServiceException (org.qi4j.api.service.NoSuchServiceException)2 EntityInstance (org.qi4j.runtime.entity.EntityInstance)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 InvalidApplicationException (org.qi4j.api.common.InvalidApplicationException)1 MetaInfo (org.qi4j.api.common.MetaInfo)1 EntityComposite (org.qi4j.api.entity.EntityComposite)1 DuplicateServiceIdentityException (org.qi4j.api.service.DuplicateServiceIdentityException)1 NoSuchEntityException (org.qi4j.api.unitofwork.NoSuchEntityException)1 AssemblyException (org.qi4j.bootstrap.AssemblyException)1 AssociationsModel (org.qi4j.runtime.association.AssociationsModel)1 ManyAssociationsModel (org.qi4j.runtime.association.ManyAssociationsModel)1 NamedAssociationsModel (org.qi4j.runtime.association.NamedAssociationsModel)1 FunctionStateResolver (org.qi4j.runtime.composite.FunctionStateResolver)1 StateResolver (org.qi4j.runtime.composite.StateResolver)1 TransientModel (org.qi4j.runtime.composite.TransientModel)1