classdef ReassignContainerStrategy < fxptds.WorkflowTopology.Strategy
    %% REASSIGNCONTAINERSTRATEGY class
    % This strategy reassigns ContainerID value for collection nodes
    
    % Copyright 2019 The MathWorks, Inc.
    
    properties(Dependent)
        VerificationFPTRunID
        BaselineFPTRunID
        ContainerID
    end
    
    methods
        function value = get.VerificationFPTRunID(this)
            % FPTRunID is given a double so because its value is used as a
            % name to create nodes, convert the number to a string
            value = num2str(this.WorkflowContext.VerificationFPTRunID);
        end
        
        function value = get.BaselineFPTRunID(this)
            % FPTRunID is given a double so because its value is used as a
            % name to create nodes, convert the number to a string
            value = num2str(this.WorkflowContext.BaselineFPTRunID);
        end
        
        function value = get.ContainerID(this)
            value = this.WorkflowContext.ContainerID;
        end
        
    end
    
    % Access is protected and extended to Facade and to the test class
    methods (Access = {?fxptds.WorkflowTopology.Strategy, ?fxptds.WorkflowTopology.Facade, ?WorkflowTopologyTestCase})
        
        function execute(this)
            
            % get all the dependent node objects that are of the
            % collection type (simulate or derive)
            query = this.createDefaultQuery();
            query.Type = "Collection";
            query.Search = "parents";
            
            baselineScenarioNames = this.executeQuery(this.BaselineFPTRunID, query);
            baselineScenarioNames = this.processQueryData(baselineScenarioNames);
            verificationScenarioNames = this.executeQuery(this.VerificationFPTRunID, query);
            verificationScenarioNames = this.processQueryData(verificationScenarioNames);
            
            % merge all the names together in a single cell array
            allCollectionNames = [this.BaselineFPTRunID, baselineScenarioNames, this.VerificationFPTRunID, verificationScenarioNames];
            
            for idx = 1:numel(allCollectionNames)
                collectionName = allCollectionNames{idx};
                collectionObject = this.Store.getNodeObject(collectionName);
                collectionObject.setAssignedContainerID(this.ContainerID)
            end
            
        end
    end
    
    methods (Access = {?fxptds.WorkflowTopology.Strategy, ?WorkflowTopologyTestCase})
        function names = processQueryData(~, names)
            if numel(names) == 1 && isempty(names{1})
                names = {};
            else
                names = names';
            end
        end
    end
    
end

