/*! * SAPUI5 * Copyright (c) 2025 SAP SE or an SAP affiliate company. All rights reserved. */ import { Sina } from "../../sina/Sina"; import { DataSource } from "../../sina/DataSource"; import { DataSourceType } from "../../sina/DataSourceType"; import { SuggestionQuery } from "../../sina/SuggestionQuery"; import { getMatchedStringValues, readFile } from "./Util"; export interface DataSourceResponse { results: DataSource[]; // total results totalCount: number; } export class DataSourceService { constructor( readonly sina: Sina, readonly dataSourceIds: string[] ) {} async loadDataSources(): Promise { if ( this.sina.dataSources.some((dataSource) => dataSource.type === DataSourceType.BusinessObject) || this.dataSourceIds.length === 0 ) { return; } try { for (const dataSourceId of this.dataSourceIds) { const content = await readFile( `/resources/sap/esh/search/ui/sinaNexTS/providers/sample2/data/${dataSourceId}.json` ); this.sina.dataSourceFromJson(JSON.parse(content)); } } catch { console.error("Error: unable to load data sources from JSON files"); } } getDataSources(): DataSource[] { return this.sina.dataSources; } getDataSource(dataSourceId: string): DataSource { return this.sina.dataSources.find((dataSource) => dataSource.id === dataSourceId); } getResponse(query: SuggestionQuery): DataSourceResponse { const matchedDataSources = []; for (const dataSource of this.sina.dataSources) { if ( getMatchedStringValues([(dataSource.labelPlural, dataSource.label)], query.filter.searchTerm) .length > 0 ) { matchedDataSources.push(dataSource); } } return { results: matchedDataSources, totalCount: matchedDataSources.length }; } }