Пример #1
0
        public SoapNode CreateNewDomain(string domainName)
        {
            SoapNode soapDomainNode = null;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (MappingToolDatabaseDataContext mappingDb = new MappingToolDatabaseDataContext())
                {
                    var nodeTypes = from dbNodeTypes in mappingDb.NodeTypes where dbNodeTypes.NodeTypeName == "DomainNode" select dbNodeTypes;
                    var metadataTypes = from dbMetadataTypes in mappingDb.MetadataTypes where dbMetadataTypes.MetadataTypeName == "string" select dbMetadataTypes;

                    NodeType nodeType = nodeTypes.First();
                    MetadataType metadataType = metadataTypes.First();

                    Domain domain = new Domain();
                    domain.DomainUid = Guid.NewGuid();

                    mappingDb.Domains.InsertOnSubmit(domain);
                    mappingDb.SubmitChanges();

                    Node domainNode = new Node();
                    domainNode.DomainUid = domain.DomainUid;
                    domainNode.NodeUid = Guid.NewGuid();
                    domainNode.NodeType = nodeType;
                    domainNode.NodeTypeUid = nodeType.NodeTypeUid;

                    mappingDb.Nodes.InsertOnSubmit(domainNode);
                    mappingDb.SubmitChanges();

                    SoapMetadataType soapMetadataType = new SoapMetadataType();
                    soapMetadataType.Id = metadataType.MetadataTypeUid;
                    soapMetadataType.Name = metadataType.MetadataTypeName;

                    soapDomainNode = UpdateNodeMetadata(domain.DomainUid, domainNode.NodeUid, Guid.Empty, null, "Name", domainName, soapMetadataType);
                }
            });
            return soapDomainNode;
        }
Пример #2
0
        public SoapNode UpdateNodeMetadata(Guid domainId, Guid soapNodeId, Guid soapRelationshipId, SoapDescriptorType soapDescriptorType, string metadataName, string metadataValue, SoapMetadataType soapMetadataType)
        {
            SoapNode soapNode = null;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (MappingToolDatabaseDataContext mappingDb = new MappingToolDatabaseDataContext())
                {
                    var nodes = from dbNode in mappingDb.Nodes where dbNode.DomainUid == domainId && dbNode.NodeUid == soapNodeId select dbNode;

                    var node = nodes.First();

                    SoapMetadata soapMetadata = UpdateMetadata(mappingDb, soapNodeId, soapRelationshipId, soapDescriptorType, metadataName, metadataValue, soapMetadataType, node);
                    soapNode = node.ToSoapObject();
                    mappingDb.SubmitChanges();
                }
            });
            return soapNode;
        }
Пример #3
0
        public bool RenameNodeMetadata(Guid domainId, Guid soapNodeId, Guid soapRelationshipId, SoapDescriptorType soapDescriptorType, string originalMetadataName, string newMetadataName)
        {
            bool success = false;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (MappingToolDatabaseDataContext mappingDb = new MappingToolDatabaseDataContext())
                {
                    try
                    {
                        var nodes = from dbNode in mappingDb.Nodes where dbNode.DomainUid == domainId && dbNode.NodeUid == soapNodeId select dbNode;
                        Node node = nodes.Single();

                        foreach (Metadata metadatum in node.Metadatas.Where(x => x.MetadataName == originalMetadataName))
                        {
                            if (soapNodeId != Guid.Empty)
                            {
                                if (metadatum.NodeUid != soapNodeId)
                                {
                                    continue; //this should never happen since it's the node's metadata
                                }
                            }
                            if (soapRelationshipId != Guid.Empty)
                            {
                                if (metadatum.RelationshipUid != soapRelationshipId)
                                {
                                    continue;
                                }
                            }
                            if (soapDescriptorType != null)
                            {
                                if (metadatum.DescriptorTypeUid != soapDescriptorType.Id)
                                {
                                    continue;
                                }
                            }

                            //same context so we can rename it
                            metadatum.MetadataName = newMetadataName;
                            mappingDb.SubmitChanges();
                        }
                    }
                    catch (Exception ex)
                    {
                        LoggingService.WriteTrace(LoggingService.Categories.WcfServices, TraceSeverity.Unexpected,
                            "An error occurred renaming the metadata entry {0}, exception={1}", originalMetadataName, ex.Message);
                    }
                    finally
                    {
                        success = true;
                        LoggingService.WriteTrace(LoggingService.Categories.WcfServices, TraceSeverity.Verbose, 
                            "Metadata property on Node with Id: {0} with name: {1} was renamed to: {2}", 
                            soapNodeId.ToString(), originalMetadataName, newMetadataName);
                    }
                }
            });
            return success;
        }
Пример #4
0
        public SoapNode AddNode(Guid domainId, SoapNodeType nodeType, string originalId)
        {
            SoapNode soapNode = null;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (MappingToolDatabaseDataContext mappingDb = new MappingToolDatabaseDataContext())
                {
                    var domains = from dbDomain in mappingDb.Domains where dbDomain.DomainUid == domainId select dbDomain;

                    Domain domain;

                    if (domains.Count() > 0)
                    {
                        domain = domains.First();
                    }
                    else
                    {
                        domain = new Domain();
                        domain.DomainUid = domainId;
                        domain.DomainOriginalId = domainId.ToString();

                        mappingDb.Domains.InsertOnSubmit(domain);
                        mappingDb.SubmitChanges();
                    }

                    Node node = CreateNode(nodeType.Id, originalId);

                    domain.Nodes.Add(node);

                    mappingDb.SubmitChanges();

                    soapNode = new SoapNode();
                    soapNode.Domain = domainId;
                    soapNode.Id = node.NodeUid;
                    soapNode.NodeType = nodeType;
                }
            });
            return soapNode;
        }
Пример #5
0
        public ConnectedNodesResult ConnectNodes(Guid domainId, Dictionary<SoapDescriptorType, Guid> nodes, SoapRelationshipType relationshipType, string originalId)
        {
            ConnectedNodesResult connectedNodesResult = null;
            if (nodes.Values.Distinct().Count() > 1)
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (MappingToolDatabaseDataContext mappingDb = new MappingToolDatabaseDataContext())
                    {
                        List<SoapNode> updatedNodes = new List<SoapNode>();

                        var domains = from dbDomain in mappingDb.Domains where dbDomain.DomainUid == domainId select dbDomain;

                        var domain = domains.First();

                        Relationship relationship = new Relationship();
                        relationship.RelationshipUid = Guid.NewGuid();
                        relationship.RelationshipOriginalId = originalId;
                        relationship.RelationshipTypeUid = relationshipType.Id;

                        SoapRelationship soapRelationship = new SoapRelationship();
                        soapRelationship.Id = relationship.RelationshipUid;
                        soapRelationship.RelationshipType = relationshipType;

                        foreach (KeyValuePair<SoapDescriptorType, Guid> keyValuePair in nodes)
                        {
                            Descriptor descriptor = new Descriptor();
                            descriptor.DescriptorUid = Guid.NewGuid();
                            descriptor.RelationshipUid = relationship.RelationshipUid;
                            descriptor.DescriptorTypeUid = keyValuePair.Key.Id;

                            Node node = domain.Nodes.Single(x => x.NodeUid == keyValuePair.Value);
                            node.Domain = domain;
                            node.Descriptors.Add(descriptor);

                            soapRelationship.Nodes.Add(keyValuePair.Key, node.NodeUid);
                            SoapNode soapNode = node.ToSoapObject();
                            soapNode.Relationships.Add(soapRelationship.Id, soapRelationship);
                            updatedNodes.Add(soapNode);

                            domain.Relationships.Add(relationship);
                        }

                        mappingDb.SubmitChanges();

                        connectedNodesResult = new ConnectedNodesResult();
                        connectedNodesResult.Relationship = soapRelationship;
                        connectedNodesResult.Nodes = new Dictionary<Guid, SoapNode>();
                        foreach (SoapNode soapNode in updatedNodes)
                        {
                            if (!connectedNodesResult.Nodes.ContainsKey(soapNode.Id))
                            {
                                connectedNodesResult.Nodes.Add(soapNode.Id, soapNode);
                            }
                        }
                    }
                });
            }
            else
            {
                LoggingService.WriteTrace(LoggingService.Categories.WcfServices, TraceSeverity.Unexpected, "ConnectNodes() failed: There must be at least 2 distinct nodes connected.");
                //This will cause an error to be returned on the WFC event, over SOAP it won't contain the fault contract.
                throw new FaultException<ConnectNodesFailureFault>(new ConnectNodesFailureFault(), "There must be at least 2 distinct nodes connected.");
            }

            return connectedNodesResult;
        }
