private void GetConnections()
        {
            Connections.ItemsSource = null;

            var         path = Path.GetDirectoryName(SelectedProject.FullName);
            XmlDocument doc  = new XmlDocument();

            if (!SharedConfigFile.ConfigFileExists(SelectedProject))
            {
                _logger.WriteToOutputWindow("Error Retrieving Connections From Config File: Missing CRMDeveloperExtensions.config file", Logger.MessageType.Error);
                return;
            }

            doc.Load(path + "\\CRMDeveloperExtensions.config");
            XmlNodeList connections = doc.GetElementsByTagName("Connection");

            if (connections.Count == 0)
            {
                return;
            }

            List <CrmConn> crmConnections = new List <CrmConn>();

            foreach (XmlNode node in connections)
            {
                CrmConn conn     = new CrmConn();
                XmlNode nameNode = node["Name"];
                if (nameNode != null)
                {
                    conn.Name = nameNode.InnerText;
                }
                XmlNode connectionStringNode = node["ConnectionString"];
                if (connectionStringNode != null)
                {
                    conn.ConnectionString = DecodeString(connectionStringNode.InnerText);
                }
                XmlNode orgIdNode = node["OrgId"];
                if (orgIdNode != null)
                {
                    conn.OrgId = orgIdNode.InnerText;
                }
                XmlNode versionNode = node["Version"];
                if (versionNode != null)
                {
                    conn.Version = versionNode.InnerText;
                }

                crmConnections.Add(conn);
            }

            Connections.ItemsSource = crmConnections;

            if (Connections.SelectedIndex == -1 && crmConnections.Count > 0)
            {
                Connections.SelectedIndex = 0;
            }
        }
        private void ModifyConnection_Click(object sender, RoutedEventArgs e)
        {
            if (SelectedConnection == null)
            {
                return;
            }
            if (string.IsNullOrEmpty(SelectedConnection.ConnectionString))
            {
                return;
            }

            string     name       = SelectedConnection.Name;
            Connection connection = new Connection(name, SelectedConnection.ConnectionString);
            bool?      result     = connection.ShowDialog();

            if (!result.HasValue || !result.Value)
            {
                return;
            }

            var configExists = SharedConfigFile.ConfigFileExists(SelectedProject);

            if (!configExists)
            {
                CreateConfigFile(SelectedProject);
            }

            Expander.IsExpanded = false;

            AddOrUpdateConnection(SelectedProject, connection.ConnectionName, connection.ConnectionString, connection.OrgId, connection.Version, false);

            //Keep to refresh the connections in the list
            GetConnections();

            foreach (CrmConn conn in Connections.Items)
            {
                if (conn.Name != connection.ConnectionName)
                {
                    continue;
                }

                Connections.SelectedItem = conn;
                OnConnectionModified(new ConnectionModifiedEventArgs
                {
                    ModifiedConnection = conn
                });
                break;
            }
        }
