Example #1
0
        internal void SyncCSSNodeChanges(
            Project teamProject,
            XmlDocument cssNodeChangesDoc,
            ConflictManager conflictManager)
        {
            XmlNodeList elemNodes = cssNodeChangesDoc.DocumentElement.SelectNodes("/StructureChanges/StructureElements/StructureElement");

            if (null == elemNodes || elemNodes.Count == 0)
            {
                return;
            }

            using (RuntimeEntityModel context = RuntimeEntityModel.CreateInstance())
            {
                var migrationSource = context.RTMigrationSourceSet.Where(s => s.UniqueId.Equals(SourceId)).FirstOrDefault();
                Debug.Assert(null != migrationSource);

                foreach (XmlNode node in elemNodes)
                {
                    XmlElement structElem = node as XmlElement;
                    string     nodePath   = GetStructureElementPath(structElem);

                    if (IsStructureElementDeleted(structElem))
                    {
                        if (string.IsNullOrEmpty(nodePath))
                        {
                            // don't know which node was deleted on source system
                            continue;
                        }

                        string remappedNodePath = RemapNodePath(nodePath, teamProject.Name);
                        if (remappedNodePath.EndsWith("\\", StringComparison.OrdinalIgnoreCase))
                        {
                            remappedNodePath = remappedNodePath.Substring(0, remappedNodePath.Length - 1);
                        }

                        string forwardingNodePath = GetStructureElementForwardingNodePath(structElem);
                        if (string.IsNullOrEmpty(forwardingNodePath))
                        {
                            // don't know which forwarding node was used on source system
                            continue;
                        }
                        forwardingNodePath = RemapNodePath(forwardingNodePath, teamProject.Name);

                        string forwardingNodeUri = string.Empty;
                        try
                        {
                            forwardingNodeUri = CSS.GetNodeFromPath(forwardingNodePath).Uri;
                        }
                        catch (Exception e)
                        {
                            // Node does not exist
                            if (e.Message.StartsWith("TF200014:", StringComparison.InvariantCultureIgnoreCase))
                            {
                                TraceManager.TraceInformation("Renaming node failed: forwarding node '{0}' no longer exists", forwardingNodePath);
                            }
                            else
                            {
                                TraceManager.TraceException(e);
                            }
                            continue;
                        }

                        Debug.Assert(!string.IsNullOrEmpty(forwardingNodeUri), "forwardingNodeUri is null");

                        try
                        {
                            NodeInfo nodeToDeleteInfo = CSS.GetNodeFromPath(remappedNodePath);
                            TraceManager.TraceInformation("Deleting node: {0}", remappedNodePath);
                            CSS.DeleteBranches(new string[] { nodeToDeleteInfo.Uri }, forwardingNodeUri);
                        }
                        catch (Exception e)
                        {
                            // Node does not exist
                            if (e.Message.StartsWith("TF200014:", StringComparison.InvariantCultureIgnoreCase))
                            {
                                TraceManager.TraceInformation("Deleting node skipped: node '{0}' no longer exists", remappedNodePath);
                            }
                            else
                            {
                                TraceManager.TraceException(e);
                            }
                            continue;
                        }
                    }
                    else
                    {
                        Debug.Assert(!string.IsNullOrEmpty(nodePath), "nodePath is null");
                        string remappedNodePath = RemapNodePath(nodePath, teamProject.Name);
                        if (remappedNodePath.EndsWith("\\", StringComparison.OrdinalIgnoreCase))
                        {
                            remappedNodePath = remappedNodePath.Substring(0, remappedNodePath.Length - 1);
                        }

                        string renamedFromPath = GetStructureElementRenameFromPath(structElem);
                        if (string.IsNullOrEmpty(renamedFromPath))
                        {
                            // add

                            try
                            {
                                NodeInfo nodeInfo   = CSS.GetNodeFromPath(remappedNodePath);
                                var      cachedNode = QueryCachedCSSNode(context, nodeInfo.Uri);
                                if (cachedNode == null)
                                {
                                    CreateCachedCSSNode(migrationSource, nodeInfo.Uri, nodeInfo.Path);
                                }
                                else
                                {
                                    cachedNode.ItemData = remappedNodePath;
                                }
                                continue;
                            }
                            catch
                            { }

                            TraceManager.TraceInformation("Creating node: {0}", remappedNodePath);

                            // Strip the last \Name off the path to get the parent path
                            string newPathParent = remappedNodePath.Substring(0, remappedNodePath.LastIndexOf('\\'));

                            // Grab the last \Name off the path to get the node name
                            string newPathName = remappedNodePath.Substring(remappedNodePath.LastIndexOf('\\') + 1);

                            // Lookup the parent node on the destination server so that we can get the parentUri
                            NodeInfo parentNode = CSS.GetNodeFromPath(newPathParent);

                            // Create the node
                            string newNodeUri = CSS.CreateNode(newPathName, parentNode.Uri);
                            CreateCachedCSSNode(migrationSource, newNodeUri, remappedNodePath);
                        }
                        else
                        {
                            // rename

                            string newNodeName      = GetStructureElementName(structElem);
                            string preRenamNodePath = RemapNodePath(renamedFromPath, teamProject.Name);

                            try
                            {
                                TraceManager.TraceInformation("Renaming node: from '{0}' to '{1}'", preRenamNodePath, remappedNodePath);
                                NodeInfo renameNodeInfo = CSS.GetNodeFromPath(preRenamNodePath);
                                CSS.RenameNode(renameNodeInfo.Uri, newNodeName);

                                var cachedNode = QueryCachedCSSNode(context, renameNodeInfo.Uri);
                                if (cachedNode == null)
                                {
                                    CreateCachedCSSNode(migrationSource, renameNodeInfo.Uri, remappedNodePath);
                                }
                                else
                                {
                                    cachedNode.ItemData = remappedNodePath;
                                }
                            }
                            catch (Exception e)
                            {
                                // Node does not exist
                                if (e.Message.StartsWith("TF200014:", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    TraceManager.TraceInformation("Renaming node failed: {0} no longer exists", preRenamNodePath);
                                }
                            }
                        }
                    }
                }

                context.TrySaveChanges();
            }
        }