public DSResource convertToDSResource(ResourceManagementClient client, ResourceObject resource, string[] loadedAttributes, bool includePermission, ResourceOption option, bool deepResolve = true) { DSResource dsResource = new DSResource { DisplayName = resource.DisplayName, ObjectID = resource.ObjectID.Value, ObjectType = resource.ObjectTypeName, HasPermissionHints = resource.HasPermissionHints }; List <RmAttribute> attributeDef = getAttributeDefinition(resource.ObjectTypeName, option.CultureKey, option.ConnectionInfo.EncryptionKey); Dictionary <string, DSAttribute> attributes = new Dictionary <string, DSAttribute>(); foreach (string attributeName in loadedAttributes) { if (resource.Attributes.ContainsAttribute(attributeName)) { AttributeValue attValue = resource.Attributes[attributeName]; if (attValue.Attribute.SystemName.Equals("ObjectID") || attValue.Attribute.SystemName.Equals("ObjectType")) { continue; } DSAttribute dsAttribute = new DSAttribute { Description = attValue.Attribute.Description, DisplayName = attValue.Attribute.DisplayName, IsMultivalued = attValue.Attribute.IsMultivalued, IsReadOnly = attValue.Attribute.IsReadOnly, IsRequired = attValue.Attribute.IsRequired, Regex = attValue.Attribute.Regex, SystemName = attValue.Attribute.SystemName, Type = attValue.Attribute.Type.ToString(), IsNull = attValue.IsNull, PermissionHint = attValue.PermissionHint.ToString(), Value = attValue.Value, Values = attValue.Values.ToList() }; if (attributeDef != null) { RmAttribute attr = attributeDef.FirstOrDefault(a => a.Name.Equals(attValue.AttributeName)); if (attr != null) { dsAttribute.DisplayName = attr.DisplayName; dsAttribute.Description = attr.Description; } } if (!dsAttribute.IsNull && dsAttribute.Type.Equals("Reference")) { dsAttribute.Value = attValue.Attribute.IsMultivalued ? attValue.StringValues.FirstOrDefault() : attValue.StringValue; dsAttribute.Values = attValue.Attribute.IsMultivalued ? attValue.StringValues.ToList <object>() : new List <object>() { attValue.StringValue }; if (!string.IsNullOrEmpty(dsAttribute.Value.ToString()) && deepResolve && option.ResolveID) { if (dsAttribute.IsMultivalued) { foreach (string value in attValue.StringValues) { ResourceObject resolvedObject = client.GetResource( value, option.AttributesToResolve, includePermission); dsAttribute.ResolvedValues.Add(convertToDSResource(client, resolvedObject, option.AttributesToResolve, includePermission, option, option.DeepResolve)); } } else { ResourceObject resolvedObject = client.GetResource( attValue.StringValue, option.AttributesToResolve, includePermission); dsAttribute.ResolvedValue = convertToDSResource(client, resolvedObject, option.AttributesToResolve, includePermission, option, option.DeepResolve); } } } attributes.Add(attValue.AttributeName, dsAttribute); } } dsResource.Attributes = attributes; return(dsResource); }
public static DSResource BuildFullResource(ResourceObject resourceObject, List <string> attributesToLoad, Dictionary <string, DSAttribute> schema, ResourceManagementClient client = null) { if (resourceObject == null) { throw new ArgumentException("resource object must be specified"); } if (attributesToLoad == null) { throw new ArgumentException("loading attributes must be specified"); } DSResource result = new DSResource(); if (!attributesToLoad.Contains("DisplayName")) { attributesToLoad.Add("DisplayName"); } if (!attributesToLoad.Contains("ObjectID")) { attributesToLoad.Add("ObjectID"); } if (!attributesToLoad.Contains("ObjectType")) { attributesToLoad.Add("ObjectType"); } foreach (string attributeName in attributesToLoad) { AttributeValue value = resourceObject.Attributes.FirstOrDefault(a => a.AttributeName.Equals(attributeName)); if (value == null || !schema.ContainsKey(attributeName)) { continue; } DSAttribute attributeSchema = schema.FirstOrDefault(s => s.Key.Equals(attributeName)).Value; if (value != null) { DSAttribute dsAttribute = new DSAttribute { DisplayName = attributeSchema.DisplayName, SystemName = attributeSchema.SystemName, Description = attributeSchema.Description, Multivalued = attributeSchema.Multivalued, Required = attributeSchema.Required, DataType = attributeSchema.DataType, StringRegex = attributeSchema.StringRegex, IntegerMaximum = attributeSchema.IntegerMaximum, IntegerMinimum = attributeSchema.IntegerMinimum, PermissionHint = value.PermissionHint.ToString() }; if (value.IsNull) { dsAttribute.Value = null; dsAttribute.Values = null; } else { if (value.Attribute.Type.ToString().Equals("Reference")) { if (client != null && !value.AttributeName.Equals("ObjectID")) { List <Dictionary <string, object> > refValues = new List <Dictionary <string, object> >(); List <string> ids = dsAttribute.Multivalued ? value.StringValues.ToList() : new List <string> { value.StringValue }; foreach (string refValue in ids) { ResourceObject refObject = client.GetResource(refValue, new string[] { "DisplayName" }); refValues.Add(new Dictionary <string, object> { { "DisplayName", refObject.DisplayName }, { "ObjectID", refObject.ObjectID.Value }, { "ObjectType", refObject.ObjectType.ToString() } }); } dsAttribute.Value = refValues.First(); dsAttribute.Values = refValues.ToList <object>(); } else { dsAttribute.Value = dsAttribute.Multivalued ? value.StringValues.First() : value.StringValue; dsAttribute.Values = dsAttribute.Multivalued ? value.StringValues.ToList <object>() : new List <object> { value.StringValue }; } } else { dsAttribute.Value = value.Value; dsAttribute.Values = value.Values.ToList(); } } result.Add(value.AttributeName, dsAttribute); } } return(result); }
public Dictionary <string, DSAttribute> GetSchema(string token, string typeName, string culture) { if (string.IsNullOrEmpty(typeName)) { throw new ArgumentException("type name must be specified"); } if (string.IsNullOrEmpty(culture)) { throw new ArgumentException("culture must be specified"); } string schemaToken = $"schema_{typeName.ToLower()}_{culture.ToLower()}"; if (this.schemaCache.Contains(schemaToken)) { schemaCache.TryGet(schemaToken, out Dictionary <string, DSAttribute> schemaType); return(schemaType); } else { Dictionary <string, DSAttribute> result = new Dictionary <string, DSAttribute>(); ResourceManagementClient client = Utiles.GetClient(this.schemaCache, token); SearchResultCollection srcBindings = client.GetResources( $"/BindingDescription[BoundObjectType=/ObjectTypeDescription[Name='{typeName}']]", new string[] { "DisplayName", "Description", "BoundAttributeType", "Required", "StringRegex", "IntegerMinimum", "IntegerMaximum" }, new CultureInfo(culture)) as SearchResultCollection; SearchResultCollection srcAttributes = client.GetResources( $"/BindingDescription[BoundObjectType=/ObjectTypeDescription[Name='{typeName}']]/BoundAttributeType", new string[] { "DisplayName", "Description", "Name", "DataType", "Multivalued", "StringRegex", "IntegerMinimum", "IntegerMaximum" }, new CultureInfo(culture)) as SearchResultCollection; if (srcBindings.Count == 0 || srcAttributes.Count == 0) { throw new ArgumentException("invalid type name"); } foreach (ResourceObject binding in srcBindings) { string attributeID = binding.Attributes["BoundAttributeType"].StringValue; ResourceObject attribute = srcAttributes.First( a => a.ObjectID.Value.Equals(attributeID, StringComparison.OrdinalIgnoreCase)); DSAttribute dsAttribute = new DSAttribute { DisplayName = string.IsNullOrEmpty(binding.DisplayName) ? attribute.DisplayName : binding.DisplayName, SystemName = attribute.Attributes["Name"].StringValue, DataType = attribute.Attributes["DataType"].StringValue, Multivalued = attribute.Attributes["Multivalued"].BooleanValue, Required = binding.Attributes["Required"].BooleanValue }; dsAttribute.Description = !binding.Attributes["Description"].IsNull ? binding.Attributes["Description"].StringValue : !attribute.Attributes["Description"].IsNull ? attribute.Attributes["Description"].StringValue : null; dsAttribute.StringRegex = !binding.Attributes["StringRegex"].IsNull ? binding.Attributes["StringRegex"].StringValue : !attribute.Attributes["StringRegex"].IsNull ? attribute.Attributes["StringRegex"].StringValue : null; if (!binding.Attributes["IntegerMaximum"].IsNull) { dsAttribute.IntegerMaximum = binding.Attributes["IntegerMaximum"].IntegerValue; } else if (!attribute.Attributes["IntegerMaximum"].IsNull) { dsAttribute.IntegerMaximum = attribute.Attributes["IntegerMaximum"].IntegerValue; } else { dsAttribute.IntegerMaximum = null; } if (!binding.Attributes["IntegerMinimum"].IsNull) { dsAttribute.IntegerMinimum = binding.Attributes["IntegerMinimum"].IntegerValue; } else if (!attribute.Attributes["IntegerMinimum"].IsNull) { dsAttribute.IntegerMinimum = attribute.Attributes["IntegerMinimum"].IntegerValue; } else { dsAttribute.IntegerMinimum = null; } result.Add(attribute.Attributes["Name"].StringValue, dsAttribute); } this.schemaCache.Set(schemaToken, result); return(result); } }