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

/NRTMAddIn/ConfigureServerShape.cs

#
C# | 277 lines | 172 code | 28 blank | 77 comment | 10 complexity | bd97d9576fdd1d7964fcecf0b8333fa2 MD5 | raw file
  1. /*++
  2. Copyright (c) 2007 Microsoft Corporation
  3. Filename:
  4. ConfigureServerShape.cs
  5. Abstract:
  6. A class that configures each shape with respect to NRTM.
  7. --*/
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Text;
  11. using System.Collections;
  12. using Visio = Microsoft.Office.Interop.Visio;
  13. using System.Windows.Forms;
  14. using System.Data.SqlClient;
  15. using System.Data;
  16. using System.Reflection;
  17. using System.IO;
  18. using System.Globalization;
  19. namespace NRTMAddIn
  20. {
  21. class ConfigureServerShape
  22. {
  23. internal const string NetworkNameProp = "Prop.NetworkName";
  24. internal const string IPAddressProp = "Prop.IPAddress";
  25. /// <summary>
  26. /// Default Constructor
  27. /// </summary>
  28. private ConfigureServerShape()
  29. {
  30. }
  31. /// <summary>
  32. /// Visio drop shape event
  33. /// </summary>
  34. /// <param name="Shape"></param>
  35. internal static void VisioApp_ShapeAdded(Visio.Shape Shape)
  36. {
  37. try
  38. {
  39. //Shape must contain IP address and Network name
  40. if (!ValidNetworkShape(Shape))
  41. {
  42. return;
  43. }
  44. //Configure server shape for NRTM usage
  45. //Add NRTMConfig property to the shape
  46. AddNRTMConfigProperties(Shape);
  47. //Add smart tag to shape
  48. LinkMonitorTagToShape(Shape);
  49. //Add NRTMStatus property to the shape
  50. AddStatusProperty(Shape);
  51. //Add the view details action menu
  52. AddViewDetailOption(Shape);
  53. //Set the tool tip to disk status
  54. Shape.get_CellsSRC(
  55. (short)Visio.VisSectionIndices.visSectionObject,
  56. (short)Visio.VisRowIndices.visRowMisc,
  57. (short)Visio.VisCellIndices.visComment).Formula = "IF(PROP.NRTMCONFIG=TRUE,Prop.NetworkName & \" \" & IF(USER.NRTMSTATUS=0,\"Unavailable\",IF(USER.NRTMSTATUS=1,\"Low Disk\",IF(USER.NRTMSTATUS=2,\"Normal\",IF(USER.NRTMSTATUS=3,\"Healthy\",\"\")))),INDEX(0,INDEX(0,MASTERNAME(),\":\"),\".\"))";
  58. }
  59. catch (NrtmException ex)
  60. {
  61. //To catch and display exceptions from sub functions
  62. NrtmException.HandleExceptions(Global.GetResourceString("Exception_MsgBox_Caption"),
  63. Global.GetResourceString("Exception_UsrMsg"), ex.InnerException);
  64. }
  65. catch (Exception ex)
  66. {
  67. //To catch exceptions from current functions
  68. NrtmException.HandleExceptions(Global.GetResourceString("Exception_MsgBox_Caption"),
  69. Global.GetResourceString("Exception_UsrMsg"), ex);
  70. }
  71. }
  72. /// <summary>
  73. /// Add NRTMConfig property to the shape
  74. /// </summary>
  75. /// <param name="shape"></param>
  76. private static void AddNRTMConfigProperties(Visio.Shape shape)
  77. {
  78. // Add one named row, if they do not already exist.
  79. bool hasNRTM_ConfigProp;
  80. short rowIndex;
  81. Visio.Cell labelCell;
  82. try
  83. {
  84. hasNRTM_ConfigProp = ShapePropertyExist(shape, "Prop.NRTMConfig");
  85. if (!hasNRTM_ConfigProp)
  86. {
  87. // Add a user-defined cell to store the NRTM Config name.
  88. rowIndex = shape.AddNamedRow(
  89. (short)(Visio.VisSectionIndices.visSectionUser),
  90. "NRTMConfig",
  91. (short)(Visio.VisRowIndices.visRowUser));
  92. // Get the Value cell and set it to point
  93. // to a value from the NodeInfo property cell.
  94. shape.get_Cells("User.NRTMConfig.Value").FormulaU = "TRUE";
  95. // Insert our NRTMConfig property.
  96. rowIndex = shape.AddNamedRow(
  97. (short)(Visio.VisSectionIndices.visSectionProp),
  98. "NRTMConfig",
  99. (short)(Visio.VisRowIndices.visRowProp));
  100. // Edit the Label cell.
  101. labelCell = shape.get_CellsSRC(
  102. (short)Visio.VisSectionIndices.visSectionProp,
  103. rowIndex,
  104. (short)Visio.VisCellIndices.visCustPropsLabel);
  105. labelCell.FormulaU = "\"NRTM Configured\"";
  106. // True if user cell is true & ip address or network name is specified
  107. shape.get_Cells("Prop.NRTMConfig.Value").Formula = "AND(TheDoc!User.NRTMAvail=TRUE,AND(USER.NRTMCONFIG=TRUE,OR(AND(LOCALFORMULAEXISTS(Prop.NetworkName),NOT(STRSAME(Prop.NetworkName,\"\"))),AND(LOCALFORMULAEXISTS(Prop.IPAddress),NOT(STRSAME(Prop.IPAddress,\"\"))))))";
  108. //Set Invisible to true.
  109. shape.get_Cells("Prop.NRTMConfig.Invisible").FormulaU = "TRUE";
  110. }
  111. }
  112. catch (Exception ex)
  113. {
  114. //Throw user-defined exception
  115. throw new NrtmException("Add NRTM Config Property", ex);
  116. }
  117. }
  118. /// <summary>
  119. /// Add smart tag to shape
  120. /// </summary>
  121. /// <param name="shape"></param>
  122. private static void LinkMonitorTagToShape(Visio.Shape shape)
  123. {
  124. bool hasNRTM_ConfigProp;
  125. try
  126. {
  127. hasNRTM_ConfigProp = ShapePropertyExist(shape, "Prop.NRTMConfig");
  128. if (hasNRTM_ConfigProp)
  129. {
  130. string NRTMConfig = shape.get_Cells("User.NRTMConfig.Value").FormulaU;
  131. //NRTMConfig should be true for a shape to add the smart tag
  132. if (NRTMConfig == "TRUE")
  133. {
  134. bool hasTag = ShapePropertyExist(shape, "SmartTags.NRTMTag");
  135. if (!hasTag)
  136. {
  137. short SmartSection = (short)(Visio.VisSectionIndices.visSectionSmartTag);
  138. shape.AddNamedRow(SmartSection, "NRTMTag", (short)Visio.VisRowIndices.visRowSmartTag);
  139. shape.get_CellsU("SmartTags.NRTMTag.TagName").FormulaU = "\"Disk Monitor\"";
  140. //Enable only if shape is NRTM configured
  141. shape.get_CellsU("SmartTags.NRTMTag.Disabled").Formula = "NOT(Prop.NRTMConfig)";
  142. //Set menu action
  143. short SmartTagAction = (short)(Visio.VisSectionIndices.visSectionAction);
  144. shape.AddNamedRow(SmartTagAction, "NRTMTag", (short)Visio.VisRowIndices.visRowAction);
  145. shape.get_CellsU("Actions.NRTMTag.TagName").FormulaU = "\"Disk Monitor\"";
  146. shape.get_CellsU("Actions.NRTMTag.Menu").FormulaU = "\"" + Global.GetResourceString("Start_SmartTag") + "\"";
  147. shape.get_CellsU("Actions.NRTMTag.Action").FormulaU = "RUNADDONWARGS(\"QUEUEMARKEREVENT\",\"" +
  148. Global.ApplicationContextString + Global.ApplicationIndex +
  149. " " + Global.CommandContextString + Global.CommandStringStartMonitoring + "\")";
  150. }
  151. }
  152. }
  153. }
  154. catch (Exception ex)
  155. {
  156. //Throw user-defined exception
  157. throw new NrtmException("Add NRTM Smart Tag", ex);
  158. }
  159. }
  160. /// <summary>
  161. /// Add NRTMStatus property to the shape
  162. /// </summary>
  163. /// <param name="shape"></param>
  164. private static void AddStatusProperty(Visio.Shape shape)
  165. {
  166. // Add one named row, if they do not already exist.
  167. bool hasNRTMStatusProp;
  168. try
  169. {
  170. hasNRTMStatusProp = ShapePropertyExist(shape, "User.NRTMStatus");
  171. if (!hasNRTMStatusProp)
  172. {
  173. // Add a user-defined cell to store the NRTM Status name.
  174. shape.AddNamedRow(
  175. (short)(Visio.VisSectionIndices.visSectionUser),
  176. "NRTMStatus",
  177. (short)(Visio.VisRowIndices.visRowUser));
  178. }
  179. //Set to default
  180. shape.get_CellsU("User.NRTMStatus").set_ResultFromInt(
  181. (short)Visio.VisUnitCodes.visNoCast,
  182. Convert.ToInt32(Global.VisioColors.Default, CultureInfo.InvariantCulture));
  183. }
  184. catch (Exception ex)
  185. {
  186. //Throw user-defined exception
  187. throw new NrtmException("Add NRTM status Property", ex);
  188. }
  189. }
  190. /// <summary>
  191. /// Add the view details action menu
  192. /// </summary>
  193. /// <param name="shape"></param>
  194. private static void AddViewDetailOption(Visio.Shape shape)
  195. {
  196. //Check if view details already exisit
  197. bool hasViewDetailAction = ShapePropertyExist(shape, "Actions.DrillAction");
  198. try
  199. {
  200. if (!hasViewDetailAction)
  201. {
  202. short ActionSection = (short)(Visio.VisSectionIndices.visSectionAction);
  203. shape.AddNamedRow(ActionSection, "DrillAction", (short)Visio.VisRowIndices.visRowAction);
  204. shape.get_CellsU("Actions.DrillAction.Action").FormulaU = "RUNADDONWARGS(\"QUEUEMARKEREVENT\",\"" +
  205. Global.ApplicationContextString + Global.ApplicationIndex +
  206. " " + Global.CommandContextString + Global.CommandStringDrill + "\")";
  207. shape.get_CellsU("Actions.DrillAction.Menu").FormulaU = "\""+Global.GetResourceString("ViewDiskDetails_Text")+"\"";
  208. //Active only if shape is NRTM configured & status is between 0-3
  209. shape.get_CellsU("Actions.DrillAction.Invisible").Formula = "NOT(AND(TheDoc!User.NRTMMonitor,AND(PROP.NRTMCONFIG,OR(OR(USER.NRTMSTATUS=0,USER.NRTMSTATUS=1),OR(USER.NRTMSTATUS=2,USER.NRTMSTATUS=3)))))";
  210. }
  211. }
  212. catch (Exception ex)
  213. {
  214. //Throw user-defined exception
  215. throw new NrtmException("Add NRTM details property", ex);
  216. }
  217. }
  218. /// <summary>
  219. /// Check if the shape contains the property
  220. /// </summary>
  221. /// <param name="shape">Visio Shape</param>
  222. /// <param name="propertyName">Property Name</param>
  223. /// <returns>True if shape contains the property</returns>
  224. internal static bool ShapePropertyExist(Visio.Shape shape,string propertyName)
  225. {
  226. return Convert.ToBoolean(shape.get_CellExists(propertyName, (short)Visio.VisExistsFlags.visExistsAnywhere));
  227. }
  228. /// <summary>
  229. /// Checks if a shape contains IP address and Network name properties
  230. /// </summary>
  231. /// <param name="shape">Shape to validate</param>
  232. /// <returns>True if IP address and Network name present</returns>
  233. internal static bool ValidNetworkShape(Visio.Shape shape)
  234. {
  235. if (ShapePropertyExist(shape, NetworkNameProp) && ShapePropertyExist(shape, IPAddressProp))
  236. {
  237. //Return true if shape contains IP address & Network name
  238. return true;
  239. }
  240. return false;
  241. }
  242. }
  243. }