Search in sources :

Example 6 with EntityModel

use of org.qi4j.runtime.entity.EntityModel 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);
}
Also used : NoSuchServiceException(org.qi4j.api.service.NoSuchServiceException) EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) EntityModel(org.qi4j.runtime.entity.EntityModel) PropertyModel(org.qi4j.runtime.property.PropertyModel) EntityStore(org.qi4j.spi.entitystore.EntityStore) FunctionStateResolver(org.qi4j.runtime.composite.FunctionStateResolver) StateResolver(org.qi4j.runtime.composite.StateResolver) IdentityGenerator(org.qi4j.api.entity.IdentityGenerator) EntityBuilderInstance(org.qi4j.runtime.unitofwork.EntityBuilderInstance) FunctionStateResolver(org.qi4j.runtime.composite.FunctionStateResolver)

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