async virtual protected Task saveTrainingDay(SynchronizationItemViewModel day)
        {
            var res = await BAService.SaveTrainingDayAsync(day.DayInfo.TrainingDay);

            await applySave(res, day);

            //var m = new ServiceManager<SaveTrainingDayCompletedEventArgs>(delegate(BodyArchitectAccessServiceClient client1, EventHandler<SaveTrainingDayCompletedEventArgs> operationCompleted)
            //{
            //    client1.SaveTrainingDayCompleted -= operationCompleted;
            //    client1.SaveTrainingDayCompleted += operationCompleted;
            //    client1.SaveTrainingDayAsync(ApplicationState.Current.SessionData.Token, day.DayInfo.TrainingDay);

            //});


            //m.OperationCompleted += (s, a) =>
            //{
            //    saveResult(a.Result, day, firstTime);
            //};

            //if (!m.Run())
            //{
            //    IsBusy = false;
            //    day.State = MergeState.Error;
            //}
        }
예제 #2
0
        async protected Task saveToTheDb(bool goBack = false)
        {
            if (ApplicationState.Current == null)
            {
                return;
            }

            if (ReadOnly)
            {
                //Translation i huj wie co
                BAMessageBox.ShowError("Cannot modify entry for another user");
                return;
            }
            ExtensionMethods.BindFocusedTextBox();
            if (!ValidateBeforeSave())
            {
                return;
            }

            progressBar.ShowProgress(true, ApplicationStrings.EntryObjectPageBase_ProgressUpdating);
            BeforeSaving();
            DateTime date = ApplicationState.Current.TrainingDay.TrainingDay.TrainingDate;

            //first save a copy to the local storage
            ThreadPool.QueueUserWorkItem(delegate
            {
                var copy        = ApplicationState.Current.TrainingDay.Copy();
                copy.IsModified = true;
                ApplicationState.Current.CurrentBrowsingTrainingDays.TrainingDays[copy.TrainingDay.TrainingDate] = copy;
            });

            try
            {
                var entryBeforeSave = Entry;//see the comment where this is used belove
                var result          = await BAService.SaveTrainingDayAsync(ApplicationState.Current.TrainingDay.TrainingDay);

                if (result.TrainingDay != null)
                {
                    var savedDay = result.TrainingDay;
                    savedDay.FillInstaneId(ApplicationState.Current.TrainingDay.TrainingDay);
                    savedDay.TrainingDate = date;//bug fixing: web service converts date to local or something like this. but this is only in SaveTrainingDay method
                    var tdi = ApplicationState.Current.CurrentBrowsingTrainingDays.TrainingDays[savedDay.TrainingDate];
                    tdi.TrainingDay = savedDay;
                    tdi.IsModified  = false;
                    ApplicationState.Current.TrainingDay = tdi.Copy();

                    //if we use instance id for determining current entry then after save we should always use globalid
                    if (Entry != null)
                    {
                        ApplicationState.Current.CurrentEntryId = new LocalObjectKey(Entry);
                    }

                    await SavingCompleted();

                    progressBar.ShowProgress(false);
                    if (result.NewRecords.Count > 0)
                    {
                        BAMessageBox.ShowInfo(ApplicationStrings.MessageNewRecords, new Uri("/Images/Records32.png", UriKind.RelativeOrAbsolute));
                    }
                }
                else
                {
                    progressBar.ShowProgress(false);
                    //if (ApplicationState.Current.TrainingDay != null && ApplicationState.Current.TrainingDay.TrainingDay.TrainingDate == date)
                    //{
                    //    ApplicationState.Current.TrainingDay = null;
                    //}
                    if (!goBack)
                    {//user remove all data from the trainig day so on the server this day is removed. so on the client we "mark" this item as a new item
                        ApplicationState.Current.TrainingDay.TrainingDay.GlobalId = Guid.Empty;
                        var tmpEntry = Entry;
                        tmpEntry.GlobalId = Guid.Empty;
                        tmpEntry.Version  = 0;
                        ApplicationState.Current.CurrentEntryId = new LocalObjectKey(tmpEntry);
                    }
                    ApplicationState.Current.CurrentBrowsingTrainingDays.TrainingDays.Remove(date);
                }
                if (ApplicationState.Current.TrainingDay != null && !IsClosing && !goBack)
                {
                    if (Entry == null && !ApplicationState.Current.TrainingDay.TrainingDay.Objects.Contains(entryBeforeSave))
                    {//this is invoked when for example user saved empty supplements entry so on the server this entry is removed and therefore after save Entry returns null. so we must restore the entry before saving operation (this is a special case)
                        ApplicationState.Current.TrainingDay.TrainingDay.Objects.Add(entryBeforeSave);
                        entryBeforeSave.TrainingDay = ApplicationState.Current.TrainingDay.TrainingDay;
                        entryBeforeSave.GlobalId    = Guid.Empty;
                        entryBeforeSave.Version     = 0;
                    }
                    show(true);
                }

                if (goBack)
                {
                    ApplicationState.Current.TrainingDay = null;
                    if (NavigationService.CanGoBack && !IsClosing)
                    {
                        NavigationService.GoBack();
                    }
                }
            }
            catch (NetworkException)
            {
                progressBar.ShowProgress(false);
                if (ApplicationState.Current.IsOffline)
                {
                    BAMessageBox.ShowWarning(ApplicationStrings.EntryObjectPageBase_SavedLocallyOnly);
                }
                else
                {
                    BAMessageBox.ShowError(ApplicationStrings.ErrNoNetwork);
                }
            }
            catch (ValidationException validEx)
            {
                progressBar.ShowProgress(false);
                if (IsClosing)
                {
                    return;
                }
                BAMessageBox.ShowError(validEx.Results.First().Key + ":" + validEx.Results.First().Message);
            }
            catch (OldDataException oldData)
            {
                progressBar.ShowProgress(false);
                if (IsClosing)
                {
                    return;
                }
                BAMessageBox.ShowError(ApplicationStrings.ErrOldData);
            }
            catch (LicenceException licence)
            {
                progressBar.ShowProgress(false);
                if (IsClosing)
                {
                    return;
                }
                BAMessageBox.ShowError(ApplicationStrings.ErrLicence);
            }
            catch (Exception ex)
            {
                progressBar.ShowProgress(false);
                BugSenseHandler.Instance.SendExceptionAsync(ex);
                if (IsClosing)
                {
                    return;
                }

                BAMessageBox.ShowWarning(ApplicationStrings.EntryObjectPageBase_SavedLocallyOnly);
            }



            //var m = new ServiceManager<SaveTrainingDayCompletedEventArgs>(delegate(BodyArchitectAccessServiceClient client1, EventHandler<SaveTrainingDayCompletedEventArgs> operationCompleted)
            //{
            //    client1.SaveTrainingDayCompleted -= operationCompleted;
            //    client1.SaveTrainingDayCompleted += operationCompleted;
            //    client1.SaveTrainingDayAsync(ApplicationState.Current.SessionData.Token, ApplicationState.Current.TrainingDay.TrainingDay);

            //});


            //m.OperationCompleted += async (s, a) =>
            //{
            //    if (a.Error != null)
            //    {
            //        progressBar.ShowProgress(false);
            //        if(IsClosing)
            //        {
            //            return;
            //        }
            //        FaultException<ValidationFault> faultEx = a.Error as FaultException<ValidationFault>;
            //        FaultException<BAServiceException> serviceEx = a.Error as FaultException<BAServiceException>;
            //        if (faultEx != null)
            //        {
            //            BAMessageBox.ShowError(faultEx.Detail.Details[0].Key + ":" + faultEx.Detail.Details[0].Message);
            //            return;
            //        }
            //        if (serviceEx!=null)
            //        {
            //            if(serviceEx.Detail.ErrorCode==ErrorCode.OldDataException)
            //            {
            //                BAMessageBox.ShowError(ApplicationStrings.ErrOldData);
            //                return;
            //            }
            //            else if(serviceEx.Detail.ErrorCode==ErrorCode.LicenceException)
            //            {
            //                BAMessageBox.ShowError(ApplicationStrings.ErrLicence);
            //                return;
            //            }
            //        }

            //        BAMessageBox.ShowWarning(ApplicationStrings.EntryObjectPageBase_SavedLocallyOnly);
            //        return;
            //    }
            //    else
            //    {
            //        if (a.Result.Result.TrainingDay != null)
            //        {
            //            var savedDay = a.Result.Result.TrainingDay;
            //            //savedDay.InstanceId = dayInstanceId;
            //            //var savedEntry=savedDay.GetEntry(EntryType);
            //            //if (savedEntry != null)
            //            //{
            //            //    savedEntry.InstanceId = entryInstanceId;
            //            //}
            //            savedDay.FillInstaneId(ApplicationState.Current.TrainingDay.TrainingDay);
            //            savedDay.TrainingDate = date;//bug fixing: web service converts date to local or something like this. but this is only in SaveTrainingDay method
            //            var tdi=ApplicationState.Current.CurrentBrowsingTrainingDays.TrainingDays[savedDay.TrainingDate];
            //            tdi.TrainingDay = savedDay;
            //            tdi.IsModified = false;
            //            ApplicationState.Current.TrainingDay = tdi.Copy();
            //            await SavingCompleted();

            //            progressBar.ShowProgress(false);
            //            if(a.Result.Result.NewRecords.Count>0)
            //            {
            //                BAMessageBox.ShowInfo(ApplicationStrings.MessageNewRecords, new Uri("/Images/Records32.png", UriKind.RelativeOrAbsolute));
            //            }
            //        }
            //        else
            //        {
            //            progressBar.ShowProgress(false);
            //            if (ApplicationState.Current.TrainingDay != null && ApplicationState.Current.TrainingDay.TrainingDay.TrainingDate == date)
            //            {
            //                ApplicationState.Current.TrainingDay = null;
            //            }
            //            ApplicationState.Current.CurrentBrowsingTrainingDays.TrainingDays.Remove(date);
            //        }
            //        if (ApplicationState.Current.TrainingDay != null && !IsClosing)
            //        {
            //            show(ApplicationState.Current.TrainingDay.TrainingDay, true);
            //        }

            //        if (goBack)
            //        {
            //            ApplicationState.Current.TrainingDay = null;
            //            if (NavigationService.CanGoBack && !IsClosing)
            //            {
            //                NavigationService.GoBack();
            //            }
            //        }
            //    }

            //};

            //if(!m.Run())
            //{
            //    progressBar.ShowProgress(false);
            //    if (ApplicationState.Current.IsOffline)
            //    {
            //        BAMessageBox.ShowWarning(ApplicationStrings.EntryObjectPageBase_SavedLocallyOnly);
            //    }
            //    else
            //    {
            //        BAMessageBox.ShowError(ApplicationStrings.ErrNoNetwork);
            //    }
            //}
        }