internal MyTraining StopMyTraining(Guid myTrainingId, Profile dbProfile)
        {
            var dbCycle = Session.QueryOver <MyTraining>()
                          .Fetch(x => x.EntryObjects).Eager
                          .Fetch(x => x.EntryObjects.First().TrainingDay).Eager
                          .Fetch(x => (((SuplementsEntry)x.EntryObjects.First()).Items).First().Suplement).Eager
                          .Fetch(x => ((StrengthTrainingEntry)x.EntryObjects.First()).Entries).Eager
                          .Fetch(x => ((StrengthTrainingEntry)x.EntryObjects.First()).MyPlace).Eager
                          .Fetch(x => (((StrengthTrainingEntry)x.EntryObjects.First()).Entries).First().Exercise).Eager
                          .Fetch(x => (((StrengthTrainingEntry)x.EntryObjects.First()).Entries.First().Series)).Eager
                          .Where(
                x => x.GlobalId == myTrainingId).SingleOrDefault();

            dbCycle = Session.Get <MyTraining>(myTrainingId);
            if (dbCycle.Profile != dbProfile)
            {
                throw new CrossProfileOperationException("MyTraining doesn't belong to this profile");
            }
            dbCycle.Complete(Configuration.TimerService);
            var plannedEntries = dbCycle.EntryObjects
                                 .Where(x => x.Status == EntryObjectStatus.Planned).ToList();
            bool trainingDayDeleted = false;

            foreach (var entryObject in plannedEntries)
            {
                dbCycle.EntryObjects.Remove(entryObject);
                var td = entryObject.TrainingDay;
                td.RemoveEntry(entryObject);
                Session.Delete(entryObject);
                if (td.IsEmpty)
                {
                    Session.Delete(td);
                    trainingDayDeleted = true;
                }
            }
            Session.Flush();
            if (trainingDayDeleted)
            {
                ProfileStatisticsUpdater.UpdateTrainindDay(Session, dbProfile);
            }
            Session.Update(dbCycle);
            return(dbCycle);
        }
        public MyTrainingDTO MyTrainingOperation(MyTrainingOperationParam param)
        {
            Log.WriteWarning("MyTrainingOperation:Username={0},operation={1}", SecurityInfo.SessionData.Profile.UserName, param.Operation);

            using (var trans = Session.BeginSaveTransaction())
            {
                var        dbProfile = Session.Load <Profile>(SecurityInfo.SessionData.Profile.GlobalId);
                MyTraining dbCycle   = null;
                if (param.Operation == MyTrainingOperationType.Start || param.Operation == MyTrainingOperationType.Simulate)
                {
                    dbCycle = param.MyTraining.Map <MyTraining>();
                    ensureCanStartMyTraining(dbCycle, dbProfile);
                    dbCycle.Profile = dbProfile;
                    if (dbCycle is A6WTraining)
                    {
                        startA6WTraining(param, (A6WTraining)dbCycle, dbProfile);
                    }
                    else if (dbCycle is SupplementCycle)
                    {
                        SupplementsCycleDTO cycleDto = (SupplementsCycleDTO)param.MyTraining;
                        startSupplementsCycle(param, (SupplementCycle)dbCycle, dbProfile, cycleDto.SupplementsCycleDefinitionId);
                    }
                    Session.Flush();
                    ProfileStatisticsUpdater.UpdateTrainindDay(Session, dbProfile);
                }
                else
                {
                    dbCycle = StopMyTraining(param.MyTraining.GlobalId, dbProfile);
                }
                Session.SaveOrUpdate(dbCycle);

                if (param.Operation != MyTrainingOperationType.Simulate)
                {//for simulate we must reject all changes in the db
                    trans.Commit();
                }

                return(dbCycle.Map <MyTrainingDTO>());
            }
        }