/// <summary> /// Tries to replace the segment in the ETP URI of the same object type with the specified segment. /// </summary> /// <param name="uri">The URI to create a new URI from with the segment replaced.</param> /// <param name="segment">The segment to replace the current URI segment with.</param> /// <param name="success"><c>true</c> if successful; <c>false</c> otherwise.</param> /// <returns>The input URI with the segment replaced or the original URI if not successful.</returns> public static EtpUri TryReplaceObjectTypeSegment(this EtpUri uri, EtpUri.Segment segment, out bool success) { success = false; if (!uri.IsValid || string.IsNullOrEmpty(segment.ObjectType)) { return(uri); } var constructedUri = uri.GetUriFamily(); foreach (var s in uri.GetObjectIds()) { if (!string.IsNullOrEmpty(s.ObjectType) && segment.ObjectType.EqualsIgnoreCase(s.ObjectType)) { constructedUri = constructedUri.Append(segment.ObjectType, segment.ObjectId); success = true; } else { constructedUri = constructedUri.Append(s.ObjectType, s.ObjectId); } } if (success) { return(new EtpUri(constructedUri.ToString() + uri.Query)); } else { return(uri); } }
/// <summary> /// Tries to get the ETP URI prefix up to the specified object type. /// </summary> /// <param name="uri">The URI to create the prefix from.</param> /// <param name="objecType">The object type to end the prefix with.</param> /// <param name="success"><c>true</c> if successful; <c>false</c> otherwise.</param> /// <returns>The prefix of the input URI or the original URI without its query if not successful.</returns> public static EtpUri TryGetObjectTypePrefix(this EtpUri uri, string objecType, out bool success) { success = false; uri = new EtpUri(uri.GetLeftPart()); if (!uri.IsValid || string.IsNullOrEmpty(objecType)) { return(uri); } var constructedUri = uri.GetUriFamily(); foreach (var s in uri.GetObjectIds()) { if (!string.IsNullOrEmpty(s.ObjectType) && objecType.EqualsIgnoreCase(s.ObjectType)) { constructedUri = constructedUri.Append(s.ObjectType, s.ObjectId); success = true; break; } else { constructedUri = constructedUri.Append(s.ObjectType, s.ObjectId); } } if (success) { return(constructedUri); } else { return(uri); } }
/// <summary> /// Gets the uri /// </summary> /// <param name="uri"></param> /// <param name="other"></param> /// <returns></returns> public static EtpUri GetResolvedHierarchyUri(this EtpUri uri, EtpUri other) { var resolvedUri = new EtpUri(); if (other.IsRootUri) { return(uri); } if (!uri.IsRelatedTo(other)) { return(resolvedUri); } var uriHierarchy = uri.GetObjectIds().ToList(); var otherHierarchy = other.GetObjectIds().ToList(); resolvedUri = uri.GetUriFamily(); var otherHierarchyMap = otherHierarchy .ToLookup(x => x.ObjectType, x => x.ObjectId, StringComparer.InvariantCultureIgnoreCase) .ToDictionary(x => x.Key, x => x.First(), StringComparer.InvariantCultureIgnoreCase); uriHierarchy.ForEach(segment => { var objectType = segment.ObjectType; var objectId = segment.ObjectId; if (string.IsNullOrWhiteSpace(objectId)) { otherHierarchyMap.TryGetValue(objectType, out objectId); } resolvedUri.Append(objectType, objectId); }); return(resolvedUri); }