Generate, update, and display diagrams within ArcGIS Server Schematics
ArcGIS_Schematics_ServingDiagrams_CSharp\Default.aspx.cs
// 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.
// 

using System;
using System.Web.UI;
using System.Collections.Specialized;
using ESRI.ArcGIS.ADF.Web.UI.WebControls;
using ESRI.ArcGIS.ADF.Web.DataSources;
using ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer;
using ESRI.ArcGIS.Schematic;


public partial class _Default : System.Web.UI.Page, ICallbackEventHandler
{
  private string m_adfCallbackFunctionString;
  private NameValueCollection m_nameValueCollection;
  private CallbackResultCollection m_adfCallbackResultCollection;

  /// <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 string ADFCallbackFunctionString
  {
    get
    {
      return m_adfCallbackFunctionString;
    }
    set
    {
      m_adfCallbackFunctionString = value;
    }
  }

  /// <summary>
  /// ICallbackEventHandler implementation
  /// </summary>
  public string GetCallbackResult()
  {
    return m_adfCallbackResultCollection.ToString();
  }

  public void RaiseCallbackEvent(string eventArgs)
  {
    m_adfCallbackResultCollection = new CallbackResultCollection();

    m_nameValueCollection = CallbackUtility.ParseStringIntoNameValueCollection(eventArgs);
    string eventArg = m_nameValueCollection["EventArg"];

    if (eventArg.Equals("checkJob"))
    {
      GPJobStatus();
    }
  }

  /// <summary>
  /// Get the GP job status for a provided job id
  /// </summary>
  protected void GPJobStatus()
  {
    // Parse GP arguments to check job status: job id, diagram name, diagram id, diagram type
    string gpJobArgs = m_nameValueCollection["gpJobArgs"];
    char[] parserChar = { ',' };
    string[] gpArguments = gpJobArgs.Split(parserChar);
    string jobID = gpArguments[0];
    string diagramName = gpArguments[1];
    string diagramID = gpArguments[2];
    string diagramTypeName = gpArguments[3];

    // Create GP functionality
    GeoprocessingResourceItem gpResourceItem = (GeoprocessingResourceItem)(GeoprocessingResourceManager1.ResourceItems[0]);
    IGeoprocessingResource gpResource = (IGeoprocessingResource)gpResourceItem.Resource;
    IGeoprocessingFunctionality gpFunctionality = (IGeoprocessingFunctionality)(gpResource.CreateFunctionality(typeof(IGeoprocessingFunctionality), null));
    GeoprocessingFunctionality agsGpFunctionality = (GeoprocessingFunctionality)gpFunctionality;

    JobStatus adfJobStatus = agsGpFunctionality.GetJobStatus(jobID);

    if (adfJobStatus == JobStatus.Failed || adfJobStatus == JobStatus.TimedOut)
    {
      // GP job failed, hide progress bar, get last job message and return it in an alert box
      JobMessage[] adfJobMessageArray = agsGpFunctionality.GetJobMessages(jobID);
      int messageCount = adfJobMessageArray.Length;
      string errorMessage = "Failed to update " + diagramName + System.Environment.NewLine + "GP job error: " + adfJobMessageArray[messageCount - 1].MessageDesc;
      CallbackResult callbackResultError = new CallbackResult(null, null, "javascript", "alert('" + errorMessage + "')");
      m_adfCallbackResultCollection.Add(SchematicCommands.Utilities.ShowAjaxIndicator(false));
      m_adfCallbackResultCollection.Add(callbackResultError);
      Session["UpdatingDiagram"] = null;
      return;
    }
    else if (!(adfJobStatus == JobStatus.Succeeded))
    {
      // GP job not finished, create a callback response to trigger another callback to check status in 5 seconds
      string callbackReplaceString = ADFCallbackFunctionString.Replace("'", "\\'");
      string timerFunctionString = string.Format("window.setTimeout('checkJob(\"gpJobArgs={0},{1},{2},{3}\",\"{4}\")',5000);", jobID, diagramName, diagramID, diagramTypeName, callbackReplaceString);
      CallbackResult checkJobCallbackResult = new CallbackResult(this, "javascript", timerFunctionString);
      m_adfCallbackResultCollection.Add(checkJobCallbackResult);
      return;

    }

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

    DropDownBox dropBox = (DropDownBox)Toolbar1.ToolbarItems.Find("DiagramList");

    string selectedDiagram = "";
    if (dropBox.SelectedIndex > 0)
    {
      selectedDiagram = dropBox.SelectedItem.Text;
    }

    if (diagramTypeName == (string)Session["CurrentViewType"] & diagramName == selectedDiagram)
    {
      ISchematicDiagramClass schDiagramClass;
      schDiagramClass = SchematicCommands.Utilities.GetCurrentDiagramClass(Map1);

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

      m_adfCallbackResultCollection.CopyFrom(Map1.CallbackResults);
      Session["DiagramFullExtent"] = Map1.Extent;
    }

    Session["UpdatingDiagram"] = null;
    m_adfCallbackResultCollection.Add(SchematicCommands.Utilities.ShowAjaxIndicator(false));
  }

  
  protected void Page_Load(object sender, EventArgs e)
  {
    // Generate custom callback function string for non-ADF controls\elements in page that generate callbacks
    m_adfCallbackFunctionString = Page.ClientScript.GetCallbackEventReference(this, "message", "processCallbackResult", "context", "postBackError", true);

    if (Session["CurrentViewType"] == null)
      Session["CurrentViewType"] = "GeographicMap";
  }

  protected void Page_PreRender(object sender, EventArgs e)
  {
    // Insert script block containing custom JavaScript to manage client browser interaction with server-side customizations in this sample.

    string scriptKeyCustom = "customChangeScript";

    if ((!(this.Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), scriptKeyCustom))) && (!Page.IsPostBack))
    {
      string scriptBlock = System.Environment.NewLine + " function checkJob(callbackArguments, callbackFunctionString)"
                         + System.Environment.NewLine + " {"
                         + System.Environment.NewLine + "   var message = \"EventArg=checkJob\";"
                         + System.Environment.NewLine + "   if (callbackArguments.length > 0) message += \"&\" + callbackArguments;"
                         + System.Environment.NewLine + "   var context = null;"
                         + System.Environment.NewLine + "   eval(callbackFunctionString);"
                         + System.Environment.NewLine + " }"
                         + System.Environment.NewLine;

      this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), scriptKeyCustom, scriptBlock, true);
    }
  }

} // partial class _Default