/// <summary>
        /// The refresh data.
        /// </summary>
        public void RefreshData()
        {
            this.badgeProgressService.Refresh();

            var badges =
                this.DatabaseSession.QueryOver <Badge>()
                .Where(x => !x.Awardable)
                .List()
                .Select(x => this.badgeProgressService.GetProgress(x))
                .ToList();

            var awardedBadges =
                this.DatabaseSession.QueryOver <BadgeAward>().Where(x => x.Agent == this.LoginService.Agent).List();

            this.BlackCount = badges.Count(x => x.CurrentLevel == BadgeLevel.Black)
                              + awardedBadges.Count(x => x.Level == BadgeLevel.Black);
            this.PlatinumCount = this.BlackCount + badges.Count(x => x.CurrentLevel == BadgeLevel.Platinum)
                                 + awardedBadges.Count(x => x.Level == BadgeLevel.Platinum);
            this.GoldCount = this.PlatinumCount + badges.Count(x => x.CurrentLevel == BadgeLevel.Gold)
                             + awardedBadges.Count(x => x.Level == BadgeLevel.Gold);
            this.SilverCount = this.GoldCount + badges.Count(x => x.CurrentLevel == BadgeLevel.Silver)
                               + awardedBadges.Count(x => x.Level == BadgeLevel.Silver);
            this.BronzeCount = this.SilverCount + badges.Count(x => x.CurrentLevel == BadgeLevel.Bronze)
                               + awardedBadges.Count(x => x.Level == BadgeLevel.Bronze);

            var agent = this.LoginService.Agent;

            Stat apstat = this.DatabaseSession.QueryOver <Stat>().Where(s => s.IsAccessPointsStat).SingleOrDefault();

            QueryOver <ValueEntry, ValueEntry> newestStat =
                QueryOver.Of <ValueEntry>()
                .SelectList(p => p.SelectMax(x => x.Timestamp))
                .Where(c => c.Agent == agent && c.Statistic == apstat);

            ValueEntry value =
                this.DatabaseSession.QueryOver <ValueEntry>()
                .Where(c => c.Agent == agent && c.Statistic == apstat)
                .WithSubquery.Where(x => x.Timestamp == newestStat.As <DateTime>())
                .SingleOrDefault();

            this.AccessPoints = value == null ? 0 : value.Value.Value;

            var currentLevel = this.levelProgressService.GetCurrentLevel(
                this.AccessPoints,
                this.SilverCount,
                this.GoldCount,
                this.PlatinumCount,
                this.BlackCount);

            var nextLevel = this.levelProgressService.GetNextLevel(currentLevel);

            this.Level = currentLevel.LevelNumber;
            this.MaxAP = nextLevel.AccessPoints;
        }
