Search in sources :

Example 26 with Module

use of org.qi4j.api.structure.Module in project qi4j-sdk by Qi4j.

the class TypeToCompositeLookupTest method entitiesAmbiguousDeclaration.

@Test
public void entitiesAmbiguousDeclaration() throws UnitOfWorkCompletionException, ActivationException, AssemblyException {
    Module module = new SingletonAssembler() {

        @Override
        public void assemble(ModuleAssembly module) throws AssemblyException {
            new EntityTestAssembler().assemble(module);
            module.entities(SomeOtherFoo.class, BasicFoo.class);
        }
    }.module();
    UnitOfWork uow = module.newUnitOfWork();
    SomeOtherFoo someOtherFoo = uow.newEntityBuilder(SomeOtherFoo.class).newInstance();
    BasicFoo basicFoo = uow.newEntityBuilder(BasicFoo.class).newInstance();
    try {
        uow.newEntityBuilder(Foo.class).newInstance();
        fail("Ambiguous type exception not detected for Entities");
    } catch (AmbiguousTypeException expected) {
    }
    // Specific Type used
    assertEquals(CATHEDRAL, uow.newEntityBuilder(SomeOtherFoo.class).newInstance().bar());
    // Specific Type used
    assertEquals(BAZAR, uow.newEntityBuilder(BasicFoo.class).newInstance().bar());
    String someOtherFooIdentity = ((Identity) someOtherFoo).identity().get();
    String basicFooIdentity = ((Identity) basicFoo).identity().get();
    uow.complete();
    uow = module.newUnitOfWork();
    assertEquals(CATHEDRAL, uow.get(SomeOtherFoo.class, someOtherFooIdentity).bar());
    assertEquals(BAZAR, uow.get(BasicFoo.class, basicFooIdentity).bar());
    assertEquals(CATHEDRAL, uow.get(Foo.class, someOtherFooIdentity).bar());
    assertEquals(BAZAR, uow.get(Foo.class, basicFooIdentity).bar());
    uow.discard();
}
Also used : ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) AssemblyException(org.qi4j.bootstrap.AssemblyException) UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) EntityTestAssembler(org.qi4j.test.EntityTestAssembler) Module(org.qi4j.api.structure.Module) AmbiguousTypeException(org.qi4j.api.composite.AmbiguousTypeException) Test(org.junit.Test)

Example 27 with Module

use of org.qi4j.api.structure.Module in project qi4j-sdk by Qi4j.

the class SQLEntityStoreMixin method readEntityState.

protected DefaultEntityState readEntityState(DefaultEntityStoreUnitOfWork unitOfWork, Reader entityState) throws EntityStoreException {
    try {
        Module module = unitOfWork.module();
        JSONObject jsonObject = new JSONObject(new JSONTokener(entityState));
        EntityStatus status = EntityStatus.LOADED;
        String version = jsonObject.getString("version");
        long modified = jsonObject.getLong("modified");
        String identity = jsonObject.getString("identity");
        // Check if version is correct
        String currentAppVersion = jsonObject.optString(MapEntityStore.JSONKeys.application_version.name(), "0.0");
        if (!currentAppVersion.equals(application.version())) {
            if (migration != null) {
                migration.migrate(jsonObject, application.version(), this);
            } else {
                // Do nothing - set version to be correct
                jsonObject.put(MapEntityStore.JSONKeys.application_version.name(), application.version());
            }
            LOGGER.trace("Updated version nr on {} from {} to {}", new Object[] { identity, currentAppVersion, application.version() });
            // State changed
            status = EntityStatus.UPDATED;
        }
        String type = jsonObject.getString("type");
        EntityDescriptor entityDescriptor = module.entityDescriptor(type);
        if (entityDescriptor == null) {
            throw new EntityTypeNotFoundException(type);
        }
        Map<QualifiedName, Object> properties = new HashMap<QualifiedName, Object>();
        JSONObject props = jsonObject.getJSONObject("properties");
        for (PropertyDescriptor propertyDescriptor : entityDescriptor.state().properties()) {
            Object jsonValue;
            try {
                jsonValue = props.get(propertyDescriptor.qualifiedName().name());
            } catch (JSONException e) {
                // Value not found, default it
                Object initialValue = propertyDescriptor.initialValue(module);
                properties.put(propertyDescriptor.qualifiedName(), initialValue);
                status = EntityStatus.UPDATED;
                continue;
            }
            if (JSONObject.NULL.equals(jsonValue)) {
                properties.put(propertyDescriptor.qualifiedName(), null);
            } else {
                Object value = valueSerialization.deserialize(propertyDescriptor.valueType(), jsonValue.toString());
                properties.put(propertyDescriptor.qualifiedName(), value);
            }
        }
        Map<QualifiedName, EntityReference> associations = new HashMap<QualifiedName, EntityReference>();
        JSONObject assocs = jsonObject.getJSONObject("associations");
        for (AssociationDescriptor associationType : entityDescriptor.state().associations()) {
            try {
                Object jsonValue = assocs.get(associationType.qualifiedName().name());
                EntityReference value = jsonValue == JSONObject.NULL ? null : EntityReference.parseEntityReference((String) jsonValue);
                associations.put(associationType.qualifiedName(), value);
            } catch (JSONException e) {
                // Association not found, default it to null
                associations.put(associationType.qualifiedName(), null);
                status = EntityStatus.UPDATED;
            }
        }
        JSONObject manyAssocs = jsonObject.getJSONObject("manyassociations");
        Map<QualifiedName, List<EntityReference>> manyAssociations = new HashMap<QualifiedName, List<EntityReference>>();
        for (AssociationDescriptor manyAssociationType : entityDescriptor.state().manyAssociations()) {
            List<EntityReference> references = new ArrayList<EntityReference>();
            try {
                JSONArray jsonValues = manyAssocs.getJSONArray(manyAssociationType.qualifiedName().name());
                for (int i = 0; i < jsonValues.length(); i++) {
                    Object jsonValue = jsonValues.getString(i);
                    EntityReference value = jsonValue == JSONObject.NULL ? null : EntityReference.parseEntityReference((String) jsonValue);
                    references.add(value);
                }
                manyAssociations.put(manyAssociationType.qualifiedName(), references);
            } catch (JSONException e) {
                // ManyAssociation not found, default to empty one
                manyAssociations.put(manyAssociationType.qualifiedName(), references);
            }
        }
        return new DefaultEntityState(unitOfWork, version, modified, EntityReference.parseEntityReference(identity), status, entityDescriptor, properties, associations, manyAssociations);
    } catch (JSONException e) {
        throw new EntityStoreException(e);
    }
}
Also used : EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) DefaultEntityState(org.qi4j.spi.entitystore.helpers.DefaultEntityState) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) EntityReference(org.qi4j.api.entity.EntityReference) EntityStatus(org.qi4j.spi.entity.EntityStatus) List(java.util.List) ArrayList(java.util.ArrayList) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) QualifiedName(org.qi4j.api.common.QualifiedName) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONTokener(org.json.JSONTokener) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) Module(org.qi4j.api.structure.Module)

