private void PublishMappedResources(LinkerData existing, List <SelectedFile> alreadyMappednMatching)
        {
            try
            {
                var webresourceIds = new Guid[alreadyMappednMatching.Count];
                var filePaths      = new string[alreadyMappednMatching.Count];

                for (var i = 0; i < alreadyMappednMatching.Count; i++)
                {
                    // bit of logic to figure out the webresourceid of the file basd on the filepath
                    webresourceIds[i] = existing.Mappings
                                        .Where(
                        a =>
                        a.SourceFilePath.Equals(alreadyMappednMatching[i].FriendlyFilePath, StringComparison.InvariantCultureIgnoreCase))
                                        .Select(a => a.WebResourceId).Take(1).SingleOrDefault();

                    filePaths[i] = alreadyMappednMatching[i].FilePath;
                }

                Publish(webresourceIds, filePaths, alreadyMappednMatching);
            }
            catch (Exception ex)
            {
                Status.Update("");
                Status.Update($"ERROR: {ex.Message}");
                Controller.Trace("ERROR: {0}", ex.Message);
                ToggleControl(connect, true);                 // enable the connect button so the user can try connecting to a different org
            }
        }
Пример #2
0
        public static LinkerData Get(string dataPath)
        {
            Status.Update(">> Reading settings ... ", false);

            LinkerData result = null;

            var dte     = Package.GetGlobalService(typeof(SDTE)) as DTE;
            var project = dte.GetSelectedProject();
            var file    = project.GetProjectDirectory() + "\\" + dataPath;

            var newLine = false;

            if (File.Exists(file))
            {
                // get latest file if in TFS
                try
                {
                    var workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(file);

                    if (workspaceInfo != null)
                    {
                        var server    = new TfsTeamProjectCollection(workspaceInfo.ServerUri);
                        var workspace = workspaceInfo.GetWorkspace(server);

                        var pending = workspace.GetPendingChanges(new[] { file });

                        if (!pending.Any())
                        {
                            workspace.Get(new[] { file }, VersionSpec.Latest, RecursionType.Full, GetOptions.GetAll | GetOptions.Overwrite);
                            Status.Update("\n\tRetrieved latest settings file from TFS' current workspace.");
                            newLine = true;
                        }
                    }
                }
                catch (Exception)
                {
                    // ignored
                }

                using (var sr = new StreamReader(file))
                {
                    result = new XmlSerializer(typeof(LinkerData)).Deserialize(sr) as LinkerData;
                }
            }

            if (result == null)
            {
                result = new LinkerData();
            }
            else
            {
                result.Password = Encryption.Decrypt(result.Password, Encryption.EncryptionKey);
            }

            Status.Update(newLine ? "Done reading settings!" : "done!");

            return(result);
        }
Пример #3
0
 private void CrmConnection_Load(object sender, EventArgs e)
 {
     if (!string.IsNullOrEmpty(this.LinkerDataPath))
     {
         var data = LinkerData.Get(this.LinkerDataPath);
         if (data != null)
         {
             this.discourl.Text = string.IsNullOrEmpty(data.DiscoveryUrl) ? "" : data.DiscoveryUrl;
         }
     }
 }
Пример #4
0
 private void CrmConnection_Load(object sender, EventArgs e)
 {
     if (!string.IsNullOrEmpty(this.LinkerDataPath))
     {
         var data = LinkerData.Get(this.LinkerDataPath);
         if (data != null)
         {
             this.discourl.Text = string.IsNullOrEmpty(data.DiscoveryUrl) ? "" : data.DiscoveryUrl.Replace("/XRMServices/2011/Discovery.svc", "");
             this.domain.Text   = data.Domain;
             this.username.Text = data.Username;
             this.password.Text = data.Password;
         }
     }
 }
