private BodyArchitect.Model.TrainingPlan publishWorkoutPlan(BodyArchitect.Model.TrainingPlan db, Profile dbProfile) { if (dbProfile != db.Profile) { throw new CrossProfileOperationException("Cannot publish cycle definition for another user"); } //we cannot modify published definition if (db.Status == PublishStatus.Published) { throw new PublishedObjectOperationException("Cannot change published cycle definition"); } if (db.Profile.Statistics.StrengthTrainingEntriesCount < Portable.Constants.StrengthTrainingEntriesCount) { throw new ProfileRankException("You must have at least " + Portable.Constants.StrengthTrainingEntriesCount + " strength training entries to publish workout plan"); } db.PublishDate = Configuration.TimerService.UtcNow; db.Status = PublishStatus.Published; var missingExercises = db.Days.SelectMany(x => x.Entries).Count(x => x.Exercise.IsDeleted); if (missingExercises > 0) { throw new ValidationException("Training plan cannot have missing exercises"); } //workoutPlanExercisesOperation(Session, dbProfile, db, delegate(Exercise exercise) //{ // //if (exercise.Status != PublishStatus.Published) // //{ // // throw new PublishedObjectOperationException("Exercise: " + exercise.Name + " is not global so you cannot publish this workout plan"); // //} //}); Session.Update(db); ProfileStatisticsUpdater.UpdateWorkoutPlans(Session, dbProfile); return(db); }
//public TrainingPlan GetWorkoutPlan(Guid planId, RetrievingInfo retrievingInfo) //{ // Log.WriteWarning("GetWorkoutPlan:Username={0},planId={1}", SecurityInfo.SessionData.Profile.UserName, planId); // var session = Session; // var result = session.QueryOver<BodyArchitect.Model.TrainingPlan>() // .Fetch(t => t.Days).Eager // .Fetch(t => t.Days.First().Entries).Eager // .Fetch(t => t.Days.First().Entries.First().Exercise).Eager // .Fetch(t => t.Days.First().Entries.First().Sets).Eager // .Where(t => t.GlobalId == planId && (t.Profile.GlobalId == SecurityInfo.SessionData.Profile.GlobalId || t.Status == PublishStatus.Published)); // var dbPlan = result.SingleOrDefault(); // var ratingDict = (from rv in session.Query<RatingUserValue>() // from tp in session.Query<BodyArchitect.Model.TrainingPlan>() // where // tp.GlobalId == rv.RatedObjectId && // rv.ProfileId == SecurityInfo.SessionData.Profile.GlobalId && tp.GlobalId == planId // select rv).SingleOrDefault(); // var workoutPlan = Mapper.Map<BodyArchitect.Model.TrainingPlan, TrainingPlan>(dbPlan); // if (ratingDict != null) // { // workoutPlan.UserShortComment = ratingDict.ShortComment; // workoutPlan.UserRating = ratingDict.Rating; // } // return workoutPlan; //} public PagedResult <TrainingPlan> GetWorkoutPlans(WorkoutPlanSearchCriteria searchCriteria, PartialRetrievingInfo pagerInfo) { Log.WriteWarning("GetWorkoutPlans: Username={0}", SecurityInfo.SessionData.Profile.UserName); var session = Session; Dictionary <Guid, RatingUserValue> ratingDict = new Dictionary <Guid, RatingUserValue>(); using (var tx = session.BeginGetTransaction()) { Profile _profile = null; var myProfile = Session.Load <Profile>(SecurityInfo.SessionData.Profile.GlobalId); var loggedProfile = Session.Load <Profile>(SecurityInfo.SessionData.Profile.GlobalId); if (searchCriteria.UserId.HasValue) { myProfile = Session.Load <Profile>(searchCriteria.UserId.Value); } var ids = myProfile.FavoriteWorkoutPlans.Select(x => x.GlobalId).ToList(); BodyArchitect.Model.TrainingPlan _plan = null; var fetchQuery = session.QueryOver <BodyArchitect.Model.TrainingPlan>(() => _plan) .Fetch(t => t.Days).Eager .Fetch(t => t.Days.First().Entries).Eager .Fetch(t => t.Days.First().Entries.First().Exercise).Eager .Fetch(t => t.Days.First().Entries.First().Sets).Eager; var planQuery = session.QueryOver <BodyArchitect.Model.TrainingPlan>(() => _plan); //if we want to get plan content then we need to inform nhibernate about this //if (pagerInfo.LongTexts) //{ // planQuery = planQuery.Fetch(x => x.PlanContent).Eager; //} var queryExercises = planQuery.JoinAlias(x => x.Profile, () => _profile); if (searchCriteria.Days.Count > 0) { //var daysOr = Restrictions.Disjunction(); //foreach (var day in searchCriteria.Days) //{ // var orderIdsCriteria = DetachedCriteria.For<BodyArchitect.Model.TrainingPlanDay>(); // orderIdsCriteria.SetProjection(Projections.CountDistinct("GlobalId")) // .Add(Restrictions.Where<BodyArchitect.Model.TrainingPlanDay>(x => x.TrainingPlan.GlobalId ==_plan.GlobalId)); // daysOr.Add(Subqueries.Eq(day, orderIdsCriteria)); //} //queryExercises = queryExercises.And(daysOr); var orderIdsCriteria = DetachedCriteria.For <BodyArchitect.Model.TrainingPlanDay>(); orderIdsCriteria.SetProjection(Projections.CountDistinct("GlobalId")) .Add(Restrictions.Where <BodyArchitect.Model.TrainingPlanDay>(x => x.TrainingPlan.GlobalId == _plan.GlobalId)); queryExercises = queryExercises.Where(Restrictions.In(Projections.SubQuery(orderIdsCriteria), (ICollection)searchCriteria.Days)); } if (searchCriteria.PlanId.HasValue) { queryExercises = queryExercises.Where(x => x.GlobalId == searchCriteria.PlanId.Value); } if (searchCriteria.Purposes.Count > 0) { var purposeOr = Restrictions.Conjunction(); foreach (var purpose in searchCriteria.Purposes) { purposeOr.Add <BodyArchitect.Model.TrainingPlan>(x => x.Purpose == (WorkoutPlanPurpose)purpose); } queryExercises = queryExercises.And(purposeOr); } if (searchCriteria.Languages.Count > 0) { var langOr = Restrictions.Disjunction(); foreach (var lang in searchCriteria.Languages) { langOr.Add <BodyArchitect.Model.TrainingPlan>(x => x.Language == lang); } queryExercises = queryExercises.And(langOr); } queryExercises = queryExercises.Where(x => x.Profile == loggedProfile || (x.Profile != loggedProfile && x.Status == PublishStatus.Published)); if (searchCriteria.WorkoutPlanType.Count > 0) { var mainOr = Restrictions.Disjunction(); foreach (TrainingType trainingType in searchCriteria.WorkoutPlanType) { var tt = (BodyArchitect.Model.TrainingType)trainingType; mainOr.Add <BodyArchitect.Model.TrainingPlan>(x => x.TrainingType == tt); } queryExercises = queryExercises.And(mainOr); } if (searchCriteria.Difficults.Count > 0) { var mainOr = Restrictions.Disjunction(); foreach (TrainingPlanDifficult diff in searchCriteria.Difficults) { var tt = (BodyArchitect.Model.TrainingPlanDifficult)diff; mainOr.Add <BodyArchitect.Model.TrainingPlan>(x => x.Difficult == tt); } queryExercises = queryExercises.And(mainOr); } var groupOr = new Disjunction(); if (searchCriteria.SearchGroups.Count > 0) { if (searchCriteria.SearchGroups.IndexOf(WorkoutPlanSearchCriteriaGroup.Mine) > -1) { groupOr.Add <BodyArchitect.Model.TrainingPlan>(x => x.Profile == myProfile); } if (searchCriteria.SearchGroups.IndexOf(WorkoutPlanSearchCriteriaGroup.Favorites) > -1) { if (myProfile.FavoriteWorkoutPlans.Count > 0) { groupOr.Add <BodyArchitect.Model.TrainingPlan>(x => x.GlobalId.IsIn((ICollection)ids)); } } if (searchCriteria.SearchGroups.IndexOf(WorkoutPlanSearchCriteriaGroup.Other) > -1) { var tmpAnd = Restrictions.Conjunction(); tmpAnd.Add <BodyArchitect.Model.TrainingPlan>(dto => dto.Profile != null && dto.Profile != myProfile && dto.Status == PublishStatus.Published); if (ids.Count > 0) { tmpAnd.Add(Restrictions.On <BodyArchitect.Model.TrainingPlan>(x => x.GlobalId).Not.IsIn((ICollection)ids)); } groupOr.Add(tmpAnd); } queryExercises = queryExercises.Where(groupOr); } queryExercises = queryExercises.ApplySorting(searchCriteria.SortOrder, searchCriteria.SortAscending); fetchQuery = fetchQuery.ApplySorting(searchCriteria.SortOrder, searchCriteria.SortAscending); //var rowCountQuery = queryExercises.ToRowCountQuery(); ratingDict = (from rv in session.Query <RatingUserValue>() from tp in session.Query <BodyArchitect.Model.TrainingPlan>() where tp.GlobalId == rv.RatedObjectId && rv.ProfileId == SecurityInfo.SessionData.Profile.GlobalId select rv).ToDictionary(t => t.RatedObjectId); //var list = queryExercises.Take(pagerInfo.PageSize).Skip(pagerInfo.PageIndex * pagerInfo.PageSize).Future(); //count = rowCountQuery.FutureValue<int>().Value; //dbList = list.ToList(); var res = fetchQuery.ToExPagedResults <TrainingPlan, BodyArchitect.Model.TrainingPlan>(pagerInfo, queryExercises, delegate(IEnumerable <BodyArchitect.Model.TrainingPlan> list) { var output = new List <TrainingPlan>(); foreach (var planDto in list) { var tmp = Mapper.Map <BodyArchitect.Model.TrainingPlan, TrainingPlan>(planDto); //if(!pagerInfo.LongTexts) //{ // tmp.PlanContent = null; //} //set the user only when workout plan was not created by current user if (planDto.Profile.GlobalId != SecurityInfo.SessionData.Profile.GlobalId) { tmp.Profile = Mapper.Map <Profile, UserDTO>(planDto.Profile); } if (ratingDict.ContainsKey(planDto.GlobalId)) { tmp.UserRating = ratingDict[planDto.GlobalId].Rating; tmp.UserShortComment = ratingDict[planDto.GlobalId].ShortComment; } output.Add(tmp); } return(output.ToArray()); }); tx.Commit(); return(res); } }