public RelatedNodesSearchResult GetNodesXmlQuery(string graphQueryXml)
        {
            GraphQuery graphQuery = null;

            try
            {
                XmlSerializer graphQueryXmlSerializer = new XmlSerializer(typeof(GraphQuery));
                StringReader graphQueryXmlReader = new StringReader(graphQueryXml);

                graphQuery = graphQueryXmlSerializer.Deserialize(graphQueryXmlReader) as GraphQuery;
            }
            finally
            {
                if (graphQuery == null)
                {
                    LoggingService.WriteTrace(LoggingService.Categories.WcfServices, TraceSeverity.Unexpected, "Graph Query XML is not in the correct format.");
                    throw new NotSupportedException("XML is not in the correct format.");
                }
            }

            RelatedNodesSearchResult nodesSearchResult = null;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (MappingToolDatabaseDataContext mappingDb = new MappingToolDatabaseDataContext())
                {
                    Guid startingNodeId;
                    List<Node> capturedNodes;

                    if (graphQuery.Context.NodeId == Guid.Empty)
                    {
                        var allMapNodes = from dbNode in mappingDb.Nodes where dbNode.DomainUid == graphQuery.Context.DomainId select dbNode;
                        capturedNodes = allMapNodes.ToList();
                        startingNodeId = Guid.Empty;
                    }
                    else
                    {
                        var startingNodes = from dbNode in mappingDb.Nodes where dbNode.DomainUid == graphQuery.Context.DomainId && dbNode.NodeUid == graphQuery.Context.NodeId select dbNode;

                        if (startingNodes.Count() <= 0)
                        {
                            throw new ArgumentOutOfRangeException("The given node context doesn't exist.");
                        }

                        var startingNode = startingNodes.First();
                        startingNodeId = startingNode.NodeUid;

                        capturedNodes = RecurseThroughGraph(graphQuery, startingNode, 1);
                        capturedNodes.Add(startingNode);
                    }

                    IEnumerable<Node> filteredNodes = graphQuery.Filters.ApplyFilters(mappingDb, capturedNodes);

                    Dictionary<Guid, SoapNode> soapNodes = new Dictionary<Guid, SoapNode>();

                    int count = 0;

                    foreach (Node node in filteredNodes)
                    {
                        count++;

                        if (count > graphQuery.Items && graphQuery.Items > 0)
                        {
                            break;
                        }

                        if (!soapNodes.ContainsKey(node.NodeUid))
                        {
                            SoapNode soapNode = node.ToSoapObject();
                            soapNodes.Add(soapNode.Id, soapNode);
                        }
                    }

                    nodesSearchResult = new RelatedNodesSearchResult();
                    nodesSearchResult.Nodes = soapNodes;
                    nodesSearchResult.SearchedNode = startingNodeId;
                }
            });
            return nodesSearchResult;
        }
        public RelatedNodesSearchResult GetMapsNodes(Guid domainId, Guid mapNodeId)
        {
            RelatedNodesSearchResult result = null;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (MappingToolDatabaseDataContext mappingDb = new MappingToolDatabaseDataContext())
                {
                    var matchingMapId = from dbNode
                                        in mappingDb.Nodes
                                        where dbNode.DomainUid == domainId && dbNode.NodeUid == mapNodeId
                                        select dbNode;
                    Dictionary<Guid, SoapNode> nodes = new Dictionary<Guid, SoapNode>();

                    if (matchingMapId.Count() > 0)
                    {
                        var mapNode = matchingMapId.First();

                        IEnumerable<Node> relatedNodes = mapNode.GetConnectedNodes(
                            (Descriptor localDescriptor) => (localDescriptor.DescriptorType.DescriptorTypeName == "To") || (localDescriptor.DescriptorType.DescriptorTypeName == "TransclusionMap"),
                            (Relationship relationship) => relationship.RelationshipType.RelationshipTypeName != "FromToRelationship");

                        foreach (Node relatedNode in relatedNodes)
                        {
                            SoapNode soapNode = relatedNode.ToSoapObject();

                            if (ContainsMetadataKeyName(soapNode, "XPosition") && ContainsMetadataKeyName(soapNode, "YPosition"))
                            {
                                if (soapNode.NodeType.Name != "DomainNode")
                                {
                                    nodes.Add(soapNode.Id, soapNode);
                                }
                            }
                        }
                    }
                    result = new RelatedNodesSearchResult() { Nodes = nodes, SearchedNode = mapNodeId };
                }
            });
            return result;
        }
        public RelatedNodesSearchResult GetNodesWithConditions(Guid domainId, Guid nodeId, int maxDepth, List<Condition> conditions)
        {
            RelatedNodesSearchResult result = null;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (MappingToolDatabaseDataContext mappingDb = new MappingToolDatabaseDataContext())
                {
                    var matchingFocusedNodes = from dbNode
                                               in mappingDb.Nodes
                                               where dbNode.DomainUid == domainId && dbNode.NodeUid == nodeId
                                               select dbNode;

                    Dictionary<Guid, SoapNode> nodes = new Dictionary<Guid, SoapNode>();

                    if (matchingFocusedNodes.Count() > 0)
                    {
                        var focusedNode = matchingFocusedNodes.First();

                        RecurseThroughLevels(nodes, domainId, focusedNode, focusedNode, maxDepth, conditions);

                        evaluatedNodes.Clear();
                        excludedNodes.Clear();
                    }

                    result = new RelatedNodesSearchResult() { Nodes = nodes, SearchedNode = nodeId };    
                }
            });
            return result;
        }
        public RelatedNodesSearchResult GetDomainMapNodes(Guid domainId, int page, int pageSize)
        {
            RelatedNodesSearchResult mapNodesResult = null;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (MappingToolDatabaseDataContext mappingDb = new MappingToolDatabaseDataContext())
                {
                    Guid domainNodesId = GetDomainNodeId(domainId);
                    var mapNodes = from dbNode in mappingDb.Nodes
                                    where dbNode.NodeType.NodeTypeName == "CompendiumMapNode" && dbNode.DomainUid == domainId
                                    select dbNode;

                    List<Node> mapNodeResults = new List<Node>();
                    foreach (Node mapNode in mapNodes) 
                    {
                        foreach (Descriptor descriptor in mapNode.Descriptors)
                        {
                            if (descriptor.Relationship.RelationshipType.RelationshipTypeName == "MapContainerRelationship" &&
                                descriptor.DescriptorType.DescriptorTypeName == "From")
                            {
                                foreach (Descriptor altDescriptor in descriptor.Relationship.Descriptors.Where(de => de.DescriptorUid != descriptor.DescriptorUid))
                                {
                                    if (altDescriptor.NodeUid == domainNodesId && !mapNodeResults.Contains(descriptor.Node))
                                    {
                                        mapNodeResults.Add(descriptor.Node);
                                    }
                                }
                            }
                        }
                    }
                    var mapNodes1 = mapNodeResults.Skip((page - 1) * pageSize).Take(pageSize);

                    mapNodesResult = new RelatedNodesSearchResult(domainId, mapNodes1);
                }
            });
            return mapNodesResult;
        }