Search in sources :

Example 11 with Function

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

Example 12 with Function

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);
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) Function(org.qi4j.functional.Function) EntityReference(org.qi4j.api.entity.EntityReference) Collection(java.util.Collection) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 13 with Function

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);
}
Also used : MemoryEntityStoreService(org.qi4j.entitystore.memory.MemoryEntityStoreService) UuidIdentityGeneratorService(org.qi4j.spi.uuid.UuidIdentityGeneratorService) Function(org.qi4j.functional.Function) Application(org.qi4j.api.structure.Application) OrgJsonValueSerializationAssembler(org.qi4j.valueserialization.orgjson.OrgJsonValueSerializationAssembler)

Example 14 with Function

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()));
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) Function(org.qi4j.functional.Function) EventSource(org.qi4j.library.eventsourcing.domain.source.EventSource) Principal(java.security.Principal) CurrentUserUoWPrincipal(org.qi4j.library.eventsourcing.domain.factory.CurrentUserUoWPrincipal) UnitOfWorkDomainEventsValue(org.qi4j.library.eventsourcing.domain.api.UnitOfWorkDomainEventsValue) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 15 with Function

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);
}
Also used : ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) Function(org.qi4j.functional.Function) OrgJsonValueSerializationService(org.qi4j.valueserialization.orgjson.OrgJsonValueSerializationService) GraphTraversalServiceImpl(org.qi4j.sample.dcicargo.pathfinder.internal.GraphTraversalServiceImpl) Application(org.qi4j.api.structure.Application) GraphDAO(org.qi4j.sample.dcicargo.pathfinder.internal.GraphDAO)

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