ArcGIS ClipShip geoprocessing
ArcGIS_ClipShip_Geoprocessing_VBNet\App_Code\Utility.vb
' Copyright 2010 ESRI
' 
' All rights reserved under the copyright laws of the United States
' and applicable international laws, treaties, and conventions.
' 
' You may freely redistribute and use this sample code, with or
' without modification, provided you include the original copyright
' notice and use restrictions.
' 
' See the use restrictions.
' 

Imports Microsoft.VisualBasic
Imports System
Public Class Utility
  ''' <summary>
  ''' Constructs a callback result that will display a javascript alert with an error message 
  ''' based on the passed-in exception
  ''' </summary>
  ''' <param name="exception">The exception from which the error message will be derived</param>
  ''' <returns></returns>
  Public Shared Function CreateErrorCallbackResult(ByVal exception As System.Exception) As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult
    ' Create a callback result to display an error message
    Dim jsAlertErrorMessage As String = GetJavaScriptErrorString(exception)
    Dim alertCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(jsAlertErrorMessage)
    Return alertCallbackResult
  End Function

  ''' <summary>
  ''' Constructs the syntax to display a javascript alert with an error message based on the 
  ''' passed-in exception
  ''' </summary>
  ''' <param name="exception">The exception from which the error message will be derived</param>
  ''' <returns></returns>
  Public Shared Function GetJavaScriptErrorString(ByVal exception As System.Exception) As String
    ' Get the website's configuration file
    Dim webConfig As System.Configuration.Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(System.Web.HttpContext.Current.Request.ApplicationPath)

    ' Get the "compilation" section of the config file
    Dim compilationSection As System.Web.Configuration.CompilationSection = TryCast(webConfig.GetSection("system.web/compilation"), System.Web.Configuration.CompilationSection)

    ' If the config file's compilation section specifies debug mode, include 
    ' stack trace information in the error message.  Otherwise, just return 
    ' the exception message.
    Dim errorMessage As String = Nothing
    If (Not compilationSection Is Nothing) AndAlso (compilationSection.Debug) Then
      Dim stackTrace As String = exception.StackTrace.Replace("\", "\\")
      stackTrace = stackTrace.Replace(Constants.vbLf, "\n")
      stackTrace = stackTrace.Replace(Constants.vbCr, "\r")
      stackTrace = stackTrace.Replace("'", "\'")
      errorMessage = exception.Message.Replace("\", "\\")
      errorMessage = errorMessage.Replace(Constants.vbLf, "\n")
      errorMessage = errorMessage.Replace(Constants.vbCr, "\r")
      errorMessage = errorMessage.Replace("'", "\'")

      errorMessage = errorMessage & "\n\n" & stackTrace.Trim()
    Else
      errorMessage = exception.Message
    End If

    ' Create a callback result to display an error message
    Dim jsAlertException As String = "alert('" & errorMessage & "')"
    Return jsAlertException
  End Function


  ''' <summary>
  ''' Finds the node having the passed-in name from among the input node or its child nodes. 
  ''' Useful for searching a Toc with group layers.
  ''' </summary>
  ''' <param name="treeViewPlusNode"></param>
  ''' <param name="nodeName"></param>
  ''' <returns></returns>
  Public Shared Function FindNodeRecursive(ByVal treeViewPlusNode As ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode, ByVal nodeName As String) As ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode
    ' Check whether the text of the passed-in node matches the text sought.  Return the node if so.
    If treeViewPlusNode.Text = nodeName Then
      Return treeViewPlusNode
    End If

    ' Iterate through the passed-in node's child nodes, calling this function on each.
    For Each childTreeViewPlusNode As ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode In treeViewPlusNode.Nodes
      Dim childNode As ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode = FindNodeRecursive(childTreeViewPlusNode, nodeName)
      If Not childNode Is Nothing Then
        Return childNode
      End If
    Next childTreeViewPlusNode

    ' If the code reaches this point, no match was found. 
    Return Nothing
  End Function
End Class