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

gorm.tools.mango.api.QueryServiceLookup.groovy Maven / Gradle / Ivy

The newest version!
/*
* Copyright 2024 Yak.Works - Licensed under the Apache License, Version 2.0 (the "License")
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*/
package gorm.tools.mango.api

import groovy.transform.CompileStatic

import org.springframework.core.ResolvableType

import gorm.tools.mango.DefaultQueryService
import yakworks.spring.AppCtx

@CompileStatic
class QueryServiceLookup {

    /**
     * Does a lookup to find QueryService for entity.
     * Returns a new DefaultQueryService if none found.
     * NOTE: the DefaultGormRepo beans that are setup when there is no concrete Repo dont retain the D generic
     * So if trying to do @Autowired QueryService fails because it does QueryService, and if multiple QueryService beans
     * are setup then it can't figure out which one because it cant match on the D generic.
     */
    static  QueryService lookup(Class entityClass){
        var rt = ResolvableType.forClassWithGenerics(QueryService, entityClass)
        var qsLookup = AppCtx.ctx.getBeanProvider(rt).getIfAvailable() as QueryService
        return qsLookup ?: DefaultQueryService.of(entityClass)
    }

    // static  S lookupService(Class entityClass, Class> serviceClass, String factoryMethod){
    //     var rt = ResolvableType.forClassWithGenerics(serviceClass, entityClass)
    //     var ctx = AppCtx.ctx
    //     var qsLookup = ctx.getBeanProvider(rt).getIfAvailable() as S
    //     if(qsLookup){
    //         return qsLookup
    //     } else {
    //         return ctx.getBean(factoryMethod, [entityClass] as Object[]) as S
    //     }
    // }

    // ResolvableType resolvableType = ResolvableType.forClass(getEntityClass());
    // QueryService myService = queryServiceProvider.getIfAvailable(
    //     (bean) -> ResolvableType.forClass(bean.getClass())
    //         .getInterfaces()[0] // Assuming MyService is the first interface
    //         .getGeneric(0) // Get the generic type parameter
    //         .isAssignableFrom(resolvableType)
    // );
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy