private void OnAssignTo(object assignToName)
        {
            Item item = GetSelectedItem();
              if (item == null)
            return;

              String uri = GetGEPUrl(true) + GetPropValue("GEP_INCIDENTS_ENDPOINT") + "/" +
                   item.GetPropertyValue(TrackIdFieldName) as String +
                   "/properties.json?" +
                   GetPropValue("ALERT_ASSIGN_TO_URI_PROP") +
                   "=" +
                   assignToName.ToString();

              HttpRequest request = new HttpRequest(uri, "POST", "application/json", 1000);
              request.SetCredentials("arcgis", "manager");
              HttpResponse response = request.ExecuteHttpRequest();
              response.ReportOnGeoEventProcessorError("Alert Assign-To error");
        }
        private void OnToggleActive(object obj)
        {
            Item item = GetSelectedItem();
              if (item == null)
            return;
              if (!item.Graphic.Attributes.ContainsKey("OBJECTID"))
            return;
              FeatureLayer featureLayer = GetFeatureLayer();
              if (featureLayer == null)
            return;
              int oid = Int32.Parse(item.Graphic.Attributes["OBJECTID"].ToString());
              if (oid == 0)
            return;

              String activeState = item.Graphic.Attributes[ActiveFieldName].ToString().ToLower();
              if (activeState.Equals("true"))
            activeState = "false";
              else
            activeState = "true";

              item.Graphic.Attributes[ActiveFieldName] = activeState;
              //featureLayer.SaveEdits();
              //featureLayer.Update();

              /*
              GraphicCollection gc = new GraphicCollection();
              gc.Add(item.Graphic);
              FeatureSet fs = new FeatureSet(gc);
              String fsJsonString = fs.ToJson();
              JObject jObjFeatureSet = JObject.Parse(fsJsonString);
              JArray jArrayFeatures = jObjFeatureSet["features"] as JArray;
              String httpRequestBodyJSON = "f=json&features=" + jArrayFeatures.ToString();
              */

              String httpRequestBodyJSON = "f=json&features=";
              httpRequestBodyJSON += "[ { \"attributes\": { \"OBJECTID\": ";
              httpRequestBodyJSON += oid;
              httpRequestBodyJSON += ", \"Active\": \"" + activeState + "\" } } ]";

              // HTTP POST the new GeoFences
              String uri = featureLayer.Url + "/updateFeatures";
              HttpRequest request = new HttpRequest(uri, "POST", "application/x-www-form-urlencoded", 10000, httpRequestBodyJSON);
              HttpResponse response = request.ExecuteHttpRequest();
              response.ReportOnArcGISServerError("Toggle Active GeoFence State error");
        }
        private void OnDismiss(object obj)
        {
            Item item = GetSelectedItem();
              if (item == null)
            return;

              String uri = GetGEPUrl(true) + GetPropValue("GEP_INCIDENTS_ENDPOINT") + "/" +
                   item.GetPropertyValue(TrackIdFieldName) as String +
                   "/properties.json?" +
                   GetPropValue("ALERT_DISMISS_STATUS_URI_PROP") +
                   "=" +
                   GetPropValue("ALERT_DISMISSED_VALUE");

              HttpRequest request = new HttpRequest(uri, "POST", "application/json", 1000);
              request.SetCredentials("arcgis", "manager");
              HttpResponse response = request.ExecuteHttpRequest();
              response.ReportOnGeoEventProcessorError("Alert dismiss error");
        }
    internal bool LoadPlan(DateTime? date)
    {
      string uri = GetGEPUrl(false) + GetPropValue("GEP_LOAD_PLAN_ENDPOINT") + ".json";
      string dateString = date.Value.ToString("yyyyMMdd");
      string requestId = GenerateRequestId("Load-Plan");
      string body = "{\"Action\":\"Load\",\"PlanFolder\":\"" + dateString + "\", \"RequestId\":\"" + requestId + "\"}";
      Log.Trace("Load Plan '" + date.ToString() + "' [" + uri + "] ...");
      HttpRequest request = new HttpRequest(uri, "POST", "application/json", 300000, body);
      HttpResponse response = request.ExecuteHttpRequest();
      if (response.ReportOnGeoEventProcessorError("Stops - Load Plan error"))
        return false;

      RefreshView();
      return true;
    }
    internal bool ClearPlan()
    {
      string uri = GetGEPUrl(false) + GetPropValue("GEP_LOAD_PLAN_ENDPOINT") + ".json";
      string requestId = GenerateRequestId("Clear-Plan");
      string body = "{\"Action\":\"Clear\",\"PlanFolder\":\"\",\"Status\":\"\", \"RequestId\":\"" + requestId + "\"}";
      Log.Trace("Clear Plan [" + uri + "] ...");
      HttpRequest request = new HttpRequest(uri, "POST", "application/json", 300000, body);
      HttpResponse response = request.ExecuteHttpRequest();
      if (response.ReportOnGeoEventProcessorError("Stops - Clear Plan error"))
        return false;

      return true;

    }
    internal void SendUpdateRouteRequest(Window owner)
    {
      using (new WaitCursor())
      {
        if (_edits.Count == 0)
          return;

        ShowProgressDialog("Saving Edits...", "Please Wait...", owner, 30);

        List<String> routeRequests = new List<string>();
        foreach (string routeName in _edits)
        {
          CollectionViewGroup group = FindCollectionViewGroup(routeName);
          if (group == null)
            continue;

          // stops
          List<String> stops = new List<string>();
          foreach (Item item in group.Items)
            stops.Add("\"" + item.GetPropertyValue(TrackIdFieldName).ToString() + "\"");

          // formulate route request body
          routeRequests.Add(FormulateHttpRouteRequestBody(routeName, stops, false));
        }

        // merge all requests to one request
        _saveEditsRequestId = GenerateRequestId("Update-Routes");
        string httpRequestBodyJSON = "{\"route\":[";
        httpRequestBodyJSON += String.Join("," , routeRequests);
        httpRequestBodyJSON += "],\"commit\":true,\"RequestId\":\"" + _saveEditsRequestId + "\"}";

        // HTTP POST GEP to update the route(s)
        String uri = GetGEPUrl(false) + GetPropValue("GEP_ROUTES_UPDATE_ENDPOINT") + ".json";
        HttpRequest request = new HttpRequest(uri, "POST", "application/json", 30000, httpRequestBodyJSON);
        Log.Trace("Sent Update Routes Request.");
        HttpResponse response = request.ExecuteHttpRequest();
      }
    }
    private string SendCalculateRouteRequest(String httpRequestBodyJSON, Window owner)
    {
      using (new WaitCursor())
      {
        ShowProgressDialog("Calculating Routes...", "Please Wait...", owner, 30);
        MakeSureWebSocketIsOpen();

        // HTTP POST GEP to calculate the route(s)
        String uri = GetGEPUrl(false) + GetPropValue("GEP_ROUTES_CALCULATE_ENDPOINT") + ".json";
        HttpRequest request = new HttpRequest(uri, "POST", "application/json", 30000, httpRequestBodyJSON);
        Log.Trace("Calculating Routes ...");
        HttpResponse response = request.ExecuteHttpRequest();
        if (response.ReportOnGeoEventProcessorError("Stops - Calculate Edits error"))
          return null;

        return request.Response.Text;
      }
    }
    private bool SendDispatchRequest(string httpRequestBodyJSON)
    {
      String uri = GetGEPUrl(false) + GetPropValue("GEP_DISPATCH_ENDPOINT") + ".json";
      HttpRequest request = new HttpRequest(uri, "POST", "application/json", 30000, httpRequestBodyJSON);
      HttpResponse response = request.ExecuteHttpRequest();
      if (response.ReportOnGeoEventProcessorError("Stops - Dispatch error"))
        return false;

      return true;
    }