Пример #6
0
        public DeleteResult DeleteRelationship(Guid domainId, Guid relationshipId)
        {
            lock (deleteLock)
            {
                DeleteResult result = new DeleteResult() { DeleteSuccessful = false, DeletedId = relationshipId };
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (MappingToolDatabaseDataContext mappingDb = new MappingToolDatabaseDataContext())
                    {
                        var deleteRelationships = from relationships in mappingDb.Relationships
                                                  where relationships.RelationshipUid == relationshipId && relationships.DomainUid == domainId
                                                  select relationships;

                        //if the relationship existed it'll be the only one and can delete all the descriptors and metadatum
                        if (deleteRelationships.Count() > 0)
                        {
                            var relationship = deleteRelationships.First();

                            var deleteDescriptors = from descriptors in mappingDb.Descriptors
                                                    where descriptors.RelationshipUid == relationshipId
                                                    select descriptors;

                            foreach (var descriptor in deleteDescriptors)
                            {
                                mappingDb.Descriptors.DeleteOnSubmit(descriptor);
                            }

                            try
                            {
                                mappingDb.SubmitChanges();
                            }
                            catch (Exception)
                            {
                                //Debug.WriteLine("An exception occurred of type: {0}\r\n{1}", e.Message, e);
                            }

                            // Delete metadata associated with the relationship just deleted
                            var deleteRelationshipMetadatum = from metadatum in mappingDb.Metadatas
                                                              where metadatum.RelationshipUid == relationshipId
                                                              select metadatum;
                            foreach (var relationshipMetadatum in deleteRelationshipMetadatum)
                            {
                                mappingDb.Metadatas.DeleteOnSubmit(relationshipMetadatum);
                            }
                            try
                            {
                                mappingDb.SubmitChanges();
                            }
                            catch (Exception)
                            {
                                //Debug.WriteLine("An exception occurred of type: {0}\r\n{1}", e.Message, e);
                            }

                            mappingDb.Relationships.DeleteOnSubmit(relationship);
                            try
                            {
                                mappingDb.SubmitChanges();
                                result.DeleteSuccessful = true;
                            }
                            catch (Exception)
                            {
                                //Debug.WriteLine("An exception occurred of type: {0}\r\n{1}", e.Message, e);
                            }
                        }
                    }
                });
                return result;
            }
        }
Пример #7
0
        public DeleteResult DeleteNode(Guid domainId, Guid nodeId)
        {
            lock (deleteLock)
            {
                List<Guid> relationshipIds = new List<Guid>();
                List<Guid> deletedDescriptors = new List<Guid>();
                DeleteResult result = new DeleteResult() { DeleteSuccessful = false, DeletedId = nodeId };
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (MappingToolDatabaseDataContext mappingDb = new MappingToolDatabaseDataContext())
                    {
                        var deleteNodes = from nodes in mappingDb.Nodes
                                          where nodes.NodeUid == nodeId && nodes.DomainUid == domainId
                                          select nodes;
                        //if the node existed it'll be the only one and delete all associated relationships/descriptors and metadatum
                        if (deleteNodes.Count() > 0)
                        {
                            // Delete the descriptors that have the node to be deleted associated with them directly
                            var deleteDescriptors = from descriptors in mappingDb.Descriptors
                                                    where descriptors.NodeUid == nodeId
                                                    select descriptors;
                            foreach (var descriptor in deleteDescriptors)
                            {
                                relationshipIds.Add(descriptor.RelationshipUid.Value);
                                deletedDescriptors.Add(descriptor.DescriptorUid);
                                mappingDb.Descriptors.DeleteOnSubmit(descriptor);
                            }
                            try
                            {
                                mappingDb.SubmitChanges();
                            }
                            catch (Exception)
                            {
                                //Debug.WriteLine("An exception occurred of type: {0}\r\n{1}", e.Message, e);
                            }

                            foreach (Guid relationshipId in relationshipIds)
                            {
                                var deleteRelationships = from relationships in mappingDb.Relationships
                                                          where relationships.RelationshipUid == relationshipId
                                                          select relationships;
                                if (deleteRelationships.Count() > 0)
                                {
                                    var relationship = deleteRelationships.First();

                                    // Delete the descriptors that dangle off the other side of the relationship just deleted
                                    var deleteAltDescriptors = from descriptors2 in mappingDb.Descriptors
                                                               where descriptors2.RelationshipUid == relationshipId
                                                               select descriptors2;
                                    foreach (var altDescriptor in deleteAltDescriptors)
                                    {
                                        if (!deletedDescriptors.Contains(altDescriptor.DescriptorUid))
                                        {
                                            mappingDb.Descriptors.DeleteOnSubmit(altDescriptor);
                                        }
                                    }

                                    try
                                    {
                                        mappingDb.SubmitChanges();
                                    }
                                    catch (Exception)
                                    {
                                        //Debug.WriteLine("An exception occurred of type: {0}\r\n{1}", e.Message, e);
                                    }

                                    // Delete metadata associated with the relationship just deleted
                                    var deleteRelationshipMetadatum = from metadatum in mappingDb.Metadatas
                                                                      where metadatum.RelationshipUid == relationshipId
                                                                      select metadatum;
                                    foreach (var relationshipMetadatum in deleteRelationshipMetadatum)
                                    {
                                        mappingDb.Metadatas.DeleteOnSubmit(relationshipMetadatum);
                                    }
                                    try
                                    {
                                        mappingDb.SubmitChanges();
                                    }
                                    catch (Exception)
                                    {
                                        //Debug.WriteLine("An exception occurred of type: {0}\r\n{1}", e.Message, e);
                                    }

                                    mappingDb.Relationships.DeleteOnSubmit(relationship);
                                    try
                                    {
                                        mappingDb.SubmitChanges();
                                    }
                                    catch (Exception)
                                    {
                                        //Debug.WriteLine("An exception occurred of type: {0}\r\n{1}", e.Message, e);
                                    }
                                }
                            }

                            var nodeToDelete = deleteNodes.First();
                            var deleteNodeMetadatum = from metadatum in mappingDb.Metadatas
                                                      where metadatum.NodeUid == nodeToDelete.NodeUid
                                                      select metadatum;
                            foreach (var metadatum in deleteNodeMetadatum)
                            {
                                mappingDb.Metadatas.DeleteOnSubmit(metadatum);
                            }
                            try
                            {
                                mappingDb.SubmitChanges();
                            }
                            catch (Exception)
                            {
                                //Debug.WriteLine("An exception occurred of type: {0}\r\n{1}", e.Message, e);
                            }

                            // Delete the actual node, it will only be one
                            mappingDb.Nodes.DeleteOnSubmit(nodeToDelete);
                            try
                            {
                                mappingDb.SubmitChanges();
                                result.DeleteSuccessful = true;
                            }
                            catch (Exception)
                            {
                                //Debug.WriteLine("An exception occurred of type: {0}\r\n{1}", e.Message, e);
                            }
                        }
                    }
                });
                return result;
            }
        }
Пример #8
0
 private static void DeleteMetadata(MetadataContext context, DeleteResult result, MappingToolDatabaseDataContext mappingDb, IQueryable<Metadata> deleteMetadatas)
 {
     if (deleteMetadatas.Count() == 1)
     {
         Metadata metadata = deleteMetadatas.First();
         if (metadata != null)
         {
             mappingDb.Metadatas.DeleteOnSubmit(metadata);
             try
             {
                 mappingDb.SubmitChanges();
                 result.DeleteSuccessful = true;
                 result.DeletedId = metadata.MetadataId;
                 LoggingService.WriteTrace(LoggingService.Categories.WcfServices, TraceSeverity.Verbose,
                     "Deleted metadata with ID: {0} the value was: '{1}'", metadata.MetadataId.ToString(),
                     metadata.MetadataValue);
             }
             catch (Exception e)
             {
                 LoggingService.WriteTrace(LoggingService.Categories.WcfServices, TraceSeverity.Unexpected,
                     "There was an error deleting the metadata with ID: {0} due to {1}: {2}",
                     metadata.MetadataId.ToString(), e.GetType().ToString(), e.Message);
             }
         }
     }
     else
     {
         LoggingService.WriteTrace(LoggingService.Categories.WcfServices, TraceSeverity.Unexpected,
             "There was an error deleting the metadata with the name {0} from the NodeUid: {1}",
             context.MetadataName, context.NodeUid);
     }
 }
