use of org.qi4j.functional.Function in project qi4j-sdk by Qi4j.
the class CompositeAssemblyImpl method concernDeclarations.
private Iterable<Class<?>> concernDeclarations(ArrayList<Type> allTypes) {
// Find all concerns and flattern them into an iterable
Function<Type, Iterable<Class<?>>> function = new Function<Type, Iterable<Class<?>>>() {
@Override
public Iterable<Class<?>> map(Type type) {
Concerns concerns = Annotations.annotationOn(type, Concerns.class);
if (concerns == null) {
return empty();
} else {
return iterable(concerns.value());
}
}
};
Iterable<Class<?>> flatten = flattenIterables(map(function, allTypes));
return toList(flatten);
}
use of org.qi4j.functional.Function in project qi4j-sdk by Qi4j.
the class ValueToEntityMixin method doUnqualifiedConversion.
private <T> EntityBuilder<?> doUnqualifiedConversion(Class<T> entityType, String identity, final AssociationStateHolder vState, final AssociationStateDescriptor vStateDesc) {
Function<PropertyDescriptor, Object> props = new Function<PropertyDescriptor, Object>() {
@Override
public Object map(PropertyDescriptor ePropDesc) {
String propName = ePropDesc.qualifiedName().name();
try {
PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName(propName);
return vState.propertyFor(vPropDesc.accessor()).get();
} catch (IllegalArgumentException propNotFoundOnValue) {
// Property not found on Value
return null;
}
}
};
Function<AssociationDescriptor, EntityReference> assocs = new Function<AssociationDescriptor, EntityReference>() {
@Override
public EntityReference map(AssociationDescriptor eAssocDesc) {
String assocName = eAssocDesc.qualifiedName().name();
try {
AssociationDescriptor vAssocDesc = vStateDesc.getAssociationByName(assocName);
Object assocEntity = vState.associationFor(vAssocDesc.accessor()).get();
return assocEntity == null ? null : EntityReference.entityReferenceFor(assocEntity);
} catch (IllegalArgumentException assocNotFoundOnValue) {
// Association not found on Value, find Property<String> and convert to Association
try {
PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName(assocName);
if (STRING_TYPE_SPEC.satisfiedBy(vPropDesc.valueType())) {
String assocId = (String) vState.propertyFor(vPropDesc.accessor()).get();
return assocId == null ? null : EntityReference.parseEntityReference(assocId);
}
return null;
} catch (IllegalArgumentException propNotFoundOnValue) {
return null;
}
}
}
};
Function<AssociationDescriptor, Iterable<EntityReference>> manyAssocs = new Function<AssociationDescriptor, Iterable<EntityReference>>() {
@Override
public Iterable<EntityReference> map(AssociationDescriptor eAssocDesc) {
String assocName = eAssocDesc.qualifiedName().name();
try {
AssociationDescriptor vAssocDesc = vStateDesc.getManyAssociationByName(assocName);
ManyAssociation<Object> vManyAssoc = vState.manyAssociationFor(vAssocDesc.accessor());
return MANY_ASSOC_TO_ENTITY_REF_ITERABLE.map(vManyAssoc);
} catch (IllegalArgumentException assocNotFoundOnValue) {
// ManyAssociation not found on Value, find List<String> and convert to ManyAssociation
try {
PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName(assocName);
if (STRING_COLLECTION_TYPE_SPEC.satisfiedBy(vPropDesc.valueType())) {
Collection<String> vAssocState = (Collection) vState.propertyFor(vPropDesc.accessor()).get();
return STRING_COLLEC_TO_ENTITY_REF_ITERABLE.map(vAssocState);
}
return Iterables.empty();
} catch (IllegalArgumentException propNotFoundOnValue) {
return Iterables.empty();
}
}
}
};
Function<AssociationDescriptor, Map<String, EntityReference>> namedAssocs = new Function<AssociationDescriptor, Map<String, EntityReference>>() {
@Override
public Map<String, EntityReference> map(AssociationDescriptor eAssocDesc) {
String assocName = eAssocDesc.qualifiedName().name();
try {
AssociationDescriptor vAssocDesc = vStateDesc.getNamedAssociationByName(assocName);
NamedAssociation<Object> vAssocState = vState.namedAssociationFor(vAssocDesc.accessor());
return NAMED_ASSOC_TO_ENTITY_REF_MAP.map(vAssocState);
} catch (IllegalArgumentException assocNotFoundOnValue) {
// Find Map<String,String> Property and convert to NamedAssociation
try {
PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName(assocName);
if (STRING_MAP_TYPE_SPEC.satisfiedBy(vPropDesc.valueType())) {
Map<String, String> vAssocState = (Map) vState.propertyFor(vPropDesc.accessor()).get();
return STRING_MAP_TO_ENTITY_REF_MAP.map(vAssocState);
}
return Collections.EMPTY_MAP;
} catch (IllegalArgumentException propNotFoundOnValue) {
return Collections.EMPTY_MAP;
}
}
}
};
return module.currentUnitOfWork().newEntityBuilderWithState(entityType, identity, props, assocs, manyAssocs, namedAssocs);
}
use of org.qi4j.functional.Function in project qi4j-sdk by Qi4j.
the class StorageModule method assemble.
@Override
public void assemble(ModuleAssembly module) throws AssemblyException {
module.services(MemoryEntityStoreService.class).visibleIn(Visibility.application);
module.services(UuidIdentityGeneratorService.class).visibleIn(Visibility.application);
new OrgJsonValueSerializationAssembler().visibleIn(Visibility.application).withValuesModuleFinder(new Function<Application, Module>() {
@Override
public Module map(Application app) {
return app.findModule("DomainLayer", "RentalModule");
}
}).assemble(module);
}
use of org.qi4j.functional.Function in project qi4j-sdk by Qi4j.
the class JdbmEventStoreServiceTest method testDomainEvent.
@Test
public void testDomainEvent() throws UnitOfWorkCompletionException, IOException {
UnitOfWork uow = module.newUnitOfWork(UsecaseBuilder.newUsecase("Create entity"));
TestEntity entity = uow.newEntity(TestEntity.class);
uow.complete();
int count = 10;
for (int i = 0; i < count; i++) {
uow = module.newUnitOfWork(UsecaseBuilder.newUsecase("Change description"));
uow.setMetaInfo(new Principal() {
public String getName() {
return "administrator";
}
});
entity = uow.get(entity);
entity.changeDescription("New description");
uow.complete();
}
EventSource source = (EventSource) module.findService(EventSource.class).get();
source.events(0, Long.MAX_VALUE).transferTo(Transforms.map(new Function<UnitOfWorkDomainEventsValue, String>() {
public String map(UnitOfWorkDomainEventsValue unitOfWorkDomainEventsValue) {
return unitOfWorkDomainEventsValue.toString();
}
}, Outputs.systemOut()));
}
use of org.qi4j.functional.Function in project qi4j-sdk by Qi4j.
the class Assembler method assembleInfrastructureLayer.
private void assembleInfrastructureLayer(LayerAssembly infrastructureLayer) throws AssemblyException {
ModuleAssembly serializationModule = infrastructureLayer.module("INFRASTRUCTURE-Serialization");
serializationModule.services(OrgJsonValueSerializationService.class).taggedWith(ValueSerialization.Formats.JSON).setMetaInfo(new Function<Application, Module>() {
@Override
public Module map(Application application) {
return application.findModule("CONTEXT", "CONTEXT-ContextSupport");
}
}).visibleIn(application);
ModuleAssembly indexingModule = infrastructureLayer.module("INFRASTRUCTURE-Indexing");
indexingModule.objects(EntityStateSerializer.class, EntityTypeSerializer.class);
indexingModule.addServices(MemoryRepositoryService.class, RdfIndexingEngineService.class).instantiateOnStartup().visibleIn(application);
ModuleAssembly entityStoreModule = infrastructureLayer.module("INFRASTRUCTURE-EntityStore");
entityStoreModule.addServices(MemoryEntityStoreService.class, UuidIdentityGeneratorService.class).instantiateOnStartup().visibleIn(application);
ModuleAssembly externalServiceModule = infrastructureLayer.module("INFRASTRUCTURE-ExternalService");
externalServiceModule.importedServices(GraphTraversalService.class).setMetaInfo(new GraphTraversalServiceImpl(new GraphDAO())).visibleIn(application);
}
Aggregations