Пример #5
0
        internal static void SaveConnectionDetails(string linkerDataPath, string connectionString, string publicUrl)
        {
            try
            {
                var existing = LinkerData.Get(linkerDataPath);

                existing.DiscoveryUrl = connectionString;
                existing.PublicUrl    = publicUrl;

                existing.Save(linkerDataPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Unable to save connection details. Error: {ex.Message}",
                                "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void PublishUnmappedResource(LinkerData existing)
        {
            try
            {
                if (webresources.SelectedNode?.Tag != null)
                {
                    // tag is only set internally so we should be ok without an error check here :P
                    var webresourceId = new Guid(webresources.SelectedNode.Tag.ToString());

                    // get rid of anything that's mapped with this sourcefile/webresourceid and add a clean one to avoid corruption on the mapping file
                    existing.Mappings.RemoveAll(a => a.WebResourceId == webresourceId
                                                // check both the id and sourcepath because we now have to support re-linking
                                                ||
                                                a.SourceFilePath.Equals(UnmappedFile.FriendlyFilePath,
                                                                        StringComparison.InvariantCultureIgnoreCase));

                    // add a clean mapping for this webresource and file
                    existing.Mappings.Add(new LinkerDataItem
                    {
                        WebResourceId  = webresourceId,
                        SourceFilePath = UnmappedFile.FriendlyFilePath
                    });

                    Publish(new[] { webresourceId }, new[] { UnmappedFile.FilePath }, new List <SelectedFile>(new[] { UnmappedFile }));
                    UnmappedFile = null;

                    currentmapping.Text = "";
                }
            }
            catch (Exception ex)
            {
                Status.Update("");
                Status.Update($"ERROR: {ex.Message}");
                Controller.Trace("ERROR: {0}", ex.Message);
                ToggleControl(connect, true);                 // enable the connect button so the user can try connecting to a different org
            }
            finally
            {
                unmappedLock.Release();
            }
        }
Пример #7
0
        internal static void SaveConnectionDetails(string linkerDataPath, string url, string domain,
                                                   string username, string password, string publicUrl)
        {
            try
            {
                var existing = LinkerData.Get(linkerDataPath);

                existing.DiscoveryUrl = url;
                existing.Domain       = domain;
                existing.Username     = username;
                existing.Password     = password;             // don't worry the password is encrypted :)
                existing.PublicUrl    = publicUrl;

                existing.Save(linkerDataPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Unable to save connection details. Error: {ex.Message}",
                                "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public void TryPublishing()
        {
            try
            {
                ToggleControls(false);

                if (Sdk != null)
                {
                    var existing = LinkerData.Get(LinkerDataPath);

                    var isFoundUnmapped = false;

                    if (!Relink && UnmappedFile == null)
                    {
                        var alreadyMappednMatching = SelectedFiles.Where(selectedFile =>
                        {
                            // grab correctly mapped files and publish them
                            return(existing.Mappings
                                   .Any(
                                       a =>
                                       a.SourceFilePath.Equals(selectedFile.FriendlyFilePath,
                                                               StringComparison.InvariantCultureIgnoreCase))
                                   &&
                                   // make sure we don't attempt to publish already published items
                                   !PublishedFiles.Any(
                                       a =>
                                       a.FilePath.Equals(selectedFile.FilePath,
                                                         StringComparison.InvariantCultureIgnoreCase)));
                        }).ToList();

                        UnMappedCount = UnMappedCount ?? SelectedFiles
                                        .Count(selectedFile =>
                        {
                            return(existing.Mappings.Where(
                                       a =>
                                       a.SourceFilePath.Equals(selectedFile.FriendlyFilePath,
                                                               StringComparison.InvariantCultureIgnoreCase))
                                   .Take(1).SingleOrDefault() == null);
                        });

                        if (alreadyMappednMatching.Count > 0)
                        {
                            PublishMappedResources(existing, alreadyMappednMatching);
                        }

                        // now find the unmapped files and mark them as unmapped, when the 'publish/link' button is clicked this unmapped file will be picked up by the same method, see 'if this.unmappedfile != null' check above
                        foreach (var selectedFile in SelectedFiles)
                        {
                            if (isFormClosing)
                            {
                                break;
                            }

                            var matchingItem = existing.Mappings
                                               .Where(
                                a =>
                                a.SourceFilePath.Equals(selectedFile.FriendlyFilePath,
                                                        StringComparison.InvariantCultureIgnoreCase))
                                               .Take(1).SingleOrDefault();

                            if (matchingItem == null)
                            {
                                unmappedLock.WaitOne();

                                if (isFormClosing)
                                {
                                    break;
                                }

                                UnMappedCount--;
                                MarkAsUnmapped(selectedFile);
                                isFoundUnmapped = true;
                            }
                        }
                    }

                    if (UnmappedFile != null && Visible)
                    // this property gets set if relink is set to true, otherwise this gets set by the logic below if there is no mapping
                    {
                        var existing1 = existing;
                        BeginInvoke(new MethodInvoker(() =>
                        {
                            try
                            {
                                PublishUnmappedResource(existing1);
                                existing.Save(LinkerDataPath);
                            }
                            catch (Exception ex)
                            {
                                Status.Update("");
                                Status.Update($"ERROR: {ex.Message}");
                                Controller.Trace("ERROR: {0}", ex.Message);
                                ToggleControl(connect, true);
                                // enable the connect button so the user can try connecting to a different org
                            }
                        }));
                        return;
                    }

                    if (isFoundUnmapped)
                    {
                        existing = LinkerData.Get(LinkerDataPath);
                    }

                    existing.Save(LinkerDataPath);
                    // save the mappings file regardless of the state of the publish since content would've been updated already
                }
                else
                {
                    ToggleControl(connect, true);                     // enable the connect button so the user can try connecting to a different org
                }
            }
            catch (Exception ex)
            {
                Status.Update("");
                Status.Update($"ERROR: {ex.Message}");
                Controller.Trace("ERROR: {0}", ex.Message);
                ToggleControl(connect, true);                 // enable the connect button so the user can try connecting to a different org
            }
        }
Пример #9
0
        internal void TryLinkOrPublish(string linkerDataPath, List <SelectedFile> selectedFiles, bool relinking)
        {
            var linked = LinkerData.Get(linkerDataPath);

            var message = relinking ? "Initializing re-link on: {0}" : "Initializing link/publish on: {0}";

            Trace(message, linked.PublicUrl);

            var wrp =
                new WebResourcePublisher
            {
                Relink         = relinking,
                Controller     = this,
                LinkerDataPath = linkerDataPath,
                SelectedFiles  = selectedFiles
            };

            // setting this will cause the wrp to mark the 1st file in selectedfiles to be relinked

            Task.Factory.StartNew(
                () =>
            {
                Trace("Connecting...");
                Status.Update("Connecting ... ", false);

                var publicUrl            = "";
                IOrganizationService sdk = null;

                try
                {
                    sdk = QuickConnection.Connect(linked.DiscoveryUrl, out publicUrl);
                    Status.Update("done!");
                }
                catch (Exception ex)
                {
                    Status.Update("");
                    Status.Update($"Connection failed: {ex.Message}");
                    Trace("Connection failed: {0}", ex.Message);
                }

                return(new object[] { sdk, publicUrl });
            })
            .ContinueWith(
                state =>
            {
                try
                {
                    if (state?.Result?.FirstOrDefault() == null)
                    {
                        Status.Update("");
                        Status.Update("ERROR: couldn't connect to CRM.");
                        Trace("ERROR: couldn't connect to CRM.");

                        wrp.Relink = false;
                        wrp.ShowConnectionWindow = true;
                        wrp.Initialize();

                        return;
                    }

                    var result = state.Result;

                    var sdk                  = (IOrganizationService)result[0];
                    wrp.Sdk                  = sdk;
                    wrp.PublicUrl            = result[1].ToString();
                    wrp.ShowConnectionWindow = wrp.Sdk == null;

                    wrp.Initialize();
                    wrp.TryPublishing();
                }
                catch (Exception ex)
                {
                    Status.Update("");
                    Status.Update($"ERROR: {ex.Message}");
                    Trace("ERROR: {0}", ex.Message);
                }
            }, TaskScheduler.FromCurrentSynchronizationContext());
        }