Пример #9
0
        public Guid CreateNewMap(Guid domainId, string mapName)
        {
            Guid newMapUid = Guid.Empty;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                Node mapNode = null;
                using (MappingToolDatabaseDataContext mappingDb = new MappingToolDatabaseDataContext())
                {
                    var mapNodeTypes = from dbNodeTypes in mappingDb.NodeTypes where dbNodeTypes.NodeTypeName == "CompendiumMapNode" select dbNodeTypes;
                    var metadataTypes = from dbMetaDataTypes in mappingDb.MetadataTypes where dbMetaDataTypes.MetadataTypeName == "string" select dbMetaDataTypes;
                    var domains = from dbDomain in mappingDb.Domains where dbDomain.DomainUid == domainId select dbDomain;

                    if (mapNodeTypes.Count() == 1 && metadataTypes.Count() == 1 && domains.Count() == 1) //there should only be one
                    {
                        Domain domain = domains.First();
                        NodeType nodeType = mapNodeTypes.First();
                        MetadataType metadataType = metadataTypes.First();

                        SoapMetadataType soapMetadataType = new SoapMetadataType();
                        soapMetadataType.Id = metadataType.MetadataTypeUid;
                        soapMetadataType.Name = metadataType.MetadataTypeName;

                        mapNode = CreateNode(nodeType.NodeTypeUid, mapName);
                        UpdateMetadata(mappingDb, mapNode.NodeUid, Guid.Empty, null, "Name", mapName, soapMetadataType, mapNode);
                        domain.Nodes.Add(mapNode);
                        mappingDb.SubmitChanges();
                    }
                    if (mapNode != null)
                    {
                        newMapUid = mapNode.NodeUid;

                        Dictionary<string, SoapNodeType> soapNodeTypes;
                        Dictionary<string, SoapRelationshipType> soapRelTypes;
                        Dictionary<string, SoapMetadataType> soapMetaTypes;
                        Dictionary<string, SoapDescriptorType> soapDescTypes;
                        GetSoapTypes(out soapNodeTypes, out soapRelTypes, out soapMetaTypes, out soapDescTypes);

                        Guid domainNodeId = GetDomainNodeId(domainId);
                        ConnectToMap(domainId, domainNodeId, newMapUid, soapDescTypes, soapRelTypes);
                    }
                }
            });
            return newMapUid;
        }
Пример #10
0
        public SoapNode PasteNodeClone(Guid domainId, Guid copiedNodeId)
        {
            SoapNode result = null;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (MappingToolDatabaseDataContext mappingDb = new MappingToolDatabaseDataContext())
                {
                    var domains = from dbDomain in mappingDb.Domains where dbDomain.DomainUid == domainId select dbDomain;

                    Domain domain;

                    if (domains.Count() > 0)
                    {
                        domain = domains.First();

                        var originalNode = from dbNode
                                           in mappingDb.Nodes
                                           where dbNode.NodeUid == copiedNodeId
                                           && dbNode.DomainUid == domainId
                                           select dbNode;

                        if (originalNode.Count() > 0)
                        {
                            var origNode = originalNode.First();

                            Node node = CreateNode(origNode.NodeTypeUid.Value, origNode.NodeUid.ToString());

                            node.Metadatas = new EntitySet<Metadata>();
                            foreach (Metadata metadata in origNode.Metadatas)
                            {
                                if (!(metadata.MetadataName == "XPosition" || metadata.MetadataName == "YPosition"))
                                {
                                    Metadata metaCopy = new Metadata();
                                    metaCopy.MetadataId = Guid.NewGuid();
                                    metaCopy.MetadataName = metadata.MetadataName;
                                    metaCopy.MetadataType = metadata.MetadataType;
                                    metaCopy.MetadataTypeUid = metadata.MetadataTypeUid;
                                    metaCopy.MetadataValue = metadata.MetadataValue;
                                    metaCopy.NodeUid = node.NodeUid;
                                    metaCopy.Node = node;
                                    metaCopy.DescriptorTypeUid = metadata.DescriptorTypeUid;
                                    metaCopy.RelationshipUid = metadata.RelationshipUid;
                                    metaCopy.Relationship = metadata.Relationship;
                                    node.Metadatas.Add(metaCopy);
                                }
                            }

                            domain.Nodes.Add(node);

                            mappingDb.SubmitChanges();

                            SoapNode soapNode = node.ToSoapObject();
                            result = soapNode;
                        }
                    }
                }
            });
            return result;
        }
        public void ExecuteCmdletBase(PSCmdlet callingCmdlet)
        {
            HashSet<Guid> validMaps = new HashSet<Guid>();
            HashSet<Guid> orphanedNodes = new HashSet<Guid>();
            HashSet<Guid> orphanedRelationships = new HashSet<Guid>();

            if (RootMap == null || !RootMap.CheckIsValid())
            {
                callingCmdlet.WriteWarning("An invalid source map has been provided.");

                return;
            }

            Model.IDatabaseInfo mapDbInfo = RootMap.Domain;

            using (MappingToolDatabaseDataContext dataContext = new MappingToolDatabaseDataContext(mapDbInfo.ConnectionString))
            {
                dataContext.CommandTimeout = 180;

                var dbRootMapNodes = from dbNode in dataContext.Nodes
                                     where dbNode.NodeUid == RootMap.NodeId
                                     select dbNode;

                var dbRootMapNode = dbRootMapNodes.FirstOrDefault();

                if (dbRootMapNode == null)
                {
                    throw new KeyNotFoundException("The specified root map doesn't exist.");
                }

                if (dbRootMapNode.NodeUid != dbRootMapNode.RootMapUid)
                {
                    throw new NotSupportedException("The provided root map isn't a root map.");
                }

                /// We need to find all the valid children maps, starting at the root map. We'll do this recursively.
                #region Find valid maps
                validMaps.Add(dbRootMapNode.NodeUid);

                RecusivelyFindValidMaps(ref validMaps, dbRootMapNode);
                #endregion

                #region Find all nodes that aren't part of a valid map
                var allNodesInMap = from dbNode in dataContext.Nodes
                                    where dbNode.RootMapUid == RootMap.NodeId
                                    select dbNode;

                foreach (var node in allNodesInMap)
                {
                    if (validMaps.Contains(node.NodeUid))
                    {
                        continue;
                    }

                    bool isNodeValid = CheckIsValid(ref validMaps, node);

                    if (!isNodeValid)
                    {
                        /// If the orphaned nodes list doesn't contain the node, then add the node to the list.
                        if (!orphanedNodes.Contains(node.NodeUid))
                        {
                            orphanedNodes.Add(node.NodeUid);
                        }

                        /// Find all the orphaned relationships.
                        FindOrphanedRelationships(ref orphanedRelationships, node);
                    }
                }
                #endregion

                int orphanedNodesCount = 0;
                int orphanedMetadataCount = 0;
                int orphanedDescriptorsCount = 0;
                int orphanedRelationshipsCount = 0;

                foreach (Guid nodeId in orphanedNodes)
                {
                    var orphanedDescriptors = from dbDescriptor in dataContext.Descriptors
                                              where dbDescriptor.NodeUid == nodeId
                                              select dbDescriptor;

                    foreach (var orphanedDescriptor in orphanedDescriptors)
                    {
                        dataContext.Descriptors.DeleteOnSubmit(orphanedDescriptor);
                        orphanedDescriptorsCount++;
                    }

                    var orphanedMetadata = from dbMetadata in dataContext.Metadatas
                                           where dbMetadata.NodeUid == nodeId
                                           select dbMetadata;

                    foreach (var orphanedMetadatum in orphanedMetadata)
                    {
                        dataContext.Metadatas.DeleteOnSubmit(orphanedMetadatum);
                        orphanedMetadataCount++;
                    }
                }

                dataContext.SubmitChanges();

                foreach (Guid relationshipId in orphanedRelationships)
                {
                    var orphanedDescriptors = from dbDescriptor in dataContext.Descriptors
                                              where dbDescriptor.RelationshipUid == relationshipId
                                              select dbDescriptor;

                    foreach (var orphanedDescriptor in orphanedDescriptors)
                    {
                        dataContext.Descriptors.DeleteOnSubmit(orphanedDescriptor);
                        orphanedDescriptorsCount++;
                    }

                    var orphanedMetadata = from dbMetadata in dataContext.Metadatas
                                           where dbMetadata.RelationshipUid == relationshipId
                                           select dbMetadata;

                    foreach (var orphanedMetadatum in orphanedMetadata)
                    {
                        dataContext.Metadatas.DeleteOnSubmit(orphanedMetadatum);
                        orphanedMetadataCount++;
                    }
                }

                dataContext.SubmitChanges();

                foreach (Guid nodeId in orphanedNodes)
                {
                    var orphanedDbNodes = from dbNode in dataContext.Nodes
                                          where dbNode.NodeUid == nodeId
                                          select dbNode;

                    foreach (var orphanedNode in orphanedDbNodes)
                    {
                        dataContext.Nodes.DeleteOnSubmit(orphanedNode);
                        orphanedNodesCount++;
                    }
                }

                dataContext.SubmitChanges();

                foreach (Guid relationshipId in orphanedRelationships)
                {
                    var orphanedDbRelationships = from dbRelationship in dataContext.Relationships
                                                  where dbRelationship.RelationshipUid == relationshipId
                                                  select dbRelationship;

                    foreach (var orphanedRelationship in orphanedDbRelationships)
                    {
                        dataContext.Relationships.DeleteOnSubmit(orphanedRelationship);
                        orphanedRelationshipsCount++;
                    }
                }

                dataContext.SubmitChanges();

                callingCmdlet.WriteVerbose(string.Format("Deleted {0} orphaned node/s.", orphanedNodesCount));
                callingCmdlet.WriteVerbose(string.Format("Deleted {0} orphaned relationship/s.", orphanedRelationshipsCount));
                callingCmdlet.WriteVerbose(string.Format("Deleted {0} orphaned metadata/s.", orphanedMetadataCount));
                callingCmdlet.WriteVerbose(string.Format("Deleted {0} orphaned descriptor/s.", orphanedDescriptorsCount));
            }
        }
