public PagedResult <MyTrainingDTO> GetMyTrainings(GetMyTrainingsParam param, PartialRetrievingInfo retrievingInfo)
        {
            Log.WriteWarning("GetMyTrainings:Username={0}", SecurityInfo.SessionData.Profile.UserName);

            using (var transactionScope = Session.BeginGetTransaction())
            {
                MyTraining _temp   = null;
                var        idQuery = Session.QueryOver <MyTraining>(() => _temp);
                idQuery = (QueryOver <MyTraining, MyTraining>)getMyTrainingsCriterias(idQuery, param);
                if (idQuery == null)
                {
                    return(new PagedResult <MyTrainingDTO>(new List <MyTrainingDTO>(), 0, 0));
                }

                var fetchQuery = Session.QueryOver <MyTraining>(() => _temp)
                                 .Fetch(x => x.EntryObjects).Eager
                                 .Fetch(x => ((SuplementsEntry)x.EntryObjects.First()).Items).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()).Entries).First().Exercise).Eager
                                 .Fetch(x => x.EntryObjects.First().LoginData).Eager
                                 .Fetch(x => x.EntryObjects.First().LoginData.ApiKey).Eager
                                 .Fetch(x => x.EntryObjects.First().Reminder).Eager
                                 .Fetch(x => x.EntryObjects.First().TrainingDay).Eager
                                 .Fetch(x => x.EntryObjects.First().TrainingDay.Objects).Lazy;

                var listPack = fetchQuery.ToExPagedResults <MyTrainingDTO, MyTraining>(retrievingInfo, idQuery);
                transactionScope.Commit();
                return(listPack);
            }
        }
        private MyTraining importMyTraining(Guid guid, Profile profie, SqlCeConnection oldConnection)
        {
            if (myTrainings.ContainsKey(guid))
            {
                return(MyTraining.GetById(myTrainings[guid]));
            }
            Log.WriteInfo("Importing MyTraining id={0}", guid);
            SqlCeCommand cmd = new SqlCeCommand();

            cmd.Connection = oldConnection;

            cmd.CommandText = string.Format("SELECT Id,StartDate,EndDate,GlobalId,EntryType,ProfileId,TrainingEnd,PercentageCompleted,Name FROM MyTraining WHERE GlobalId='{0}'", guid);
            var reader = cmd.ExecuteReader();

            Log.WriteInfo("MyTraining reader executed");

            if (reader.Read())
            {
                MyTraining myTraining = new MyTraining();
                myTraining.Name = getString(reader, "Name");
                var endDate     = getDateTime(reader, "EndDate");
                var startDate   = getDateTime(reader, "StartDate").Value;
                var trainingEnd = (TrainingEnd)getInt32(reader, "TrainingEnd").Value;
                myTraining.SetData(startDate, endDate, trainingEnd);
                myTraining.EntryType           = getString(reader, "EntryType");
                myTraining.ProfileId           = profie.Id;
                myTraining.PercentageCompleted = getInt32(reader, "PercentageCompleted");
                myTraining.CreateAndFlush();
                myTrainings.Add(guid, myTraining.Id);
                return(myTraining);
            }
            return(null);
        }
        public void A6WEntries_TrainingNotFullyFinish()
        {
            var        profile       = CreateProfile("profile");
            MyTraining endedTraining = new MyTraining();

            endedTraining.EndDate             = DateTime.UtcNow.AddDays(-1).Date;
            endedTraining.StartDate           = DateTime.UtcNow.AddDays(-3).Date;
            endedTraining.TrainingEnd         = TrainingEnd.Break;
            endedTraining.Name                = "TempName";
            endedTraining.PercentageCompleted = null;
            endedTraining.Profile             = profile;
            insertToOldDatabase(endedTraining);

            var day1 = new TrainingDay(DateTime.UtcNow.AddDays(-10));

            day1.Comment = "cmt";
            day1.Profile = profile;
            var a6w1 = new A6WEntry();

            a6w1.Comment    = "test";
            a6w1.Completed  = true;
            a6w1.MyTraining = endedTraining;
            a6w1.DayNumber  = 1;
            day1.AddEntry(a6w1);
            insertToOldDatabase(day1);

            var day2 = new TrainingDay(DateTime.UtcNow.AddDays(-10));

            day2.Comment = "cmt";
            day2.Profile = profile;
            var a6w2 = new A6WEntry();

            a6w2.Comment    = "test";
            a6w2.MyTraining = endedTraining;
            a6w2.Completed  = false;
            a6w2.DayNumber  = 2;
            a6w2.Set1       = 2;
            a6w2.Set2       = 3;
            day2.AddEntry(a6w2);
            insertToOldDatabase(day2);

            Convert();

            var dbProfile = SessionNew.QueryOver <Model.Profile>().Where(x => x.UserName == profile.UserName).SingleOrDefault();
            var count     = SessionNew.QueryOver <Model.A6WEntry>().RowCount();

            Assert.AreEqual(count, dbProfile.Statistics.A6WEntriesCount);
        }
 private void ensureCanStartMyTraining(MyTraining myTraining, Profile dbProfile)
 {
     if (!myTraining.IsNew)
     {
         throw new InvalidOperationException("This cycle cannot be started");
     }
     if (myTraining.EndDate.HasValue || myTraining.TrainingEnd != TrainingEnd.NotEnded ||
         myTraining.PercentageCompleted != 0)
     {
         throw new ArgumentException("Wrong parameters");
     }
     if (myTraining.StartDate == DateTime.MinValue)
     {
         throw new ArgumentException("Wrong start date");
     }
     if (myTraining.Customer != null && myTraining.Customer.Profile != dbProfile)
     {
         throw new CrossProfileOperationException("Customer not belong to this profile");
     }
 }
        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>());
            }
        }
