function domainName = getDomainString(this, obj)
% Return domain string for the passed object. It is roughly a package
% name except for some special cases.

%   Copyright 2009-2022 The MathWorks, Inc.

% By default it is 'Other'
domainName = 'Other';
% Check for Shortcuts
if isa(obj, 'DAStudio.Shortcut')
    obj = obj.getForwardedObject;
end
% Get Package information.
packageName = '';
if isa(obj,"handle.handle")
    %udd
    classH = classhandle(obj);
    if ~isempty(classH)
        packageName = classH.Package.Name;
    end
else
   %mcos
   classH = metaclass(obj);
   if ~isempty(classH)
       packageName = classH.ContainingPackage.Name;
       dotIdx = findstr(packageName, '.');
       if ~isempty(dotIdx)
           packageName = packageName(1:dotIdx(1)-1);
       end
   end
   
end

if ~isempty(packageName)
    domainName = packageName;
    % Special case for Workspace
    if isa(obj, 'DAStudio.WorkspaceNode')
        domainName = 'Workspace';
    elseif isa(obj, 'Simulink.ModelAdvisor') ...
            || isa(obj, 'Simulink.ConfigSet') ...
            || isa(obj, 'Simulink.Directory')
        domainName = 'Other';
    elseif isa(obj, 'Simulink.Configurations') || (isa(obj, 'DAStudio.DAObjectProxy') && (isa(obj.getMCOSObjectReference, 'Simulink.Configurations')))
        domainName = 'Configurations';
    elseif isa(obj, 'Simulink.DataDictionaryRootNode')
        domainName = 'DataDictionary';
    elseif isa(obj, 'Simulink.DataDictionaryScopeNode')
        domainName = 'DataDictionary';
        nodeName = obj.getNodeName;
        if strcmp(nodeName, 'Other')
            domainName = [domainName '_' nodeName];
        end
    elseif isa(obj, 'Simulink.SlidDASectionNode')
        domainName = 'Workspace';
    elseif isa(obj, 'Simulink.SlidDAContainerNode')
        domainName = 'DataDictionary';
    elseif isa(obj, 'Simulink.Root')
        domainName = 'Other';
        %Special case for DAObejctProxy
    elseif isa(obj, 'DAStudio.DAObjectProxy') && (isa(obj.getMCOSObjectReference, 'Simulink.BlockDiagram')||isa(obj.getMCOSObjectReference, 'Simulink.ModelReference')|| isa(obj.getMCOSObjectReference, 'Simulink.Root'))
        domainName = 'Simulink';
    end
    % TODO: Add more special cases if needed.
end

% Get actual domain by this name.
domainInfo = find(this.Domains, 'Name', domainName);
% Check if this domain really exists. If not, create it at run time.
if isempty(domainInfo)
	this.createDomain(domainName);
end
