Search in sources :

Example 1 with Function

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);
}
Also used : Function(org.qi4j.functional.Function) Annotations.isType(org.qi4j.api.util.Annotations.isType) Type(java.lang.reflect.Type) Classes.wrapperClass(org.qi4j.api.util.Classes.wrapperClass) SideEffects(org.qi4j.api.sideeffect.SideEffects)

Example 2 with Function

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);
}
Also used : Function(org.qi4j.functional.Function) Annotations.isType(org.qi4j.api.util.Annotations.isType) Type(java.lang.reflect.Type) Constraints(org.qi4j.api.constraint.Constraints) Constraint(org.qi4j.api.constraint.Constraint) Classes.wrapperClass(org.qi4j.api.util.Classes.wrapperClass)

Example 3 with Function

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();
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) Function(org.qi4j.functional.Function) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) EntityReference(org.qi4j.api.entity.EntityReference) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 4 with Function

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
}
Also used : C3P0DataSourceServiceAssembler(org.qi4j.library.sql.c3p0.C3P0DataSourceServiceAssembler) DataSourceAssembler(org.qi4j.library.sql.assembly.DataSourceAssembler) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) DataSource(javax.sql.DataSource) ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) Function(org.qi4j.functional.Function) Databases(org.qi4j.library.sql.common.Databases) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) EntityTestAssembler(org.qi4j.test.EntityTestAssembler) ActivationEventListener(org.qi4j.api.activation.ActivationEventListener) ResultSet(java.sql.ResultSet) ActivationEvent(org.qi4j.api.activation.ActivationEvent) Module(org.qi4j.api.structure.Module) Application(org.qi4j.api.structure.Application) Test(org.junit.Test)

Example 5 with Function

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);
}
Also used : Function(org.qi4j.functional.Function) Type(java.lang.reflect.Type) ArrayList(java.util.ArrayList) SideEffects(org.qi4j.api.sideeffect.SideEffects)

Aggregations

Function (org.qi4j.functional.Function)25 Type (java.lang.reflect.Type)8 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 Application (org.qi4j.api.structure.Application)7 ModuleAssembly (org.qi4j.bootstrap.ModuleAssembly)6 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)5 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)4 GraphDAO (org.qi4j.sample.dcicargo.pathfinder.internal.GraphDAO)4 GraphTraversalServiceImpl (org.qi4j.sample.dcicargo.pathfinder.internal.GraphTraversalServiceImpl)4 OrgJsonValueSerializationService (org.qi4j.valueserialization.orgjson.OrgJsonValueSerializationService)4 EntityReference (org.qi4j.api.entity.EntityReference)3 Annotations.isType (org.qi4j.api.util.Annotations.isType)3 Classes.wrapperClass (org.qi4j.api.util.Classes.wrapperClass)3 MemoryEntityStoreService (org.qi4j.entitystore.memory.MemoryEntityStoreService)3 UuidIdentityGeneratorService (org.qi4j.spi.uuid.UuidIdentityGeneratorService)3 Principal (java.security.Principal)2 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)2 Concerns (org.qi4j.api.concern.Concerns)2 Constraint (org.qi4j.api.constraint.Constraint)2