import DataProvider = require("./DataProvider"); import DataCollection = require('./DataCollection'); import BackendConnector = require("./BackendConnector"); import DataModel = require('./DataModel') import DataCollectionOptions = require("./DataCollectionOptions"); import ObjectMap = require("./ObjectMap"); import DataCollectionFactoryConfig = require('./DataCollectionFactoryConfig') const DEFAULT_ROOT_FOLDER = './' const DEFAULT_MODEL_FOLDER = 'models/' const DEFAULT_CONFIG_FOLDER = 'configs/' class DataCollectionFactory { protected backendConnector:BackendConnector protected dataProviders:{[s:string]: DataProvider} = {} // private rootPath = DEFAULT_ROOT_FOLDER // private modelFolder = DEFAULT_MODEL_FOLDER // private configFolder = DEFAULT_CONFIG_FOLDER public createCollection(dataProviderName:string, options?:DataCollectionOptions):DataCollection { dataProviderName = this.normalizeName(dataProviderName) options = options || {} let dataProvider = this.dataProviders[dataProviderName] if (!dataProvider) { // TODO: This have to be done from outside (on init) let dataProviderConfigClass = null//this.getDataProviderConfigClass(dataProviderName, options) let dataModelClass = null//this.getDataProviderModelClass(dataProviderName, options) this.dataProviders[dataProviderName] = new DataProvider(this, new dataProviderConfigClass(dataProviderName, this.backendConnector), dataModelClass) dataProvider = this.dataProviders[dataProviderName] } return (dataProvider as DataProvider).createCollection(options) as DataCollection } public buildInstance(dataProviderName:string, map?:ObjectMap):T { dataProviderName = this.normalizeName(dataProviderName) let dataProvider = this.dataProviders[dataProviderName] if (!dataProvider) { throw new Error(`There is no such DataProvider for ${dataProviderName}`) } return dataProvider.createDataModel(map || {}, true) as T } private normalizeName(str:string):string { return this.capitalize(this.removeNonAlphaNumericChars(str)) } private removeNonAlphaNumericChars(str:string):string { return str.replace(/\W/g, '') } private capitalize(str:string):string { return str.charAt(0).toUpperCase() + str.slice(1) } // private getDataProviderConfigClass(dataProviderName:string, options:DataCollectionOptions) // { // let className = dataProviderName + 'DataProviderConfig' // return this.getClass(className, this.configFolder, options) // } // private getDataProviderModelClass(dataProviderName:string, options:DataCollectionOptions) // { // let className = dataProviderName + 'DataModel' // return this.getClass(className, this.modelFolder, options, DataModel) // } // private getClass(className:string, folder:string, options:DataCollectionOptions, fallbackClass?:Object) // { // let clazz // let path // try // { // path = `${this.rootPath}${folder}${className}` // clazz = require(`${path}`) // } // catch (e) // { // if (fallbackClass) // { // clazz = fallbackClass // } // else // { // throw new Error(`Please implement ${className} in ${path} before.`) // } // } // return clazz // } constructor(backendConnector:BackendConnector, config?:DataCollectionFactoryConfig) { this.backendConnector = backendConnector // if (config) // { // if (config.configFolder) // { // this.configFolder = config.configFolder // } // if (config.modelFolder) // { // this.modelFolder = config.modelFolder // } // if (config.rootPath) // { // this.rootPath = config.rootPath // } // } } } export = DataCollectionFactory