function [hConfigDb,fileLoaded] = createAndLoad(cfgFile,hMessageLog)
%CREATEANDLOAD Load configuration file and return new database object.
%  CREATEANDLOAD(cfgFile) loads serialization of configuration database
%  from file cfgFile.  If no extension is specified in cfgFile, '.ext'
%  is automatically appended.  A success message is added to the message
%  log.  File must contain a variable named hConfigDb.
%
%  If cfgFile is not found, an empty database is returned, and a message
%  is added to the message log.
%
%  If cfgFile is empty, file load is not attempted, and no warning
%  messages will occur.
%
%  This is a STATIC METHOD and does NOT take the object as an argument.
%  It must be called using the full package.class name.  For example,
%     hConfigDb = extmgr.ConfigurationSet.createAndLoad('cfgFile.scf');
% Allow empty or no string to return early without failure message
% But, fileLoaded will still be false.

%   Copyright 2014-2020 The MathWorks, Inc.

fileLoaded = false;
if isempty( cfgFile )
    % Prepare for failure with empty database
    hConfigDb = extmgr.ConfigurationSet;
    return
end
if nargin<2
    hMessageLog = [  ];
end
% Add .ext to filename if an extension is not specified
[~,~,e] = fileparts( cfgFile );
if isempty( e )
    cfgFile = [ cfgFile, '.cfg' ];
end
% Try to load configuration file
try
    s = load( cfgFile, '-mat' );
    if isfield( s, 'hConfigDb' )
        % Success
        fileLoaded = true;
        hConfigDb = s.hConfigDb;
        if isa( hConfigDb, 'extmgr.ConfigDb' )
            hConfigDb = convertToNewFormat( hConfigDb );
        end
        if ~isempty( hMessageLog )
            summary = uiscopes.message( 'TextSummaryCfgFileLoad' );
            details = sprintf( [ '%s<br>', '<ul>', '<li>%s %s', '</ul><br>' ], summary, uiscopes.message( 'TextDetailFileName' ), cfgFile );
            hMessageLog.add( 'Info', 'Configuration', summary, details, extmgr.Labels.Configuration );
        end
    else
        hConfigDb = extmgr.ConfigurationSet;
        % Failed to find required variable
        summary = uiscopes.message( 'TextSummaryInvalidFileContents' );
        details = sprintf( [ '%s<br>', '<ul>', '<li>%s %s', '</ul>', '%s<br>' ], summary, uiscopes.message( 'TextDetailFileName' ), cfgFile, uiscopes.message( 'TextDetailFailFindVariable' ) );
        if isempty( hMessageLog )
            warning( 'Spcuilib:extmgr:InvalidFileContents', summary );
        else
            hMessageLog.add( 'Fail', 'Configuration', summary, details, extmgr.Labels.Configuration );
        end
    end
catch e
    hConfigDb = extmgr.ConfigurationSet;
    % Failure during file load - wrong file type?
    if strcmp( e.identifier, 'MATLAB:load:couldNotReadFile' )
        id = 'Spcuilib:extmgr:ConfigFileNotFound';
        summary = uiscopes.message( 'TextSummaryConfigFileNotFound' );
        details = sprintf( [ '%s<br>', '<ul>', '<li>%s %s', '</ul>', '%s<br>', '%s<br>' ], summary, uiscopes.message( 'TextDetailFileName' ), cfgFile, uiscopes.message( 'TextDetailNoExtensionsEnabled' ), uiscopes.message( 'TextDetailDefaultValueUsed' ) );
    else
        id = 'Spcuilib:extmgr:InvalidConfigurationFile';
        summary = uiscopes.message( 'TextSummaryInvalidConfFile' );
        details = sprintf( [ '%s<br>', '<ul>', '<li>%s %s', '</ul>', '<b>%s</b><br>', '%s<br>' ], summary, uiscopes.message( 'TextDetailFileName' ), cfgFile, uiscopes.message( 'TextDetailErrorMsg' ), uiservices.cleanErrorMessage( e ) );
    end
    if isempty( hMessageLog )
        warning( id, summary );
    else
        hMessageLog.add( 'Fail', 'Configuration', summary, details, extmgr.Labels.Configuration );
    end
end
end
