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AC,MAAM,CAAC,qBAAqB;CAwBvC","file":"tool-connection-query.d.ts","sourcesContent":["import { concat, Observable, Observer } from 'rxjs';\r\nimport { AppContext } from '../data/app-context';\r\nimport {\r\n    EnvironmentModule,\r\n    EnvironmentModuleEntryPoint,\r\n    EnvironmentModuleEntryPointType,\r\n    EnvironmentModuleEntryPointWithToolConditionResult\r\n} from '../manifest/environment-modules';\r\nimport { InventoryQueryCaches } from '../shared/inventory-query-caches';\r\nimport { Connection } from './connection';\r\nimport { ToolConditionValidator, ToolConditionWeight } from './tool-condition-validator';\r\n\r\n/**\r\n * Tool Connection query class.\r\n * @dynamic\r\n */\r\nexport class ToolConnectionQuery {\r\n    /**\r\n     * Create an observable to determine the collection of tools to be applicable to the connection.\r\n     *\r\n     * @param appContext the application context.\r\n     * @param caches the share inventory query caches.\r\n     * @param connection the connection object.\r\n     * @param solution the entry point object of the solution.\r\n     */\r\n    public static queryToolsList(\r\n        appContext: AppContext,\r\n        caches: InventoryQueryCaches,\r\n        connection: Connection,\r\n        solution: EnvironmentModuleEntryPoint): Observable<EnvironmentModuleEntryPointWithToolConditionResult[]> {\r\n        return ToolConnectionQuery.queryEntryPointList(\r\n            appContext, caches, connection, solution, [EnvironmentModuleEntryPointType.Tool]);\r\n    }\r\n\r\n    /**\r\n     * Create an observable to determine the collection of settings to be applicable to the connection.\r\n     *\r\n     * @param appContext the application context.\r\n     * @param caches the share inventory query caches.\r\n     * @param connection the connection object.\r\n     * @param solution the entry point object of the solution.\r\n     */\r\n    public static querySettingsFormsList(\r\n        appContext: AppContext,\r\n        caches: InventoryQueryCaches,\r\n        connection: Connection,\r\n        solution: EnvironmentModuleEntryPoint): Observable<EnvironmentModuleEntryPointWithToolConditionResult[]> {\r\n        return ToolConnectionQuery.queryEntryPointList(\r\n            appContext, caches, connection, solution, [EnvironmentModuleEntryPointType.SettingsForm]);\r\n    }\r\n\r\n    /**\r\n     * Create an observable to determine the collection of entry points to be applicable to the connection.\r\n     *\r\n     * @param appContext the application context.\r\n     * @param caches the share inventory query caches.\r\n     * @param connection the connection object.\r\n     * @param solution the entry point object of the solution.\r\n     * @param entryPoints the list of entry points.\r\n     */\r\n    public static validateEntryPoints(\r\n        appContext: AppContext,\r\n        caches: InventoryQueryCaches,\r\n        connection: Connection,\r\n        solution: EnvironmentModuleEntryPoint,\r\n        entryPoints: EnvironmentModuleEntryPoint[]): Observable<EnvironmentModuleEntryPointWithToolConditionResult[]> {\r\n        return ToolConnectionQuery.validateEntryPointList(\r\n            appContext, caches, connection, solution, entryPoints);\r\n    }\r\n\r\n    /**\r\n     * gets the list of tools available to the solution and connection\r\n     * @param connection the connection\r\n     * @param solution the solution\r\n     */\r\n    public static getToolsList(connection: Connection, solution: EnvironmentModuleEntryPoint): EnvironmentModuleEntryPoint[] {\r\n        const tools = EnvironmentModule.getEntryPointsByType([EnvironmentModuleEntryPointType.Tool])\r\n            .filter(tool => ToolConnectionQuery.checkToolRequirements(connection, solution, tool));\r\n        // TODO, explore if we should cache this list\r\n        return tools;\r\n    }\r\n\r\n    /**\r\n     * get the list of settings forms available to the solution and connection\r\n     * @param connection the connection\r\n     * @param solution the solution\r\n     */\r\n    public static getSettingsFormList(connection: Connection, solution: EnvironmentModuleEntryPoint) {\r\n        const settings = EnvironmentModule.getEntryPointsByType([EnvironmentModuleEntryPointType.SettingsForm])\r\n            .filter(setting => ToolConnectionQuery.checkToolRequirements(connection, solution, setting));\r\n        // TODO, explore if we should cache this list\r\n        return settings;\r\n    }\r\n\r\n    /**\r\n     * query for the list of a particular type of entry point\r\n     * @param appContext the app context\r\n     * @param caches the inventory query caches\r\n     * @param connection the connection\r\n     * @param solution the solution entry point.\r\n     * @param entryPointTypes the type list of entry point we want to query.\r\n     */\r\n    private static queryEntryPointList(\r\n        appContext: AppContext,\r\n        caches: InventoryQueryCaches,\r\n        connection: Connection,\r\n        solution: EnvironmentModuleEntryPoint,\r\n        entryPointTypes: EnvironmentModuleEntryPointType[]): Observable<EnvironmentModuleEntryPointWithToolConditionResult[]> {\r\n\r\n        const observablesCollection: Observable<EnvironmentModuleEntryPointWithToolConditionResult>[][] = [];\r\n        const tools: EnvironmentModuleEntryPointWithToolConditionResult[] = [];\r\n        const endpoint = !connection ? null : appContext.authorizationManager.getJeaEndpoint(connection.name);\r\n        EnvironmentModule.getEntryPointsByType(entryPointTypes)\r\n            .filter(entryPoint => !endpoint || entryPoint.parentModule.jea)\r\n            .map(tool => {\r\n                const item = ToolConditionValidator.current(appContext, caches)\r\n                    .scanToolCondition(connection, solution, tool);\r\n                if (item.weight === ToolConditionWeight.NonObservable) {\r\n                    tools.push(<EnvironmentModuleEntryPointWithToolConditionResult>item.result);\r\n                } else if (observablesCollection[item.weight]) {\r\n                    observablesCollection[item.weight].push(<Observable<EnvironmentModuleEntryPointWithToolConditionResult>>item.result);\r\n                } else {\r\n                    observablesCollection[item.weight] = [<Observable<EnvironmentModuleEntryPointWithToolConditionResult>>item.result];\r\n                }\r\n            });\r\n\r\n        const observables: Observable<EnvironmentModuleEntryPointWithToolConditionResult>[] = [];\r\n        for (let weight = ToolConditionWeight.Property; weight <= ToolConditionWeight.Script; weight++) {\r\n            if (observablesCollection[weight]) {\r\n                observablesCollection[weight].forEach(item => observables.push(item));\r\n            }\r\n        }\r\n\r\n        const observable = new Observable((observer: Observer<EnvironmentModuleEntryPointWithToolConditionResult[]>) => {\r\n            // respond tools that have no conditional observable.\r\n            observer.next(tools);\r\n            // process observable sequentially by using concat.\r\n            const subscription = concat(...observables).subscribe({\r\n                next: data => {\r\n                    data.connectionId = connection && connection.id;\r\n                    tools.push(data);\r\n                    observer.next(tools);\r\n                },\r\n                error: error => observer.error(error),\r\n                complete: () => {\r\n                    observer.complete();\r\n                }\r\n            });\r\n\r\n            return () => subscription.unsubscribe();\r\n        });\r\n\r\n        return observable;\r\n    }\r\n\r\n    /**\r\n     * query for the list of a particular type of entry point\r\n     * @param appContext the app context\r\n     * @param caches the inventory query caches\r\n     * @param connection the connection\r\n     * @param solution the solution entry point.\r\n     * @param entryPointTypes the type list of entry point we want to query.\r\n     */\r\n    private static validateEntryPointList(\r\n        appContext: AppContext,\r\n        caches: InventoryQueryCaches,\r\n        connection: Connection,\r\n        solution: EnvironmentModuleEntryPoint,\r\n        entryPoints: EnvironmentModuleEntryPoint[]): Observable<EnvironmentModuleEntryPointWithToolConditionResult[]> {\r\n\r\n        const observablesCollection: Observable<EnvironmentModuleEntryPointWithToolConditionResult>[][] = [];\r\n        const tools: EnvironmentModuleEntryPointWithToolConditionResult[] = [];\r\n        entryPoints\r\n            .map(tool => {\r\n                const item = ToolConditionValidator.current(appContext, caches).scanToolCondition(connection, solution, tool);\r\n                if (item.weight === ToolConditionWeight.NonObservable) {\r\n                    tools.push(<EnvironmentModuleEntryPointWithToolConditionResult>item.result);\r\n                } else if (observablesCollection[item.weight]) {\r\n                    observablesCollection[item.weight].push(<Observable<EnvironmentModuleEntryPointWithToolConditionResult>>item.result);\r\n                } else {\r\n                    observablesCollection[item.weight] = [<Observable<EnvironmentModuleEntryPointWithToolConditionResult>>item.result];\r\n                }\r\n            });\r\n\r\n        const observables: Observable<EnvironmentModuleEntryPointWithToolConditionResult>[] = [];\r\n        for (let weight = ToolConditionWeight.Property; weight <= ToolConditionWeight.Script; weight++) {\r\n            if (observablesCollection[weight]) {\r\n                observablesCollection[weight].forEach(item => observables.push(item));\r\n            }\r\n        }\r\n\r\n        const observable = new Observable((observer: Observer<EnvironmentModuleEntryPointWithToolConditionResult[]>) => {\r\n            if (observables.length === 0) {\r\n                observer.next(tools);\r\n                observer.complete();\r\n                return;\r\n            }\r\n\r\n            // process observable sequentially by using concat, and aggregate to the single tools[] array.\r\n            const subscription = concat(...observables).subscribe({\r\n                next: data => {\r\n                    data.connectionId = connection.id;\r\n                    tools.push(data);\r\n                },\r\n                error: error => observer.error(error),\r\n                complete: () => {\r\n                    observer.next(tools);\r\n                    observer.complete();\r\n                }\r\n            });\r\n            return () => subscription.unsubscribe();\r\n        });\r\n\r\n        return observable;\r\n    }\r\n\r\n    private static checkToolRequirements(\r\n        connection: Connection, solution: EnvironmentModuleEntryPoint, tool: EnvironmentModuleEntryPoint): boolean {\r\n        if (!tool.requirements) {\r\n            // tool is missing requirements, never show.\r\n            return false;\r\n        }\r\n\r\n        const solutionId = EnvironmentModule.createFormattedEntrypoint(solution);\r\n\r\n        // process each tool requirement.\r\n        for (let i = 0; i < tool.requirements.length; i++) {\r\n            const req = tool.requirements[i];\r\n            if (req.solutionIds\r\n                && ((!connection && !req.connectionTypes) ||\r\n                    (connection && req.connectionTypes\r\n                        && req.solutionIds.some(s => s === solutionId)\r\n                        && req.connectionTypes.some(c => c === connection.type)\r\n                    )\r\n                )) {\r\n                return true;\r\n            }\r\n        }\r\n        return false;\r\n    }\r\n}\r\n"]}