///
/// Copyright (c) 2020-2023 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using Common;
using Events.Entities;
namespace Events
{
public static class DB
{
///
/// Shared data (initially empty)
///
public static string BenchName = "undefined";
public static IList RecentEvents = null;
static IList allSubscribers = null;
///
/// Set test bench name used in Events.Entities.Event constructors
///
public static void SetBenchName(string benchName)
{
DB.BenchName = benchName;
}
///
/// Loads shared data from the database
///
public static void LoadSubscribers(ISession session)
{
allSubscribers = session.QueryOver().List();
}
///
/// Loads shared data from the database
///
public static void LoadRecentEvents(ISession session, string benchName, int days)
{
BenchName = benchName;
RecentEvents = session.QueryOver()
.Where(e => (e.Bench == benchName))
.And(e => (e.TimeStamp >= DateTime.Now - new TimeSpan(days, 0, 0, 0)))
.List();
}
public static void SaveEvent(ISession session, Event evnt, SubscriberGroup groups)
{
if (allSubscribers == null) LoadSubscribers(session);
IList thisEventSubscribers = new List();
foreach (var s in allSubscribers)
{
if ((s.Groups & (long)groups) != 0)
{
/// Subscriber 's' belongs to at least one of SubscriberGroup(s) in 'groups' bit field
thisEventSubscribers.Add(s);
}
}
evnt.Subscribers = thisEventSubscribers;
evnt.Bench = BenchName;
session.SaveOrUpdate(evnt);
}
}
}