/// <summary>
        /// Delete a single deployment record 
        /// </summary>
        /// <param name="d"></param>
        public void DeleteDeployment(Deployments d)
        {
            dbMutex.WaitOne();

            db.Deployment.DeleteOnSubmit(d);

            SaveChangesToDB();
            dbMutex.ReleaseMutex();
        }
        private void _DownloadDeployments(List<Parameter> Parameters, string urlPart="")
        {
            var restClient = new RestClient(TrackerUrl+urlPart);
            var request = new RestRequest(Method.GET);
            foreach (Parameter p in Parameters)
            {

                request.AddParameter(p);

            }
            restClient.ExecuteAsync(request, response =>
            {

                if (response.StatusCode == HttpStatusCode.OK)
                {

                    JToken job = JToken.Parse(response.Content);
                    List<Deployments> deployments = new List<Deployments>();
                    foreach (JObject jvalue in job.Values())
                    {

                        Deployments deploy = new Deployments
                        {
                            id = (string)jvalue.SelectToken("id"),
                            description = (string)jvalue.SelectToken("description"),
                            url = (string)jvalue.SelectToken("url"),
                            name = (string)jvalue.SelectToken("name"),
                            latitude = (string)jvalue.SelectToken("latitude"),
                            longitude = (string)jvalue.SelectToken("longitude"),
                            category_id = (string)jvalue.SelectToken("category_id"),
                            discovery_date = (string)jvalue.SelectToken("discovery_date"),
                            ss1 = (string)jvalue.SelectToken("ss1")
                        };

                        deployments.Add(deploy);
                    }

                    DataDownloadComplete.Invoke(this, new DownloadCompleteArgs()
                    {
                        DownloadObject = deployments,
                        Response = response

                    });

                }

            });
        }
        public void DownloadCategories(Deployments deployment, string IncidentId)
        {
            var restClient = new RestClient(deployment.url + "/api?task=comments&by=reportid&id=" + IncidentId);
            var request = new RestRequest(Method.GET);

            restClient.ExecuteAsync(request, response =>
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(RootObjectComments));
                RootObjectComments root;
                using (MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(response.Content)))
                {
                    root = serializer.ReadObject(stream) as RootObjectComments;
                }

                if (root.payload == null)
                {
                    DataDownloadCompleteWithError.Invoke(this, new DownloadCompleteArgs()
                    {
                        DownloadObject = new Error() { message = "Comments download Failed", code = "0" },
                        Response = response

                    });
                }
                else
                {
                    DataDownloadComplete.Invoke(this, new DownloadCompleteArgs()
                    {
                        DownloadObject = root,
                        Response = response
                    });
                }
            });
        }
        /// <summary>
        /// Save a Deployment record
        /// </summary>
        /// <param name="deployments"></param>
        public void saveDeployment(Deployments deployments)
        {
            if (deployments.isLocal)
            {
                db.Deployment.InsertOnSubmit(deployments);
            }
            else
            {
                if (deployments.name.ToLower() != "ushahidi")
                {
                    Deployments deployment = getDeployment(deployments.id);

                    if (deployment == null)
                    {
                        db.Deployment.InsertOnSubmit(deployments);
                    }
                }
            }

            SaveChangesToDB();
        }
        private void SaveButton_Click(object sender, System.EventArgs e)
        {
            if (isEdit)
            {
                saveEdit();
            }
            else
            {

                if (ValidateAndGetUri(UrlTextBox.Text))
                {
                    if (NameTextBox.Text.Trim() != string.Empty)
                    {
                        if (selectedLocation != null)
                        {
                            Deployments deployment = new Deployments();
                            deployment.name = NameTextBox.Text;
                            deployment.isLocal = true;
                            deployment.description = DescriptionTextBox.Text;
                            deployment.url = UrlTextBox.Text;
                            deployment.latitude = selectedLocation.Latitude.ToString();
                            deployment.longitude = selectedLocation.Longitude.ToString();
                            deployment.discovery_date = DateTime.Now.ToString();
                            App.DataBaseUtility.saveDeployment(deployment);
                            NavigationService.GoBack();

                        }
                        else
                        {
                            MessageBox.Show("Select Location from the map.");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Deployment Name missing.");
                    }
                }
                else
                {
                    MessageBox.Show("URL is not valid");
                }
            }
        }
        /// <summary>
        /// Try to download incidents from the server.
        /// </summary>
        /// <param name="deployment"></param>
        public void DownloadIncidents(Deployments deployment)
        {
            var restClient = new RestClient(deployment.url + "/api?task=incidents&by=all&limit=100");
            var request = new RestRequest(Method.GET);
            //request.AddParameter("by", "all");
            //request.AddParameter("limit", "100");
            restClient.ExecuteAsync(request, response =>
             {

                 if (response.Content.Trim() == string.Empty)
                 {
                     DataDownloadComplete.Invoke(this, new DownloadCompleteArgs()
                     {
                         DownloadObject = new List<Incident>(),
                         Response = response

                     });
                 }
                 else
                 {

                     DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(RootObject));
                     RootObject root;
                     using (MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(response.Content)))
                     {
                         root = serializer.ReadObject(stream) as RootObject;
                     }

                     if (root.payload == null)
                     {
                         RestSharp.Deserializers.JsonDeserializer dj = new RestSharp.Deserializers.JsonDeserializer();
                         Error requestError = dj.Deserialize<Error>(response);
                         DataDownloadCompleteWithError.Invoke(this, new DownloadCompleteArgs()
                         {
                             DownloadObject = requestError,
                             Response = response

                         });

                     }
                     else
                     {
                         DataDownloadComplete.Invoke(this, new DownloadCompleteArgs()
                         {
                             DownloadObject = root.payload.incidents,
                             Response = response

                         });
                     }
                 }

             });
        }