classdef FPTRepository < handle
    % FPTREPOSITORY Singleton class that stores FPTDataset objects.
    
    %   Copyright 2012-2020 The MathWorks, Inc.
    
    properties(Constant,GetAccess=private)
        % Stores the class instance as a constant property
        FPTRepositoryInstance = fxptds.FPTRepository;
    end
    
    properties (GetAccess=private, SetAccess=private)
        ModelDatasetMap;    % Mapping between the source(model/project) and the dataset object.
        ModelHandleMap;     % Mapping between the source(model/project) handle and the source name.
    end
    
    methods (Static)
        function obj = getInstance
            % Returns the stored instance of the repository.
            obj = fxptds.FPTRepository.FPTRepositoryInstance;
        end
    end
    
    methods (Access=private)
        function this = FPTRepository
            this.ModelDatasetMap = Simulink.sdi.Map(char('a'),?handle);
            this.ModelHandleMap = Simulink.sdi.Map(double(1), char('a'));
            mlock; % Prevents clearing of the class from MATLAB.
        end
    end
    
    methods
        datasetObj = getDatasetForSource(this, srcName);
        
        function modelName = getModelNameFromHandle(this, modelHandle)
            % if the handle was not found, retrun empty string
            modelName = '';
            if this.ModelHandleMap.isKey(modelHandle)
                modelName = this.ModelHandleMap.getDataByKey(modelHandle);
            end
        end
    end
    
    methods(Hidden)
        createDataset(this, model);
        allDatasets = getAllDatasets(this);
        updateSourceNameForDataset(this, oldSourceName, newSourceName);
        allSrc = getAllSources(this);
        removeDatasetForSource(this,srcName);
        
        function updateModelHandleMap(this, model)
            if Simulink.ID.isValid(model)
                modelHandle = Simulink.ID.getHandle(model);
                this.ModelHandleMap.insert(modelHandle, model);
            end
        end
        
    end
end