Example #2
0
        /// <summary>
        /// The get statistic progress.
        /// </summary>
        /// <param name="agent">
        /// The agent.
        /// </param>
        /// <param name="statistic">
        /// The statistic.
        /// </param>
        /// <returns>
        /// The <see cref="StatisticProgress"/>.
        /// </returns>
        public StatisticProgress GetStatisticProgress(User agent, Stat statistic)
        {
            QueryOver <ValueEntry, ValueEntry> newestStat =
                QueryOver.Of <ValueEntry>()
                .SelectList(p => p.SelectMax(x => x.Timestamp))
                .Where(c => c.Agent == agent && c.Statistic == statistic);

            ValueEntry value =
                this.databaseSession.QueryOver <ValueEntry>()
                .Where(c => c.Agent == agent && c.Statistic == statistic)
                .WithSubquery.Where(x => x.Timestamp == newestStat.As <DateTime>())
                .SingleOrDefault();

            if (value != null)
            {
                var weekTime  = value.Timestamp.AddDays(-7);
                var monthTime = value.Timestamp.AddDays(-28);

                QueryOver <ValueEntry, ValueEntry> week =
                    QueryOver.Of <ValueEntry>()
                    .SelectList(p => p.SelectMax(x => x.Timestamp))
                    .Where(c => c.Agent == agent && c.Statistic == statistic && c.Timestamp < weekTime);

                QueryOver <ValueEntry, ValueEntry> month =
                    QueryOver.Of <ValueEntry>()
                    .SelectList(p => p.SelectMax(x => x.Timestamp))
                    .Where(c => c.Agent == agent && c.Statistic == statistic && c.Timestamp < monthTime);

                ValueEntry weekValue =
                    this.databaseSession.QueryOver <ValueEntry>()
                    .Where(c => c.Agent == agent && c.Statistic == statistic)
                    .WithSubquery.Where(x => x.Timestamp == week.As <DateTime>())
                    .SingleOrDefault();

                ValueEntry monthValue =
                    this.databaseSession.QueryOver <ValueEntry>()
                    .Where(c => c.Agent == agent && c.Statistic == statistic)
                    .WithSubquery.Where(x => x.Timestamp == month.As <DateTime>())
                    .SingleOrDefault();

                return(new StatisticProgress(value, monthValue, weekValue));
            }

            return(new StatisticProgress(null, null, null));
        }
        public void SubQuery()
        {
            if (!Dialect.SupportsScalarSubSelects)
            {
                Assert.Ignore(Dialect.GetType().Name + " does not support scalar sub-queries");
            }

            using (var s = OpenSession())
            {
                Person personAlias     = null;
                object childCountAlias = null;

                QueryOver <Child> averageChildAge = QueryOver.Of <Child>()
                                                    .SelectList(list => list.SelectAvg(c => c.Age));

                QueryOver <Child> childCountQuery = QueryOver.Of <Child>()
                                                    .Where(c => c.Parent.Id == personAlias.Id)
                                                    .Select(Projections.RowCount());

                var nameAndChildCount = s.QueryOver <Person>(() => personAlias)
                                        .WithSubquery.Where(p => p.Age <= averageChildAge.As <int>())
                                        .SelectList(list => list
                                                    .Select(p => p.Name)
                                                    .SelectSubQuery(childCountQuery).WithAlias(() => childCountAlias))
                                        .OrderByAlias(() => childCountAlias).Desc
                                        .List <object[]>()
                                        .Select(props => new
                {
                    Name       = (string)props[0],
                    ChildCount = (int)props[1],
                })
                                        .ToList();

                Assert.That(nameAndChildCount.Count, Is.EqualTo(2));

                Assert.That(nameAndChildCount[0].Name, Is.EqualTo("Name 2"));
                Assert.That(nameAndChildCount[0].ChildCount, Is.EqualTo(2));

                Assert.That(nameAndChildCount[1].Name, Is.EqualTo("Name 1"));
                Assert.That(nameAndChildCount[1].ChildCount, Is.EqualTo(1));
            }
        }
        public void SubQuery()
        {
            using (ISession s = OpenSession())
                using (ITransaction t = s.BeginTransaction())
                {
                    s.Save(new Person()
                    {
                        Name = "Name 1", Age = 1
                    }
                           .AddChild(new Child()
                    {
                        Nickname = "Name 1.1", Age = 1
                    }));

                    s.Save(new Person()
                    {
                        Name = "Name 2", Age = 2
                    }
                           .AddChild(new Child()
                    {
                        Nickname = "Name 2.1", Age = 2
                    })
                           .AddChild(new Child()
                    {
                        Nickname = "Name 2.2", Age = 2
                    }));

                    s.Save(new Person()
                    {
                        Name = "Name 3", Age = 3
                    }
                           .AddChild(new Child()
                    {
                        Nickname = "Name 3.1", Age = 3
                    }));

                    t.Commit();
                }

            using (ISession s = OpenSession())
            {
                Person personAlias     = null;
                object childCountAlias = null;

                QueryOver <Child> averageChildAge =
                    QueryOver.Of <Child>()
                    .SelectList(list => list.SelectAvg(c => c.Age));

                QueryOver <Child> childCountQuery =
                    QueryOver.Of <Child>()
                    .Where(c => c.Parent.Id == personAlias.Id)
                    .Select(Projections.RowCount());

                var nameAndChildCount =
                    s.QueryOver <Person>(() => personAlias)
                    .WithSubquery.Where(p => p.Age <= averageChildAge.As <int>())
                    .SelectList(list => list
                                .Select(p => p.Name)
                                .SelectSubQuery(childCountQuery).WithAlias(() => childCountAlias))
                    .OrderByAlias(() => childCountAlias).Desc
                    .List <object[]>()
                    .Select(props => new {
                    Name       = (string)props[0],
                    ChildCount = (int)props[1],
                })
                    .ToList();

                Assert.That(nameAndChildCount.Count, Is.EqualTo(2));

                Assert.That(nameAndChildCount[0].Name, Is.EqualTo("Name 2"));
                Assert.That(nameAndChildCount[0].ChildCount, Is.EqualTo(2));

                Assert.That(nameAndChildCount[1].Name, Is.EqualTo("Name 1"));
                Assert.That(nameAndChildCount[1].ChildCount, Is.EqualTo(1));
            }
        }