Example #1
0
        public static SoapNode ToSoapObject(this Node node)
        {
            SoapNode soapNode = new SoapNode();

            if (node.DomainUid == null || !node.DomainUid.HasValue)
            {
                throw new NullReferenceException("There is no Domain ID for this node.");
            }

            soapNode.Domain = node.DomainUid.Value;
            soapNode.Id     = node.NodeUid;

            if (node.NodeTypeUid == null || !node.NodeTypeUid.HasValue)
            {
                throw new NullReferenceException("There is no Node Type for this node.");
            }

            soapNode.NodeType = (SoapNodeType)SoapTypeFactory.GetSoapType <SoapNodeType>(node.NodeTypeUid.Value);
            soapNode.Node     = node;

            var allMetadata = node.Metadatas.OrderBy(x => (x.RelationshipUid != null) ? x.RelationshipUid : Guid.Empty).OrderBy(x => (x.DescriptorTypeUid != null) ? x.DescriptorTypeUid : Guid.Empty);

            foreach (var datum in allMetadata)
            {
                MetadataContext key = new MetadataContext()
                {
                    MetadataName      = datum.MetadataName,
                    NodeUid           = datum.NodeUid,
                    RelationshipUid   = datum.RelationshipUid,
                    DescriptorTypeUid = datum.DescriptorTypeUid
                };

                SoapMetadata soapDatum = new SoapMetadata();
                soapDatum.MetadataName  = datum.MetadataName;
                soapDatum.MetadataValue = datum.MetadataValue;

                if (datum.MetadataTypeUid.HasValue)
                {
                    soapDatum.MetadataType = (SoapMetadataType)SoapTypeFactory.GetSoapType <SoapMetadataType>(datum.MetadataTypeUid.Value);
                }

                soapNode.Metadata[key] = soapDatum;
            }

            foreach (Descriptor localDescriptor in node.Descriptors)
            {
                SoapRelationship relationship = localDescriptor.Relationship.ToSoapObject();

                if (relationship != null && !soapNode.Relationships.ContainsKey(relationship.Id))
                {
                    soapNode.Relationships.Add(relationship.Id, relationship);
                }
            }

            return(soapNode);
        }
        public static SoapNode ToSoapObject(this Node node)
        {
            SoapNode soapNode = new SoapNode();

            if (node.DomainUid == null || !node.DomainUid.HasValue)
            {
                throw new NullReferenceException("There is no Domain ID for this node.");
            }

            soapNode.Domain = node.DomainUid.Value;
            soapNode.Id = node.NodeUid;

            if (node.NodeTypeUid == null || !node.NodeTypeUid.HasValue)
            {
                throw new NullReferenceException("There is no Node Type for this node.");
            }

            soapNode.NodeType = (SoapNodeType)SoapTypeFactory.GetSoapType<SoapNodeType>(node.NodeTypeUid.Value);
            soapNode.Node = node;

            var allMetadata = node.Metadatas.OrderBy(x => (x.RelationshipUid != null) ? x.RelationshipUid : Guid.Empty).OrderBy(x => (x.DescriptorTypeUid != null) ? x.DescriptorTypeUid : Guid.Empty);

            foreach (var datum in allMetadata)
            {
                MetadataContext key = new MetadataContext() { 
                    MetadataName = datum.MetadataName,
                    NodeUid = datum.NodeUid, 
                    RelationshipUid = datum.RelationshipUid, 
                    DescriptorTypeUid = datum.DescriptorTypeUid };

                SoapMetadata soapDatum = new SoapMetadata();
                soapDatum.MetadataName = datum.MetadataName;
                soapDatum.MetadataValue = datum.MetadataValue;

                if (datum.MetadataTypeUid.HasValue)
                {
                    soapDatum.MetadataType = (SoapMetadataType)SoapTypeFactory.GetSoapType<SoapMetadataType>(datum.MetadataTypeUid.Value);
                }

                soapNode.Metadata[key] = soapDatum;
            }

            foreach (Descriptor localDescriptor in node.Descriptors)
            {
                SoapRelationship relationship = localDescriptor.Relationship.ToSoapObject();

                if (relationship != null && !soapNode.Relationships.ContainsKey(relationship.Id))
                {
                    soapNode.Relationships.Add(relationship.Id, relationship);
                }
            }

            return soapNode;
        }
        public Dictionary<Guid, SoapNode> GetAllNodes(Guid domainId)
        {
            Dictionary<Guid, SoapNode> nodes = new Dictionary<Guid, SoapNode>();
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (MappingToolDatabaseDataContext mappingDb = new MappingToolDatabaseDataContext())
                {
                    var matchingNodes = from dbNode
                                        in mappingDb.Nodes
                                        where dbNode.DomainUid == domainId
                                        select dbNode;

                    foreach (var node in matchingNodes)
                    {
                        SoapNode soapNode = new SoapNode();
                        soapNode.Domain = domainId;
                        soapNode.Id = node.NodeUid;
                        soapNode.NodeType = (SoapNodeType)SoapTypeFactory.GetSoapType<SoapNodeType>(node.NodeTypeUid.Value);

                        /// Get the relationships for the node
                        /// 

                        foreach (var originatingDescriptor in node.Descriptors)
                        {
                            Relationship relationship = originatingDescriptor.Relationship;

                            var relationshipMetadata = from dbDatum
                                              in relationship.Metadatas
                                                       where !dbDatum.NodeUid.HasValue && !dbDatum.DescriptorTypeUid.HasValue
                                                       select dbDatum;

                            SoapRelationship soapRelationship = new SoapRelationship();
                            soapRelationship.Id = relationship.RelationshipUid;
                            soapRelationship.RelationshipType = (SoapRelationshipType)SoapTypeFactory.GetSoapType<SoapRelationshipType>(relationship.RelationshipTypeUid.Value);

                            foreach (var datum in relationshipMetadata)
                            {
                                SoapMetadata soapDatum = new SoapMetadata();
                                soapDatum.MetadataName = datum.MetadataName;
                                soapDatum.MetadataValue = datum.MetadataValue;

                                if (datum.MetadataTypeUid.HasValue)
                                {
                                    soapDatum.MetadataType = (SoapMetadataType)SoapTypeFactory.GetSoapType<SoapMetadataType>(datum.MetadataTypeUid.Value);
                                }

                                soapRelationship.Metadata.Add(datum.MetadataName, soapDatum);
                            }

                            foreach (var destinationDescriptor in relationship.Descriptors)
                            {
                                if (destinationDescriptor.DescriptorUid == originatingDescriptor.DescriptorUid)
                                {
                                    continue;
                                }

                                if (nodes.ContainsKey(destinationDescriptor.Node.NodeUid))
                                {
                                    continue;
                                }

                                Node childNode = destinationDescriptor.Node;

                                /// Connect the node to the relationships.
                                /// 

                                if (!destinationDescriptor.DescriptorTypeUid.HasValue)
                                {
                                    throw new NotSupportedException("The descriptor doesn't have a type ID.");
                                }

                                SoapDescriptorType soapDescriptorType = (SoapDescriptorType)SoapTypeFactory.GetSoapType<SoapDescriptorType>(destinationDescriptor.DescriptorTypeUid.Value);
                                soapRelationship.Nodes.Add(soapDescriptorType, childNode.NodeUid);
                            }

                            soapNode.Relationships.Add(soapRelationship.Id, soapRelationship);
                        }

                        /// Get the metadata for the node according to the current context.
                        /// 
                        var metadata = from dbDatum in node.Metadatas select dbDatum;

                        foreach (var datum in metadata.OrderBy(x => (x.RelationshipUid != null) ? x.RelationshipUid : Guid.Empty).OrderBy(x => (x.DescriptorTypeUid != null) ? x.DescriptorTypeUid : Guid.Empty))
                        {
                            MetadataContext key = new MetadataContext()
                            {
                                MetadataName = datum.MetadataName,
                                NodeUid = datum.NodeUid,
                                RelationshipUid = datum.RelationshipUid,
                                DescriptorTypeUid = datum.DescriptorTypeUid
                            };

                            SoapMetadata soapDatum = new SoapMetadata();
                            soapDatum.MetadataName = datum.MetadataName;
                            soapDatum.MetadataValue = datum.MetadataValue;

                            if (datum.MetadataTypeUid.HasValue)
                            {
                                soapDatum.MetadataType = (SoapMetadataType)SoapTypeFactory.GetSoapType<SoapMetadataType>(datum.MetadataTypeUid.Value);
                            }

                            soapNode.Metadata.Add(key, soapDatum);
                        }
                        /// End getting metadata.
                        /// 

                        nodes.Add(soapNode.Id, soapNode);
                    }
                }
            });
            return nodes;
        }
 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);
     }
 }
        private void RecurseThroughLevels(Dictionary<Guid, SoapNode> nodes, Guid domainId, Node currentNode, Node initialSearchNode, int depth, List<Condition> conditions)
        {
            SoapNode currentSoapNode;

            if (!nodes.ContainsKey(currentNode.NodeUid))
            {
                currentSoapNode = new SoapNode();
                currentSoapNode.Id = currentNode.NodeUid;
                currentSoapNode.Domain = domainId;

                if (!currentNode.NodeTypeUid.HasValue)
                {
                    throw new NotSupportedException("The node doesn't have a type ID.");
                }

                currentSoapNode.NodeType = (SoapNodeType)SoapTypeFactory.GetSoapType<SoapNodeType>(currentNode.NodeTypeUid.Value);

                if (currentSoapNode.NodeType.Name != "DomainNode")
                {
                    /// Get the metadata for the child node according to the current context.
                    var currentNodeMetadata = from dbDatum in currentNode.Metadatas select dbDatum;

                    foreach (var datum in currentNodeMetadata.OrderBy(x => (x.RelationshipUid != null) ? x.RelationshipUid : Guid.Empty).OrderBy(x => (x.DescriptorTypeUid != null) ? x.DescriptorTypeUid : Guid.Empty))
                    {
                        MetadataContext key = new MetadataContext()
                        {
                            MetadataName = datum.MetadataName,
                            NodeUid = datum.NodeUid,
                            RelationshipUid = datum.RelationshipUid,
                            DescriptorTypeUid = datum.DescriptorTypeUid
                        };

                        SoapMetadata soapDatum = new SoapMetadata();
                        soapDatum.MetadataName = datum.MetadataName;
                        soapDatum.MetadataValue = datum.MetadataValue;

                        if (datum.MetadataTypeUid.HasValue)
                        {
                            soapDatum.MetadataType = (SoapMetadataType)SoapTypeFactory.GetSoapType<SoapMetadataType>(datum.MetadataTypeUid.Value);
                        }

                        currentSoapNode.Metadata[key] = soapDatum;
                    }
                    /// End getting metadata.

                    //let the conditions determine if the node is added or not.
                    if (conditions == null)
                    {
                        nodes.Add(currentSoapNode.Id, currentSoapNode);
                    }
                }
            }
            else
            {
                currentSoapNode = nodes[currentNode.NodeUid];
            }

            foreach (var originatingDescriptor in currentNode.Descriptors)
            {
                Relationship relationship = originatingDescriptor.Relationship;

                var relationshipMetadata = from dbDatum
                                           in relationship.Metadatas
                                           where !dbDatum.NodeUid.HasValue && !dbDatum.DescriptorTypeUid.HasValue
                                           select dbDatum;

                SoapRelationship soapRelationship = new SoapRelationship();
                soapRelationship.Id = relationship.RelationshipUid;
                soapRelationship.RelationshipType = (SoapRelationshipType)SoapTypeFactory.GetSoapType<SoapRelationshipType>(relationship.RelationshipTypeUid.Value);

                foreach (var datum in relationshipMetadata)
                {
                    SoapMetadata soapDatum = new SoapMetadata();
                    soapDatum.MetadataName = datum.MetadataName;
                    soapDatum.MetadataValue = datum.MetadataValue;

                    if (datum.MetadataTypeUid.HasValue)
                    {
                        soapDatum.MetadataType = (SoapMetadataType)SoapTypeFactory.GetSoapType<SoapMetadataType>(datum.MetadataTypeUid.Value);
                    }

                    soapRelationship.Metadata.Add(datum.MetadataName, soapDatum);
                }

                SoapDescriptorType soapOriginatingDescriptorType = (SoapDescriptorType)SoapTypeFactory.GetSoapType<SoapDescriptorType>(originatingDescriptor.DescriptorTypeUid.Value);
                soapRelationship.Nodes.Add(soapOriginatingDescriptorType, currentNode.NodeUid);

                if (!currentSoapNode.Relationships.ContainsKey(soapRelationship.Id))
                {
                    currentSoapNode.Relationships.Add(soapRelationship.Id, soapRelationship);
                }

                foreach (var destinationDescriptor in relationship.Descriptors)
                {
                    if (destinationDescriptor.DescriptorUid == originatingDescriptor.DescriptorUid)
                    {
                        continue; //original end of the relationship
                    }

                    if (nodes.ContainsKey(destinationDescriptor.Node.NodeUid))
                    {
                        continue; //already know about this node
                    }

                    Node childNode = destinationDescriptor.Node;

                    /// Connect the parent node to the relationships.
                    if (!destinationDescriptor.DescriptorTypeUid.HasValue)
                    {
                        throw new NotSupportedException("The descriptor doesn't have a type ID.");
                    }

                    SoapDescriptorType soapDestinationDescriptorType = (SoapDescriptorType)SoapTypeFactory.GetSoapType<SoapDescriptorType>(destinationDescriptor.DescriptorTypeUid.Value);
                    soapRelationship.Nodes.Add(soapDestinationDescriptorType, childNode.NodeUid);

                    SoapNode soapNode = new SoapNode();
                    soapNode.Id = childNode.NodeUid;
                    soapNode.Domain = domainId;

                    if (!soapNode.Relationships.ContainsKey(soapRelationship.Id))
                    {
                        soapNode.Relationships.Add(soapRelationship.Id, soapRelationship);
                    }

                    if (!childNode.NodeTypeUid.HasValue)
                    {
                        throw new NotSupportedException("The node doesn't have a type ID.");
                    }

                    soapNode.NodeType = (SoapNodeType)SoapTypeFactory.GetSoapType<SoapNodeType>(childNode.NodeTypeUid.Value);

                    /// Get the metadata for the child or parent node according to the current context.
                    /// 

                    var metadata = from dbDatum in childNode.Metadatas select dbDatum;

                    foreach (var datum in metadata.OrderBy(x => (x.RelationshipUid != null) ? x.RelationshipUid : Guid.Empty).OrderBy(x => (x.DescriptorTypeUid != null) ? x.DescriptorTypeUid : Guid.Empty))
                    {
                        MetadataContext key = new MetadataContext()
                        {
                            MetadataName = datum.MetadataName,
                            NodeUid = datum.NodeUid,
                            RelationshipUid = datum.RelationshipUid,
                            DescriptorTypeUid = datum.DescriptorTypeUid
                        };

                        SoapMetadata soapDatum = new SoapMetadata();
                        soapDatum.MetadataName = datum.MetadataName;
                        soapDatum.MetadataValue = datum.MetadataValue;

                        if (datum.MetadataTypeUid.HasValue)
                        {
                            soapDatum.MetadataType = (SoapMetadataType)SoapTypeFactory.GetSoapType<SoapMetadataType>(datum.MetadataTypeUid.Value);
                        }

                        soapNode.Metadata[key] = soapDatum;
                    }

                    /// End getting metadata.
                    /// 

                    if (depth >= 0 && conditions == null)
                    {
                        // Add the child or parent node to the collection
                        nodes.Add(destinationDescriptor.Node.NodeUid, soapNode);

                        if (depth > 0) //don't recurse any further
                        {
                            int nextDepthLevel = depth - 1;

                            RecurseThroughLevels(nodes, domainId, destinationDescriptor.Node, initialSearchNode, nextDepthLevel, conditions);
                        }
                    }
                    else if (conditions != null)
                    {
                        //Map equality test code for debugging - doesn't work correctly
                        //MapEqualityMatch mapMatch = new MapEqualityMatch();
                        //mapMatch.Evaluate(initialSearchNode, destinationDescriptor.Node, destinationDescriptor.Relationship, destinationDescriptor);
                        bool nodeAlreadyAdded = false;
                        bool nodeExcludedAlready = false;
                        if (!evaluatedNodes.Contains(destinationDescriptor.Node.NodeUid.ToString()))
                        {
                            foreach (Condition conditionRule in conditions)
                            {
                                //no need to evaluate already excluded nodes against any conditions
                                if (!excludedNodes.Contains(destinationDescriptor.Node.NodeUid.ToString()))
                                {
                                    ConditionResult conditionResult = conditionRule.Evaluate(initialSearchNode, destinationDescriptor.Node, destinationDescriptor.Relationship, destinationDescriptor);
                                    if (!evaluatedNodes.Contains(destinationDescriptor.Node.NodeUid.ToString()))
                                    {
                                        evaluatedNodes.Add(destinationDescriptor.Node.NodeUid.ToString());
                                    }
                                    if (conditionResult.Value)
                                    {
                                        if (!conditionResult.IncludeNode)
                                        {
                                            //if the node isn't to be included and the rule matches remove it
                                            excludedNodes.Add(destinationDescriptor.Node.NodeUid.ToString());
                                            nodeExcludedAlready = true;
                                            if (nodeAlreadyAdded)
                                            {
                                                nodes.Remove(destinationDescriptor.Node.NodeUid);
                                            }
                                        }
                                        if (conditionResult.IncludeNode && !nodeAlreadyAdded && !nodeExcludedAlready && soapNode.NodeType.Name != "DomainNode")
                                        {
                                            //if the node is to be included, hasn't matched an exclusion rule and isn't the domain node add it
                                            nodes.Add(destinationDescriptor.Node.NodeUid, soapNode);
                                            nodeAlreadyAdded = true;
                                        }
                                        if (conditionResult.Action == Action.Continue)
                                        {
                                            //keep looking through the related nodes
                                            RecurseThroughLevels(nodes, domainId, destinationDescriptor.Node, initialSearchNode, -1, conditions);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        public DeleteResult DeleteMetadata(MetadataContext context)
        {
            DeleteResult result = new DeleteResult() { DeleteSuccessful = false, DeletedId = Guid.Empty };
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (MappingToolDatabaseDataContext mappingDb = new MappingToolDatabaseDataContext())
                {
                    if ((context.RelationshipUid != null && context.RelationshipUid.HasValue) && 
                        (context.DescriptorTypeUid != null && context.DescriptorTypeUid.HasValue))
                    {
                        var deleteMetadatas = from metadata in mappingDb.Metadatas
                                          where metadata.NodeUid.Value == context.NodeUid.Value
                                          && metadata.DescriptorTypeUid.Value == context.DescriptorTypeUid.Value
                                          && metadata.RelationshipUid.Value == context.RelationshipUid.Value
                                          && metadata.MetadataName == context.MetadataName
                                          select metadata;
                        DeleteMetadata(context, result, mappingDb, deleteMetadatas);
                    }
                    else if ((context.RelationshipUid == null || !context.RelationshipUid.HasValue) && 
                        (context.DescriptorTypeUid != null || context.DescriptorTypeUid.HasValue))
                    {
                        var deleteMetadatas = from metadata in mappingDb.Metadatas
                                          where metadata.NodeUid.Value == context.NodeUid.Value
                                          && metadata.DescriptorTypeUid.Value == context.DescriptorTypeUid.Value
                                          && metadata.MetadataName == context.MetadataName
                                          select metadata;
                        DeleteMetadata(context, result, mappingDb, deleteMetadatas);
                    }
                    else if ((context.RelationshipUid != null || context.RelationshipUid.HasValue) &&
                        (context.DescriptorTypeUid == null || !context.DescriptorTypeUid.HasValue))
                    {
                        var deleteMetadatas = from metadata in mappingDb.Metadatas
                                          where metadata.NodeUid.Value == context.NodeUid.Value
                                          && metadata.RelationshipUid.Value == context.RelationshipUid.Value
                                          && metadata.MetadataName == context.MetadataName
                                          select metadata;
                        DeleteMetadata(context, result, mappingDb, deleteMetadatas);
                    }
                    else
                    {
                        var deleteMetadatas = from metadata in mappingDb.Metadatas
                                              where metadata.NodeUid.Value == context.NodeUid.Value
                                              && metadata.MetadataName == context.MetadataName
                                              select metadata;

                        DeleteMetadata(context, result, mappingDb, deleteMetadatas);
                    }

                    
                }
            });
            return result;
        }