// Called by SummariesController when new data is retrieved
 // the data may not belong to our current placemark so we need to check the ids
 void OnSummaryUpdate(object sender, SummaryUpdateArgs e)
 {
     PointDataSummary summary = e.result;
         if (CurrentPlaceMark != null && summary.Guid == CurrentPlaceMark.Id)
         {
             // NOTE: because we are called by an ASynch thread,
             // we have to marshall to the UI thread with Dispatcher
             this.Dispatcher.BeginInvoke(delegate()
             {
                 CurrentSummary = summary;
                 infoViewBox.Data = summary;
                 ratingViewBox.Data = summary;
                 tagViewBox.Data = summary;
                 commentViewBox.Data = summary;
             });
         }
 }
        private void ShowByGuid_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                WebClient client = (WebClient)sender;
                string guid = client.Headers["RequestGuid"];
                if (PDSummaries.ContainsKey(guid))
                {
                    // TODO: Create a new summary on the server based on the info we have
                    if(this.baseController.User.IsAuthenticated)
                    {
                        CreatePointDataSummary(PDSummaries[guid]);
                    }
                }
            }
            else
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(PointDataSummary));
                PointDataSummary res = (PointDataSummary)serializer.ReadObject(e.Result);
                res.LastRefreshed = DateTime.Now;
                if (res != null && res.Id != 0 )
                {
                    if (PDSummaries.ContainsKey(res.Guid))
                    {
                        res.CurrentUserRating = PDSummaries[res.Guid].CurrentUserRating;
                        PDSummaries[res.Guid] = res;
                    }
                    if (res.CommentCount > 0)
                    {
                        GetCommentsForSummary(res.Guid);
                    }

                }
                else // does not exist, let's create it
                {

                }
                SummaryUpdateArgs args = new SummaryUpdateArgs(res);
                SummaryUpdate(this, args);
            }
        }
        void onEditPlaceMarkSummary(IAsyncResult asyncResult)
        {
            string result;
            WebRequest request = (WebRequest)asyncResult.AsyncState;

            // Get the response stream.
            WebResponse response = null;

            try
            {
                response = request.EndGetResponse(asyncResult);
                Stream responseStream = response.GetResponseStream();
                // Read the returned text.
                // StreamReader reader = new StreamReader(responseStream);
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(PointDataSummary));
                PointDataSummary summary = (PointDataSummary)serializer.ReadObject(responseStream);
                if (PDSummaries.ContainsKey(summary.Guid))
                {
                    summary.CurrentUserRating = PDSummaries[summary.Guid].CurrentUserRating;
                }
                PDSummaries[summary.Guid] = summary;
                SummaryUpdateArgs args = new SummaryUpdateArgs(summary);
                SummaryUpdate(this, args);
            }
            catch (Exception e)
            {
                result = "Error contacting service. " + e.Message;
            }
            finally
            {
                if (response != null)
                    response.Close();
            }
        }
        void OnAddTag(IAsyncResult asyncResult)
        {
            string result = "";

            WebRequest request = (WebRequest)asyncResult.AsyncState;

            // Get the response stream.
            WebResponse response = request.EndGetResponse(asyncResult);
            Stream responseStream = response.GetResponseStream();
            try
            {
                // Read the returned text.
                // StreamReader reader = new StreamReader(responseStream);
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(PointDataSummary));
                PointDataSummary addTagResult = (PointDataSummary)serializer.ReadObject(responseStream);
                string guid = request.Headers["RequestGuid"];
                if (PDSummaries.ContainsKey(guid))
                {
                    PointDataSummary tempSummary = PDSummaries[guid];
                    tempSummary.Tag = addTagResult.Tag;
                    tempSummary.CurrentUserRating = PDSummaries[guid].CurrentUserRating;
                    PDSummaries[guid] = tempSummary;

                    SummaryUpdateArgs args = new SummaryUpdateArgs(tempSummary);
                    SummaryUpdate(this, args);
                }
            }
            catch (Exception e)
            {
                result = "Error contacting service." + e.Message;
            }
            finally
            {
                response.Close();
            }
        }
        private void GetComments_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            WebClient client = (WebClient)sender;
            string guid = client.Headers["RequestGuid"];

            if (e.Error != null)
            {
                if (PDSummaries.ContainsKey(guid))
                {
                    // ??
                }

            }
            else
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(UserComment[]));
                UserComment[] res = (UserComment[])serializer.ReadObject(e.Result);
                if (res != null && res.Length != 0)
                {
                    if (PDSummaries.ContainsKey(guid))
                    {
                        PDSummaries[guid].UserComments = res;
                        SummaryUpdateArgs args = new SummaryUpdateArgs(PDSummaries[guid]);
                        SummaryUpdate(this, args);
                    }
                }
                else
                {
                    // failed, hmm, it should at least be an empty array ...
                }
            }
        }
 // Called by SummariesController when new data is retrieved
 // the data may not belong to our current placemark so we need to check the ids
 void OnSummaryUpdate(object sender, SummaryUpdateArgs e)
 {
     PointDataSummary summary = e.result;
     if (CurrentSummary != null && summary.Guid == CurrentSummary.Guid)
     {
         // NOTE: because we are called by an ASynch thread,
         // we have to marshall to the UI thread with Dispatcher
         this.Dispatcher.BeginInvoke(delegate()
         {
             createViewBox.ClearForm();
             MainPage.PointCreated(summary);
             CurrentSummary = summary;
         });
     }
 }