Ejemplo n.º 3
0
        private void AddConnection_Click(object sender, RoutedEventArgs e)
        {
            var connection = new Connection(null, null);

            bool?result = connection.ShowDialog();

            if (!result.HasValue || !result.Value)
            {
                return;
            }

            var configExists = SharedConfigFile.ConfigFileExists(SelectedProject);

            if (!configExists)
            {
                CreateConfigFile(SelectedProject);
            }

            Expander.IsExpanded = false;

            AddOrUpdateConnection(SelectedProject, connection.ConnectionName, connection.ConnectionString, connection.OrgId, connection.Version, true);

            GetConnections();

            foreach (CrmConn conn in Connections.Items)
            {
                if (conn.Name != connection.ConnectionName)
                {
                    continue;
                }

                Connections.SelectedItem = conn;
                // TODO: Should this actually fire OnConnectionSelected?
                OnConnectionAdded(new ConnectionAddedEventArgs
                {
                    AddedConnection = conn
                });

                break;
            }
        }
        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (SelectedConnection == null)
                {
                    return;
                }
                if (string.IsNullOrEmpty(SelectedConnection.ConnectionString))
                {
                    return;
                }

                var path = Path.GetDirectoryName(SelectedProject.FullName);
                if (!SharedConfigFile.ConfigFileExists(SelectedProject))
                {
                    _logger.WriteToOutputWindow("Error Deleting Connection: Missing CRMDeveloperExtensions.config File", Logger.MessageType.Error);
                    return;
                }

                MessageBoxResult result = MessageBox.Show("Are you sure?" + Environment.NewLine + Environment.NewLine +
                                                          "This will delete the connection information and all associated mappings.", "Delete Connection", MessageBoxButton.YesNo);
                if (result != MessageBoxResult.Yes)
                {
                    return;
                }

                if (!SharedConfigFile.ConfigFileExists(SelectedProject))
                {
                    return;
                }

                //Delete Connection
                XmlDocument doc = new XmlDocument();
                doc.Load(path + "\\CRMDeveloperExtensions.config");
                XmlNodeList connections = doc.GetElementsByTagName("Connection");
                if (connections.Count == 0)
                {
                    return;
                }

                List <XmlNode> nodesToRemove = new List <XmlNode>();
                foreach (XmlNode connection in connections)
                {
                    XmlNode orgId = connection["OrgId"];
                    if (orgId == null)
                    {
                        continue;
                    }
                    if (orgId.InnerText.ToUpper() != SelectedConnection.OrgId.ToUpper())
                    {
                        continue;
                    }

                    nodesToRemove.Add(connection);
                }

                foreach (XmlNode xmlNode in nodesToRemove)
                {
                    if (xmlNode.ParentNode != null)
                    {
                        xmlNode.ParentNode.RemoveChild(xmlNode);
                    }
                }

                if (SharedConfigFile.IsConfigReadOnly(path + "\\CRMDeveloperExtensions.config"))
                {
                    FileInfo file = new FileInfo(path + "\\CRMDeveloperExtensions.config")
                    {
                        IsReadOnly = false
                    };
                }

                doc.Save(path + "\\CRMDeveloperExtensions.config");

                //Delete related Files
                doc.Load(path + "\\CRMDeveloperExtensions.config");

                string type;
                switch (SourceWindow)
                {
                case "PluginDeployer":
                    type = "Assembly";
                    break;

                case "SolutionPackager":
                    type = "Solution";
                    break;

                default:     //WebResourceDeployer or ReportDeployer
                    type = "File";
                    break;
                }

                XmlNodeList mapping = doc.GetElementsByTagName(type);
                if (mapping.Count > 0)
                {
                    nodesToRemove = new List <XmlNode>();
                    foreach (XmlNode fileNode in mapping)
                    {
                        XmlNode orgId = fileNode["OrgId"];
                        if (orgId == null)
                        {
                            continue;
                        }
                        if (orgId.InnerText.ToUpper() != SelectedConnection.OrgId.ToUpper())
                        {
                            continue;
                        }

                        nodesToRemove.Add(fileNode);
                    }

                    foreach (XmlNode xmlNode in nodesToRemove)
                    {
                        if (xmlNode.ParentNode != null)
                        {
                            xmlNode.ParentNode.RemoveChild(xmlNode);
                        }
                    }
                    doc.Save(path + "\\CRMDeveloperExtensions.config");
                }

                GetConnections();

                OnConnectionDeleted();
            }
            catch (Exception ex)
            {
                _logger.WriteToOutputWindow("Error Deleting Connection: Missing CRMDeveloperExtensions.config File: " + ex.Message + Environment.NewLine + ex.StackTrace, Logger.MessageType.Error);
            }
        }
        private void AddOrUpdateConnection(Project vsProject, string connectionName, string connString, string orgId, string versionNum, bool showPrompt)
        {
            try
            {
                var path = Path.GetDirectoryName(vsProject.FullName);
                if (!SharedConfigFile.ConfigFileExists(vsProject))
                {
                    _logger.WriteToOutputWindow("Error Adding Or Updating Connection: Missing CRMDeveloperExtensions.config File", Logger.MessageType.Error);
                    return;
                }

                FileInfo file = new FileInfo(path + "\\CRMDeveloperExtensions.config");

                XmlDocument doc = new XmlDocument();
                doc.Load(path + "\\CRMDeveloperExtensions.config");

                //Check if connection already exists for project
                XmlNodeList connectionNodes = doc.GetElementsByTagName("Connection");
                if (connectionNodes.Count > 0)
                {
                    foreach (XmlNode node in connectionNodes)
                    {
                        XmlNode nameNode = node["Name"];
                        if (nameNode != null && nameNode.InnerText != connectionName)
                        {
                            continue;
                        }

                        if (showPrompt)
                        {
                            MessageBoxResult result = MessageBox.Show("Update Connection?", "Connection Already Added",
                                                                      MessageBoxButton.YesNo);

                            //Update existing connection
                            if (result != MessageBoxResult.Yes)
                            {
                                return;
                            }
                        }

                        bool changed = false;
                        if (nameNode != null)
                        {
                            string oldConnectionName = nameNode.InnerText;
                            if (oldConnectionName != connectionName)
                            {
                                nameNode.InnerText = connectionName;
                                changed            = true;
                            }
                        }
                        XmlNode versionNode = node["Version"];
                        if (versionNode != null)
                        {
                            string oldVersionNum = versionNode.InnerText;
                            if (oldVersionNum != versionNum)
                            {
                                versionNode.InnerText = versionNum;
                                changed = true;
                            }
                        }
                        XmlNode connectionStringNode = node["ConnectionString"];
                        if (connectionStringNode != null)
                        {
                            string oldConnectionString     = connectionStringNode.InnerText;
                            string encodedConnectionString = EncodeString(connString);
                            if (oldConnectionString != encodedConnectionString)
                            {
                                connectionStringNode.InnerText = encodedConnectionString;
                                changed = true;
                            }
                        }

                        if (!changed)
                        {
                            return;
                        }

                        if (SharedConfigFile.IsConfigReadOnly(path + "\\CRMDeveloperExtensions.config"))
                        {
                            file.IsReadOnly = false;
                        }

                        doc.Save(path + "\\CRMDeveloperExtensions.config");

                        return;
                    }
                }

                //Add the connection elements
                XmlNodeList connections = doc.GetElementsByTagName("Connections");
                XmlElement  connection  = doc.CreateElement("Connection");
                XmlElement  name        = doc.CreateElement("Name");
                name.InnerText = connectionName;
                connection.AppendChild(name);
                XmlElement org = doc.CreateElement("OrgId");
                org.InnerText = orgId;
                connection.AppendChild(org);
                XmlElement connectionString = doc.CreateElement("ConnectionString");
                connectionString.InnerText = EncodeString(connString);
                connection.AppendChild(connectionString);
                XmlElement version = doc.CreateElement("Version");
                version.InnerText = versionNum;
                connection.AppendChild(version);
                connections[0].AppendChild(connection);

                _connectionAdded = true;

                if (SharedConfigFile.IsConfigReadOnly(path + "\\CRMDeveloperExtensions.config"))
                {
                    file.IsReadOnly = false;
                }

                doc.Save(path + "\\CRMDeveloperExtensions.config");
            }
            catch (Exception ex)
            {
                _logger.WriteToOutputWindow("Error Adding Or Updating Connection: " + ex.Message + Environment.NewLine + ex.StackTrace, Logger.MessageType.Error);
            }
        }