Generate, update, and display diagrams within ArcGIS Server Schematics
ArcGIS_Schematics_ServingDiagrams_VBNet\Default.aspx.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 System
Imports System.Web.UI
Imports System.Collections.Specialized
Imports ESRI.ArcGIS.ADF.Web.UI.WebControls
Imports ESRI.ArcGIS.ADF.Web.DataSources
Imports ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer
Imports ESRI.ArcGIS.Schematic


Partial Public Class _Default

  Inherits System.Web.UI.Page
  Implements ICallbackEventHandler

  Private m_adfCallbackFunctionString As String
  Private m_nameValueCollection As NameValueCollection
  Private m_adfCallbackResultCollection As CallbackResultCollection

  ''' <summary>
  ''' Store the callback function string used by the browser to initiate a callback request.
  ''' Added to the aspx page as a server variable - populated at runtime.
  ''' </summary>
  Public Property ADFCallbackFunctionString() As String
    Get
      Return m_ADFCallbackFunctionString
    End Get
    Set(ByVal value As String)
      m_ADFCallbackFunctionString = value
    End Set
  End Property

  ''' <summary>
  ''' ICallbackEventHandler implementation
  ''' </summary>
  Public Function GetCallbackResult() As String Implements ICallbackEventHandler.GetCallbackResult
    Return m_ADFCallbackResultCollection.ToString()
  End Function


  Public Sub RaiseCallbackEvent(ByVal eventArgs As String) Implements ICallbackEventHandler.RaiseCallbackEvent

    m_ADFCallbackResultCollection = New CallbackResultCollection()

    m_nameValueCollection = CallbackUtility.ParseStringIntoNameValueCollection(eventArgs)
    Dim eventArg As String = m_nameValueCollection("EventArg")

    If eventArg.Equals("checkJob") Then GPJobStatus()

  End Sub

  ''' <summary>
  ''' Get the GP job status for a provided job id
  ''' </summary>
  Protected Sub GPJobStatus()

    ' Parse GP arguments to check job status: job id, diagram name, diagram id, diagram type
    Dim gpJobArgs As String = m_nameValueCollection("gpJobArgs")
    Dim parserChar As Char() = {","c}
    Dim gpArguments As String() = gpJobArgs.Split(parserChar)
    Dim jobID As String = gpArguments(0)
    Dim diagramName As String = gpArguments(1)
    Dim diagramID As String = gpArguments(2)
    Dim diagramTypeName As String = gpArguments(3)

    ' Create GP functionality
    Dim gpResourceItem As GeoprocessingResourceItem = CType(GeoprocessingResourceManager1.ResourceItems(0), GeoprocessingResourceItem)
    Dim gpResource As IGeoprocessingResource = CType(gpResourceItem.Resource, IGeoprocessingResource)
    Dim gpFunctionality As IGeoprocessingFunctionality = CType(gpResource.CreateFunctionality(GetType(IGeoprocessingFunctionality), Nothing), IGeoprocessingFunctionality)
    Dim agsGpFunctionality As GeoprocessingFunctionality = CType(gpFunctionality, GeoprocessingFunctionality)

    Dim adfJobStatus As JobStatus = agsGpFunctionality.GetJobStatus(jobID)

    If adfJobStatus = JobStatus.Failed OrElse adfJobStatus = JobStatus.TimedOut Then

      ' GP job failed, hide progress bar, get last job message and return it in an alert box
      Dim adfJobMessageArray As JobMessage() = agsGpFunctionality.GetJobMessages(jobID)
      Dim messageCount As Integer = adfJobMessageArray.Length
      Dim errorMessage As String = "Failed to update " & diagramName & ControlChars.CrLf & "GP job error: " & adfJobMessageArray(messageCount - 1).MessageDesc
      Dim callbackResultError As CallbackResult = New CallbackResult(Nothing, Nothing, "javascript", "alert('" & errorMessage & "')")
      m_ADFCallbackResultCollection.Add(SchematicCommands.Utilities.ShowAjaxIndicator(False))
      m_adfCallbackResultCollection.Add(callbackResultError)
      Session("UpdatingDiagram") = Nothing
      Return

    ElseIf Not adfJobStatus = JobStatus.Succeeded Then

      ' GP job not finished, create a callback response to trigger another callback to check status in 5 seconds
      Dim callbackReplaceString As String = ADFCallbackFunctionString.Replace("'", "\'")
      Dim timerFunctionString As String = String.Format("window.setTimeout('checkJob(""gpJobArgs={0},{1},{2},{3}"",""{4}"")',5000);", jobID, diagramName, diagramID, diagramTypeName, callbackReplaceString)
      Dim checkJobCallbackResult As CallbackResult = New CallbackResult(Me, "javascript", timerFunctionString)
      m_ADFCallbackResultCollection.Add(checkJobCallbackResult)
      Return

    End If

    'GP job is finished, refresh map if updated diagram is currently visible, then hide progress bar

    Dim dropBox As DropDownBox = Toolbar1.ToolbarItems.Find("DiagramList")

    Dim selectedDiagram As String = ""
    If dropBox.SelectedIndex > 0 Then selectedDiagram = dropBox.SelectedItem.Text

    If diagramTypeName = Session("CurrentViewType") And diagramName = selectedDiagram Then

      Dim schDiagramClass As ISchematicDiagramClass = SchematicCommands.Utilities.GetCurrentDiagramClass(Map1)

      SchematicCommands.Utilities.CalculateExtent(Map1, schDiagramClass, diagramName)
      Map1.Refresh()

      m_adfCallbackResultCollection.CopyFrom(Map1.CallbackResults)

      Session("DiagramFullExtent") = Map1.Extent

    End If

    Session("UpdatingDiagram") = Nothing
    m_ADFCallbackResultCollection.Add(SchematicCommands.Utilities.ShowAjaxIndicator(False))

  End Sub


  Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    ' Generate custom callback function string for non-ADF controls\elements in page that generate callbacks
    m_ADFCallbackFunctionString = Page.ClientScript.GetCallbackEventReference(Me, "message", "processCallbackResult", "context", "postBackError", True)

    If Session("CurrentViewType") Is Nothing Then Session("CurrentViewType") = "GeographicMap"

  End Sub


  Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As EventArgs)

    ' Insert script block containing custom JavaScript to manage client browser interaction with server-side customizations in this sample.

    Dim scriptKeyCustom As String = "customChangeScript"

    If (Not Me.Page.ClientScript.IsClientScriptBlockRegistered(Me.GetType(), scriptKeyCustom)) AndAlso (Not Page.IsPostBack) Then

      Dim scriptBlock As String = ControlChars.CrLf & " function checkJob(callbackArguments, callbackFunctionString)" _
                   & ControlChars.CrLf & " {" _
                   & ControlChars.CrLf & "   var message = ""EventArg=checkJob"";" _
                   & ControlChars.CrLf & "   if (callbackArguments.length > 0) message += ""&"" + callbackArguments;" _
                   & ControlChars.CrLf & "   var context = null;" _
                   & ControlChars.CrLf & "   eval(callbackFunctionString);" _
                   & ControlChars.CrLf & " }" _
                   & ControlChars.CrLf

      Me.Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), scriptKeyCustom, scriptBlock, True)

    End If

  End Sub

End Class