Skip to content

Commit

Permalink
Fix issue #69: saving the wizard pages state (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
unchase authored Apr 8, 2020
1 parent 46f7866 commit a25360f
Show file tree
Hide file tree
Showing 7 changed files with 262 additions and 204 deletions.
5 changes: 4 additions & 1 deletion src/Models/UserSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class UserSettings
private ConnectedServiceLogger logger;

[DataMember]
public ObservableCollection<string> MruEndpoints { get; private set; }
public ObservableCollection<string> MruEndpoints { get; set; }

[DataMember]
public string ServiceName { get; set; }
Expand Down Expand Up @@ -80,6 +80,9 @@ internal class UserSettings
[DataMember]
public bool IncludeCustomHeaders { get; set; }

[DataMember]
public List<string> ExcludedOperationImports { get; set; }

public UserSettings()
{
this.MruEndpoints = new ObservableCollection<string>();
Expand Down
196 changes: 99 additions & 97 deletions src/ODataConnectedServiceWizard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,101 +26,43 @@ internal class ODataConnectedServiceWizard : ConnectedServiceWizard

public ODataConnectedServiceInstance ServiceInstance => this.serviceInstance ?? (this.serviceInstance = new ODataConnectedServiceInstance());


public Version EdmxVersion => this.ConfigODataEndpointViewModel.EdmxVersion;

public UserSettings UserSettings { get; }

private readonly ServiceConfigurationV4 _serviceConfig;

internal string ProcessedEndpointForOperationImports;

public ODataConnectedServiceWizard(ConnectedServiceProviderContext context)
{
this.Context = context;
this.UserSettings = UserSettings.Load(context.Logger);

// Since ServiceConfigurationV4 is a derived type of ServiceConfiguration. So we can deserialize a ServiceConfiguration into a ServiceConfigurationV4.
this._serviceConfig = this.Context.GetExtendedDesignerData<ServiceConfigurationV4>();

ConfigODataEndpointViewModel = new ConfigODataEndpointViewModel(this.UserSettings, this);
AdvancedSettingsViewModel = new AdvancedSettingsViewModel(this.UserSettings);
OperationImportsViewModel = new OperationImportsViewModel();
OperationImportsViewModel = new OperationImportsViewModel(this.UserSettings);

ServiceConfigurationV4 serviceConfig = null;

OperationImportsViewModel.PageEntering += ObjectSelectionViewModel_PageEntering;
OperationImportsViewModel.PageEntering += OperationImportsViewModel_PageEntering;

if (this.Context.IsUpdating)
{
//Since ServiceConfigurationV4 is a derived type of ServiceConfiguration. So we can deserialize a ServiceConfiguration into a ServiceConfigurationV4.
serviceConfig = this.Context.GetExtendedDesignerData<ServiceConfigurationV4>();
ConfigODataEndpointViewModel.Endpoint = serviceConfig.Endpoint;
ConfigODataEndpointViewModel.EdmxVersion = serviceConfig.EdmxVersion;
ConfigODataEndpointViewModel.ServiceName = serviceConfig.ServiceName;
ConfigODataEndpointViewModel.CustomHttpHeaders = serviceConfig.CustomHttpHeaders;

ConfigODataEndpointViewModel.Endpoint = this._serviceConfig.Endpoint;
ConfigODataEndpointViewModel.EdmxVersion = this._serviceConfig.EdmxVersion;
ConfigODataEndpointViewModel.ServiceName = this._serviceConfig.ServiceName;
ConfigODataEndpointViewModel.CustomHttpHeaders = this._serviceConfig.CustomHttpHeaders;

// Restore the main settings to UI elements.
ConfigODataEndpointViewModel.PageEntering += ConfigODataEndpointViewModel_PageEntering;

// The ViewModel should always be filled otherwise if the wizard is completed without visiting this page the generated code becomes wrong
AdvancedSettingsViewModel.UseNamespacePrefix = serviceConfig.UseNamespacePrefix;
AdvancedSettingsViewModel.NamespacePrefix = serviceConfig.NamespacePrefix;
AdvancedSettingsViewModel.UseDataServiceCollection = serviceConfig.UseDataServiceCollection;
AdvancedSettingsViewModel_PageEntering(AdvancedSettingsViewModel, EventArgs.Empty);

if (serviceConfig.EdmxVersion == Common.Constants.EdmxVersion4)
{
AdvancedSettingsViewModel.IgnoreUnexpectedElementsAndAttributes =
serviceConfig.IgnoreUnexpectedElementsAndAttributes;
AdvancedSettingsViewModel.EnableNamingAlias = serviceConfig.EnableNamingAlias;
AdvancedSettingsViewModel.IncludeT4File = serviceConfig.IncludeT4File;
AdvancedSettingsViewModel.MakeTypesInternal = serviceConfig.MakeTypesInternal;
}


ConfigODataEndpointViewModel.PageEntering += (sender, args) =>
{
var configOdataViewModel = sender as ConfigODataEndpointViewModel;
if (configOdataViewModel != null)
{
var configOdataView = configOdataViewModel.View as ConfigODataEndpoint;
configOdataView.Endpoint.IsEnabled = false;
configOdataView.OpenConnectedServiceJsonFileButton.IsEnabled = false;
configOdataView.OpenEndpointFileButton.IsEnabled = !serviceConfig.Endpoint.StartsWith("http");
configOdataView.ServiceName.IsEnabled = false;
configOdataViewModel.IncludeCustomHeaders = serviceConfig.IncludeCustomHeaders;
configOdataViewModel.IncludeWebProxy = serviceConfig.IncludeWebProxy;
configOdataViewModel.WebProxyHost = serviceConfig.WebProxyHost;
configOdataViewModel.IncludeWebProxyNetworkCredentials = serviceConfig.IncludeWebProxyNetworkCredentials;
configOdataViewModel.WebProxyNetworkCredentialsDomain = serviceConfig.WebProxyNetworkCredentialsDomain;
// don't accept any credentials from the restored settings
configOdataViewModel.WebProxyNetworkCredentialsUsername = null;
configOdataViewModel.WebProxyNetworkCredentialsPassword = null;
}
};


//Restore the advanced settings to UI elements.
AdvancedSettingsViewModel.PageEntering += (sender, args) =>
{
if (sender is AdvancedSettingsViewModel advancedSettingsViewModel)
{
if (advancedSettingsViewModel.View is AdvancedSettings advancedSettings)
{
advancedSettingsViewModel.GeneratedFileNamePrefix = serviceConfig.GeneratedFileNamePrefix;
advancedSettings.ReferenceFileName.IsEnabled = false;
advancedSettingsViewModel.UseNamespacePrefix = serviceConfig.UseNamespacePrefix;
advancedSettingsViewModel.NamespacePrefix = serviceConfig.NamespacePrefix;
advancedSettingsViewModel.UseDataServiceCollection = serviceConfig.UseDataServiceCollection;
advancedSettingsViewModel.GenerateMultipleFiles = serviceConfig.GenerateMultipleFiles;
advancedSettings.GenerateMultipleFiles.IsEnabled = false;
if (serviceConfig.EdmxVersion == Common.Constants.EdmxVersion4)
{
advancedSettingsViewModel.IgnoreUnexpectedElementsAndAttributes = serviceConfig.IgnoreUnexpectedElementsAndAttributes;
advancedSettingsViewModel.EnableNamingAlias = serviceConfig.EnableNamingAlias;
advancedSettingsViewModel.IncludeT4File = serviceConfig.IncludeT4File;
advancedSettingsViewModel.MakeTypesInternal = serviceConfig.MakeTypesInternal;
advancedSettings.IncludeT4File.IsEnabled = false;
}
}
}
};
// Restore the advanced settings to UI elements.
AdvancedSettingsViewModel.PageEntering += AdvancedSettingsViewModel_PageEntering;
}

this.Pages.Add(ConfigODataEndpointViewModel);
Expand Down Expand Up @@ -149,19 +91,22 @@ private ServiceConfiguration CreateServiceConfiguration()
ServiceConfiguration serviceConfiguration;
if (ConfigODataEndpointViewModel.EdmxVersion == Common.Constants.EdmxVersion4)
{
var ServiceConfigurationV4 = new ServiceConfigurationV4();
ServiceConfigurationV4.ExcludedOperationImports = OperationImportsViewModel.ExcludedOperationImportsNames.ToList();
ServiceConfigurationV4.IgnoreUnexpectedElementsAndAttributes = AdvancedSettingsViewModel.IgnoreUnexpectedElementsAndAttributes;
ServiceConfigurationV4.EnableNamingAlias = AdvancedSettingsViewModel.EnableNamingAlias;
ServiceConfigurationV4.IncludeT4File = AdvancedSettingsViewModel.IncludeT4File;
ServiceConfigurationV4.OpenGeneratedFilesInIDE = AdvancedSettingsViewModel.OpenGeneratedFilesInIDE;
serviceConfiguration = ServiceConfigurationV4;
var serviceConfigurationV4 = new ServiceConfigurationV4
{
ExcludedOperationImports = OperationImportsViewModel.ExcludedOperationImportsNames.ToList(),
IgnoreUnexpectedElementsAndAttributes =
AdvancedSettingsViewModel.IgnoreUnexpectedElementsAndAttributes,
EnableNamingAlias = AdvancedSettingsViewModel.EnableNamingAlias,
IncludeT4File = AdvancedSettingsViewModel.IncludeT4File,
OpenGeneratedFilesInIDE = AdvancedSettingsViewModel.OpenGeneratedFilesInIDE
};
serviceConfiguration = serviceConfigurationV4;
}
else
{
serviceConfiguration = new ServiceConfiguration();
}

serviceConfiguration.ServiceName = ConfigODataEndpointViewModel.ServiceName;
serviceConfiguration.Endpoint = ConfigODataEndpointViewModel.Endpoint;
serviceConfiguration.EdmxVersion = ConfigODataEndpointViewModel.EdmxVersion;
Expand All @@ -172,9 +117,7 @@ private ServiceConfiguration CreateServiceConfiguration()
serviceConfiguration.WebProxyNetworkCredentialsUsername = ConfigODataEndpointViewModel.WebProxyNetworkCredentialsUsername;
serviceConfiguration.WebProxyNetworkCredentialsPassword = ConfigODataEndpointViewModel.WebProxyNetworkCredentialsPassword;
serviceConfiguration.WebProxyNetworkCredentialsDomain = ConfigODataEndpointViewModel.WebProxyNetworkCredentialsDomain;

serviceConfiguration.IncludeCustomHeaders = ConfigODataEndpointViewModel.IncludeCustomHeaders;

serviceConfiguration.UseDataServiceCollection = AdvancedSettingsViewModel.UseDataServiceCollection;
serviceConfiguration.GeneratedFileNamePrefix = AdvancedSettingsViewModel.GeneratedFileNamePrefix;
serviceConfiguration.UseNamespacePrefix = AdvancedSettingsViewModel.UseNamespacePrefix;
Expand All @@ -192,30 +135,87 @@ private ServiceConfiguration CreateServiceConfiguration()

#region "Event Handlers"

public void ObjectSelectionViewModel_PageEntering(object sender, EventArgs args)
public void ConfigODataEndpointViewModel_PageEntering(object sender, EventArgs args)
{
if (sender is OperationImportsViewModel objectSelectionViewModel)
if (sender is ConfigODataEndpointViewModel configOdataViewModel)
{
if (ConfigODataEndpointViewModel.EdmxVersion != Constants.EdmxVersion4)
if (configOdataViewModel.View is ConfigODataEndpoint configOdataView)
{
objectSelectionViewModel.View.IsEnabled = false;
objectSelectionViewModel.IsSupportedODataVersion = false;
return;
configOdataView.Endpoint.IsEnabled = false;
configOdataView.OpenConnectedServiceJsonFileButton.IsEnabled = false;
configOdataView.OpenEndpointFileButton.IsEnabled = !this._serviceConfig.Endpoint.StartsWith("http");
configOdataView.ServiceName.IsEnabled = false;
}

configOdataViewModel.IncludeCustomHeaders = this._serviceConfig.IncludeCustomHeaders;
configOdataViewModel.IncludeWebProxy = this._serviceConfig.IncludeWebProxy;
configOdataViewModel.WebProxyHost = this._serviceConfig.WebProxyHost;
configOdataViewModel.IncludeWebProxyNetworkCredentials = this._serviceConfig.IncludeWebProxyNetworkCredentials;
configOdataViewModel.WebProxyNetworkCredentialsDomain = this._serviceConfig.WebProxyNetworkCredentialsDomain;

// don't accept any credentials from the restored settings
configOdataViewModel.WebProxyNetworkCredentialsUsername = null;
configOdataViewModel.WebProxyNetworkCredentialsPassword = null;
}
}

public void AdvancedSettingsViewModel_PageEntering(object sender, EventArgs args)
{
if (sender is AdvancedSettingsViewModel advancedSettingsViewModel)
{
if (advancedSettingsViewModel.View is AdvancedSettings advancedSettings)
{
advancedSettingsViewModel.GeneratedFileNamePrefix = this._serviceConfig.GeneratedFileNamePrefix;
advancedSettings.ReferenceFileName.IsEnabled = !this.Context.IsUpdating;
advancedSettingsViewModel.UseNamespacePrefix = this._serviceConfig.UseNamespacePrefix;
advancedSettingsViewModel.NamespacePrefix = this._serviceConfig.NamespacePrefix ?? Common.Constants.DefaultReferenceFileName;
advancedSettingsViewModel.UseDataServiceCollection = this._serviceConfig.UseDataServiceCollection;
advancedSettingsViewModel.OpenGeneratedFilesInIDE = this._serviceConfig.OpenGeneratedFilesInIDE;
advancedSettingsViewModel.GenerateMultipleFiles = this._serviceConfig.GenerateMultipleFiles;
advancedSettings.GenerateMultipleFiles.IsEnabled = !this.Context.IsUpdating;

if (this._serviceConfig.EdmxVersion == Common.Constants.EdmxVersion4)
{
advancedSettingsViewModel.IgnoreUnexpectedElementsAndAttributes = this._serviceConfig.IgnoreUnexpectedElementsAndAttributes;
advancedSettingsViewModel.EnableNamingAlias = this._serviceConfig.EnableNamingAlias;
advancedSettingsViewModel.IncludeT4File = this._serviceConfig.IncludeT4File;
advancedSettingsViewModel.MakeTypesInternal = this._serviceConfig.MakeTypesInternal;
advancedSettings.IncludeT4File.IsEnabled = !this.Context.IsUpdating;
}
}
}
}

public void OperationImportsViewModel_PageEntering(object sender, EventArgs args)
{
if (sender is OperationImportsViewModel operationImportsViewModel)
{
if (this.ProcessedEndpointForOperationImports != ConfigODataEndpointViewModel.Endpoint)
{
if (ConfigODataEndpointViewModel.EdmxVersion != Constants.EdmxVersion4)
{
operationImportsViewModel.View.IsEnabled = false;
operationImportsViewModel.IsSupportedODataVersion = false;
return;
}
var model = EdmHelper.GetEdmModelFromFile(ConfigODataEndpointViewModel.MetadataTempPath);
var operations = EdmHelper.GetOperationImports(model);
OperationImportsViewModel.LoadOperationImports(operations);
}
var model = EdmHelper.GetEdmModelFromFile(ConfigODataEndpointViewModel.MetadataTempPath);
var operations = EdmHelper.GetOperationImports(model);
OperationImportsViewModel.LoadOperationImports(operations);

if (Context.IsUpdating)
{
var serviceConfig = Context.GetExtendedDesignerData<ServiceConfigurationV4>();
objectSelectionViewModel.ExcludeOperationImports(serviceConfig?.ExcludedOperationImports ?? Enumerable.Empty<string>());
operationImportsViewModel.ExcludeOperationImports(this._serviceConfig?.ExcludedOperationImports ?? Enumerable.Empty<string>());
}

this.ProcessedEndpointForOperationImports = ConfigODataEndpointViewModel.Endpoint;
}
}

#endregion

#region Disposing

/// <summary>
/// Cleanup object references
/// </summary>
Expand Down Expand Up @@ -256,5 +256,7 @@ protected override void Dispose(bool disposing)
base.Dispose(disposing);
}
}

#endregion
}
}
Loading

0 comments on commit a25360f

Please sign in to comment.