Example #6
0
        public override void BuildDatabase()
        {
            using (var tx = Session.BeginTransaction())
            {
                profiles.Clear();
                myTrainings.Clear();
                profiles.Add(CreateProfile(Session, "test1"));
                profiles.Add(CreateProfile(Session, "test2"));
                profiles.Add(CreateProfile(Session, "test3"));

                //set friendship
                profiles[0].Friends.Add(profiles[1]);
                profiles[1].Friends.Add(profiles[0]);
                Session.Update(profiles[0]);
                Session.Update(profiles[1]);

                customer = CreateCustomer("Cust1", profiles[0]);

                var sup = CreateSupplement("sup");
                definition = CreateSupplementsCycleDefinition("def", sup, profiles[0]);
                var myTraining = CreateTraining <SupplementCycle>("sup1", DateTime.UtcNow.Date.AddDays(-3), TrainingEnd.Complete, 21, profiles[0], DateTime.UtcNow.Date);
                myTrainings.Add(myTraining);

                myTraining = CreateTraining <SupplementCycle>("sup2", DateTime.UtcNow.Date.AddDays(-7), TrainingEnd.Complete, 100, profiles[0], DateTime.UtcNow.Date.AddDays(-4));
                myTrainings.Add(myTraining);

                myTraining = CreateTraining <SupplementCycle>("sup3", DateTime.UtcNow.Date.AddDays(-5), TrainingEnd.NotEnded, 2, profiles[0]);
                myTrainings.Add(myTraining);

                var a6WTraining = CreateTraining <A6WTraining>("a6w1", DateTime.UtcNow.Date.AddDays(-4), TrainingEnd.NotEnded, 12, profiles[0]);
                myTrainings.Add(a6WTraining);

                a6WTraining = CreateTraining <A6WTraining>("a6w2", DateTime.UtcNow.Date.AddDays(-14), TrainingEnd.Complete, 100, profiles[0], DateTime.UtcNow.Date.AddDays(-10));
                myTrainings.Add(a6WTraining);

                customerTraining = CreateTraining <A6WTraining>("a6w3", DateTime.UtcNow.Date.AddDays(-14), TrainingEnd.Complete, 100, profiles[0], DateTime.UtcNow.Date.AddDays(-10), customer);
                tx.Commit();
            }
        }
        private void createEntryObject(MyTrainingDTO param, int dayIndex, Profile dbProfile, MyTraining dbCycle, Func <IEnumerable <EntryObject> > createMethod)
        {
            var entries = createMethod();

            foreach (var entry in entries)
            {
                var newDate     = dbCycle.StartDate.Date.AddDays(dayIndex);
                var trainingDay = Session.QueryOver <TrainingDay>().Where(x => x.TrainingDate == newDate && x.Profile == dbCycle.Profile && x.Customer == dbCycle.Customer).SingleOrDefault();
                if (trainingDay == null)
                {
                    trainingDay               = new TrainingDay(newDate);
                    trainingDay.Profile       = dbProfile;
                    trainingDay.AllowComments = dbProfile.Settings.AllowTrainingDayComments;
                    trainingDay.Customer      = dbCycle.Customer;
                }

                entry.Status = EntryObjectStatus.Planned;
                trainingDay.AddEntry(entry);
                dbCycle.EntryObjects.Add(entry);
                entry.LoginData  = SecurityInfo.LoginData;
                entry.MyTraining = dbCycle;

                if (param.RemindBefore.HasValue)
                {
                    entry.Reminder = new ReminderItem();

                    entry.Reminder.DateTime     = entry.TrainingDay.TrainingDate;
                    entry.Reminder.Profile      = dbProfile;
                    entry.Reminder.Type         = ReminderType.EntryObject;
                    entry.Reminder.RemindBefore = param.RemindBefore != TimeSpan.Zero
                                                      ? param.RemindBefore.Value
                                                      : (TimeSpan?)null;
                    entry.Reminder.Repetitions = ReminderRepetitions.Once;
                    entry.Reminder.Name        = string.Format(LocalizedStrings.SupplementDefinitionEntry_ReminderName, dbCycle.Name);
                    Session.Save(entry.Reminder);
                }

                Session.SaveOrUpdate(trainingDay);
                Session.Flush();
                if (entry.Reminder != null)
                {
                    entry.Reminder.ConnectedObject = string.Format("EntryObjectDTO:{0}", entry.GlobalId);
                    Session.Update(entry.Reminder);
                }
            }
        }