<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using ININ.IceLib.Connection;
using System.Web.Configuration;
using ININ.IceLib.Configuration;
using ININ.IceLib.Configuration.Dialer;
using ININ.IceLib.People;
using System.Collections.ObjectModel;

namespace iSelectManager
{
    public class Application : System.Web.HttpApplication
    {
        public static Session ICSession { get; private set; }
        public static ReadOnlyCollection<CampaignConfiguration>  CampaignConfigurations { get; private set; }
        public static ReadOnlyCollection<WorkgroupConfiguration> WorkgroupConfigurations { get; private set; }
        public static ReadOnlyCollection<ContactListConfiguration> ContactListConfigurations { get; private set; }
        public static ReadOnlyCollection<PolicySetConfiguration> PolicySetConfigurations { get; private set; }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            try
            {
                var session_settings = new SessionSettings();
                var host_settings = new HostSettings(new HostEndpoint(WebConfigurationManager.AppSettings["ICServer"]));
                var auth_settings = new ICAuthSettings(WebConfigurationManager.AppSettings["ICUser"], WebConfigurationManager.AppSettings["ICPassword"]);

                ICSession = new Session();
                session_settings.ApplicationName = "iSelectManager";
                ICSession.Connect(session_settings, host_settings, auth_settings, new StationlessSettings());

                InitializeCampaigns(ICSession);
                InitializeWorkgroups(ICSession);
                InitializeContactLists(ICSession);
                InitializePolicySets(ICSession);
            }
            catch (Exception e)
            {
                HttpContext.Current.Trace.Warn("CIC", "Unable to connect", e);
            }
        }

        protected void Application_End()
        {
            if (ICSession != null && ICSession.ConnectionState == ConnectionState.Up)
            {
                ICSession.Disconnect();
            }
        }

        private void InitializeCampaigns(ININ.IceLib.Connection.Session session)
        {
            try
            {
                var configurations = new CampaignConfigurationList(new DialerConfigurationManager(session).ConfigurationManager);
                var query_settings = configurations.CreateQuerySettings();
<commit_msg>Logout CIC when application stops<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using ININ.IceLib.Connection;
using System.Web.Configuration;
using ININ.IceLib.Configuration;
using ININ.IceLib.Configuration.Dialer;
using ININ.IceLib.People;
using System.Collections.ObjectModel;

namespace iSelectManager
{
    public class Application : System.Web.HttpApplication
    {
        public static Session ICSession { get; private set; }
        public static ReadOnlyCollection<CampaignConfiguration>  CampaignConfigurations { get; private set; }
        public static ReadOnlyCollection<WorkgroupConfiguration> WorkgroupConfigurations { get; private set; }
        public static ReadOnlyCollection<ContactListConfiguration> ContactListConfigurations { get; private set; }
        public static ReadOnlyCollection<PolicySetConfiguration> PolicySetConfigurations { get; private set; }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }

        protected void Application_End()
        {
            if (ICSession != null && ICSession.ConnectionState == ConnectionState.Up)
            {
                ICSession.Disconnect();
            }
        }

        public bool LoggedIn { get; private set; }

        public string Username
        {
            get
            {
                return LoggedIn ? ICSession.DisplayName : string.Empty;
            }
        }

        public void Login()
        {
            if (LoggedIn) return;
            try
            {
                var session_settings = new SessionSettings();
                var host_settings = new HostSettings(new HostEndpoint(WebConfigurationManager.AppSettings["ICServer"]));
                var auth_settings = new ICAuthSettings(WebConfigurationManager.AppSettings["ICUser"], WebConfigurationManager.AppSettings["ICPassword"]);

                ICSession = new Session();
                session_settings.ApplicationName = "iSelectManager";

                ICSession.ConnectionStateChanged += ICSession_ConnectionStateChanged;
                ICSession.Connect(session_settings, host_settings, auth_settings, new StationlessSettings());

                InitializeCampaigns(ICSession);
                InitializeWorkgroups(ICSession);
                InitializeContactLists(ICSession);
                InitializePolicySets(ICSession);
            }
            catch (Exception e)
            {
                HttpContext.Current.Trace.Warn("CIC", "Unable to connect", e);
            }
        }

        void ICSession_ConnectionStateChanged(object sender, ConnectionStateChangedEventArgs args)
        {
            LoggedIn = args.State == ConnectionState.Up;
        }

        private void InitializeCampaigns(ININ.IceLib.Connection.Session session)
        {
            try
            {
                var configurations = new CampaignConfigurationList(new DialerConfigurationManager(session).ConfigurationManager);
                var query_settings = configurations.CreateQuerySettings();

{'fraction_non_alphanumeric': 0.07535545023696683, 'fraction_numerical': 0.0, 'mean_word_length': 2.6197827329902803, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3840769', 'n_tokens_mistral': 1511, 'n_tokens_neox': 1465, 'n_words': 306}