Пример #12
0
        public void ExecuteCmdletBase(PSCmdlet callingCmdlet)
        {
            Dictionary <Guid, QueryMapNode> originalNodes         = new Dictionary <Guid, QueryMapNode>();
            List <QueryMapRelationship>     originalRelationships = new List <QueryMapRelationship>();
            List <QueryMapDescriptor>       originalDescriptors   = new List <QueryMapDescriptor>();
            List <QueryMapMetadata>         originalMetadata      = new List <QueryMapMetadata>();

            if (DestinationDomain == null || !DestinationDomain.CheckIsValid())
            {
                callingCmdlet.WriteWarning("An invalid destination domain has been provided.");

                return;
            }

            if (SourceMap == null || !SourceMap.CheckIsValid())
            {
                callingCmdlet.WriteWarning("An invalid source map has been provided.");

                return;
            }

            Model.IDatabaseInfo sourceMapDbInfo = SourceMap.Domain;

            using (MappingToolDatabaseDataContext dataContext = new MappingToolDatabaseDataContext(sourceMapDbInfo.ConnectionString))
            {
                dataContext.CommandTimeout = 180;
                var queryMapResultSets = dataContext.QueryMapMultiDepth(SourceMap.Domain.DomainId, SourceMap.NodeId, -1, false);

                var queryMapResultSet = queryMapResultSets.GetResult <QueryMapMultiDepthResult>();

                while (queryMapResultSet != null)
                {
                    foreach (var queryMapResult in queryMapResultSet)
                    {
                        if (queryMapResult.Level != null)
                        {
                            if (queryMapResult.NodeUid.HasValue && queryMapResult.NodeUid != Guid.Empty)
                            {
                                /// Make sure that we aren't copying across a domain node.
                                if (queryMapResult.NodeTypeUid.HasValue && queryMapResult.NodeTypeUid != new Guid("263754C2-2F31-4D21-B9C4-6509E00A5E94"))
                                {
                                    /// The QueryMap procedure returns ALL nodes by following relationships to max depth meaning some nodes are repeated in some of the levels multiple nodes may connect to them.
                                    if (!originalNodes.ContainsKey(queryMapResult.NodeUid.Value))
                                    {
                                        /// TODO: Need to consider copying the NodeOriginalId.
                                        QueryMapNode node = new QueryMapNode();
                                        node.NodeUid     = queryMapResult.NodeUid.Value;
                                        node.DomainUid   = DestinationDomain.DomainId;
                                        node.NodeTypeUid = queryMapResult.NodeTypeUid;
                                        node.RootMapUid  = queryMapResult.RootMapUid;
                                        node.Created     = queryMapResult.Created;
                                        node.Modified    = queryMapResult.Modified;
                                        node.CreatedBy   = queryMapResult.CreatedBy;
                                        node.ModifiedBy  = queryMapResult.ModifiedBy;

                                        originalNodes[queryMapResult.NodeUid.Value] = node;
                                    }
                                }
                            }
                        }
                        else if (queryMapResult.MetadataId != null)
                        {
                            if (queryMapResult.MetadataId.HasValue && queryMapResult.MetadataId != Guid.Empty)
                            {
                                QueryMapMetadata metadatum = new QueryMapMetadata();
                                metadatum.MetadataId        = queryMapResult.MetadataId.Value;
                                metadatum.NodeUid           = queryMapResult.NodeUid;
                                metadatum.RelationshipUid   = queryMapResult.RelationshipUid;
                                metadatum.DescriptorTypeUid = queryMapResult.DescriptorTypeUid;
                                metadatum.MetadataTypeUid   = queryMapResult.MetadataTypeUid;
                                metadatum.MetadataName      = queryMapResult.MetadataName;
                                metadatum.MetadataValue     = queryMapResult.MetadataValue;
                                metadatum.DomainUid         = queryMapResult.DomainUid;
                                metadatum.RootMapUid        = queryMapResult.RootMapUid;
                                metadatum.Created           = queryMapResult.Created;
                                metadatum.Modified          = queryMapResult.Modified;
                                metadatum.CreatedBy         = queryMapResult.CreatedBy;
                                metadatum.ModifiedBy        = queryMapResult.ModifiedBy;

                                originalMetadata.Add(metadatum);
                            }
                        }
                        else if (queryMapResult.DescriptorUid != null)
                        {
                            if (queryMapResult.DescriptorUid.HasValue && queryMapResult.DescriptorUid != Guid.Empty)
                            {
                                QueryMapDescriptor descriptor = new QueryMapDescriptor();
                                descriptor.DescriptorUid     = queryMapResult.DescriptorUid.Value;
                                descriptor.NodeUid           = queryMapResult.NodeUid;
                                descriptor.RelationshipUid   = queryMapResult.RelationshipUid;
                                descriptor.DescriptorTypeUid = queryMapResult.DescriptorTypeUid;

                                originalDescriptors.Add(descriptor);
                            }
                        }
                        else
                        {
                            if (queryMapResult.RelationshipUid.HasValue && queryMapResult.RelationshipUid != Guid.Empty)
                            {
                                /// TODO: Need to consider copying the RelationshipOriginalId.
                                QueryMapRelationship relationship = new QueryMapRelationship();
                                relationship.RelationshipUid     = queryMapResult.RelationshipUid.Value;
                                relationship.DomainUid           = DestinationDomain.DomainId;
                                relationship.RelationshipTypeUid = queryMapResult.RelationshipTypeUid;
                                relationship.RootMapUid          = queryMapResult.RootMapUid;
                                relationship.Created             = queryMapResult.Created;
                                relationship.Modified            = queryMapResult.Modified;
                                relationship.CreatedBy           = queryMapResult.CreatedBy;
                                relationship.ModifiedBy          = queryMapResult.ModifiedBy;

                                originalRelationships.Add(relationship);
                            }
                        }
                    }

                    queryMapResultSet = queryMapResultSets.GetResult <QueryMapMultiDepthResult>();
                }
            }

            int totalNodes         = originalNodes.Count;
            int totalRelationships = originalRelationships.Count;
            int totalMetadata      = originalMetadata.Count;
            int totalDescriptors   = originalDescriptors.Count;

            Dictionary <Guid, Guid> newNodeIds         = new Dictionary <Guid, Guid>();
            Dictionary <Guid, Guid> newRelationshipIds = new Dictionary <Guid, Guid>();

            Model.IDatabaseInfo destinationDomainDbInfo = DestinationDomain;

            /// The following performs the creation of nodes in the new database
            using (MappingToolDatabaseDataContext dataContext = new MappingToolDatabaseDataContext(destinationDomainDbInfo.ConnectionString))
            {
                dataContext.CommandTimeout = 180;
                int count = 0;

                callingCmdlet.WriteVerbose("Processing of nodes starting.");

                DateTime currentTime = DateTime.Now;

                WindowsIdentity currentUserIdentity = WindowsIdentity.GetCurrent();
                string          currentUserName     = "******";

                if (currentUserIdentity != null)
                {
                    currentUserName = currentUserIdentity.Name;
                }

                Guid newRootMapId = Guid.NewGuid();

                /// If it is the root map node, we want to assign it the pre-determined ID as we've been using this as the RootMapUid for everything;
                QueryMapNode sourceMapNode = originalNodes[SourceMap.NodeId];

                Node newRootMapNode = new Node();
                newRootMapNode.NodeUid     = newRootMapId;
                newRootMapNode.DomainUid   = DestinationDomain.DomainId;
                newRootMapNode.NodeTypeUid = sourceMapNode.NodeTypeUid;
                newRootMapNode.RootMapUid  = newRootMapId;
                newRootMapNode.Created     = sourceMapNode.Created;
                newRootMapNode.Modified    = currentTime;
                newRootMapNode.CreatedBy   = sourceMapNode.CreatedBy;
                newRootMapNode.ModifiedBy  = currentUserName;

                newNodeIds[sourceMapNode.NodeUid] = newRootMapNode.NodeUid;

                dataContext.Nodes.InsertOnSubmit(newRootMapNode);

                /// Iterate through all nodes in memory and change their node IDs and root map IDs so that they won't clash with the map from where they were copied.
                foreach (QueryMapNode memoryNode in originalNodes.Values)
                {
                    if (memoryNode.NodeUid == SourceMap.NodeId)
                    {
                        continue;
                    }

                    count++;

                    if (count % 10 == 0)
                    {
                        callingCmdlet.WriteVerbose(count + " / " + totalNodes + " Nodes Completed");
                    }

                    Node node = new Node();
                    node.NodeUid     = Guid.NewGuid();
                    node.DomainUid   = DestinationDomain.DomainId;
                    node.NodeTypeUid = memoryNode.NodeTypeUid;
                    node.RootMapUid  = newRootMapId;
                    node.Created     = memoryNode.Created;
                    node.Modified    = currentTime;
                    node.CreatedBy   = memoryNode.CreatedBy;
                    node.ModifiedBy  = currentUserName;

                    newNodeIds[memoryNode.NodeUid] = node.NodeUid;

                    dataContext.Nodes.InsertOnSubmit(node);
                }

                if (!newNodeIds.ContainsKey(SourceMap.NodeId))
                {
                    callingCmdlet.WriteWarning("Unexpected scenario. The source map NodeId can't be found in the list of copied nodes.");

                    return;
                }

                callingCmdlet.WriteVerbose("Committing nodes starting.");

                /// Commit node additions.
                dataContext.SubmitChanges();

                callingCmdlet.WriteVerbose("Committing nodes completed.");

                count = 0;

                callingCmdlet.WriteVerbose("Processing of relationships starting.");

                foreach (QueryMapRelationship memoryRelationship in originalRelationships)
                {
                    count++;

                    if (count % 10 == 0)
                    {
                        callingCmdlet.WriteVerbose(count + " / " + totalRelationships + " Relationships Completed");
                    }

                    Relationship relationship = new Relationship();
                    relationship.RelationshipUid     = Guid.NewGuid();
                    relationship.DomainUid           = DestinationDomain.DomainId;
                    relationship.RelationshipTypeUid = memoryRelationship.RelationshipTypeUid;
                    relationship.RootMapUid          = newRootMapId;
                    relationship.Created             = memoryRelationship.Created;
                    relationship.Modified            = currentTime;
                    relationship.CreatedBy           = memoryRelationship.CreatedBy;
                    relationship.ModifiedBy          = currentUserName;

                    newRelationshipIds[memoryRelationship.RelationshipUid] = relationship.RelationshipUid;

                    dataContext.Relationships.InsertOnSubmit(relationship);
                }

                callingCmdlet.WriteVerbose("Committing relationships starting.");

                /// Commit relationship additions.
                dataContext.SubmitChanges();

                callingCmdlet.WriteVerbose("Committing relationships completed.");

                count = 0;

                callingCmdlet.WriteVerbose("Processing of descriptors starting.");

                foreach (QueryMapDescriptor memoryDescriptor in originalDescriptors)
                {
                    count++;

                    if (count % 10 == 0)
                    {
                        callingCmdlet.WriteVerbose(count + " / " + totalDescriptors + " Descriptors Completed");
                    }

                    if (newNodeIds.ContainsKey(memoryDescriptor.NodeUid.Value) && newRelationshipIds.ContainsKey(memoryDescriptor.RelationshipUid.Value))
                    {
                        Descriptor descriptor = new Descriptor();
                        descriptor.DescriptorUid     = Guid.NewGuid();
                        descriptor.NodeUid           = newNodeIds[memoryDescriptor.NodeUid.Value];
                        descriptor.RelationshipUid   = newRelationshipIds[memoryDescriptor.RelationshipUid.Value];
                        descriptor.DescriptorTypeUid = memoryDescriptor.DescriptorTypeUid;

                        dataContext.Descriptors.InsertOnSubmit(descriptor);
                    }
                }

                callingCmdlet.WriteVerbose("Committing descriptors starting.");

                /// Commit descriptor additions.
                dataContext.SubmitChanges();

                callingCmdlet.WriteVerbose("Committing descriptors completed.");

                count = 0;

                callingCmdlet.WriteVerbose("Processing of metadata starting.");

                foreach (QueryMapMetadata memoryMetadatum in originalMetadata)
                {
                    count++;

                    if (count % 10 == 0)
                    {
                        callingCmdlet.WriteVerbose(count + " / " + totalMetadata + " Metadata Completed");
                    }

                    if (memoryMetadatum.NodeUid == null || newNodeIds.ContainsKey(memoryMetadatum.NodeUid.Value))
                    {
                        if (memoryMetadatum.RelationshipUid == null || newRelationshipIds.ContainsKey(memoryMetadatum.RelationshipUid.Value))
                        {
                            Metadata metadatum = new Metadata();
                            metadatum.MetadataId = Guid.NewGuid();

                            if (memoryMetadatum.NodeUid != null)
                            {
                                metadatum.NodeUid = newNodeIds[memoryMetadatum.NodeUid.Value];
                            }
                            else
                            {
                                metadatum.NodeUid = null;
                            }

                            if (memoryMetadatum.RelationshipUid != null)
                            {
                                metadatum.RelationshipUid = newRelationshipIds[memoryMetadatum.RelationshipUid.Value];
                            }
                            else
                            {
                                metadatum.RelationshipUid = null;
                            }

                            metadatum.DescriptorTypeUid = memoryMetadatum.DescriptorTypeUid;
                            metadatum.MetadataTypeUid   = memoryMetadatum.MetadataTypeUid;
                            metadatum.MetadataName      = memoryMetadatum.MetadataName;
                            metadatum.MetadataValue     = memoryMetadatum.MetadataValue;
                            metadatum.DomainUid         = DestinationDomain.DomainId;
                            metadatum.RootMapUid        = newRootMapId;
                            metadatum.Created           = memoryMetadatum.Created;
                            metadatum.Modified          = currentTime;
                            metadatum.CreatedBy         = memoryMetadatum.CreatedBy;
                            metadatum.ModifiedBy        = currentUserName;

                            dataContext.Metadatas.InsertOnSubmit(metadatum);
                        }
                    }
                }

                callingCmdlet.WriteVerbose("Committing metadata starting.");

                /// Commit metadata additions.
                dataContext.SubmitChanges();

                callingCmdlet.WriteVerbose("Committing metadata completed.");

                callingCmdlet.WriteVerbose("Connecting map to domain.");

                Relationship domainNodeMapRelationship = new Relationship();
                domainNodeMapRelationship.RelationshipUid     = Guid.NewGuid();
                domainNodeMapRelationship.DomainUid           = DestinationDomain.DomainId;
                domainNodeMapRelationship.RelationshipTypeUid = new Guid("4AFF46D7-87BE-48DD-B703-A93E38EF8FFB");
                domainNodeMapRelationship.RootMapUid          = newRootMapId;
                domainNodeMapRelationship.Created             = currentTime;
                domainNodeMapRelationship.Modified            = currentTime;
                domainNodeMapRelationship.CreatedBy           = currentUserName;
                domainNodeMapRelationship.ModifiedBy          = currentUserName;

                Descriptor domainNodeMapFromDescriptor = new Descriptor();
                domainNodeMapFromDescriptor.DescriptorUid     = Guid.NewGuid();
                domainNodeMapFromDescriptor.DescriptorTypeUid = new Guid("96DA1782-058C-4F9B-BB1A-31B048F8C75A");
                domainNodeMapFromDescriptor.NodeUid           = newNodeIds[SourceMap.NodeId];
                domainNodeMapFromDescriptor.RelationshipUid   = domainNodeMapRelationship.RelationshipUid;

                Descriptor domainNodeMapToDescriptor = new Descriptor();
                domainNodeMapToDescriptor.DescriptorUid     = Guid.NewGuid();
                domainNodeMapToDescriptor.DescriptorTypeUid = new Guid("07C91D35-4DAC-431B-966B-64C924B8CDAB");
                domainNodeMapToDescriptor.NodeUid           = DestinationDomain.NodeId;
                domainNodeMapToDescriptor.RelationshipUid   = domainNodeMapRelationship.RelationshipUid;

                dataContext.Relationships.InsertOnSubmit(domainNodeMapRelationship);
                dataContext.Descriptors.InsertOnSubmit(domainNodeMapFromDescriptor);
                dataContext.Descriptors.InsertOnSubmit(domainNodeMapToDescriptor);

                dataContext.SubmitChanges();

                callingCmdlet.WriteVerbose("Completed connecting map to domain.");

                callingCmdlet.WriteVerbose("Copy completed.");
            }
        }
