<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CalendarDll;
using CalendarDll.Data;
using WebMatrix.Data;
using System.IO;
using Ical;
using Ical.Net;

/// <summary>
/// Calendaring Tasks, wrapper for some CalendarDll features.
/// </summary>
public static partial class LcCalendar
{
    #region Booking
    public const string sqlInsBookingEvent = @"
        INSERT INTO [CalendarEvents]
                    ([UserId]
                    ,[CalendarAvailabilityTypeID]
                    ,[EventType]
                    ,[Summary]
                    ,[Description]
                    ,[StartTime]
                    ,[EndTime]
                    ,[TimeZone]
                    ,[CreatedDate]
                    ,[UpdatedDate]
                    ,[ModifyBy])
                VALUES (@0
                    ,@1
                    ,1 -- Booking
                    ,@2
                    ,@3
                    ,@4
                    ,@5
                    ,@6
                    ,getdate()
                    ,getdate()
                    ,'sys')
        SELECT Cast(@@Identity As int) As CalendarEventID
    ";
    public const string sqlUpdBookingEvent = @"
        UPDATE [CalendarEvents] SET
            [Summary] = coalesce(@4, Summary)
            ,[Description] = coalesce(@5, Description)
            ,[StartTime] = @1
            ,[EndTime] = @2
            ,[TimeZone] = coalesce(@3, TimeZone)
            ,[UpdatedDate] = getdate()
            ,[ModifyBy] = 'sys'
        WHERE Id = @0
    ";
    #endregion

    #region Availability

    /// <summary>
    /// Fixed values for each type syncrhonized with the database values
    /// for the table CalendarAvailabilityType
    /// </summary>
    public enum AvailabilityType : short
    {
        //"unavailable", "free", "busy", "tentative", "offline"
        Unavailable = 0,
        Free = 1,
        Busy = 2,
        Tentative = 3,
        /// <summary>
        /// TODO This has no sense. An offline must be a type of event, is nothing about availability.
        /// But there is another kind of availability: 'transparent' that just means event must be discarded
        /// when computing availability.
        /// </summary>
        Offline = 4
    }

    public const string serverTimeZoneID = "America/Los_Angeles";

    /// <summary>
    /// Get availability table for the user between given date and times
    /// </summary>
    /// <param name="userID"></param>
    /// <param name="dateStart"></param>
    /// <param name="dateEnd"></param>
    /// <param name="excludeAdvanceTime">This avoids the check of 'advance time allowed to booking' by passing the lowest datetime possible
    /// to the Calendar API. It's needed when the user checking availability is the same provider and don't want this time to block itself
    /// but only clients.</param>
    /// <returns></returns>
    [Obsolete("Refactor to use the new GetAvailability.GetTimeline logic, far faster")]
    public static List<CalendarDll.ProviderAvailabilityResult> GetUserAvailability(int userID, DateTimeOffset dateStart, DateTimeOffset dateEnd, bool excludeAdvanceTime = false)
    {
        var lcCalendar = new CalendarDll.CalendarUtils();
        return
            lcCalendar.GetFreeEvents(
                new CalendarDll.CalendarUser(userID),
                dateStart,
                dateEnd,
                excludeAdvanceTime ? DateTime.MinValue : DateTime.Now);
    }
    /// <summary>
    /// Check if the user is available for all the time between dateStart and dateEnd
    /// </summary>
    /// <param name="userID">UserID as in database</param>
    /// <param name="dateStart">Start date and time for the time range (greater or equals than dateStart)</param>
    /// <param name="dateEnd">End date and time for the time range (less than dateEnd)</param>
    /// <returns>True when is available, False when not</returns>
    [Obsolete("Refactor to use the new GetAvailability.GetTimeline logic, far faster")]
    public static bool CheckUserAvailability(int userID, DateTimeOffset dateStart, DateTimeOffset dateEnd, bool excludeAdvanceTime = false)
    {
        foreach (var e in GetUserAvailability(userID, dateStart, dateEnd, excludeAdvanceTime))
        {
            var edt = e.DateSet + e.TimeBlock;
            if ((e.CalendarAvailabilityTypeID == (int)CalendarDll.AvailabilityTypes.BUSY ||
                e.CalendarAvailabilityTypeID == (int)CalendarDll.AvailabilityTypes.UNAVAILABLE ||
                e.CalendarAvailabilityTypeID == (int)CalendarDll.AvailabilityTypes.TENTATIVE) &&
                edt >= dateStart &&
                edt < dateEnd)
                return false;
        }
        return true;
    }

