// internal for testing
        internal string GetTextBoxConnectionStringValue(IVsDataProviderManager dataProviderManager, Guid providerGuid, string maskedConnectionString)
        {
            // providerGuid will be the design-time provider guid from DDEX, so we need to translate to the runtime provider invariant name
            var designTimeProviderInvariantName = DataConnectionUtils.GetProviderInvariantName(dataProviderManager, providerGuid);
            var runtimeProviderInvariantName    = ConnectionManager.TranslateInvariantName(
                ServiceProvider, designTimeProviderInvariantName, maskedConnectionString, true);

            var runtimeConnectionString = ConnectionManager.TranslateConnectionString(ServiceProvider,
                                                                                      Wizard.Project, designTimeProviderInvariantName, maskedConnectionString, true);

            if (Wizard.ModelBuilderSettings.GenerationOption == ModelGenerationOption.GenerateFromDatabase ||
                Wizard.ModelBuilderSettings.GenerationOption == ModelGenerationOption.GenerateDatabaseScript)
            {
                var metadataFiles = ConnectionManager.GetMetadataFileNamesFromArtifactFileName(
                    Wizard.Project, Wizard.ModelBuilderSettings.ModelPath, ServiceProvider);

                return(ConnectionManager.CreateEntityConnectionString(
                           Wizard.Project,
                           Wizard.ModelBuilderSettings.VSApplicationType,
                           metadataFiles,
                           runtimeConnectionString,
                           runtimeProviderInvariantName).Text);
            }
            else
            {
                Debug.Assert(
                    Wizard.ModelBuilderSettings.GenerationOption == ModelGenerationOption.CodeFirstFromDatabase,
                    "Unexpected model generation option");

                return(ConnectionManager.InjectEFAttributesIntoConnectionString(
                           runtimeConnectionString, runtimeProviderInvariantName));
            }
        }
        private string GetDefaultAppConfigEntryName(IVsDataConnection dataConnection)
        {
            // get the initial catalog string from the connection
            var initialCatalog = DataConnectionUtils.GetInitialCatalog(_dataProviderManager, dataConnection);

            Wizard.ModelBuilderSettings.InitialCatalog = initialCatalog;

            // compute the connection string name
            return(Wizard.GetUniqueConnectionStringName(initialCatalog));
        }
        private string GetDefaultAppConfigEntryName(IVsDataConnection dataConnection)
        {
            // get the initial catalog string from the connection
            var initialCatalog = DataConnectionUtils.GetInitialCatalog(_dataProviderManager, dataConnection);

            Wizard.ModelBuilderSettings.InitialCatalog = initialCatalog;

            // compute the connection string name
            return(ConnectionManager.GetUniqueConnectionStringName(
                       _configFileUtils,
                       Wizard.ModelBuilderSettings.GenerationOption == ModelGenerationOption.CodeFirstFromDatabase
                    ? Wizard.ModelBuilderSettings.ModelName
                    : GetBaseEdmxConnectionStringName(initialCatalog)));
        }
 private static bool ContainsSensitiveData(IVsDataConnection dataConnection)
 {
     if (dataConnection == null)
     {
         return(false);
     }
     // need to compare ignoring case as DecryptConnectionString() can turn e.g. 'Integrated Security' into 'integrated security'
     else if (dataConnection.DisplayConnectionString.Equals(
                  DataConnectionUtils.DecryptConnectionString(dataConnection), StringComparison.CurrentCultureIgnoreCase))
     {
         return(false);
     }
     else
     {
         return(true);
     }
 }
        // <summary>
        //     Helper to update ModelBuilderSettings from GUI
        //     TODO: validate settings on this page are correct
        // </summary>
        private void UpdateSettingsFromGui()
        {
            // don't bother saving anything if there isn't a connection; the user isn't allowed to proceed forward in the wizard anyways
            if (_dataConnection == null)
            {
                return;
            }

            var decryptedConnectionString = DataConnectionUtils.DecryptConnectionString(_dataConnection);
            var appConfigConnectionString = String.Empty;

            if (checkBoxSaveInAppConfig.Enabled &&
                checkBoxSaveInAppConfig.Checked)
            {
                Wizard.ModelBuilderSettings.SaveConnectionStringInAppConfig = true;
                Wizard.ModelBuilderSettings.AppConfigConnectionPropertyName = textBoxAppConfigConnectionName.Text;

                if (allowSensitiveInfoButton.Checked)
                {
                    appConfigConnectionString = decryptedConnectionString;
                }
                else
                {
                    appConfigConnectionString = _dataConnection.SafeConnectionString;
                }
            }
            else
            {
                Wizard.ModelBuilderSettings.SaveConnectionStringInAppConfig = false;
                Wizard.ModelBuilderSettings.AppConfigConnectionPropertyName = string.Empty;
            }

            // save the initial catalog
            Wizard.ModelBuilderSettings.InitialCatalog = DataConnectionUtils.GetInitialCatalog(_dataProviderManager, _dataConnection);

            // these connection strings & invariant names are coming from the ddex provider, so these are "design-time"
            var invariantName = DataConnectionUtils.GetProviderInvariantName(_dataProviderManager, _dataConnection.Provider);

            Wizard.ModelBuilderSettings.SetInvariantNamesAndConnectionStrings(ServiceProvider,
                                                                              Wizard.Project, invariantName, decryptedConnectionString, appConfigConnectionString, true);
        }
        private string GetTextBoxConnectionStringValue(Guid providerGuid, string maskedConnectionString)
        {
            // providerGuid will be the design-time provider guid from DDEX, so we need to translate to the runtime provider invariant name
            var designTimeProviderInvariantName = DataConnectionUtils.GetProviderInvariantName(_dataProviderManager, providerGuid);
            var runtimeProviderInvariantName    = ConnectionManager.TranslateInvariantName(
                designTimeProviderInvariantName, maskedConnectionString, true);

            var translatedConnectionString = ConnectionManager.TranslateConnectionString(
                Wizard.Project, designTimeProviderInvariantName, maskedConnectionString, true);

            var metadataFiles = ConnectionManager.GetMetadataFileNamesFromArtifactFileName(
                Wizard.ModelBuilderSettings.Project, Wizard.ModelBuilderSettings.ModelPath, PackageManager.Package);

            var connStringText = ConnectionManager.CreateEntityConnectionString(
                Wizard.Project,
                metadataFiles,
                translatedConnectionString,
                runtimeProviderInvariantName,
                true /* assume true; this is just for display purposes */).Text;

            return(connStringText);
        }
        internal string GenerateNewSqlMobileConnectionString(string oldConnectionString, string newFilePath, bool useDataDirectoryMacro)
        {
            var connPropertiesSvc = DataConnectionUtils.GetConnectionProperties(_dataProviderManager, _dataConnection);

            if (connPropertiesSvc == null)
            {
                Debug.Fail("Could not get connection properties service for Data Connection " + _dataConnection.DisplayConnectionString);
                return(oldConnectionString);
            }
            connPropertiesSvc.Parse(oldConnectionString);

            // We need to ensure a path to the device is not converted to a desktop path
            object dataSourceObject;

            connPropertiesSvc.TryGetValue(LocalDataUtil.CONNECTION_PROPERTY_DATA_SOURCE, out dataSourceObject);
            var dataSource = dataSourceObject as string;

            if (!string.IsNullOrEmpty(dataSource) &&
                dataSource.StartsWith(LocalDataUtil.SQL_MOBILE_DEVICE, StringComparison.OrdinalIgnoreCase))
            {
                return(oldConnectionString);
            }

            // Update the database file path
            if (useDataDirectoryMacro)
            {
                connPropertiesSvc[LocalDataUtil.CONNECTION_PROPERTY_DATA_SOURCE] = DataDirectoryMacro + Path.DirectorySeparatorChar
                                                                                   + Path.GetFileName(newFilePath);
            }
            else
            {
                connPropertiesSvc[LocalDataUtil.CONNECTION_PROPERTY_DATA_SOURCE] = newFilePath;
            }

            return(connPropertiesSvc.ToString());
        }
        private void Init()
        {
            if (_isInitialized)
            {
                // need to reset settings on the dialog to avoid displaying stale information
                // especially showing EntityConnectionString for CodeFirst from Database
                NewDataSourceSelected();
                return;
            }

            using (new VsUtils.HourglassHelper())
            {
                //
                //  look up the DDEX Service Providers we use in this wizard
                //
                _dataConnectionManager         = ServiceProvider.GetService(typeof(IVsDataConnectionManager)) as IVsDataConnectionManager;
                _dataExplorerConnectionManager =
                    ServiceProvider.GetService(typeof(IVsDataExplorerConnectionManager)) as IVsDataExplorerConnectionManager;
                _dataProviderManager = ServiceProvider.GetService(typeof(IVsDataProviderManager)) as IVsDataProviderManager;

                var providerMapper = ServiceProvider.GetService(typeof(IDTAdoDotNetProviderMapper)) as IDTAdoDotNetProviderMapper;
                Debug.Assert(providerMapper != null, "providerMapper == null");

                // populate the combo box with project connections first
                var globalConnectionService = ServiceProvider.GetService(typeof(IGlobalConnectionService)) as IGlobalConnectionService;

                var dataConnections = new Dictionary <string, DataConnection>();
                if (null != globalConnectionService)
                {
                    try
                    {
                        var connections = globalConnectionService.GetConnections(ServiceProvider, Wizard.Project);
                        foreach (var connection in connections)
                        {
                            if (connection.Location == ConnectionLocation.SettingsFile ||
                                connection.Location == ConnectionLocation.Both)
                            {
                                var providerGuid = providerMapper.MapInvariantNameToGuid(
                                    connection.ProviderName, connection.DesignTimeConnectionString, false);
                                if (DataConnectionUtils.HasEntityFrameworkProvider(
                                        _dataProviderManager, providerGuid, Wizard.Project, ServiceProvider) &&
                                    DataProviderProjectControl.IsProjectSupported(providerGuid, Wizard.Project))
                                {
                                    dataSourceComboBox.Items.Add(
                                        new DataSourceComboBoxItem(
                                            connection.Name + " (Settings)", providerGuid, connection.DesignTimeConnectionString, false));
                                    dataConnections.Add(connection.DesignTimeConnectionString, connection);
                                }
                            }
                        }
                    }
                    catch
                    {
                        // there is a bug in the VSDesigner; it throws an exception when attempting to call GetConnections if there is a bad connection
                        // if there is a problem, the only thing we can do is just add all connections, and the project connections won't be sorted in order.
                    }
                }

                // populate the combo box with connection names from server explorer
                foreach (var connection in _dataExplorerConnectionManager.Connections.Values)
                {
                    if (DataConnectionUtils.HasEntityFrameworkProvider(
                            _dataProviderManager, connection.Provider, Wizard.Project, ServiceProvider) &&
                        DataProviderProjectControl.IsProjectSupported(connection.Provider, Wizard.Project) &&
                        !dataConnections.ContainsKey(DataConnectionUtils.DecryptConnectionString(connection.Connection)))
                    {
                        dataSourceComboBox.Items.Add(new DataSourceComboBoxItem(connection.DisplayName, connection.Connection));
                    }
                }

                // highlight the first one in the list
                if (dataSourceComboBox.Items.Count > 0)
                {
                    dataSourceComboBox.SelectedIndex = 0;
                }

                // set a minimum height for the connection string text box
                if (textBoxConnectionString.Height < textBoxConnectionString.Font.Height)
                {
                    textBoxConnectionString.Height = textBoxConnectionString.Font.Height;
                }

                // mark as initialized
                _isInitialized = true;
            } // restore cursor
        }
        // <summary>
        //     Changes connection string to point to new file path
        // </summary>
        // <param name="newFilePath">New file path</param>
        // <returns>whether successful</returns>
        private bool RetargetConnectionString(string newFilePath)
        {
            // compute the Design and Runtime Connection String values based on the new file path
            // Note: newAppConfigConnectionString should use the |DataDirectory| macro because
            // that is what's displayed on screen and interpreted at runtime, but newConnectionString
            // should not use the macro as it will be used by the wizard at the next step to create
            // a working DB connection
            var filePathKey = LocalDataUtil.GetFilePathKey(
                Wizard.ModelBuilderSettings.DesignTimeProviderInvariantName,
                Wizard.ModelBuilderSettings.DesignTimeConnectionString);
            var newConnectionString = ConvertConnectionStringToNewPath(
                Wizard.ModelBuilderSettings.DesignTimeProviderInvariantName, filePathKey,
                Wizard.ModelBuilderSettings.DesignTimeConnectionString, newFilePath, false);
            var newAppConfigConnectionString = ConvertConnectionStringToNewPath(
                Wizard.ModelBuilderSettings.DesignTimeProviderInvariantName, filePathKey,
                Wizard.ModelBuilderSettings.AppConfigConnectionString, newFilePath, true);

            // update the DataConnection itself with the new path
            var oldProvider = DataConnectionUtils.GetVsProvider(_dataProviderManager, _dataConnection);

            if (null == oldProvider)
            {
                Debug.Fail("Could not find VS Provider for DataConnection " + _dataConnection.DisplayConnectionString);
                return(false);
            }

            if (null == _dataConnectionManager)
            {
                Debug.Fail("this._dataConnectionManager is null - cannot construct new DataConnection");
                return(false);
            }

            var dataConnection = _dataConnectionManager.GetConnection(oldProvider.Guid, newConnectionString, false);

            if (null == dataConnection)
            {
                Debug.Fail(
                    "DataConnectionManager could not get a connection for provider " + oldProvider.Guid + ", newConnectionString "
                    + newConnectionString);
                return(false);
            }

            // update this WizardPage's DataConnection and also set the DataSourceComboBoxItem's
            // dataConnection to this so that this connection is used if the user re-selects this
            // item from the drop-down
            SetDataConnection(dataConnection);
            var currentDataSourceComboBoxItem = dataSourceComboBox.SelectedItem as DataSourceComboBoxItem;

            Debug.Assert(null != currentDataSourceComboBoxItem, "Currently selected should not be null");
            if (null != currentDataSourceComboBoxItem)
            {
                currentDataSourceComboBoxItem.ResetDataConnection(dataConnection);
            }

            // update the Design and Runtime Connection String values stored in ModelBuilderSettings
            // these connection strings & invariant names are coming from the ddex provider, so these are "design-time"
            Wizard.ModelBuilderSettings.SetInvariantNamesAndConnectionStrings(ServiceProvider,
                                                                              Wizard.Project, Wizard.ModelBuilderSettings.DesignTimeProviderInvariantName, newConnectionString,
                                                                              newAppConfigConnectionString, true);
            return(true);
        }