Пример #13
0
        public void ExecuteCmdletBase(PSCmdlet callingCmdlet)
        {
            HashSet <Guid> validMaps             = new HashSet <Guid>();
            HashSet <Guid> orphanedNodes         = new HashSet <Guid>();
            HashSet <Guid> orphanedRelationships = new HashSet <Guid>();

            if (RootMap == null || !RootMap.CheckIsValid())
            {
                callingCmdlet.WriteWarning("An invalid source map has been provided.");

                return;
            }

            Model.IDatabaseInfo mapDbInfo = RootMap.Domain;

            using (MappingToolDatabaseDataContext dataContext = new MappingToolDatabaseDataContext(mapDbInfo.ConnectionString))
            {
                dataContext.CommandTimeout = 180;

                var dbRootMapNodes = from dbNode in dataContext.Nodes
                                     where dbNode.NodeUid == RootMap.NodeId
                                     select dbNode;

                var dbRootMapNode = dbRootMapNodes.FirstOrDefault();

                if (dbRootMapNode == null)
                {
                    throw new KeyNotFoundException("The specified root map doesn't exist.");
                }

                if (dbRootMapNode.NodeUid != dbRootMapNode.RootMapUid)
                {
                    throw new NotSupportedException("The provided root map isn't a root map.");
                }

                /// We need to find all the valid children maps, starting at the root map. We'll do this recursively.
                #region Find valid maps
                validMaps.Add(dbRootMapNode.NodeUid);

                RecusivelyFindValidMaps(ref validMaps, dbRootMapNode);
                #endregion

                #region Find all nodes that aren't part of a valid map
                var allNodesInMap = from dbNode in dataContext.Nodes
                                    where dbNode.RootMapUid == RootMap.NodeId
                                    select dbNode;

                foreach (var node in allNodesInMap)
                {
                    if (validMaps.Contains(node.NodeUid))
                    {
                        continue;
                    }

                    bool isNodeValid = CheckIsValid(ref validMaps, node);

                    if (!isNodeValid)
                    {
                        /// If the orphaned nodes list doesn't contain the node, then add the node to the list.
                        if (!orphanedNodes.Contains(node.NodeUid))
                        {
                            orphanedNodes.Add(node.NodeUid);
                        }

                        /// Find all the orphaned relationships.
                        FindOrphanedRelationships(ref orphanedRelationships, node);
                    }
                }
                #endregion

                int orphanedNodesCount         = 0;
                int orphanedMetadataCount      = 0;
                int orphanedDescriptorsCount   = 0;
                int orphanedRelationshipsCount = 0;

                foreach (Guid nodeId in orphanedNodes)
                {
                    var orphanedDescriptors = from dbDescriptor in dataContext.Descriptors
                                              where dbDescriptor.NodeUid == nodeId
                                              select dbDescriptor;

                    foreach (var orphanedDescriptor in orphanedDescriptors)
                    {
                        dataContext.Descriptors.DeleteOnSubmit(orphanedDescriptor);
                        orphanedDescriptorsCount++;
                    }

                    var orphanedMetadata = from dbMetadata in dataContext.Metadatas
                                           where dbMetadata.NodeUid == nodeId
                                           select dbMetadata;

                    foreach (var orphanedMetadatum in orphanedMetadata)
                    {
                        dataContext.Metadatas.DeleteOnSubmit(orphanedMetadatum);
                        orphanedMetadataCount++;
                    }
                }

                dataContext.SubmitChanges();

                foreach (Guid relationshipId in orphanedRelationships)
                {
                    var orphanedDescriptors = from dbDescriptor in dataContext.Descriptors
                                              where dbDescriptor.RelationshipUid == relationshipId
                                              select dbDescriptor;

                    foreach (var orphanedDescriptor in orphanedDescriptors)
                    {
                        dataContext.Descriptors.DeleteOnSubmit(orphanedDescriptor);
                        orphanedDescriptorsCount++;
                    }

                    var orphanedMetadata = from dbMetadata in dataContext.Metadatas
                                           where dbMetadata.RelationshipUid == relationshipId
                                           select dbMetadata;

                    foreach (var orphanedMetadatum in orphanedMetadata)
                    {
                        dataContext.Metadatas.DeleteOnSubmit(orphanedMetadatum);
                        orphanedMetadataCount++;
                    }
                }

                dataContext.SubmitChanges();

                foreach (Guid nodeId in orphanedNodes)
                {
                    var orphanedDbNodes = from dbNode in dataContext.Nodes
                                          where dbNode.NodeUid == nodeId
                                          select dbNode;

                    foreach (var orphanedNode in orphanedDbNodes)
                    {
                        dataContext.Nodes.DeleteOnSubmit(orphanedNode);
                        orphanedNodesCount++;
                    }
                }

                dataContext.SubmitChanges();

                foreach (Guid relationshipId in orphanedRelationships)
                {
                    var orphanedDbRelationships = from dbRelationship in dataContext.Relationships
                                                  where dbRelationship.RelationshipUid == relationshipId
                                                  select dbRelationship;

                    foreach (var orphanedRelationship in orphanedDbRelationships)
                    {
                        dataContext.Relationships.DeleteOnSubmit(orphanedRelationship);
                        orphanedRelationshipsCount++;
                    }
                }

                dataContext.SubmitChanges();

                callingCmdlet.WriteVerbose(string.Format("Deleted {0} orphaned node/s.", orphanedNodesCount));
                callingCmdlet.WriteVerbose(string.Format("Deleted {0} orphaned relationship/s.", orphanedRelationshipsCount));
                callingCmdlet.WriteVerbose(string.Format("Deleted {0} orphaned metadata/s.", orphanedMetadataCount));
                callingCmdlet.WriteVerbose(string.Format("Deleted {0} orphaned descriptor/s.", orphanedDescriptorsCount));
            }
        }
