function load(h)
% load
%
% Load ModelExplorer views.
%

%   Copyright 2009-2011 The MathWorks, Inc.

% remove any existing views w/o side effects
h.disableLiveliness;
delete(find(h, '-isa', 'DAStudio.MEView'));
h.enableLiveliness;

if ~exist(h.getFileName(), 'file')
    h.reset;
else
    try
        % Read from file
        readData = load(h.getFileName);
        meViews  = readData.meViews;
        prevRelease = readData.viewsVersion(2:6); % e.g 2016b        
        % Construct views w/o side effects
        h.disableLiveliness;
        for i = 1:length(meViews)
            % Create view
            addDefaultName = false;
            view = DAStudio.MEView(meViews(i).Name, meViews(i).Description);
            % Since 18a, 'Name' is now a part of default views.
            if numel(meViews(i).Properties) > 1
                if ~strcmp(meViews(i).Properties(1).Name, 'Name')
                    addDefaultName = true;
                end
            else
                if ~strcmp(meViews(i).Properties.Name, 'Name')
                    addDefaultName = true;
                end
            end
            if addDefaultName
                view.Properties = DAStudio.MEViewProperty('Name');
                savedProperties = meViews(i).Properties;
                for j = 1:numel(savedProperties)                    
                    propertySaved = savedProperties(j).Name;
                    if ~strcmp(propertySaved, 'Name')
                        view.Properties = [view.Properties; savedProperties(j)]; 
                    end
                end
            else
                view.Properties = meViews(i).Properties;
            end
            % Take care of domains.
            if ~isempty(meViews(i).Domain)
                domainName = meViews(i).Domain;
                % Create domain and make this view default view.
                domain = h.createDomain(domainName);
                % Make this view an active view for that domain.
                domain.setActiveView(view);
            end
            % New addition in this release. If previous release, default values
            % already set in constructor.
            if strcmp(readData.viewsVersion,h.MATLABVersion)
                view.GroupName = meViews(i).GroupName;
                view.SortName = meViews(i).SortName;
                view.SortOrder = meViews(i).SortOrder;            
            end
            
            % Adjust reference migration, e.g, 'Argument' property is newly
            % added in R2017a and is critical for model reference workflow.So if 
            % old preference is dated before R2017a we should add back this column.
            for garbage=1:1                    
                tempFactoryView = h.getFactoryViews(view.name);
                if isempty(tempFactoryView)
                    break
                end
                if isempty(tempFactoryView.ReleaseChanges)
                    break
                end
                
                % get the rules for preference migration
                Deltas = jsondecode(tempFactoryView.ReleaseChanges);
                releases = fieldnames(Deltas);
                for relInd = 1:length(releases)
                    sinceRel = releases{relInd}; 
                    if loc_CompareRelease(prevRelease, sinceRel(2:6)) >= 0
                        continue;
                    end
                    
                    relRules = getfield(Deltas, sinceRel);
                    % Handle newly removed columns first
                    if isfield(relRules, 'Delete')
                        toDel = relRules.Delete;
                        colNames = view.Properties.get('Name');
                        indToDel = (ismember(colNames, toDel));
                        view.Properties(indToDel) = [];
                    end
                    % Handle newly added columns next
                    if isfield(relRules, 'Add')
                        % get column names of the view of preference
                        existedProps = cell(1, length(view.Properties));
                        for ind = 1:length(view.Properties)
                            existedProps{ind} = view.Properties(ind).Name;
                        end                        
                        propsToAdd = setdiff(relRules.Add, existedProps); % ensure no duplicates due to additions
                        for newProp = propsToAdd
                            view.Properties = [view.Properties; DAStudio.MEViewProperty(newProp{1})]; 
                        end
                    end
                    
                end
            end
            % View construction completed. Give it to the view manager.
            h.addView(view);
            if isfield(meViews(i),'FilterTypes')
                if isprop(view,'FilterTypes')
                    if ~isempty(meViews(i).FilterTypes)                                                
                        h.registerViewFilterTypes(view.Name, meViews(i).FilterTypes);                        
                    end
                end
            end                        
            % Internal name
            if ~isempty(meViews(i).InternalName)
                view.InternalName = meViews(i).InternalName;
            end                
        end
        h.enableLiveliness;
        if isempty(h.ActiveView)            
            h.ActiveView = h.getSuggestedView();
            % If domain stuff fails, just assign last view.
            if isempty(h.ActiveView)
                h.ActiveView = h.getView(readData.activeView);
            end
        end
        % Set auto-suggest
        h.SuggestionMode = readData.suggestionMode;        
    catch ME
        % Default to factory views if the preferences are unavailable
        h.reset;
        disp(ME)
        MSLDiagnostic('modelexplorer:DAS:ErrorReadingViewDefinitions').reportAsWarning;
    end
end

%=====================================================================
% HELPER SUBFUNCTIONS
%=====================================================================
function result = loc_CompareRelease(rel1, rel2)
% if rel1 predates rel2, return -1; if rel1 is same as rel2 return 0; else, return 1
year1 = str2double(rel1(1:4));
year2 = str2double(rel2(1:4));
if year1 < year2
    result = -1;
elseif year1 > year2
    result = 1;
else
    minor1 = rel1(5);
    minor2 = rel2(5);
    if minor1 < minor2
        result = -1;
    elseif minor1 == minor2
        result = 0;
    else
        result = 1;
    end
end
        
% EOF