    /// <summary>
    /// Check if the user has some block available for between dateStart and dateEnd.
    /// With almost one block available (free), will return true, and false for when there is
    /// no available block at all, just the opposite to GetUserAvailability.
    /// </summary>
    /// <param name="userID"></param>
    /// <param name="dateStart"></param>
    /// <param name="dateEnd"></param>
    /// <returns></returns>
    [Obsolete("Refactor to use the new GetAvailability.GetTimeline logic, far faster")]
    public static bool HasUserSomeAvailability(int userID, DateTime dateStart, DateTime dateEnd)
    {
        foreach (var e in GetUserAvailability(userID, dateStart, dateEnd))<commit_msg>Fixed bug with checkAvailability after update to DateTimeOffset #16 #651<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CalendarDll;
using CalendarDll.Data;
using WebMatrix.Data;
using System.IO;
using Ical;
using Ical.Net;

/// <summary>
/// Calendaring Tasks, wrapper for some CalendarDll features.
/// </summary>
public static partial class LcCalendar
{
    #region Booking
    public const string sqlInsBookingEvent = @"
        INSERT INTO [CalendarEvents]
                    ([UserId]
                    ,[CalendarAvailabilityTypeID]
                    ,[EventType]
                    ,[Summary]
                    ,[Description]
                    ,[StartTime]
                    ,[EndTime]
                    ,[TimeZone]
                    ,[CreatedDate]
                    ,[UpdatedDate]
                    ,[ModifyBy])
                VALUES (@0
                    ,@1
                    ,1 -- Booking
                    ,@2
                    ,@3
                    ,@4
                    ,@5
                    ,@6
                    ,getdate()
                    ,getdate()
                    ,'sys')
        SELECT Cast(@@Identity As int) As CalendarEventID
    ";
    public const string sqlUpdBookingEvent = @"
        UPDATE [CalendarEvents] SET
            [Summary] = coalesce(@4, Summary)
            ,[Description] = coalesce(@5, Description)
            ,[StartTime] = @1
            ,[EndTime] = @2
            ,[TimeZone] = coalesce(@3, TimeZone)
            ,[UpdatedDate] = getdate()
            ,[ModifyBy] = 'sys'
        WHERE Id = @0
    ";
    #endregion

    #region Availability

    /// <summary>
    /// Fixed values for each type syncrhonized with the database values
    /// for the table CalendarAvailabilityType
    /// </summary>
    public enum AvailabilityType : short
    {
        //"unavailable", "free", "busy", "tentative", "offline"
        Unavailable = 0,
        Free = 1,
        Busy = 2,
        Tentative = 3,
        /// <summary>
        /// TODO This has no sense. An offline must be a type of event, is nothing about availability.
        /// But there is another kind of availability: 'transparent' that just means event must be discarded
        /// when computing availability.
        /// </summary>
        Offline = 4
    }

    public const string serverTimeZoneID = "America/Los_Angeles";

    /// <summary>
    /// Get availability table for the user between given date and times
    /// </summary>
    /// <param name="userID"></param>
    /// <param name="dateStart"></param>
    /// <param name="dateEnd"></param>
    /// <param name="excludeAdvanceTime">This avoids the check of 'advance time allowed to booking' by passing the lowest datetime possible
    /// to the Calendar API. It's needed when the user checking availability is the same provider and don't want this time to block itself
    /// but only clients.</param>
    /// <returns></returns>
    [Obsolete("Refactor to use the new GetAvailability.GetTimeline logic, far faster")]
    public static List<CalendarDll.ProviderAvailabilityResult> GetUserAvailability(int userID, DateTimeOffset dateStart, DateTimeOffset dateEnd, bool excludeAdvanceTime = false)
    {
        var lcCalendar = new CalendarDll.CalendarUtils();
        return
            lcCalendar.GetFreeEvents(
                new CalendarDll.CalendarUser(userID),
                dateStart,
                dateEnd,
                excludeAdvanceTime ? DateTime.MinValue : DateTime.Now);
    }
    /// <summary>
    /// Check if the user is available for all the time between dateStart and dateEnd
    /// </summary>
    /// <param name="userID">UserID as in database</param>
    /// <param name="dateStart">Start date and time for the time range (greater or equals than dateStart)</param>
    /// <param name="dateEnd">End date and time for the time range (less than dateEnd)</param>
    /// <returns>True when is available, False when not</returns>
    [Obsolete("Refactor to use the new GetAvailability.GetTimeline logic, far faster")]
    public static bool CheckUserAvailability(int userID, DateTimeOffset dateStart, DateTimeOffset dateEnd, bool excludeAdvanceTime = false)
    {
        foreach (var e in GetUserAvailability(userID, dateStart, dateEnd, excludeAdvanceTime))
        {
            var edt = e.DT;
            if ((e.CalendarAvailabilityTypeID == (int)CalendarDll.AvailabilityTypes.BUSY ||
                e.CalendarAvailabilityTypeID == (int)CalendarDll.AvailabilityTypes.UNAVAILABLE ||
                e.CalendarAvailabilityTypeID == (int)CalendarDll.AvailabilityTypes.TENTATIVE) &&
                edt >= dateStart &&
                edt < dateEnd)
                return false;
        }
        return true;
    }

    /// <summary>
    /// Check if the user has some block available for between dateStart and dateEnd.
    /// With almost one block available (free), will return true, and false for when there is
    /// no available block at all, just the opposite to GetUserAvailability.
    /// </summary>
    /// <param name="userID"></param>
    /// <param name="dateStart"></param>
    /// <param name="dateEnd"></param>
    /// <returns></returns>
    [Obsolete("Refactor to use the new GetAvailability.GetTimeline logic, far faster")]
    public static bool HasUserSomeAvailability(int userID, DateTime dateStart, DateTime dateEnd)
    {
        foreach (var e in GetUserAvailability(userID, dateStart, dateEnd))
{'fraction_non_alphanumeric': 0.1046701799018717, 'fraction_numerical': 0.003906959840087225, 'mean_word_length': 2.0515663986692543, 'pattern_counts': {'":': 0, '<': 83, '<?xml version=': 0, '>': 83, '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': '5801694', 'n_tokens_mistral': 2962, 'n_tokens_neox': 2755, 'n_words': 909}