private static int SyncFxGetObjectsForSync(SyncFxDialog sfd, DatabaseMenuCommandParameters databaseInfo)
 {
     int totalCount;
     using (var repository = Helpers.DataConnectionHelper.CreateRepository(databaseInfo.DatabaseInfo))
     {
         sfd.Tables =
             repository.GetAllTableNames().Where(
                 t => !t.EndsWith("scope_info") && !t.EndsWith("scope_config") && !t.EndsWith("schema_info") && !t.EndsWith("_tracking")).ToList();
         sfd.Columns =
             repository.GetAllColumns().Where(
                 t =>
                 !t.TableName.EndsWith("scope_info") && !t.TableName.EndsWith("scope_config") && !t.TableName.EndsWith("schema_info") &&
                 !t.TableName.EndsWith("_tracking")).ToList();
         sfd.PrimaryKeyColumns =
             repository.GetAllPrimaryKeys().Where(
                 t =>
                 !t.TableName.EndsWith("scope_info") && !t.TableName.EndsWith("scope_config") && !t.TableName.EndsWith("schema_info") &&
                 !t.TableName.EndsWith("_tracking")).ToList();
         totalCount = sfd.Tables.Count;
     }
     return totalCount;
 }
        public void SyncFxGenerateSyncCodeInProject(object sender, ExecutedRoutedEventArgs e)
        {
            var databaseInfo = ValidateMenuInfo(sender);
            if (databaseInfo == null) return;

            if (package == null) return;
            var dte = package.GetServiceHelper(typeof(EnvDTE.DTE)) as EnvDTE.DTE;

            var dteH = new Helpers.EnvDTEHelper();

            var project = dteH.GetProject(dte);
            if (project == null)
            {
                EnvDTEHelper.ShowError("Please select a project in Solution Explorer, where you want the SyncFx classes to be placed");
                return;
            }
            if (!dteH.AllowedProjectKinds.Contains(new Guid(project.Kind)))
            {
                EnvDTEHelper.ShowError("The selected project type does not support Sync Framework (please let me know if I am wrong)");
                return;
            }
            if (project.CodeModel.Language != CodeModelLanguageConstants.vsCMLanguageCSharp)
            {
                EnvDTEHelper.ShowError("Unsupported code language, only C# is currently supported");
                return;
            }
            if (project.Properties.Item("TargetFrameworkMoniker") == null)
            {
                EnvDTEHelper.ShowError("The selected project type does not support Sync Framework - missing TargetFrameworkMoniker");
                return;
            }
            if (!project.Properties.Item("TargetFrameworkMoniker").Value.ToString().Contains(".NETFramework"))
            {
                EnvDTEHelper.ShowError("The selected project type does not support .NET Desktop - wrong TargetFrameworkMoniker: " + project.Properties.Item("TargetFrameworkMoniker").Value.ToString());
                return;
            }
            if (databaseInfo.DatabaseInfo.DatabaseType != DatabaseType.SQLCE35)
            {
                EnvDTEHelper.ShowError("Sorry, only version 3.5 databases are supported for now");
                return;
            }

            try
            {
                string model = Path.GetFileNameWithoutExtension(databaseInfo.DatabaseInfo.Caption).Replace(" ", string.Empty).Replace("#", string.Empty).Replace(".", string.Empty).Replace("-", string.Empty);
                SyncFxDialog sfd = new SyncFxDialog();
                int totalCount = 0;
                totalCount = SyncFxGetObjectsForSync(sfd, databaseInfo);
                sfd.ModelName = model;

                bool? res = sfd.ShowModal();
                if (res.HasValue && res.Value == true && (sfd.Tables.Count > 0))
                {
                    model = sfd.ModelName;
                    var defaultNamespace = project.Properties.Item("DefaultNamespace").Value.ToString();

                    var classes = new Dictionary<string, string>();

                    classes = new SyncFxHelper().GenerateCodeForScope(string.Empty, databaseInfo.DatabaseInfo.ConnectionString, "SQLCE", model, sfd.Columns.Where(c => sfd.Tables.Contains(c.TableName)).ToList(), defaultNamespace);
                    var projectPath = project.Properties.Item("FullPath").Value.ToString();

                    foreach (var item in classes)
                    {
                        string fileName = Path.Combine(projectPath, item.Key + ".cs");
                        if (File.Exists(fileName))
                        {
                            File.Delete(fileName);
                        }
                        File.WriteAllText(fileName, item.Value);
                        project.ProjectItems.AddFromFile(fileName);
                    }
                    //Adding references - http://blogs.msdn.com/b/murat/archive/2008/07/30/envdte-adding-a-refernce-to-a-project.aspx
                    EnvDTEHelper.AddReference(project, "System.Data.SqlServerCe, Version=3.5.1.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91");

                    EnvDTEHelper.AddReference(project, "Microsoft.Synchronization, Version=2.1.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91");
                    EnvDTEHelper.AddReference(project, "Microsoft.Synchronization.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91");
                    EnvDTEHelper.AddReference(project, "Microsoft.Synchronization.Data.SqlServer, Version=3.1.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91");
                    EnvDTEHelper.AddReference(project, "Microsoft.Synchronization.Data.SqlServerCe, Version=3.1.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91");
                    EnvDTEHelper.ShowMessage("Scope: " + model + " code generated.");
                    Helpers.DataConnectionHelper.LogUsage("DatabaseSyncCodegen");
                }
            }
            catch (Exception ex)
            {
                Helpers.DataConnectionHelper.SendError(ex, databaseInfo.DatabaseInfo.DatabaseType, false);
            }
        }
        public void SyncFxGenerateLocalDatabaseCacheCode(object sender, ExecutedRoutedEventArgs e)
        {
            // http://www.mztools.com/articles/2007/MZ2007011.aspx
            var menuItem = sender as MenuItem;
            if (menuItem == null) return;

            Scope scope = (Scope)menuItem.Tag;

            var dte = package.GetServiceHelper(typeof(EnvDTE.DTE)) as EnvDTE.DTE;

            var dteH = new Helpers.EnvDTEHelper();

            var project = dteH.GetProject(dte);
            if (project == null)
            {
                EnvDTEHelper.ShowError("Please select a project in Solution Explorer, where you want the SyncFx classes to be placed");
                return;
            }
            if (!dteH.AllowedProjectKinds.Contains(new Guid(project.Kind)))
            {
                EnvDTEHelper.ShowError("The selected project type does not support Sync Framework (please let me know if I am wrong)");
                return;
            }
            if (project.CodeModel.Language != CodeModelLanguageConstants.vsCMLanguageCSharp)
            {
                EnvDTEHelper.ShowError("Unsupported code language, only C# is currently supported");
                return;
            }
            if (project.Properties.Item("TargetFrameworkMoniker") == null)
            {
                EnvDTEHelper.ShowError("The selected project type does not support Sync Framework - missing TargetFrameworkMoniker");
                return;
            }
            if (!project.Properties.Item("TargetFrameworkMoniker").Value.ToString().Contains(".NETFramework"))
            {
                EnvDTEHelper.ShowError("The selected project type does not support .NET Desktop - wrong TargetFrameworkMoniker: " + project.Properties.Item("TargetFrameworkMoniker").Value.ToString());
                return;
            }

            try
            {
                string serverConnectionString = DataConnectionHelper.PromptForConnectionString(package);
                if (!string.IsNullOrEmpty(serverConnectionString))
                {
                    string clientConnectionString = string.Empty;

                    //grab target SQL CE Database
                    var dialog = new ConnectionDialog();
                    dialog.DbType = DatabaseType.SQLCE35;
                    bool? result = dialog.ShowModal();
                    if (result.HasValue && result.Value == true && !string.IsNullOrWhiteSpace(dialog.ConnectionString))
                    {
                        clientConnectionString = dialog.ConnectionString;
                    }
                    else
                    {
                        return;
                    }

                    string model = string.Empty;
                    SyncFxDialog sfd = new SyncFxDialog();
                    int totalCount = 0;
                    var databaseInfo = new DatabaseMenuCommandParameters
                    {
                        DatabaseInfo = new DatabaseInfo
                        {
                            ConnectionString = serverConnectionString,
                            DatabaseType = DatabaseType.SQLServer
                        }
                    };
                    totalCount = SyncFxGetObjectsForSync(sfd, databaseInfo);
                    sfd.ModelName = model;

                    bool? res = sfd.ShowModal();
                    if (res.HasValue && res.Value == true && (sfd.Tables.Count > 0) && !string.IsNullOrWhiteSpace(sfd.ModelName))
                    {
                        model = sfd.ModelName;
                        var defaultNamespace = project.Properties.Item("DefaultNamespace").Value.ToString();

                        var columns = sfd.Columns.Where(c => sfd.Tables.Contains(c.TableName)).ToList();
                        var classes = new SyncFxHelper().GenerateCodeForScope(serverConnectionString, clientConnectionString, "SQL", model, columns, defaultNamespace);
                        var projectPath = project.Properties.Item("FullPath").Value.ToString();

                        foreach (var item in classes)
                        {
                            string fileName = Path.Combine(projectPath, item.Key + ".cs");
                            if (File.Exists(fileName))
                            {
                                File.Delete(fileName);
                            }
                            File.WriteAllText(fileName, item.Value);
                            project.ProjectItems.AddFromFile(fileName);
                        }
                        //Adding references - http://blogs.msdn.com/b/murat/archive/2008/07/30/envdte-adding-a-refernce-to-a-project.aspx
                        EnvDTEHelper.AddReference(project, "System.Data.SqlServerCe, Version=3.5.1.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91");

                        EnvDTEHelper.AddReference(project, "Microsoft.Synchronization, Version=2.1.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91");
                        EnvDTEHelper.AddReference(project, "Microsoft.Synchronization.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91");
                        EnvDTEHelper.AddReference(project, "Microsoft.Synchronization.Data.SqlServer, Version=3.1.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91");
                        EnvDTEHelper.AddReference(project, "Microsoft.Synchronization.Data.SqlServerCe, Version=3.1.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91");
                        EnvDTEHelper.ShowMessage("Scope: " + model + " code generated.");
                        Helpers.DataConnectionHelper.LogUsage("DatabasesSyncAddLocalDBCache");
                    }
                }
            }
            catch (Exception ex)
            {
                Helpers.DataConnectionHelper.SendError(ex, DatabaseType.SQLServer);
            }
        }
        public void SyncFxProvisionScope(object sender, ExecutedRoutedEventArgs e)
        {
            var databaseInfo = ValidateMenuInfo(sender);
            if (databaseInfo == null) return;

            if (databaseInfo.DatabaseInfo.DatabaseType != DatabaseType.SQLCE35)
            {
                EnvDTEHelper.ShowError("Sorry, only version 3.5 databases are supported for now");
                return;
            }

            try
            {
                string model = Path.GetFileNameWithoutExtension(databaseInfo.DatabaseInfo.Caption).Replace(" ", string.Empty).Replace("#", string.Empty).Replace(".", string.Empty).Replace("-", string.Empty);
                SyncFxDialog sfd = new SyncFxDialog();
                int totalCount = 0;
                totalCount = SyncFxGetObjectsForSync(sfd, databaseInfo);
                sfd.ModelName = model;

                bool? res = sfd.ShowModal();

                if (res.HasValue && res.Value == true && (sfd.Tables.Count > 0))
                {
                    if (SyncFxHelper.SqlCeScopeExists(databaseInfo.DatabaseInfo.ConnectionString, model))
                    {
                        EnvDTEHelper.ShowError("Scope name is already in use. Please enter a different scope name.");
                        return;
                    }

                    model = sfd.ModelName;
                    new SyncFxHelper().ProvisionScope(databaseInfo.DatabaseInfo.ConnectionString, model, sfd.Columns.Where(c => sfd.Tables.Contains(c.TableName)).ToList());
                    EnvDTEHelper.ShowMessage("Scope: " + model + " has been provisioned.");
                    Helpers.DataConnectionHelper.LogUsage("DatabaseSyncProvision");
                }
            }
            catch (Exception ex)
            {
                Helpers.DataConnectionHelper.SendError(ex, databaseInfo.DatabaseInfo.DatabaseType, false);
            }
        }