internal List <TrainingDay> FindTrainingDay(TUnitType userUnit, TrainingDayCriteria trainingDayCriteria, TrainingDayScenario trainingDayScenario) { var trainingDays = _trainingDayModule.Find(userUnit, trainingDayCriteria); if (trainingDayScenario != null && trainingDayScenario.ManageExercise && trainingDays != null && trainingDays.Count > 0) { var criteriaList = new CriteriaList <TrainingExerciseCriteria>(); string userId = null; foreach (var trainingDay in trainingDays) { criteriaList.Add(CreateTrainingExerciseCriteria(trainingDay)); if (userId == null) { userId = trainingDay.UserId; } } var trainingExerciseList = _trainingExercisesService.FindTrainingExercise(criteriaList); if (trainingExerciseList != null) { foreach (var trainingDay in trainingDays) { foreach (var trainingExercise in trainingExerciseList) { if (trainingExercise != null && (trainingDay.UserId != null && trainingDay.UserId == trainingExercise.UserId) && (trainingDay.Year == trainingExercise.Year) && (trainingDay.WeekOfYear == trainingExercise.WeekOfYear) && (trainingDay.DayOfWeek == trainingExercise.DayOfWeek) && (trainingDay.TrainingDayId == trainingExercise.TrainingDayId)) { if (trainingDay.TrainingExercises == null) { trainingDay.TrainingExercises = new List <TrainingExercise>(); } trainingDay.TrainingExercises.Add(trainingExercise); } } } } } return(trainingDays); }
private List <TrainingExercise> FindTrainingExerciseSet(List <TrainingExercise> trainingExercises) { if (trainingExercises != null && trainingExercises.Count > 0) { var criteriaList = new CriteriaList <TrainingExerciseSetCriteria>(); foreach (var trainingExercise in trainingExercises) { criteriaList.Add(CreateTrainingExerciseSetCriteria(trainingExercise)); } var trainingExerciseSetList = _trainingExerciseSetModule.Find(criteriaList); if (trainingExerciseSetList != null) { foreach (var trainingExerciseSet in trainingExerciseSetList) { foreach (var trainingExercise in trainingExercises) { if (trainingExerciseSet != null && (trainingExercise.UserId != null && trainingExercise.UserId == trainingExerciseSet.UserId) && (trainingExercise.Year == trainingExerciseSet.Year) && (trainingExercise.WeekOfYear == trainingExerciseSet.WeekOfYear) && (trainingExercise.DayOfWeek == trainingExerciseSet.DayOfWeek) && (trainingExercise.TrainingDayId == trainingExerciseSet.TrainingDayId) && (trainingExercise.Id == trainingExerciseSet.TrainingExerciseId)) { if (trainingExercise.TrainingExerciseSets == null) { trainingExercise.TrainingExerciseSets = new List <TrainingExerciseSet>(); } trainingExercise.TrainingExerciseSets.Add(trainingExerciseSet); } } } } } return(trainingExercises); }
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); }