Esempio n. 1
0
        private void RetreiveLocalData()
        {
            var trainingWeekScenario = new TrainingWeekScenario()
            {
                ManageTrainingDay = false
            };

            _trainingWeekList = _trainingWeekService.FindTrainingWeek(null, trainingWeekScenario);
        }
Esempio n. 2
0
        public static async Task <bool> SynchronizeTrainingWeeksAsync(SQLiteConnection dbContext)
        {
            bool result = false;

            try
            {
                //Synchronize TrainingWeek with server (with trainingday and exercise)
                var criteria = new TrainingWeekCriteria();
                criteria.UserId = new StringCriteria()
                {
                    Equal = UserData.Instance.UserInfo.UserId
                };
                TrainingWeekScenario trainingWeekScenario = new TrainingWeekScenario()
                {
                    ManageTrainingDay = false
                };
                var trainingWeekService = new TrainingWeekService(dbContext);
                //retreive local data
                var localTrainingWeekList = trainingWeekService.FindTrainingWeek(criteria, trainingWeekScenario);
                //retreive online data
                var criteriaList = new CriteriaList <TrainingWeekCriteria>()
                {
                    criteria
                };
                var onlineTrainingWeekList = await TrainingWeekWebService.FindTrainingWeeksAsync(criteriaList, trainingWeekScenario);

                bool found;
                //Delete local data if not found on server
                if (localTrainingWeekList != null)
                {
                    var deletedTrainingWeekList = new List <TrainingWeek>();
                    foreach (var localTrainingWeek in localTrainingWeekList)
                    {
                        found = false;
                        if (onlineTrainingWeekList != null)
                        {
                            foreach (var onlineTrainingWeek in onlineTrainingWeekList)
                            {
                                if (TrainingWeekKey.IsEqualByKey(onlineTrainingWeek, localTrainingWeek))
                                {
                                    found = true;
                                    break;
                                }
                            }
                        }
                        if (!found)
                        {
                            deletedTrainingWeekList.Add(localTrainingWeek);
                        }
                    }
                    if (deletedTrainingWeekList.Count > 0)
                    {
                        var deletedTrainingWeekKeyList = new List <TrainingWeekKey>();
                        foreach (var key in deletedTrainingWeekList)
                        {
                            deletedTrainingWeekKeyList.Add(key);
                        }
                        //Delete in local database
                        trainingWeekService.DeleteTrainingWeekList(deletedTrainingWeekKeyList);
                        foreach (var deleteTrainingWeek in deletedTrainingWeekList)
                        {
                            localTrainingWeekList.Remove(deleteTrainingWeek);
                        }
                    }
                }
                //if modification date online != local, get full trainingWeek online data and save them on local database
                var synchronizeTrainingWeekList = new List <TrainingWeek>();
                if (onlineTrainingWeekList != null)
                {
                    foreach (var onlineTrainingWeek in onlineTrainingWeekList)
                    {
                        found = false;
                        if (localTrainingWeekList != null)
                        {
                            foreach (var localTrainingWeek in localTrainingWeekList)
                            {
                                //Same trainingWeek
                                if (TrainingWeekKey.IsEqualByKey(onlineTrainingWeek, localTrainingWeek))
                                {
                                    if (onlineTrainingWeek.ModificationDate.ToUniversalTime() != localTrainingWeek.ModificationDate.ToUniversalTime()) //ToUniversalTime for security...
                                    {
                                        synchronizeTrainingWeekList.Add(onlineTrainingWeek);
                                    }
                                    found = true;
                                    break;
                                }
                            }
                        }
                        if (!found)
                        {
                            synchronizeTrainingWeekList.Add(onlineTrainingWeek);
                        }
                    }
                }

                //Synchronize all trainingWeek data
                trainingWeekScenario = new TrainingWeekScenario()
                {
                    ManageTrainingDay   = true,
                    TrainingDayScenario = new TrainingDayScenario()
                    {
                        ManageExercise = true
                    }
                };
                if (synchronizeTrainingWeekList.Count > 0)
                {
                    criteriaList.Clear();
                    foreach (var trainingWeek in synchronizeTrainingWeekList)
                    {
                        criteria        = new TrainingWeekCriteria();
                        criteria.UserId = new StringCriteria()
                        {
                            Equal = trainingWeek.UserId
                        };
                        criteria.Year = new IntegerCriteria()
                        {
                            Equal = trainingWeek.Year
                        };
                        criteria.WeekOfYear = new IntegerCriteria()
                        {
                            Equal = trainingWeek.WeekOfYear
                        };
                        criteriaList.Add(criteria);
                    }
                    onlineTrainingWeekList = await TrainingWeekWebService.FindTrainingWeeksAsync(criteriaList, trainingWeekScenario);

                    if (onlineTrainingWeekList != null && onlineTrainingWeekList.Count > 0)
                    {
                        trainingWeekService.UpdateTrainingWeekList(onlineTrainingWeekList, trainingWeekScenario);
                    }
                }
                result = true;
            }
            catch (Exception except)
            {
                ILogger.Instance.Error("Unable to synchronize training weeks", except);
            }
            return(result);
        }