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();
}
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);
}
}
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;
}
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();
}
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();
}
Aggregations