public void TestGetWorkoutPlans_OnlyBeginners() { PagedResult <WorkoutPlanDTO> result = null; WorkoutPlanSearchCriteria criteria = WorkoutPlanSearchCriteria.CreateFindAllCriteria(); criteria.Difficults.Add(Service.Model.TrainingPlans.TrainingPlanDifficult.Beginner); var profile = (ProfileDTO)profiles[0].Tag; SessionData data = SecurityManager.CreateNewSession(profile, ClientInformation); PartialRetrievingInfo pageInfo = new PartialRetrievingInfo(); pageInfo.PageSize = 50; RunServiceMethod(delegate(InternalBodyArchitectService service) { result = service.GetWorkoutPlans(data.Token, criteria, pageInfo); }); assertWorkoutPlansListPack(result, 5, new[] { "test1-1", "test1-3", "test1-5", "test1-7", "test2-3" }, null); profile = (ProfileDTO)profiles[1].Tag; data = SecurityManager.CreateNewSession(profile, ClientInformation); RunServiceMethod(delegate(InternalBodyArchitectService service) { result = service.GetWorkoutPlans(data.Token, criteria, pageInfo); }); assertWorkoutPlansListPack(result, 6, new[] { "test1-1", "test1-3", "test1-7", "test2-3", "test2-5", "test2-8" }, null); }
private static void loadPlans() { plansEvent.Reset(); dictPlans = null; var task = Task.Factory.StartNew(delegate { ControlHelper.EnsureThreadLocalized(); PagedResultRetriever retriever = new PagedResultRetriever(); var res = retriever.GetAll(delegate(PartialRetrievingInfo pageInfo) { try { WorkoutPlanSearchCriteria criteria = WorkoutPlanSearchCriteria.CreateFindAllCriteria(); criteria.SearchGroups.Remove(WorkoutPlanSearchCriteriaGroup.Other); var list = ServiceManager.GetWorkoutPlans(criteria, pageInfo); return(list); } catch (Exception) { plansEvent.Set(); throw; } }); dictPlans = res.ToDictionary(t => t.GlobalId); plansEvent.Set(); }, plansEvent); LogUnhandledExceptions(task); }
public PagedResult <TrainingPlan> GetWorkoutPlans(Token token, WorkoutPlanSearchCriteria searchCriteria, PartialRetrievingInfo pagerInfo) { var securityInfo = SecurityManager.EnsureAuthentication(token); WorkoutPlanService service = new WorkoutPlanService(Session, securityInfo, Configuration); return(service.GetWorkoutPlans(searchCriteria, pagerInfo)); }
public void TestGetWorkoutPlans_LoadPlanContent() { WorkoutPlanSearchCriteria criteria = WorkoutPlanSearchCriteria.CreateFindAllCriteria(); criteria.UserName = profiles[0].UserName; PagedResult <WorkoutPlanDTO> result = null; var profile = (ProfileDTO)profiles[0].Tag; SessionData data = SecurityManager.CreateNewSession(profile, ClientInformation); PartialRetrievingInfo pageInfo = new PartialRetrievingInfo(); pageInfo.LongTexts = false; pageInfo.PageSize = 50; RunServiceMethod(delegate(InternalBodyArchitectService service) { result = service.GetWorkoutPlans(data.Token, criteria, pageInfo); }); foreach (var plan in result.Items) { Assert.IsNull(plan.PlanContent); } pageInfo.LongTexts = true; criteria.UserName = profiles[1].UserName; RunServiceMethod(delegate(InternalBodyArchitectService service) { result = service.GetWorkoutPlans(data.Token, criteria, pageInfo); }); foreach (var plan in result.Items) { Assert.AreEqual("plan content", plan.PlanContent); } }
public void TestGetWorkoutPlans_OnlyOther() { WorkoutPlanSearchCriteria criteria = new WorkoutPlanSearchCriteria(); criteria.SearchGroups.Add(WorkoutPlanSearchCriteriaGroup.Other); PagedResult <Service.V2.Model.TrainingPlans.TrainingPlan> result = null; var profile = (ProfileDTO)profiles[0].Tag; SessionData data = CreateNewSession(profile, ClientInformation); PartialRetrievingInfo pageInfo = new PartialRetrievingInfo(); pageInfo.PageSize = 50; RunServiceMethod(delegate(InternalBodyArchitectService service) { result = service.GetWorkoutPlans(data.Token, criteria, pageInfo); }); assertWorkoutPlansListPack(result, 3, new[] { "test2-3", "test2-4", "test2-7" }, null); profile = (ProfileDTO)profiles[1].Tag; data = CreateNewSession(profile, ClientInformation); RunServiceMethod(delegate(InternalBodyArchitectService service) { result = service.GetWorkoutPlans(data.Token, criteria, pageInfo); }); assertWorkoutPlansListPack(result, 2, new[] { "test1-3", "test1-7" }, null); }
public void TestGetWorkoutPlans_All() { PagedResult <Service.V2.Model.TrainingPlans.TrainingPlan> result = null; WorkoutPlanSearchCriteria criteria = new WorkoutPlanSearchCriteria(); var profile = (ProfileDTO)profiles[0].Tag; SessionData data = CreateNewSession(profile, ClientInformation); PartialRetrievingInfo pageInfo = new PartialRetrievingInfo(); pageInfo.PageSize = 50; RunServiceMethod(delegate(InternalBodyArchitectService service) { result = service.GetWorkoutPlans(data.Token, criteria, pageInfo); }); assertWorkoutPlansListPack(result, 11, null, new[] { "test2-1", "test2-5", "test2-6", "test2-8" }); profile = (ProfileDTO)profiles[1].Tag; data = CreateNewSession(profile, ClientInformation); RunServiceMethod(delegate(InternalBodyArchitectService service) { result = service.GetWorkoutPlans(data.Token, criteria, pageInfo); }); assertWorkoutPlansListPack(result, 12, null, new[] { "test1-2", "test1-4", "test1-5" }); }
public void TestGetWorkoutPlans_None() { PagedResult <WorkoutPlanDTO> result = null; WorkoutPlanSearchCriteria criteria = new WorkoutPlanSearchCriteria(); var profile = (ProfileDTO)profiles[0].Tag; SessionData data = SecurityManager.CreateNewSession(profile, ClientInformation); PartialRetrievingInfo pageInfo = new PartialRetrievingInfo(); pageInfo.PageSize = 50; RunServiceMethod(delegate(InternalBodyArchitectService service) { result = service.GetWorkoutPlans(data.Token, criteria, pageInfo); }); assertWorkoutPlansListPack(result, 0, null, null); profile = (ProfileDTO)profiles[1].Tag; data = SecurityManager.CreateNewSession(profile, ClientInformation); RunServiceMethod(delegate(InternalBodyArchitectService service) { result = service.GetWorkoutPlans(data.Token, criteria, pageInfo); }); assertWorkoutPlansListPack(result, 0, null, null); }
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { base.OnNavigatedTo(e); lblOfflineMode.Visibility = ApplicationState.Current.IsOffline ? Visibility.Visible : Visibility.Collapsed; StateHelper helper = new StateHelper(State); pivot.SelectedIndex = helper.GetValue("PivotItem", 0); allPageIndex = helper.GetValue("AllPageIndex", 0); allItemsCount = helper.GetValue("AllItemsCount", 0); allCriteria = helper.GetValue <WorkoutPlanSearchCriteria>("AllCriteria", null); //allPlans = helper.GetValue("AllPlans", new ObservableCollection<WorkoutPlanViewModel>()); allPlans = PagesState.Current.SearchedPlans ?? new ObservableCollection <WorkoutPlanViewModel>(); lstAllPlans.ItemsSource = null; lstAllPlans.ItemsSource = allPlans; buildApplicationBar(); if (viewModel == null || !ApplicationState.Current.Cache.TrainingPlans.IsLoaded) { lstMessages.ItemsSource = null; lstFavorites.ItemsSource = null; viewModel = new WorkoutPlansViewModel(); DataContext = viewModel; viewModel.WorkoutPlansLoaded += new EventHandler(_viewModel_PlansLoaded); progressBar.ShowProgress(true, ApplicationStrings.WorkoutPlans_ProgressRetrievingPlans, false); } viewModel.LoadPlans(); this.RestoreState(); // <- second line }
public void TestGetWorkoutPlans_UserName() { WorkoutPlanSearchCriteria criteria = WorkoutPlanSearchCriteria.CreateFindAllCriteria(); criteria.UserName = profiles[0].UserName; var profile = (ProfileDTO)profiles[0].Tag; SessionData data = SecurityManager.CreateNewSession(profile, ClientInformation); PartialRetrievingInfo pageInfo = new PartialRetrievingInfo(); pageInfo.PageSize = 50; PagedResult <WorkoutPlanDTO> result = null; RunServiceMethod(delegate(InternalBodyArchitectService service) { result = service.GetWorkoutPlans(data.Token, criteria, pageInfo); }); assertWorkoutPlansListPack(result, 7, new[] { "test1-1", "test1-2", "test1-3", "test1-4", "test1-5", "test1-6", "test1-7" }, null); criteria.UserName = profiles[1].UserName; RunServiceMethod(delegate(InternalBodyArchitectService service) { result = service.GetWorkoutPlans(data.Token, criteria, pageInfo); }); assertWorkoutPlansListPack(result, 4, new[] { "test2-2", "test2-3", "test2-4", "test2-7" }, null); }
public void TestGetWorkoutPlans_OnlySplit() { WorkoutPlanSearchCriteria criteria = WorkoutPlanSearchCriteria.CreateFindAllCriteria(); criteria.WorkoutPlanType.Add(BodyArchitect.Service.Model.TrainingPlans.TrainingType.Split); PagedResult <WorkoutPlanDTO> result = null; var profile = (ProfileDTO)profiles[0].Tag; SessionData data = SecurityManager.CreateNewSession(profile, ClientInformation); PartialRetrievingInfo pageInfo = new PartialRetrievingInfo(); pageInfo.PageSize = 50; RunServiceMethod(delegate(InternalBodyArchitectService service) { result = service.GetWorkoutPlans(data.Token, criteria, pageInfo); }); assertWorkoutPlansListPack(result, 2, new[] { "test1-4", "test1-6" }, null); profile = (ProfileDTO)profiles[1].Tag; data = SecurityManager.CreateNewSession(profile, ClientInformation); RunServiceMethod(delegate(InternalBodyArchitectService service) { result = service.GetWorkoutPlans(data.Token, criteria, pageInfo); }); assertWorkoutPlansListPack(result, 3, new[] { "test1-6", "test2-5", "test2-8" }, null); }
public void TestGetWorkoutPlans_OnlyOther() { WorkoutPlanSearchCriteria criteria = WorkoutPlanSearchCriteria.CreateFindAllCriteria(); criteria.SearchGroups.Remove(WorkoutPlanSearchCriteriaGroup.Mine); criteria.SearchGroups.Remove(WorkoutPlanSearchCriteriaGroup.Favorites); PagedResult <WorkoutPlanDTO> result = null; var profile = (ProfileDTO)profiles[0].Tag; SessionData data = SecurityManager.CreateNewSession(profile, ClientInformation); PartialRetrievingInfo pageInfo = new PartialRetrievingInfo(); pageInfo.PageSize = 50; RunServiceMethod(delegate(InternalBodyArchitectService service) { result = service.GetWorkoutPlans(data.Token, criteria, pageInfo); }); assertWorkoutPlansListPack(result, 3, new[] { "test2-3", "test2-4", "test2-7" }, null); profile = (ProfileDTO)profiles[1].Tag; data = SecurityManager.CreateNewSession(profile, ClientInformation); RunServiceMethod(delegate(InternalBodyArchitectService service) { result = service.GetWorkoutPlans(data.Token, criteria, pageInfo); }); assertWorkoutPlansListPack(result, 2, new[] { "test1-3", "test1-7" }, null); }
protected override PagedResult <TrainingPlan> GetItemsMethod(PartialRetrievingInfo pageInfo) { WorkoutPlanSearchCriteria criteria = new WorkoutPlanSearchCriteria(); criteria.SearchGroups.Add(WorkoutPlanSearchCriteriaGroup.Mine); criteria.SearchGroups.Add(WorkoutPlanSearchCriteriaGroup.Favorites); return(ServiceManager.GetWorkoutPlans(criteria, pageInfo)); }
public static PagedResult <TrainingPlan> GetWorkoutPlans(WorkoutPlanSearchCriteria searchCriteria, PartialRetrievingInfo pagerInfo) { return(exceptionHandling(delegate { pagerInfo.LongTexts = false; return Instance.GetWorkoutPlans(Token, searchCriteria, pagerInfo); })); }
protected override void RetrieveObjects(BodyArchitectAccessServiceClient client1, PartialRetrievingInfo pageInfo, EventHandler <GetWorkoutPlansCompletedEventArgs> operationCompleted) { WorkoutPlanSearchCriteria criteria = new WorkoutPlanSearchCriteria(); criteria.SearchGroups.Add(WorkoutPlanSearchCriteriaGroup.Mine); criteria.SearchGroups.Add(WorkoutPlanSearchCriteriaGroup.Favorites); client1.GetWorkoutPlansAsync(ApplicationState.Current.SessionData.Token, criteria, pageInfo); client1.GetWorkoutPlansCompleted -= operationCompleted; client1.GetWorkoutPlansCompleted += operationCompleted; }
public void Fill(WorkoutPlanSearchCriteria criteria) { if (currentTask != null) { currentTask.Cancel(); changeOperationState(false); currentTask = null; } this.criteria = criteria; pagerInfo = new PartialRetrievingInfo(); fillPage(0); }
private void btnSearch_Click(object sender, EventArgs e) { if (UserContext.LoginStatus != LoginStatus.Logged) { throw new AuthenticationException("You must be logged"); } usrWorkoutPlansPagerListView1.Clear(); ParentWindow.TasksManager.TaskStateChanged += new EventHandler <BodyArchitect.Controls.UserControls.TaskStateChangedEventArgs>(TasksManager_TaskStateChanged); criteria = WorkoutPlanSearchCriteria.CreateFindAllCriteria(); foreach (CheckedListBoxItem value in cmbDays.Properties.Items) { if (value.CheckState == CheckState.Checked) { criteria.Days.Add((int)value.Value); } } foreach (CheckedListBoxItem value in cmbWorkoutPlanDifficult.Properties.Items) { if (value.CheckState == CheckState.Checked) { criteria.Difficults.Add((TrainingPlanDifficult)value.Value); } } foreach (CheckedListBoxItem value in cmbPurposes.Properties.Items) { if (value.CheckState == CheckState.Checked) { criteria.Purposes.Add((WorkoutPlanPurpose)value.Value); } } foreach (CheckedListBoxItem value in cmbLanguages.Properties.Items) { if (value.CheckState == CheckState.Checked) { criteria.Languages.Add(((Language)value.Value).Shortcut); } } foreach (CheckedListBoxItem value in cmbWorkoutPlanType.Properties.Items) { if (value.CheckState == CheckState.Checked) { criteria.WorkoutPlanType.Add((TrainingType)value.Value); } } criteria.SortOrder = (WorkoutPlanSearchOrder)cmbSortOrder.SelectedIndex; usrWorkoutPlansPagerListView1.Fill(criteria); }
protected override void BeforeSearch(object param = null) { criteria = new WorkoutPlanSearchCriteria(); foreach (var value in Days) { if (value.IsChecked) { criteria.Days.Add(value.Value); } } foreach (var value in Difficulties) { if (value.IsChecked) { criteria.Difficults.Add(value.Value); } } foreach (var value in Purposes) { if (value.IsChecked) { criteria.Purposes.Add(value.Value); } } foreach (var value in Languages) { if (value.IsChecked) { criteria.Languages.Add((value.Value).Shortcut); } } foreach (var value in TrainingTypes) { if (value.IsChecked) { criteria.WorkoutPlanType.Add(value.Value); } } criteria.SortOrder = SelectedOrder; criteria.SortAscending = SortAscending; }
public void TestGetWorkoutPlans_ById() { PagedResult <Service.V2.Model.TrainingPlans.TrainingPlan> result = null; WorkoutPlanSearchCriteria criteria = new WorkoutPlanSearchCriteria(); criteria.PlanId = workoutPlans.Values.ElementAt(0).GlobalId; var profile = (ProfileDTO)profiles[0].Tag; SessionData data = CreateNewSession(profile, ClientInformation); PartialRetrievingInfo pageInfo = new PartialRetrievingInfo(); pageInfo.PageSize = 50; RunServiceMethod(delegate(InternalBodyArchitectService service) { result = service.GetWorkoutPlans(data.Token, criteria, pageInfo); }); assertWorkoutPlansListPack(result, 1, new[] { workoutPlans.Values.ElementAt(0).Name }, null); }
public WorkoutPlanSearchCriteria GetSearchCriteria() { var allCriteria = new WorkoutPlanSearchCriteria(); allCriteria.SearchGroups.Add(WorkoutPlanSearchCriteriaGroup.Other); if (lpType.SelectedIndex > 0) { allCriteria.WorkoutPlanType.Add((TrainingType)lpType.SelectedIndex - 1); } if (lpPurpose.SelectedIndex > 0) { allCriteria.Purposes.Add((WorkoutPlanPurpose)lpPurpose.SelectedIndex - 1); } if (lpDifficult.SelectedIndex > 0) { allCriteria.Difficults.Add((TrainingPlanDifficult)lpDifficult.SelectedIndex - 1); } allCriteria.SortOrder = tsOrderBy.IsChecked == true ? SearchSortOrder.Newest : SearchSortOrder.HighestRating; return(allCriteria); }
public void TestGetWorkoutPlans_OnlyFavoritesAndOther_bugWhenNoFavorites() { WorkoutPlanSearchCriteria criteria = WorkoutPlanSearchCriteria.CreateFindAllCriteria(); criteria.SearchGroups.Remove(WorkoutPlanSearchCriteriaGroup.Mine); //clear favorites plans profiles[0].FavoriteWorkoutPlans.Clear(); Session.SaveOrUpdate(profiles[0]); PagedResult <WorkoutPlanDTO> result = null; var profile = (ProfileDTO)profiles[0].Tag; SessionData data = SecurityManager.CreateNewSession(profile, ClientInformation); PartialRetrievingInfo pageInfo = new PartialRetrievingInfo(); pageInfo.PageSize = 50; RunServiceMethod(delegate(InternalBodyArchitectService service) { result = service.GetWorkoutPlans(data.Token, criteria, pageInfo); }); assertWorkoutPlansListPack(result, 4, new[] { "test2-2" }, null); }
public void Fill(ProfileInformationDTO user, bool isActive) { if (user != null) { if (isActive && (!filled || currentUser == null || currentUser.User.Id != user.User.Id)) { filled = true; usrWorkoutPlansPagerListView1.Clear(); usrWorkoutCommentsList1.Fill(null); WorkoutPlanSearchCriteria criteria = WorkoutPlanSearchCriteria.CreateFindAllCriteria(); criteria.UserName = user.User.UserName; usrWorkoutPlansPagerListView1.Fill(criteria); } } else { filled = false; usrWorkoutPlansPagerListView1.Clear(); usrWorkoutCommentsList1.Fill(null); } currentUser = user; }
private void showFilterDlg() { var ctrl = new WorkoutPlansFilterControl(); ctrl.ShowPopup(messagePrompt => { messagePrompt.IsCancelVisible = true; messagePrompt.Completed += (a1, s1) => { var commentCtrl = (WorkoutPlansFilterControl)((MessagePrompt)a1).Body; if (s1.PopUpResult == PopUpResult.Ok) { lstAllPlans.ItemsSource = allPlans = new ObservableCollection <WorkoutPlanViewModel>(); allPageIndex = 0; allCriteria = commentCtrl.GetSearchCriteria(); retrievePlansPage(allPageIndex); } messagePrompt = null; }; }, this); }
public TrainingPlan SaveWorkoutPlan(TrainingPlan dto) { Log.WriteWarning("SaveWorkoutPlan: Username={0},planId={1}", SecurityInfo.SessionData.Profile.UserName, dto.GlobalId); if (!SecurityInfo.Licence.IsPremium) { throw new LicenceException("This feature is allowed for Premium account"); } if (dto.Status != Model.PublishStatus.Private) { throw new InvalidOperationException("Wrong status. Only Private can be saved"); } var session = Session; var dbPlan = Mapper.Map <TrainingPlan, BodyArchitect.Model.TrainingPlan>(dto); using (var tx = session.BeginSaveTransaction()) { var dbProfile = session.Get <Profile>(SecurityInfo.SessionData.Profile.GlobalId); dbPlan.Profile = dbProfile; //now we need to check if the user is publishing the workout plan //var planFromDb = (from p in session.Query<BodyArchitect.Model.TrainingPlan>() // where p.GlobalId == dbPlan.GlobalId // select p).SingleOrDefault(); var planFromDb = session.Get <BodyArchitect.Model.TrainingPlan>(dbPlan.GlobalId); if (planFromDb != null) { if (SecurityInfo.SessionData.Profile.GlobalId != planFromDb.Profile.GlobalId) { throw new CrossProfileOperationException("Cannot modify training plan for another user"); } //we cannot modify published workout plan) if (planFromDb.Status == PublishStatus.Published) { throw new PublishedObjectOperationException("Cannot change published workout plan"); } if (dto.Status == Model.PublishStatus.Published && planFromDb.Status != PublishStatus.Published) { throw new InvalidOperationException("Cannot publish workout using SaveWorkoutPlan method. Use PublishWorkoutPlan method instead."); } dbPlan.Rating = planFromDb.Rating; } else { dbPlan.CreationDate = Configuration.TimerService.UtcNow; } //XmlSerializationTrainingPlanFormatter formatter = new XmlSerializationTrainingPlanFormatter(); //TrainingPlan plan = formatter.FromXml(dto.PlanContent,null); var validator = new ObjectValidator(typeof(TrainingPlan)); var result = validator.Validate(dto); if (!result.IsValid) { throw new ValidationException(result.ToValidationResults()); } dbPlan = session.Merge(dbPlan); //now update cache modification date dbProfile.DataInfo.WorkoutPlanHash = Guid.NewGuid(); tx.Commit(); } //return Mapper.Map<BodyArchitect.Model.TrainingPlan, WorkoutPlanDTO>(dbPlan); var param = new WorkoutPlanSearchCriteria(); param.PlanId = dbPlan.GlobalId; var res = GetWorkoutPlans(param, new PartialRetrievingInfo()); if (res.Items.Count > 0) { return(res.Items[0]); } return(null); }
protected override void BeforeSearch(object param = null) { criteria = new WorkoutPlanSearchCriteria(); criteria.SearchGroups.Add(WorkoutPlanSearchCriteriaGroup.Mine); criteria.UserId = currentUser.User.GlobalId; }
public PagedResult <WorkoutPlanDTO> GetWorkoutPlans(Token token, WorkoutPlanSearchCriteria searchCriteria, PartialRetrievingInfo pagerInfo) { throw new NotImplementedException(); }
public PagedResult <TrainingPlan> GetWorkoutPlans(Token token, WorkoutPlanSearchCriteria searchCriteria, PartialRetrievingInfo pagerInfo) { return(exceptionHandling(token, () => InternalService.GetWorkoutPlans(token, searchCriteria, pagerInfo))); }
//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); } }