Search in sources :

Example 1 with IdentityGenerator

use of org.qi4j.api.entity.IdentityGenerator 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;
}
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 2 with IdentityGenerator

use of org.qi4j.api.entity.IdentityGenerator 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

IdentityGenerator (org.qi4j.api.entity.IdentityGenerator)2 NoSuchServiceException (org.qi4j.api.service.NoSuchServiceException)2 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)2 EntityModel (org.qi4j.runtime.entity.EntityModel)2 EntityStore (org.qi4j.spi.entitystore.EntityStore)2 FunctionStateResolver (org.qi4j.runtime.composite.FunctionStateResolver)1 StateResolver (org.qi4j.runtime.composite.StateResolver)1 PropertyModel (org.qi4j.runtime.property.PropertyModel)1 EntityBuilderInstance (org.qi4j.runtime.unitofwork.EntityBuilderInstance)1