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