service-locator.ts 3.17 KB
import {FileHandler} from './api/file-handler';
import {ElectronFileHandler} from './file-handler/electron-file-handler';
import {CordovaDownloader} from './file-handler/cordova-file-handler';
import {FileReplicator} from './file-replicator';
import {
    Config,
    CONFIG_RETRY_TIMEOUT,
    CONFIG_ITEM_KEY,
    CONFIG_ITEM_VALUE,
    CONFIG_ITEM_TARGET_ATTRIBUTE,
    CONFIG_ITEM_SOURCE_ATTRIBUTE
} from './config';

export const ENV_ELECTRON = "electron";
export const ENV_CORDOVA  = "cordova";
export const ENV_UNKNOWN  = "unknown";

export class ServiceLocator {

    protected static fileHandlers:any = {};
    protected static fileReplicator: FileReplicator;
    protected static config: Config;

    static addFileHandler(environment:string, fileHandler:FileHandler) {
        ServiceLocator.fileHandlers[environment] = fileHandler;
    }

    static getConfig() : Config {
        if (!ServiceLocator.config) {
            ServiceLocator.config = new Config();
        }

        return ServiceLocator.config;
    }

    static getEnvironment() {
        if (typeof window['require'] === 'function' && window['require']('electron')) {
            return ENV_ELECTRON;
        }
        if (typeof window['FileTransfer'] === 'function') {
            return ENV_CORDOVA;
        }

        return ENV_UNKNOWN;
    }

    static getFileHandler() : FileHandler {
        let environment = ServiceLocator.getEnvironment();

        if (ServiceLocator.fileHandlers[environment]) {
            return ServiceLocator.fileHandlers[environment];
        }

        if (environment === ENV_ELECTRON) {
            return new ElectronFileHandler(window['require']('electron').ipcRenderer);
        }

        if (environment === ENV_CORDOVA) {
            return new CordovaDownloader();
        }
        
        return null;
    }

    static getFileReplicator() : FileReplicator {
        if (!ServiceLocator.fileReplicator) {

            ServiceLocator.fileReplicator = new FileReplicator();

            ServiceLocator.fileReplicator.fileHandler = ServiceLocator.getFileHandler();

            if (ServiceLocator.getConfig().hasConfig(CONFIG_RETRY_TIMEOUT)) {
                ServiceLocator.fileReplicator.retryTimeout = ServiceLocator.getConfig().getConfig(CONFIG_RETRY_TIMEOUT);
            }
            if (ServiceLocator.getConfig().hasConfig(CONFIG_ITEM_KEY)) {
                ServiceLocator.fileReplicator.itemKey = ServiceLocator.getConfig().getConfig(CONFIG_ITEM_KEY);
            }
            if (ServiceLocator.getConfig().hasConfig(CONFIG_ITEM_VALUE)) {
                ServiceLocator.fileReplicator.itemValue = ServiceLocator.getConfig().getConfig(CONFIG_ITEM_VALUE);
            }
            if (ServiceLocator.getConfig().hasConfig(CONFIG_ITEM_SOURCE_ATTRIBUTE)) {
                ServiceLocator.fileReplicator.itemSourceAttribute = ServiceLocator.getConfig().getConfig(CONFIG_ITEM_SOURCE_ATTRIBUTE);
            }
            if (ServiceLocator.getConfig().hasConfig(CONFIG_ITEM_TARGET_ATTRIBUTE)) {
                ServiceLocator.fileReplicator.itemTargetAttribute = ServiceLocator.getConfig().getConfig(CONFIG_ITEM_TARGET_ATTRIBUTE);
            }
        }   

        return ServiceLocator.fileReplicator;
    }

}