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 SoapRelationship ToSoapObject(this Relationship relationship) { if (relationship != null) { SoapRelationship soapRelationship = new SoapRelationship(); soapRelationship.Id = relationship.RelationshipUid; if (relationship.RelationshipTypeUid == null || !relationship.RelationshipTypeUid.HasValue) { throw new NullReferenceException("There is no Relationship Type for this relationship."); } soapRelationship.RelationshipType = (SoapRelationshipType)SoapTypeFactory.GetSoapType<SoapRelationshipType>(relationship.RelationshipTypeUid.Value); soapRelationship.Relationship = relationship; var allMetadata = relationship.Metadatas.OrderBy(x => (x.DescriptorTypeUid != null) ? x.DescriptorTypeUid : Guid.Empty); foreach (var datum in allMetadata) { 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[datum.MetadataName] = soapDatum; } foreach (var descriptor in relationship.Descriptors) { SoapDescriptorType soapDescriptor = (SoapDescriptorType)SoapTypeFactory.GetSoapType<SoapDescriptorType>(descriptor.DescriptorTypeUid.Value); soapRelationship.Nodes.Add(soapDescriptor, descriptor.NodeUid.Value); } return soapRelationship; } return null; }
public static SoapRelationship ToSoapObject(this Relationship relationship) { if (relationship != null) { SoapRelationship soapRelationship = new SoapRelationship(); soapRelationship.Id = relationship.RelationshipUid; if (relationship.RelationshipTypeUid == null || !relationship.RelationshipTypeUid.HasValue) { throw new NullReferenceException("There is no Relationship Type for this relationship."); } soapRelationship.RelationshipType = (SoapRelationshipType)SoapTypeFactory.GetSoapType <SoapRelationshipType>(relationship.RelationshipTypeUid.Value); soapRelationship.Relationship = relationship; var allMetadata = relationship.Metadatas.OrderBy(x => (x.DescriptorTypeUid != null) ? x.DescriptorTypeUid : Guid.Empty); foreach (var datum in allMetadata) { 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[datum.MetadataName] = soapDatum; } foreach (var descriptor in relationship.Descriptors) { SoapDescriptorType soapDescriptor = (SoapDescriptorType)SoapTypeFactory.GetSoapType <SoapDescriptorType>(descriptor.DescriptorTypeUid.Value); soapRelationship.Nodes.Add(soapDescriptor, descriptor.NodeUid.Value); } return(soapRelationship); } return(null); }
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; }
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; }
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); } } } } } } } } }