PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/FIMPSResource.cs

#
C# | 252 lines | 174 code | 33 blank | 45 comment | 17 complexity | c72133ead2d614a8570ef5f06bdd3e50 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections.ObjectModel;
  6. using Microsoft.ResourceManagement.ObjectModel;
  7. namespace Quest.FIMPowerShellSnapin
  8. {
  9. /// <summary>
  10. /// The FIMPSCmdletResource class represents a FIM resource object as a PowerShell object. It contains a reference to the RmResource object
  11. /// as well as a reference to the FIMPSCmdletSession that the RmResource object is associate with.
  12. /// </summary>
  13. public class FIMPSResource
  14. {
  15. private readonly RmResource _resource;
  16. private readonly FIMPSSession _session;
  17. private RmResourceChanges _changes;
  18. public FIMPSResource(FIMPSSession session)
  19. : this(new RmResource(), session)
  20. {
  21. }
  22. internal FIMPSResource(RmResource obj, FIMPSSession session)
  23. {
  24. if (session == null)
  25. {
  26. throw new ArgumentNullException();
  27. }
  28. _resource = obj;
  29. _session = session;
  30. _changes = new RmResourceChanges(_resource);
  31. _changes.BeginChanges(); // Copies current attribute values so we can calculate changeset later
  32. }
  33. /// <summary>
  34. /// Determines if two FIM PS cmdlet resource objects are equal. To be equal, they must be from the same FIM server, the
  35. /// credentials used to obtain the object must be the same, and the property values must all be the same.
  36. /// </summary>
  37. /// <param name="obj">The PS object to compare</param>
  38. /// <returns>true if the two objects are equal, false if they are not equal</returns>
  39. public override bool Equals(object obj)
  40. {
  41. if (obj == this)
  42. {
  43. return true;
  44. }
  45. FIMPSResource other = obj as FIMPSResource;
  46. if (other == null)
  47. {
  48. return false;
  49. }
  50. if (Session.Server.ToLowerInvariant() != other.Session.Server.ToLowerInvariant())
  51. {
  52. return false;
  53. }
  54. if (Session.Credential.UserName.Trim().ToLowerInvariant() != other.Session.Credential.UserName.Trim().ToLowerInvariant())
  55. {
  56. return false;
  57. }
  58. return Resource.Equals(other.Resource);
  59. }
  60. public override int GetHashCode()
  61. {
  62. return _resource.GetHashCode();
  63. }
  64. public RmResource Resource
  65. {
  66. get
  67. {
  68. return _resource;
  69. }
  70. }
  71. public FIMPSSession Session
  72. {
  73. get
  74. {
  75. return _session;
  76. }
  77. }
  78. /// <summary>
  79. /// Indexer. Allows PoSh to retrieve attributes from the object using indexed array notation. Attribute values are
  80. /// returned as strings, except for attributes defined in the schema as dates, which are returned as DateTime.
  81. /// Multi-valued attributes are returned as a List of either strings or DateTimes.
  82. /// </summary>
  83. /// <param name="attributeName">The name of the attribute to retrieve.</param>
  84. /// <returns></returns>
  85. public object this[string attributeName]
  86. {
  87. get
  88. {
  89. object pSValue;
  90. try // or else PoSh suppresses exceptions
  91. {
  92. RmAttributeName resourceAttributeName = new RmAttributeName(attributeName);
  93. RmAttributeValue resourceValue = _resource[resourceAttributeName];
  94. bool isDateTime = (_session.Client.Schema.GetAttributeType(attributeName) == SchemaCache.AttributeType.DateTime);
  95. if (_session.Client.Schema.IsMultiValued(resourceAttributeName))
  96. {
  97. List<object> listOfValues = new List<object>(resourceValue.Values.Count);
  98. foreach (RmAttributeValue rmValue in resourceValue.Values)
  99. {
  100. listOfValues.Add(isDateTime ? ConvertValueToDateTime(rmValue) : rmValue);
  101. }
  102. pSValue = new ReadOnlyCollection<object>(listOfValues);
  103. }
  104. else
  105. {
  106. pSValue = isDateTime ? ConvertValueToDateTime(resourceValue) : resourceValue;
  107. }
  108. }
  109. catch
  110. {
  111. throw;
  112. }
  113. return pSValue;
  114. }
  115. }
  116. private object ConvertValueToDateTime(RmAttributeValue rmValue)
  117. {
  118. if (rmValue.IsNullValue())
  119. {
  120. return new DateTime();
  121. }
  122. else
  123. {
  124. return DateTime.Parse(rmValue.ToString());
  125. }
  126. }
  127. #if false
  128. public ICollection<ILMAttributeInfo> Attributes
  129. {
  130. get
  131. {
  132. List<retList = new List<ILMAttributeInfo>();
  133. foreach (String key in RmObject.AttributeNames)
  134. {
  135. retList.Add(new ILMAttributeInfo(RmObject[key]));
  136. }
  137. return new ReadOnlyCollection<ILMAttributeInfo>(retList);
  138. }
  139. }
  140. #endif
  141. /// <summary>
  142. /// Adds a value to the specified attribute. If the attribute doesn't exist, it will be created. If the
  143. /// attribute is single-valued, it throws an exception. If the attribute is multi-valued the specified value
  144. /// will be added to the attribute. Note that this code does not check for the prior existence of the
  145. /// value, so it will be possible to create a multi-valued attribute with multiple identical values, which
  146. /// would be rejected by the FIM service.
  147. /// </summary>
  148. /// <param name="attributeName">The name of the attribute to modify</param>
  149. /// <param name="attributeValue">The value to set or add to the attribute</param>
  150. public void AddValue(string attributeName, IComparable attributeValue)
  151. {
  152. RmAttributeName rmAttrName = new RmAttributeName(attributeName);
  153. bool isMultivalued = _session.Client.Schema.IsMultiValued(rmAttrName);
  154. if(!isMultivalued)
  155. {
  156. throw new ArgumentException(string.Format(Constants.Messages.Error_IllegalOpForSVA, attributeName));
  157. }
  158. RmAttributeValue rmAttrValue = null;
  159. if(_resource.TryGetValue(rmAttrName, out rmAttrValue))
  160. {
  161. rmAttrValue.Values.Add(attributeValue);
  162. }
  163. else
  164. {
  165. rmAttrValue.Value = attributeValue;
  166. }
  167. _resource[rmAttrName] = rmAttrValue;
  168. }
  169. /// <summary>
  170. /// Removes a value from the specified multi-valued attribute. If the attribute does not exist, or if the
  171. /// attribute is defined as single-valued, or if the value does not exist in the multi-value, throws an exception.
  172. /// </summary>
  173. /// <param name="attributeName">The name of the attribute to remove or modify</param>
  174. /// <param name="attributeValue">The value to remove from the attribute</param>
  175. public void RemoveValue(string attributeName, IComparable attributeValue)
  176. {
  177. RmAttributeName rmAttrName = new RmAttributeName(attributeName);
  178. bool isMultivalued = _session.Client.Schema.IsMultiValued(rmAttrName);
  179. if(!isMultivalued)
  180. {
  181. throw new ArgumentException(string.Format(Constants.Messages.Error_IllegalOpForSVA, attributeName));
  182. }
  183. //!! Debateable as to whether this should throw an exception if the attr doesn't exist. Could just ignore it.
  184. RmAttributeValue rmAttrValue = null;
  185. if(!_resource.TryGetValue(rmAttrName, out rmAttrValue))
  186. {
  187. throw new ArgumentException(string.Format(Constants.Messages.Error_AttributeMissing, attributeName));
  188. }
  189. rmAttrValue.Values.Remove(attributeValue);
  190. _resource[rmAttrName] = rmAttrValue;
  191. }
  192. /// <summary>
  193. /// Sets the value of the specified attribute to the specified value. If the attribute value does not exist, it
  194. /// will be added. If the attribute is single valued, any existing value will be replaced. If the attribute
  195. /// is multi-valued, the value will be replaced with the specified value. Null is a valid value.
  196. /// </summary>
  197. /// <param name="attributeName">The name of the attribute to remove or modify</param>
  198. /// <param name="attributeValue">The value to remove from the attribute</param>
  199. public void SetValue(string attributeName, object attributeValue)
  200. {
  201. RmAttributeName rmAttrName = new RmAttributeName(attributeName);
  202. RmAttributeValue rmAttrValue = new RmAttributeValue(attributeValue as IComparable);
  203. _resource[rmAttrName] = rmAttrValue;
  204. }
  205. /// <summary>
  206. /// Retrieves the RmResourceChanges object associated with this object. The RmResourceChanges object contains
  207. /// the original value of the objects attributes so that a delta can be computed to update the FIM web service.
  208. /// </summary>
  209. /// <returns></returns>
  210. public RmResourceChanges GetTransactionChanges()
  211. {
  212. return _changes;
  213. }
  214. public void AcceptChanges()
  215. {
  216. _changes.AcceptChanges();
  217. }
  218. }
  219. }