/// <summary> /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>. /// </summary> /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param> /// <returns> /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false. /// </returns> /// <exception cref="T:System.NullReferenceException"> /// The <paramref name="obj"/> parameter is null. /// </exception> public override bool Equals(object obj) { RmAttributeName other = obj as RmAttributeName; if (other as Object == null) { return(false); } else { return(this.key.Equals(other.key)); } }
/// <summary> /// Return a string with the value of the attribute whose name is passed /// as format. /// If the attribute is multi valued, returns a string with the /// concatenation of the values separated by ';' /// </summary> /// <example> /// obj.ToString("DisplayName") -> returns DisplayName of the object. /// string.Format("{0:DisplayName}",obj) -> returns DisplayName of the object. /// </example> public string ToString( string format, IFormatProvider formatProvider) { if (string.IsNullOrEmpty(format)) { return this.ToString(); } RmAttributeName key = new RmAttributeName(format); if (!attributes.ContainsKey(key)) { return string.Empty; } RmAttributeValue value = attributes[key]; if (value.IsMultiValue) { // make an array of string values string[] values = value.Values.ConvertAll<string>(x => GetString(x)).ToArray(); return string.Join("; ", values); } return GetString(value.Value); }
private RmResource ConvertToResource(ExportObject exportObject) { var sourceObject = exportObject.ResourceManagementObject; var resource = new RmResource(); resource.ObjectType = sourceObject.ObjectType; foreach (var attribute in sourceObject.ResourceManagementAttributes) { var rmAttributeName = new RmAttributeName(attribute.AttributeName); var rmAttributeValue = attribute.IsMultiValue ? (RmAttributeValue)new RmAttributeValueMulti(attribute.Values) : (RmAttributeValue)new RmAttributeValueSingle(attribute.Value) ; if (rmAttributeValue.Value is string) { string s = (string)rmAttributeValue.Value; if (s.StartsWith("urn:uuid:")) { rmAttributeValue.Value = new RmReference(s); } } if (resource.ContainsKey(rmAttributeName)) { resource[rmAttributeName] = rmAttributeValue; } else { resource.Add(rmAttributeName, rmAttributeValue); } } return resource; }
/// <summary> /// Return a string with the value of the attribute whose name is passed /// as format. /// If the attribute is multi valued, returns a string with the /// concatenation of the values separated by ';' /// </summary> /// <example> /// obj.ToString("DisplayName") -> returns DisplayName of the object. /// string.Format("{0:DisplayName}",obj) -> returns DisplayName of the object. /// </example> public string ToString( string format, IFormatProvider formatProvider) { if (string.IsNullOrEmpty(format)) { return(this.ToString()); } RmAttributeName key = new RmAttributeName(format); if (!attributes.ContainsKey(key)) { return(string.Empty); } RmAttributeValue value = attributes[key]; if (value.IsMultiValue) { // make an array of string values string[] values = value.Values.ConvertAll <string>(x => GetString(x)).ToArray(); return(string.Join("; ", values)); } return(GetString(value.Value)); }
/// <summary> /// GetAttributeType /// </summary> /// <param name="attributeName">Name of the attribute.</param> /// <returns>The FIM type of the attribute if found, null otherwise.</returns> /// <remarks>This method is only for testing, and should not be used /// directly by clients.</remarks> internal RmAttributeType? GetAttributeType(RmAttributeName attributeName) { RmAttributeInfo retValue = null; RmAttributeCache.TryGetValue(attributeName, out retValue); if (retValue == null) { return null; } else { return retValue.AttributeType; } }
/// <summary> /// Creates an indexable cache of Resource Management attributes and objects based on the given schema set. /// </summary> /// <param name="rmSchema"></param> public RmFactory(XmlSchemaSet rmSchema) { if (rmSchema == null) { throw new ArgumentNullException("rmSchema"); } lock (rmSchema) { this.RmSchema = rmSchema; if (this.RmSchema.IsCompiled == false) { this.RmSchema.Compile(); } this.RmAttributeCache = new Dictionary<RmAttributeName, RmAttributeInfo>(); this.RmObjectCache = new Dictionary<string, Dictionary<RmAttributeName, RmAttributeInfo>>(); this.RmDoc = new XmlDocument(); this.RmNsManager = new XmlNamespaceManager(this.RmDoc.NameTable); this.RmNsManager.AddNamespace("rm", RmNamespace); foreach (XmlSchemaObject schemaObj in this.RmSchema.GlobalTypes.Values) { XmlSchemaComplexType schemaObjComplexType = schemaObj as XmlSchemaComplexType; if (schemaObjComplexType != null) { if (schemaObjComplexType.Name == null || schemaObjComplexType.Particle == null) { continue; } RmObjectCache[schemaObjComplexType.Name] = new Dictionary<RmAttributeName, RmAttributeInfo>(); XmlSchemaSequence schemaObjSequence = schemaObjComplexType.Particle as XmlSchemaSequence; if (schemaObjSequence != null) { foreach (XmlSchemaObject sequenceObj in schemaObjSequence.Items) { XmlSchemaElement sequenceElement = sequenceObj as XmlSchemaElement; if (sequenceElement != null) { RmAttributeInfo info = new RmAttributeInfo(); if (sequenceElement.MaxOccurs > Decimal.One) { info.IsMultiValue = true; } if (sequenceElement.MinOccurs > Decimal.Zero) { info.IsRequired = true; } string attributeTypeName = sequenceElement.ElementSchemaType.QualifiedName.Name.ToUpperInvariant(); if (string.IsNullOrEmpty(attributeTypeName)) { attributeTypeName = sequenceElement.SchemaType.TypeCode.ToString().ToUpperInvariant(); //Trace.TraceWarning("No type information for attribute '{0}' [{1}]", // sequenceElement.Name, // sequenceElement.ElementSchemaType.TypeCode); } if (string.IsNullOrEmpty(attributeTypeName)) { throw new ArgumentException(string.Format( "Could not get type for attribute {0}", sequenceElement.Name)); } if (attributeTypeName.Contains("COLLECTION")) { info.IsMultiValue = true; } if (attributeTypeName.Contains("REFERENCE")) { info.AttributeType = RmAttributeType.Reference; } else if (attributeTypeName.Contains("BOOLEAN")) { info.AttributeType = RmAttributeType.Boolean; } else if (attributeTypeName.Contains("INTEGER")) { info.AttributeType = RmAttributeType.Integer; } else if (attributeTypeName.Contains("DATETIME")) { info.AttributeType = RmAttributeType.DateTime; } else if (attributeTypeName.Contains("BINARY")) { info.AttributeType = RmAttributeType.Binary; } else if (attributeTypeName.Contains("STRING")) { info.AttributeType = RmAttributeType.String; } else if (attributeTypeName.Contains("TEXT")) { info.AttributeType = RmAttributeType.String; } else { throw new ArgumentException(string.Format( "Uknown type '{0}' for attribute {1}", attributeTypeName, sequenceElement.Name)); } RmAttributeName attributeName = new RmAttributeName(sequenceElement.Name); RmObjectCache[schemaObjComplexType.Name][attributeName] = info; RmAttributeCache[attributeName] = info; } } } } } } }
/// <summary> /// IsRequired /// </summary> /// <param name="attributeName">Name of the attribute.</param> /// <returns>True if the attribute is required, false otherwise.</returns> /// <remarks>This method is only for testing, and should not be used /// directly by clients.</remarks> internal bool IsRequired(string objectType, RmAttributeName attributeName) { Dictionary<RmAttributeName, RmAttributeInfo> attributeValue = null; RmObjectCache.TryGetValue(objectType, out attributeValue); if (attributeValue == null) { return false; } else { RmAttributeInfo attributeInfo = null; attributeValue.TryGetValue(attributeName, out attributeInfo); if (attributeInfo == null) { return false; } else { return attributeInfo.IsRequired; } } }
/// <summary> /// IsReference /// </summary> /// <param name="attributeName">Name of the attribute.</param> /// <returns>True if the attribute is a reference, false otherwise.</returns> /// <remarks>This method is only for testing, and should not be used /// directly by clients.</remarks> internal bool IsReference(RmAttributeName attributeName) { RmAttributeInfo retValue = null; RmAttributeCache.TryGetValue(attributeName, out retValue); if (retValue == null) { return false; } else { return retValue.AttributeType == RmAttributeType.Reference; } }
/// <summary> /// IsMultiValued /// </summary> /// <param name="attributeName">Name of the attribute.</param> /// <returns>True if the attribute is multivalued, false otherwise.</returns> /// <remarks>This method is only for testing, and should not be used /// directly by clients.</remarks> internal bool IsMultiValued(RmAttributeName attributeName) { RmAttributeInfo retValue = null; RmAttributeCache.TryGetValue(attributeName, out retValue); if (retValue == null) { return false; } else { return retValue.IsMultiValue; } }
/// <summary> /// Creates a new object that is a copy of the current instance. /// </summary> /// <returns> /// A new object that is a copy of this instance. /// </returns> public object Clone() { RmAttributeName newObject = new RmAttributeName(this.Name, this.Culture); return newObject; }
/// <summary> /// Returns a generic RmResource object from the getResponse object. /// </summary> /// <param name="getResponse">The get response from the server.</param> /// <returns>The RmResource object with the attributes returned in getResponse.</returns> public RmResource CreateResource(GetResponse getResponse) { if (getResponse == null) { throw new ArgumentNullException("getResponse"); } if (getResponse.BaseObjectSearchResponse == null) { throw new ArgumentNullException("getResponse.BaseObjectSearchResponse"); } lock (getResponse) { // look ahead for the type String objectType = null; foreach (PartialAttributeType partialAttribute in getResponse.BaseObjectSearchResponse.PartialAttributes) { if (partialAttribute.Values.Count > 0) { String localName = partialAttribute.Values[0].LocalName; if (String.IsNullOrEmpty(localName)) { continue; } if (localName.Equals(ObjectType)) { objectType = partialAttribute.Values[0].InnerText; break; } } } if (objectType == null) { objectType = string.Empty; } RmResource rmResource = this.resourceTypeFactory.CreateResource(objectType); // fill in the attribute values foreach (PartialAttributeType partialAttribute in getResponse.BaseObjectSearchResponse.PartialAttributes) { RmAttributeName attributeName = null; RmAttributeValue newAttribute = null; if (partialAttribute.Values.Count > 0) { String localName = partialAttribute.Values[0].LocalName; if (String.IsNullOrEmpty(localName)) { continue; } else { attributeName = new RmAttributeName(localName); } } else { continue; } if (rmResource.TryGetValue(attributeName, out newAttribute) == false) { newAttribute = CreateRmAttributeValue(attributeName); // PATCHED: the following line was missing rmResource[attributeName] = newAttribute; } // add values to the typed list foreach (XmlNode value in partialAttribute.Values) { IComparable newValue = this.ConstructAttributeValue(attributeName, value.InnerText); if (base.IsMultiValued(attributeName) == false) newAttribute.Values.Clear(); if (attributeName.Name.Equals(ObjectType) || attributeName.Name.Equals(ObjectID)) newAttribute.Values.Clear(); newAttribute.Values.Add(newValue); } } return rmResource; } }
protected IComparable ConstructAttributeValue(RmAttributeName attributeName, String innerText) { if (innerText == null) return null; RmAttributeInfo info = null; if (base.RmAttributeCache.TryGetValue(attributeName, out info) == false) { // just in case they forget to load schema... we know that ObjectId must remove the uuid reference if (attributeName.Name.Equals(ObjectID)) { return new RmReference(innerText); } else { return innerText; } } try { switch (info.AttributeType) { case RmAttributeType.String: return innerText; case RmAttributeType.DateTime: return DateTime.Parse(innerText); case RmAttributeType.Integer: return Int32.Parse(innerText); case RmAttributeType.Reference: return new RmReference(innerText); case RmAttributeType.Binary: return new RmBinary(innerText); case RmAttributeType.Boolean: return Boolean.Parse(innerText); default: return innerText; } } catch (FormatException ex) { throw new ArgumentException( String.Format( "Failed to parse attribute {0} with value {1} into type {2}. Please ensure the resource management schema is up to date.", attributeName, innerText, info.AttributeType.ToString()), ex); } catch (System.Text.EncoderFallbackException ex) { throw new ArgumentException( String.Format( "Failed to convert the string on binary attribute {0} into byte array.", attributeName), ex); } }
/// <summary> /// Creates a list of resources based on the pull or enumerate response. /// </summary> /// <param name="pullOrEnumerateResponse">The pull or enumerate response to use when creating resources.</param> /// <returns>The list of strongly-typed resources in the pull or enumerate response.</returns> public List<RmResource> CreateResource(PullResponse pullOrEnumerateResponse) { if (pullOrEnumerateResponse == null) { throw new ArgumentNullException("pullOrEnumerateResponse"); } if (pullOrEnumerateResponse.Items == null || pullOrEnumerateResponse.Items.Values == null) { return new List<RmResource>(); } lock (pullOrEnumerateResponse) { List<RmResource> retList = new List<RmResource>(); foreach (XmlNode obj in pullOrEnumerateResponse.Items.Values) { // look ahead for the type info; String objectType = null; foreach (XmlNode child in obj.ChildNodes) { if (child.NodeType == XmlNodeType.Element) { if (child.LocalName.Equals(@"ObjectType")) { objectType = child.InnerText; break; } } } if (objectType == null) { objectType = String.Empty; } RmResource rmResource = this.resourceTypeFactory.CreateResource(objectType); // now add the attributes to the resource object foreach (XmlNode child in obj.ChildNodes) { if (child.NodeType == XmlNodeType.Element) { RmAttributeName attributeName = new RmAttributeName(child.LocalName); IComparable attributeValue = this.ConstructAttributeValue(attributeName, child.InnerText); if (attributeValue == null) continue; RmAttributeValue newAttribute = null; if (rmResource.TryGetValue(attributeName, out newAttribute) == false) { newAttribute = CreateRmAttributeValue(attributeName); rmResource[attributeName] = newAttribute; } if (base.IsMultiValued(attributeName) == false) newAttribute.Values.Clear(); if (attributeName.Name.Equals(ObjectType) || attributeName.Name.Equals(ObjectID)) newAttribute.Values.Clear(); newAttribute.Values.Add(attributeValue); } } retList.Add(rmResource); } return retList; } }
DirectoryAccessChange BuildDirectoryAccessChange(RmAttributeName name, IComparable value) { if (value == null) { throw new ArgumentNullException("name", string.Format("Attribute '{0}' is null.", name)); } DirectoryAccessChange retReqChange = new DirectoryAccessChange(); retReqChange.AttributeType = name.Name; XmlElement attributeValueElem = base.RmDoc.CreateElement(retReqChange.AttributeType, RmNamespace); attributeValueElem.InnerText = value.ToString(); retReqChange.AttributeValue.Values.Add(attributeValueElem); return retReqChange; }
internal RmAttributeChange(RmAttributeName name, IComparable atomicValue, RmAttributeChangeOperation operation) { this.name = name; this.attributeValue = atomicValue; this.operation = operation; }
/// <summary> /// Gets the differences from source and destination attributes. /// </summary> /// <param name="sourceAttributes">The source attributes.</param> /// <param name="destinationAttributes">The destination attributes.</param> /// <returns></returns> private static IList <RmAttributeChange> GetDifference( Dictionary <RmAttributeName, RmAttributeValue> sourceAttributes, Dictionary <RmAttributeName, RmAttributeValue> destinationAttributes) { IList <RmAttributeChange> changedAttributes = new List <RmAttributeChange>(); // iterate source attributes foreach (KeyValuePair <RmAttributeName, RmAttributeValue> sourceItem in sourceAttributes) { RmAttributeName sourceName = sourceItem.Key; RmAttributeValue sourceValue = sourceItem.Value; if (!destinationAttributes.ContainsKey(sourceName)) { // the destination does not contain the attribute if (sourceValue.IsMultiValue) { // add all the values foreach (IComparable value1 in sourceValue.Values) { changedAttributes.Add(new RmAttributeChange(sourceName, value1, RmAttributeChangeOperation.Add)); } } else { changedAttributes.Add(new RmAttributeChange(sourceItem.Key, null, RmAttributeChangeOperation.Replace)); } } else { // the destination contains the attribute RmAttributeValue destinationValue = destinationAttributes[sourceItem.Key]; if (sourceValue.IsMultiValue) { foreach (IComparable value1 in sourceValue.Values) { if (destinationValue.Values.Contains(value1) == false) { changedAttributes.Add(new RmAttributeChange(sourceItem.Key, value1, RmAttributeChangeOperation.Add)); } } foreach (IComparable value2 in destinationValue.Values) { if (sourceItem.Value.Values.Contains(value2) == false) { changedAttributes.Add(new RmAttributeChange(sourceItem.Key, value2, RmAttributeChangeOperation.Delete)); } } } else { if (destinationValue.Value != sourceValue.Value) { changedAttributes.Add(new RmAttributeChange(sourceItem.Key, sourceValue.Value, RmAttributeChangeOperation.Replace)); } } } } // iterate destination attributes foreach (KeyValuePair <RmAttributeName, RmAttributeValue> destinationItem in destinationAttributes) { if (sourceAttributes.ContainsKey(destinationItem.Key) == false) { foreach (IComparable value2 in destinationItem.Value.Values) { changedAttributes.Add(new RmAttributeChange(destinationItem.Key, value2, RmAttributeChangeOperation.Delete)); } } } return(changedAttributes); }
/// <summary> /// Gets the differences from source and destination attributes. /// </summary> /// <param name="sourceAttributes">The source attributes.</param> /// <param name="destinationAttributes">The destination attributes.</param> /// <returns></returns> private static IList <RmAttributeChange> GetDifference( Dictionary <RmAttributeName, RmAttributeValue> sourceAttributes, Dictionary <RmAttributeName, RmAttributeValue> destinationAttributes) { IList <RmAttributeChange> changedAttributes = new List <RmAttributeChange>(); // iterate source attributes foreach (KeyValuePair <RmAttributeName, RmAttributeValue> sourceItem in sourceAttributes) { RmAttributeName sourceName = sourceItem.Key; RmAttributeValue sourceValue = sourceItem.Value; if (!destinationAttributes.ContainsKey(sourceName)) { // the destination does not contain the attribute if (sourceValue.IsMultiValue) { // add all the values foreach (IComparable value1 in sourceValue.Values) { changedAttributes.Add(new RmAttributeChange(sourceName, value1, RmAttributeChangeOperation.Add)); } } else { changedAttributes.Add(new RmAttributeChange(sourceItem.Key, null, RmAttributeChangeOperation.Replace)); } } else { // the destination contains the attribute RmAttributeValue destinationValue = destinationAttributes[sourceItem.Key]; if (sourceValue.IsMultiValue) { foreach (IComparable value1 in sourceValue.Values) { if (destinationValue.Values.Contains(value1) == false) { changedAttributes.Add(new RmAttributeChange(sourceItem.Key, value1, RmAttributeChangeOperation.Add)); } } foreach (IComparable value2 in destinationValue.Values) { if (sourceItem.Value.Values.Contains(value2) == false) { changedAttributes.Add(new RmAttributeChange(sourceItem.Key, value2, RmAttributeChangeOperation.Delete)); } } } else { if (destinationValue.Value != sourceValue.Value) { RmAttributeChangeOperation operation; if ( ( // <[MA] change 24-11-2011> - cleared reference attributes require Delete operation destinationValue.Value is RmReference || // <[MA] change 05-03-2012> - cleared date attributes require Delete operation destinationValue.Value is DateTime? ) && sourceValue.Value == null ) { operation = RmAttributeChangeOperation.Delete; } else { operation = RmAttributeChangeOperation.Replace; } changedAttributes.Add(new RmAttributeChange(sourceItem.Key, sourceValue.Value, operation)); // </[MA]> } } } } // iterate destination attributes foreach (KeyValuePair <RmAttributeName, RmAttributeValue> destinationItem in destinationAttributes) { if (sourceAttributes.ContainsKey(destinationItem.Key) == false) { foreach (IComparable value2 in destinationItem.Value.Values) { changedAttributes.Add(new RmAttributeChange(destinationItem.Key, value2, RmAttributeChangeOperation.Delete)); } } } return(changedAttributes); }
public RmAttributeValue CreateRmAttributeValue(RmAttributeName attributeName) { if (IsMultiValued(attributeName)) { return new RmAttributeValueMulti(); } else { return new RmAttributeValueSingle(); } }
/// <summary> /// Creates a new object that is a copy of the current instance. /// </summary> /// <returns> /// A new object that is a copy of this instance. /// </returns> public object Clone() { RmAttributeName newObject = new RmAttributeName(this.Name, this.Culture); return(newObject); }