PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/CmdletHelper.cs

#
C# | 123 lines | 98 code | 19 blank | 6 comment | 15 complexity | 2627d42536046405aec0397b99d72c88 MD5 | raw file
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Management.Automation;
  5. using Microsoft.ResourceManagement.ObjectModel;
  6. namespace Quest.FIMPowerShellSnapin
  7. {
  8. public class AttributeHashtable : IEnumerable<KeyValuePair<RmAttributeName, RmAttributeValue>>, IEnumerable
  9. {
  10. private Hashtable _hashtable;
  11. public AttributeHashtable(Hashtable hashtable)
  12. {
  13. _hashtable = hashtable;
  14. }
  15. public IEnumerator<KeyValuePair<RmAttributeName, RmAttributeValue>> GetEnumerator()
  16. {
  17. foreach (DictionaryEntry entry in _hashtable)
  18. {
  19. RmAttributeName attrName = new RmAttributeName(entry.Key as string); //!!! do we need to ensure that it is a string?
  20. RmAttributeValue attrValue = new RmAttributeValue(entry.Value as string);
  21. yield return new KeyValuePair<RmAttributeName, RmAttributeValue>(attrName, attrValue);
  22. }
  23. }
  24. IEnumerator IEnumerable.GetEnumerator()
  25. {
  26. return GetEnumerator();
  27. }
  28. }
  29. public static class CmdletHelper
  30. {
  31. #if false //!!! clean this up
  32. public static bool ShouldProcess(PSCmdlet cmdlet, string descriptionTemplate, string questionTemplate, ResourceManagementObject obj)
  33. {
  34. var displayNameAttrValue = obj.GetOneValueOfAttribute(WsClient.Constants.AttributeTypeDisplayName);
  35. var idAttrValue = obj.GetOneValueOfAttribute(WsClient.Constants.AttributeTypeObjectId);
  36. string displayName = null;
  37. string id = null;
  38. if (displayNameAttrValue != null)
  39. {
  40. displayName = displayNameAttrValue.ToString();
  41. }
  42. if (idAttrValue != null)
  43. {
  44. id = idAttrValue.ToString();
  45. }
  46. return ShouldProcess(cmdlet, descriptionTemplate, questionTemplate, displayName, id);
  47. }
  48. public static bool ShouldProcess(PSCmdlet cmdlet, string descriptionTemplate, string questionTemplate, string id)
  49. {
  50. return ShouldProcess(cmdlet, descriptionTemplate, questionTemplate, null, id);
  51. }
  52. static bool ShouldProcess(PSCmdlet cmdlet, string descriptionTemplate, string questionTemplate, string displayName, string id)
  53. {
  54. string description = ComposeObjectDetails(displayName, id);
  55. return cmdlet.ShouldProcess(string.Format(descriptionTemplate, description), string.Format(questionTemplate, description), Messages.ConfirmCaption);
  56. }
  57. #endif
  58. public delegate void AttributeHandler(String attrName, Object attrValue);
  59. public static void ProcessHashtable(Hashtable hashtable, String paramName, AttributeHandler attrHandler)
  60. {
  61. if (hashtable != null)
  62. {
  63. foreach (DictionaryEntry entry in hashtable)
  64. {
  65. string attrName = entry.Key as string;
  66. if (attrName == null)
  67. {
  68. throw new ArgumentException(string.Format(Constants.Messages.ErrorDictKeyNotString, paramName));
  69. }
  70. attrHandler(attrName, entry.Value);
  71. }
  72. }
  73. }
  74. /// <summary>
  75. /// Extension method for RmAttributeValue type that indicates whether the attribute value is null, i.e. has no
  76. /// values.
  77. /// </summary>
  78. /// <param name="rmValue">Instance of rmAttributeValue to test</param>
  79. /// <returns></returns>
  80. public static bool IsNullValue(this RmAttributeValue rmValue)
  81. {
  82. return rmValue.Values.Count == 0;
  83. }
  84. static string ComposeObjectDetails(string displayName, string id)
  85. {
  86. if (!string.IsNullOrEmpty(displayName) && !string.IsNullOrEmpty(id))
  87. {
  88. return string.Format(Constants.Messages.ObjectDetailsDisplayNameAndID, displayName, id);
  89. }
  90. else if (!string.IsNullOrEmpty(displayName))
  91. {
  92. return string.Format(Constants.Messages.ObjectDetailsDisplayName, displayName);
  93. }
  94. else if (!string.IsNullOrEmpty(id))
  95. {
  96. return string.Format(Constants.Messages.ObjectDetailsID, id);
  97. }
  98. else
  99. {
  100. return string.Empty;
  101. }
  102. }
  103. }
  104. }