Пример #14
0
        public void ExecuteCmdletBase(PSCmdlet callingCmdlet)
        {
            Dictionary<Guid, QueryMapNode> originalNodes = new Dictionary<Guid, QueryMapNode>();
            List<QueryMapRelationship> originalRelationships = new List<QueryMapRelationship>();
            List<QueryMapDescriptor> originalDescriptors = new List<QueryMapDescriptor>();
            List<QueryMapMetadata> originalMetadata = new List<QueryMapMetadata>();

            if (DestinationDomain == null || !DestinationDomain.CheckIsValid())
            {
                callingCmdlet.WriteWarning("An invalid destination domain has been provided.");

                return;
            }

            if (SourceMap == null || !SourceMap.CheckIsValid())
            {
                callingCmdlet.WriteWarning("An invalid source map has been provided.");

                return;
            }

            Model.IDatabaseInfo sourceMapDbInfo = SourceMap.Domain;

            using (MappingToolDatabaseDataContext dataContext = new MappingToolDatabaseDataContext(sourceMapDbInfo.ConnectionString))
            {
                dataContext.CommandTimeout = 180;
                var queryMapResultSets = dataContext.QueryMapMultiDepth(SourceMap.Domain.DomainId, SourceMap.NodeId, -1, false);

                var queryMapResultSet = queryMapResultSets.GetResult<QueryMapMultiDepthResult>();

                while (queryMapResultSet != null)
                {
                    foreach (var queryMapResult in queryMapResultSet)
                    {
                        if (queryMapResult.Level != null)
                        {
                            if (queryMapResult.NodeUid.HasValue && queryMapResult.NodeUid != Guid.Empty)
                            {
                                /// Make sure that we aren't copying across a domain node.
                                if (queryMapResult.NodeTypeUid.HasValue && queryMapResult.NodeTypeUid != new Guid("263754C2-2F31-4D21-B9C4-6509E00A5E94"))
                                {
                                    /// The QueryMap procedure returns ALL nodes by following relationships to max depth meaning some nodes are repeated in some of the levels multiple nodes may connect to them.
                                    if (!originalNodes.ContainsKey(queryMapResult.NodeUid.Value))
                                    {
                                        /// TODO: Need to consider copying the NodeOriginalId.
                                        QueryMapNode node = new QueryMapNode();
                                        node.NodeUid = queryMapResult.NodeUid.Value;
                                        node.DomainUid = DestinationDomain.DomainId;
                                        node.NodeTypeUid = queryMapResult.NodeTypeUid;
                                        node.RootMapUid = queryMapResult.RootMapUid;
                                        node.Created = queryMapResult.Created;
                                        node.Modified = queryMapResult.Modified;
                                        node.CreatedBy = queryMapResult.CreatedBy;
                                        node.ModifiedBy = queryMapResult.ModifiedBy;

                                        originalNodes[queryMapResult.NodeUid.Value] = node;
                                    }
                                }
                            }
                        }
                        else if (queryMapResult.MetadataId != null)
                        {
                            if (queryMapResult.MetadataId.HasValue && queryMapResult.MetadataId != Guid.Empty)
                            {
                                QueryMapMetadata metadatum = new QueryMapMetadata();
                                metadatum.MetadataId = queryMapResult.MetadataId.Value;
                                metadatum.NodeUid = queryMapResult.NodeUid;
                                metadatum.RelationshipUid = queryMapResult.RelationshipUid;
                                metadatum.DescriptorTypeUid = queryMapResult.DescriptorTypeUid;
                                metadatum.MetadataTypeUid = queryMapResult.MetadataTypeUid;
                                metadatum.MetadataName = queryMapResult.MetadataName;
                                metadatum.MetadataValue = queryMapResult.MetadataValue;
                                metadatum.DomainUid = queryMapResult.DomainUid;
                                metadatum.RootMapUid = queryMapResult.RootMapUid;
                                metadatum.Created = queryMapResult.Created;
                                metadatum.Modified = queryMapResult.Modified;
                                metadatum.CreatedBy = queryMapResult.CreatedBy;
                                metadatum.ModifiedBy = queryMapResult.ModifiedBy;

                                originalMetadata.Add(metadatum);
                            }
                        }
                        else if (queryMapResult.DescriptorUid != null)
                        {
                            if (queryMapResult.DescriptorUid.HasValue && queryMapResult.DescriptorUid != Guid.Empty)
                            {
                                QueryMapDescriptor descriptor = new QueryMapDescriptor();
                                descriptor.DescriptorUid = queryMapResult.DescriptorUid.Value;
                                descriptor.NodeUid = queryMapResult.NodeUid;
                                descriptor.RelationshipUid = queryMapResult.RelationshipUid;
                                descriptor.DescriptorTypeUid = queryMapResult.DescriptorTypeUid;

                                originalDescriptors.Add(descriptor);
                            }
                        }
                        else
                        {
                            if (queryMapResult.RelationshipUid.HasValue && queryMapResult.RelationshipUid != Guid.Empty)
                            {
                                /// TODO: Need to consider copying the RelationshipOriginalId.
                                QueryMapRelationship relationship = new QueryMapRelationship();
                                relationship.RelationshipUid = queryMapResult.RelationshipUid.Value;
                                relationship.DomainUid = DestinationDomain.DomainId;
                                relationship.RelationshipTypeUid = queryMapResult.RelationshipTypeUid;
                                relationship.RootMapUid = queryMapResult.RootMapUid;
                                relationship.Created = queryMapResult.Created;
                                relationship.Modified = queryMapResult.Modified;
                                relationship.CreatedBy = queryMapResult.CreatedBy;
                                relationship.ModifiedBy = queryMapResult.ModifiedBy;

                                originalRelationships.Add(relationship);
                            }
                        }
                    }

                    queryMapResultSet = queryMapResultSets.GetResult<QueryMapMultiDepthResult>();
                }
            }

            int totalNodes = originalNodes.Count;
            int totalRelationships = originalRelationships.Count;
            int totalMetadata = originalMetadata.Count;
            int totalDescriptors = originalDescriptors.Count;

            Dictionary<Guid, Guid> newNodeIds = new Dictionary<Guid, Guid>();
            Dictionary<Guid, Guid> newRelationshipIds = new Dictionary<Guid, Guid>();

            Model.IDatabaseInfo destinationDomainDbInfo = DestinationDomain;

            /// The following performs the creation of nodes in the new database
            using (MappingToolDatabaseDataContext dataContext = new MappingToolDatabaseDataContext(destinationDomainDbInfo.ConnectionString))
            {
                dataContext.CommandTimeout = 180;
                int count = 0;

                callingCmdlet.WriteVerbose("Processing of nodes starting.");

                DateTime currentTime = DateTime.Now;

                WindowsIdentity currentUserIdentity = WindowsIdentity.GetCurrent();
                string currentUserName = "******";

                if (currentUserIdentity != null)
                {
                    currentUserName = currentUserIdentity.Name;
                }

                Guid newRootMapId = Guid.NewGuid();

                /// If it is the root map node, we want to assign it the pre-determined ID as we've been using this as the RootMapUid for everything;
                QueryMapNode sourceMapNode = originalNodes[SourceMap.NodeId];

                Node newRootMapNode = new Node();
                newRootMapNode.NodeUid = newRootMapId;
                newRootMapNode.DomainUid = DestinationDomain.DomainId;
                newRootMapNode.NodeTypeUid = sourceMapNode.NodeTypeUid;
                newRootMapNode.RootMapUid = newRootMapId;
                newRootMapNode.Created = sourceMapNode.Created;
                newRootMapNode.Modified = currentTime;
                newRootMapNode.CreatedBy = sourceMapNode.CreatedBy;
                newRootMapNode.ModifiedBy = currentUserName;

                newNodeIds[sourceMapNode.NodeUid] = newRootMapNode.NodeUid;

                dataContext.Nodes.InsertOnSubmit(newRootMapNode);

                /// Iterate through all nodes in memory and change their node IDs and root map IDs so that they won't clash with the map from where they were copied.
                foreach (QueryMapNode memoryNode in originalNodes.Values)
                {
                    if (memoryNode.NodeUid == SourceMap.NodeId)
                    {
                        continue;
                    }

                    count++;

                    if (count % 10 == 0)
                    {
                        callingCmdlet.WriteVerbose(count + " / " + totalNodes + " Nodes Completed");
                    }

                    Node node = new Node();
                    node.NodeUid = Guid.NewGuid();
                    node.DomainUid = DestinationDomain.DomainId;
                    node.NodeTypeUid = memoryNode.NodeTypeUid;
                    node.RootMapUid = newRootMapId;
                    node.Created = memoryNode.Created;
                    node.Modified = currentTime;
                    node.CreatedBy = memoryNode.CreatedBy;
                    node.ModifiedBy = currentUserName;

                    newNodeIds[memoryNode.NodeUid] = node.NodeUid;

                    dataContext.Nodes.InsertOnSubmit(node);
                }

                if (!newNodeIds.ContainsKey(SourceMap.NodeId))
                {
                    callingCmdlet.WriteWarning("Unexpected scenario. The source map NodeId can't be found in the list of copied nodes.");

                    return;
                }

                callingCmdlet.WriteVerbose("Committing nodes starting.");

                /// Commit node additions.
                dataContext.SubmitChanges();

                callingCmdlet.WriteVerbose("Committing nodes completed.");

                count = 0;

                callingCmdlet.WriteVerbose("Processing of relationships starting.");

                foreach (QueryMapRelationship memoryRelationship in originalRelationships)
                {
                    count++;

                    if (count % 10 == 0)
                    {
                        callingCmdlet.WriteVerbose(count + " / " + totalRelationships + " Relationships Completed");
                    }

                    Relationship relationship = new Relationship();
                    relationship.RelationshipUid = Guid.NewGuid();
                    relationship.DomainUid = DestinationDomain.DomainId;
                    relationship.RelationshipTypeUid = memoryRelationship.RelationshipTypeUid;
                    relationship.RootMapUid = newRootMapId;
                    relationship.Created = memoryRelationship.Created;
                    relationship.Modified = currentTime;
                    relationship.CreatedBy = memoryRelationship.CreatedBy;
                    relationship.ModifiedBy = currentUserName;

                    newRelationshipIds[memoryRelationship.RelationshipUid] = relationship.RelationshipUid;

                    dataContext.Relationships.InsertOnSubmit(relationship);
                }

                callingCmdlet.WriteVerbose("Committing relationships starting.");

                /// Commit relationship additions.
                dataContext.SubmitChanges();

                callingCmdlet.WriteVerbose("Committing relationships completed.");

                count = 0;

                callingCmdlet.WriteVerbose("Processing of descriptors starting.");

                foreach (QueryMapDescriptor memoryDescriptor in originalDescriptors)
                {
                    count++;

                    if (count % 10 == 0)
                    {
                        callingCmdlet.WriteVerbose(count + " / " + totalDescriptors + " Descriptors Completed");
                    }

                    if (newNodeIds.ContainsKey(memoryDescriptor.NodeUid.Value) && newRelationshipIds.ContainsKey(memoryDescriptor.RelationshipUid.Value))
                    {
                        Descriptor descriptor = new Descriptor();
                        descriptor.DescriptorUid = Guid.NewGuid();
                        descriptor.NodeUid = newNodeIds[memoryDescriptor.NodeUid.Value];
                        descriptor.RelationshipUid = newRelationshipIds[memoryDescriptor.RelationshipUid.Value];
                        descriptor.DescriptorTypeUid = memoryDescriptor.DescriptorTypeUid;

                        dataContext.Descriptors.InsertOnSubmit(descriptor);
                    }
                }

                callingCmdlet.WriteVerbose("Committing descriptors starting.");

                /// Commit descriptor additions.
                dataContext.SubmitChanges();

                callingCmdlet.WriteVerbose("Committing descriptors completed.");

                count = 0;

                callingCmdlet.WriteVerbose("Processing of metadata starting.");

                foreach (QueryMapMetadata memoryMetadatum in originalMetadata)
                {
                    count++;

                    if (count % 10 == 0)
                    {
                        callingCmdlet.WriteVerbose(count + " / " + totalMetadata + " Metadata Completed");
                    }

                    if (memoryMetadatum.NodeUid == null || newNodeIds.ContainsKey(memoryMetadatum.NodeUid.Value))
                    {
                        if (memoryMetadatum.RelationshipUid == null || newRelationshipIds.ContainsKey(memoryMetadatum.RelationshipUid.Value))
                        {
                            Metadata metadatum = new Metadata();
                            metadatum.MetadataId = Guid.NewGuid();

                            if (memoryMetadatum.NodeUid != null)
                            {
                                metadatum.NodeUid = newNodeIds[memoryMetadatum.NodeUid.Value];
                            }
                            else
                            {
                                metadatum.NodeUid = null;
                            }

                            if (memoryMetadatum.RelationshipUid != null)
                            {
                                metadatum.RelationshipUid = newRelationshipIds[memoryMetadatum.RelationshipUid.Value];
                            }
                            else
                            {
                                metadatum.RelationshipUid = null;
                            }

                            metadatum.DescriptorTypeUid = memoryMetadatum.DescriptorTypeUid;
                            metadatum.MetadataTypeUid = memoryMetadatum.MetadataTypeUid;
                            metadatum.MetadataName = memoryMetadatum.MetadataName;
                            metadatum.MetadataValue = memoryMetadatum.MetadataValue;
                            metadatum.DomainUid = DestinationDomain.DomainId;
                            metadatum.RootMapUid = newRootMapId;
                            metadatum.Created = memoryMetadatum.Created;
                            metadatum.Modified = currentTime;
                            metadatum.CreatedBy = memoryMetadatum.CreatedBy;
                            metadatum.ModifiedBy = currentUserName;

                            dataContext.Metadatas.InsertOnSubmit(metadatum);
                        }
                    }
                }

                callingCmdlet.WriteVerbose("Committing metadata starting.");

                /// Commit metadata additions.
                dataContext.SubmitChanges();

                callingCmdlet.WriteVerbose("Committing metadata completed.");

                callingCmdlet.WriteVerbose("Connecting map to domain.");

                Relationship domainNodeMapRelationship = new Relationship();
                domainNodeMapRelationship.RelationshipUid = Guid.NewGuid();
                domainNodeMapRelationship.DomainUid = DestinationDomain.DomainId;
                domainNodeMapRelationship.RelationshipTypeUid = new Guid("4AFF46D7-87BE-48DD-B703-A93E38EF8FFB");
                domainNodeMapRelationship.RootMapUid = newRootMapId;
                domainNodeMapRelationship.Created = currentTime;
                domainNodeMapRelationship.Modified = currentTime;
                domainNodeMapRelationship.CreatedBy = currentUserName;
                domainNodeMapRelationship.ModifiedBy = currentUserName;

                Descriptor domainNodeMapFromDescriptor = new Descriptor();
                domainNodeMapFromDescriptor.DescriptorUid = Guid.NewGuid();
                domainNodeMapFromDescriptor.DescriptorTypeUid = new Guid("96DA1782-058C-4F9B-BB1A-31B048F8C75A");
                domainNodeMapFromDescriptor.NodeUid = newNodeIds[SourceMap.NodeId];
                domainNodeMapFromDescriptor.RelationshipUid = domainNodeMapRelationship.RelationshipUid;

                Descriptor domainNodeMapToDescriptor = new Descriptor();
                domainNodeMapToDescriptor.DescriptorUid = Guid.NewGuid();
                domainNodeMapToDescriptor.DescriptorTypeUid = new Guid("07C91D35-4DAC-431B-966B-64C924B8CDAB");
                domainNodeMapToDescriptor.NodeUid = DestinationDomain.NodeId;
                domainNodeMapToDescriptor.RelationshipUid = domainNodeMapRelationship.RelationshipUid;

                dataContext.Relationships.InsertOnSubmit(domainNodeMapRelationship);
                dataContext.Descriptors.InsertOnSubmit(domainNodeMapFromDescriptor);
                dataContext.Descriptors.InsertOnSubmit(domainNodeMapToDescriptor);

                dataContext.SubmitChanges();

                callingCmdlet.WriteVerbose("Completed connecting map to domain.");

                callingCmdlet.WriteVerbose("Copy completed.");
            }
        }