use of org.qi4j.functional.Function in project qi4j-sdk by Qi4j.
the class CompositeAssemblyImpl method sideEffectDeclarations.
protected Iterable<Class<?>> sideEffectDeclarations(ArrayList<Type> allTypes) {
// Find all side-effects and flattern them into an iterable
Function<Type, Iterable<Class<?>>> function = new Function<Type, Iterable<Class<?>>>() {
@Override
public Iterable<Class<?>> map(Type type) {
SideEffects sideEffects = Annotations.annotationOn(type, SideEffects.class);
if (sideEffects == null) {
return empty();
} else {
return iterable(sideEffects.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 CompositeAssemblyImpl method constraintDeclarations.
private Iterable<Class<? extends Constraint<?, ?>>> constraintDeclarations(ArrayList<Type> allTypes) {
// Find all constraints and flatten them into an iterable
Function<Type, Iterable<Class<? extends Constraint<?, ?>>>> function = new Function<Type, Iterable<Class<? extends Constraint<?, ?>>>>() {
@Override
public Iterable<Class<? extends Constraint<?, ?>>> map(Type type) {
Constraints constraints = Annotations.annotationOn(type, Constraints.class);
if (constraints == null) {
return empty();
} else {
return iterable(constraints.value());
}
}
};
Iterable<Class<? extends Constraint<?, ?>>> flatten = flattenIterables(map(function, allTypes));
return toList(flatten);
}
use of org.qi4j.functional.Function in project qi4j-sdk by Qi4j.
the class EntityBuilderWithStateTest method test.
@Test
public void test() throws UnitOfWorkCompletionException {
final String associatedIdentity;
try (UnitOfWork uow = module.newUnitOfWork()) {
EntityBuilder<SomeEntity> builder = uow.newEntityBuilder(SomeEntity.class);
builder.instance().prop().set("Associated");
SomeEntity entity = builder.newInstance();
associatedIdentity = entity.identity().get();
uow.complete();
}
try (UnitOfWork uow = module.newUnitOfWork()) {
SomeEntity entity = uow.newEntityBuilderWithState(SomeEntity.class, new Function<PropertyDescriptor, Object>() {
@Override
public Object map(PropertyDescriptor descriptor) {
if ("prop".equals(descriptor.qualifiedName().name())) {
return "Foo";
}
return null;
}
}, new Function<AssociationDescriptor, EntityReference>() {
@Override
public EntityReference map(AssociationDescriptor descriptor) {
if ("ass".equals(descriptor.qualifiedName().name())) {
return EntityReference.parseEntityReference(associatedIdentity);
}
return null;
}
}, new Function<AssociationDescriptor, Iterable<EntityReference>>() {
@Override
public Iterable<EntityReference> map(AssociationDescriptor descriptor) {
if ("manyAss".equals(descriptor.qualifiedName().name())) {
return Arrays.asList(EntityReference.parseEntityReference(associatedIdentity));
}
return null;
}
}, new Function<AssociationDescriptor, Map<String, EntityReference>>() {
@Override
public Map<String, EntityReference> map(AssociationDescriptor descriptor) {
if ("namedAss".equals(descriptor.qualifiedName().name())) {
return Collections.singletonMap("foo", EntityReference.parseEntityReference(associatedIdentity));
}
return null;
}
}).newInstance();
assertThat(entity.prop().get(), equalTo("Foo"));
assertThat(entity.ass().get().identity().get(), equalTo(associatedIdentity));
assertThat(entity.manyAss().get(0).identity().get(), equalTo(associatedIdentity));
assertThat(entity.namedAss().get("foo").identity().get(), equalTo(associatedIdentity));
uow.complete();
}
}
use of org.qi4j.functional.Function in project qi4j-sdk by Qi4j.
the class LiquibaseServiceTest method testLiquibase.
@Test
public void testLiquibase() throws SQLException, IOException, ActivationException, AssemblyException {
final SingletonAssembler assembler = new SingletonAssembler() {
@Override
public void assemble(ModuleAssembly module) throws AssemblyException {
new C3P0DataSourceServiceAssembler().identifiedBy("datasource-service").assemble(module);
new DataSourceAssembler().withDataSourceServiceIdentity("datasource-service").identifiedBy("testds-liquibase").withCircuitBreaker().assemble(module);
module.values(SomeValue.class);
// Set up Liquibase service that will create the tables
ModuleAssembly configModule = module;
// START SNIPPET: assembly
new LiquibaseAssembler(Visibility.module).withConfigIn(configModule, Visibility.layer).assemble(module);
// END SNIPPET: assembly
module.forMixin(LiquibaseConfiguration.class).declareDefaults().enabled().set(true);
module.forMixin(LiquibaseConfiguration.class).declareDefaults().changeLog().set("changelog.xml");
// Create in-memory store for configurations
new EntityTestAssembler().assemble(module);
}
@Override
public void beforeActivation(Application application) {
application.registerActivationEventListener(new ActivationEventListener() {
@Override
public void onEvent(ActivationEvent event) {
System.out.println(event);
}
});
}
};
Module module = assembler.module();
// START SNIPPET: io
// Look up the DataSource
DataSource ds = module.findService(DataSource.class).get();
// Instanciate Databases helper
Databases database = new Databases(ds);
// Assert that insertion works
assertTrue(database.update("insert into test values ('someid', 'bar')") == 1);
// END SNIPPET: io
database.query("select * from test", new Databases.ResultSetVisitor() {
@Override
public boolean visit(ResultSet visited) throws SQLException {
assertThat(visited.getString("id"), equalTo("someid"));
assertThat(visited.getString("foo"), equalTo("bar"));
return true;
}
});
Function<ResultSet, SomeValue> toValue = new Function<ResultSet, SomeValue>() {
@Override
public SomeValue map(ResultSet resultSet) {
ValueBuilder<SomeValue> builder = assembler.module().newValueBuilder(SomeValue.class);
try {
builder.prototype().id().set(resultSet.getString("id"));
builder.prototype().foo().set(resultSet.getString("foo"));
} catch (SQLException e) {
throw new IllegalArgumentException("Could not convert to SomeValue", e);
}
return builder.newInstance();
}
};
// START SNIPPET: io
// Select rows and load them in a List
List<SomeValue> rows = new ArrayList<SomeValue>();
database.query("select * from test").transferTo(map(toValue, collection(rows)));
// Transfer all rows to System.out
Inputs.iterable(rows).transferTo(Outputs.systemOut());
// END SNIPPET: io
}
use of org.qi4j.functional.Function in project qi4j-sdk by Qi4j.
the class CompositeAssemblyImpl method sideEffectDeclarations.
protected Iterable<Class<?>> sideEffectDeclarations(Iterable<? extends Class<?>> typess) {
// Find side-effect declarations
ArrayList<Type> allTypes = new ArrayList<Type>();
for (Class<?> type : typess) {
Iterable<Type> types = typesOf(type);
Iterables.addAll(allTypes, types);
}
// Find all side-effects and flattern them into an iterable
Function<Type, Iterable<Class<?>>> function = new Function<Type, Iterable<Class<?>>>() {
@Override
public Iterable<Class<?>> map(Type type) {
SideEffects sideEffects = Annotations.annotationOn(type, SideEffects.class);
if (sideEffects == null) {
return Iterables.empty();
} else {
return iterable(sideEffects.value());
}
}
};
Iterable<Class<?>> flatten = Iterables.flattenIterables(Iterables.map(function, allTypes));
return Iterables.toList(flatten);
}
Aggregations