Example 28 with Module

use of org.qi4j.api.structure.Module in project qi4j-sdk by Qi4j.

the class ModuleAssemblyImpl method assembleModule.

ModuleModel assembleModule(AssemblyHelper helper) throws AssemblyException {
    List<TransientModel> transientModels = new ArrayList<TransientModel>();
    List<ObjectModel> objectModels = new ArrayList<ObjectModel>();
    List<ValueModel> valueModels = new ArrayList<ValueModel>();
    List<ServiceModel> serviceModels = new ArrayList<ServiceModel>();
    List<ImportedServiceModel> importedServiceModels = new ArrayList<ImportedServiceModel>();
    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<EntityModel>();
    for (EntityAssemblyImpl entityDeclaration : entityAssemblies.values()) {
        entityModels.add(entityDeclaration.newEntityModel(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<Module>(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<String>();
    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) {
            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) Module(org.qi4j.api.structure.Module)

Example 29 with Module

use of org.qi4j.api.structure.Module in project qi4j-sdk by Qi4j.

the class ConstructorInjectionOfThisTest method givenSideEffectWithThisInConstructorWhenCreatingModelExpectException.

@Test
public void givenSideEffectWithThisInConstructorWhenCreatingModelExpectException() throws ActivationException, AssemblyException {
    SingletonAssembler singletonAssembler = new SingletonAssembler() {

        @Override
        public void assemble(ModuleAssembly module) throws AssemblyException {
            module.values(Does.class).withSideEffects(DoesSideEffect.class);
        }
    };
    Module module = singletonAssembler.application().findModule("Layer 1", "Module 1");
    Does does = module.newValue(Does.class);
    does.doSomething();
}
Also used : ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) Module(org.qi4j.api.structure.Module) Test(org.junit.Test)

Example 30 with Module

use of org.qi4j.api.structure.Module in project qi4j-sdk by Qi4j.

the class ConstructorInjectionOfThisTest method givenConcernWithThisInConstructorWhenCreatingModelExpectException.

@Test
public void givenConcernWithThisInConstructorWhenCreatingModelExpectException() throws ActivationException, AssemblyException {
    SingletonAssembler singletonAssembler = new SingletonAssembler() {

        @Override
        public void assemble(ModuleAssembly module) throws AssemblyException {
            module.values(Does.class).withConcerns(DoesConcern.class);
        }
    };
    Module module = singletonAssembler.application().findModule("Layer 1", "Module 1");
    Does does = module.newValue(Does.class);
    does.doSomething();
}
Also used : ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) Module(org.qi4j.api.structure.Module) Test(org.junit.Test)

Aggregations

Module (org.qi4j.api.structure.Module)41 Test (org.junit.Test)28 ModuleAssembly (org.qi4j.bootstrap.ModuleAssembly)18 Application (org.qi4j.api.structure.Application)17 AssemblyException (org.qi4j.bootstrap.AssemblyException)16 SingletonAssembler (org.qi4j.bootstrap.SingletonAssembler)15 ArrayList (java.util.ArrayList)6 Energy4Java (org.qi4j.bootstrap.Energy4Java)6 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)5 UnitOfWorkFactory (org.qi4j.api.unitofwork.UnitOfWorkFactory)5 AmbiguousTypeException (org.qi4j.api.composite.AmbiguousTypeException)4 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)4 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)4 Assembler (org.qi4j.bootstrap.Assembler)4 HashMap (java.util.HashMap)3 List (java.util.List)3 JSONException (org.json.JSONException)3 JSONObject (org.json.JSONObject)3 JSONTokener (org.json.JSONTokener)3 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)3