All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cloudimpl.outstack.spring.component.SpringQueryService Maven / Gradle / Ivy

There is a newer version: 4.0.551
Show newest version
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cloudimpl.outstack.spring.component;

import com.cloudimpl.outstack.common.CloudMessage;
import com.cloudimpl.outstack.core.Inject;
import com.cloudimpl.outstack.core.Named;
import com.cloudimpl.outstack.runtime.EntityQueryHandler;
import com.cloudimpl.outstack.runtime.EventRepositoryFactory;
import com.cloudimpl.outstack.runtime.domainspec.RootEntity;
import com.cloudimpl.outstack.runtime.util.Util;
import java.util.HashSet;
import java.util.Set;
import org.reactivestreams.Publisher;
import com.cloudimpl.outstack.runtime.Handler;
import com.cloudimpl.outstack.runtime.ServiceQueryProvider;
import com.cloudimpl.outstack.runtime.domainspec.ChildEntity;
import com.cloudimpl.outstack.runtime.domainspec.Entity;
import java.util.Collection;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import reactor.core.publisher.Mono;

/**
 *
 * @author nuwan
 * @param 
 */
public class SpringQueryService implements Function {

    private static final Set>> HANDLERS = new HashSet<>();
    private static final Set> QUERY_ENTITIES = new HashSet<>();
    private final ServiceQueryProvider serviceProvider;

    @Inject
    @Named("RRHnd")
    private BiFunction requestHandler;

    public SpringQueryService(EventRepositoryFactory factory) {
       // Supplier> supplier = ()-> this.requestHandler;
        Class root = Util.extractGenericParameter(this.getClass(), SpringQueryService.class, 0);
        serviceProvider = new ServiceQueryProvider<>(root, factory.createOrGetRepository(root), factory::createOrGetRepository,()->this.requestHandler,()->Cluster.autoWireInstance());

        HANDLERS.stream()
                .filter(h -> SpringQueryService.filter(root, h))
                .filter(h -> EntityQueryHandler.class.isAssignableFrom(h))
                .forEach(e -> serviceProvider.registerQueryHandler((Class) e));

        QUERY_ENTITIES.stream().filter(e -> SpringQueryService.filterEntity(root, e))
                .forEach(e -> serviceProvider.registerDefaultQueryHandlersForEntity(e));
    }

    public static void $(Class> handler) {
        HANDLERS.add(handler);
    }

    public static void $$(Class entityType) {
        QUERY_ENTITIES.add(entityType);
    }

    @Override
    public Publisher apply(CloudMessage msg) {

        return serviceProvider.apply(msg.data());
    }

    public static boolean filter(Class rootType, Class> handlerType) {
        Class entityType = Util.extractGenericParameter(handlerType, handlerType.getSuperclass(), 0);
        Class root = RootEntity.isMyType(entityType) ? entityType : Util.extractGenericParameter(entityType, ChildEntity.class, 0);
        return rootType == root;
    }

    public static boolean filterEntity(Class rootType, Class entityType) {
        if (RootEntity.isMyType(entityType)) {
            return rootType == entityType;
        }
        Class root = Util.extractGenericParameter(entityType, ChildEntity.class, 0);
        return root == rootType;

    }

    public static Collection>> handlers(Class rootType) {
        return HANDLERS.stream().filter(h -> filter(rootType, h)).collect(Collectors.toList());
    }

    public static Collection> queryEntities(Class rootType) {
        return QUERY_ENTITIES.stream().filter(h -> filterEntity(rootType, h)).collect(Collectors.toList());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy