/// <summary> /// Test if a URI string is valid /// </summary> /// <param name="uri">URI string</param> /// <returns>true iff valid, false otherwise</returns> public static bool IsValid(string uri) { try { _ = new Bo4eUri(uri); } catch (Exception) { return(false); } return(true); }
/// <summary> /// Get an URI for a Business Object /// </summary> /// <see cref="GetQueryObject"/> for the reverse operation. /// <param name="bo">Business Object</param> /// <param name="includeUserProperties">set true to add userProperties as query parameters</param> /// <returns>Bo4eUri</returns> /// call e.g. <see cref="System.Uri.GetComponents"/> or <see cref="System.Uri.ToString"/> on the returned object. public static Bo4eUri GetUri(BusinessObject bo, bool includeUserProperties = false) { if (bo == null) { throw new ArgumentNullException("Business Object must not be null."); } string baseUriString = BO4E_SCHEME + bo.GetType().Name + "/"; Bo4eUri baseUri = new Bo4eUri(baseUriString); string relativeUriString = string.Empty; foreach (PropertyInfo keyProp in GetKeyFields(bo)) { //relativeUriString += keyField.Name; // line is useful for debugging if (keyProp.GetValue(bo) != null) { if (keyProp.GetValue(bo).GetType() == typeof(string)) { if (keyProp.GetValue(bo).ToString() == string.Empty) { relativeUriString += NULL_KEY_PLACEHOLDER + "/"; } else { relativeUriString += keyProp.GetValue(bo) + "/"; } } else if (keyProp.GetValue(bo).GetType() == typeof(int)) { relativeUriString += keyProp.GetValue(bo).ToString() + "/"; } else if (keyProp.GetValue(bo) is Enum) { relativeUriString += keyProp.GetValue(bo).ToString() + "/"; } else if (keyProp.GetValue(bo).GetType().IsSubclassOf(typeof(BusinessObject))) { BusinessObject innerBo = (BusinessObject)keyProp.GetValue(bo); relativeUriString += GetUri(innerBo).GetComponents(UriComponents.Path, UriFormat.UriEscaped).ToString(); } else { throw new NotImplementedException($"Using {keyProp.GetValue(bo).GetType()} as [BoKey] is not supported yet."); } } else { // there must be some value for null, because if the key is a composite key // you could not distinguish which field was null. // Think of two Ansprechpartners: // - Günther Hermann but Günther is not set/null ==> /~/Hermann // - Hermann Maier but Maier is not set/null ==> /Hermann/~/ relativeUriString += NULL_KEY_PLACEHOLDER + "/"; } } if (includeUserProperties && bo.UserProperties != null && bo.UserProperties.Count > 0) { int n = 0; foreach (var up in bo.UserProperties) { if (n == 0) { relativeUriString += "?"; } else { relativeUriString += "&"; } relativeUriString += $"{up.Key}={up.Value}"; n += 1; } } Uri relativeUri = new Uri(Uri.EscapeUriString(relativeUriString), UriKind.Relative); if (TryCreate(baseUri, relativeUri, out Uri resultUri)) { return(new Bo4eUri(resultUri.AbsoluteUri)); } else { return(null); } }