<commit_before>﻿using LNF.Cache;
using LNF.Models.Scheduler;
using LNF.Repository;
using LNF.Repository.Data;
using LNF.Repository.Scheduler;
using LNF.Scheduler;
using NHibernate;
using NHibernate.Context;
using NHibernate.Criterion;
using System;
using System.Collections.Generic;
using System.Linq;

namespace LNF.Impl.DataAccess.Scheduler
{
    public class ReservationRepository<TContext> : Repository<TContext, Reservation>, IReservationRepository where TContext : ICurrentSessionContext
    {
        private IRepository<Activity> Activity
        {
            get { return SchedulerRepositoryCollection<TContext>.Current.Activity; }
        }

        private IReservationHistoryRepository ReservationHistory
        {
            get { return SchedulerRepositoryCollection<TContext>.Current.ReservationHistory; }
        }

        private IRepository<Resource> Resource
        {
            get { return SchedulerRepositoryCollection<TContext>.Current.Resource; }
        }

        private IRepository<ReservationInvitee> ReservationInvitee
        {
            get { return SchedulerRepositoryCollection<TContext>.Current.ReservationInvitee; }
        }

        public Reservation GetRepairReservationInProgress(int resourceId)
        {
            // procReservationSelect @Action = 'SelectRepairReserv'

            //SELECT Rv.*
            //FROM dbo.Reservation Rv
            //INNER JOIN dbo.Activity A ON Rv.ActivityID = A.ActivityID
            //WHERE A.Editable = 0
            //    AND ResourceID = @ResourceID
            //    AND ActualEndDateTime IS NULL
            //    AND EndDateTime > GETDATE()

            Reservation result = Query().FirstOrDefault(x =>
                !x.Activity.Editable
                && x.Resource.ResourceID == resourceId
                && x.ActualEndDateTime == null
                && x.EndDateTime > DateTime.Now);

            return result;
        }

        public Reservation InsertRepair(int resourceId, int clientId, DateTime beginDateTime, DateTime endDateTime, DateTime actualBeginDateTime, string notes, int? modifiedByClientId)
        {
            // procReservationInsert @Action = 'InsertRepair'

            //SELECT @ActivityID = ActivityID
            //FROM dbo.Activity
            //WHERE Editable = 0

            Activity repairActivity = Activity.Query().FirstOrDefault(x => !x.Editable);

            if (repairActivity == null)
                throw new InvalidOperationException("Cannot start a repair reservation because a repair activity is not defined (Editable = false).");

            /*
            INSERT INTO dbo.Reservation(ResourceID, ClientID, AccountID, ActivityID,
              BeginDateTime, EndDateTime, ActualBeginDateTime, ActualEndDateTime,
              ClientIDBegin, ClientIDEnd, CreatedOn, LastModifiedOn,
              Duration, Notes, ChargeMultiplier, RecurrenceID, ApplyLateChargePenalty, AutoEnd,
              HasProcessInfo, HasInvitees, IsActive, IsStarted, IsUnloaded, MaxReservedDuration, KeepAlive)
            VALUES
            (
                @ResourceID, @ClientID, sselData.dbo.udf_GetLabAccountID(), @ActivityID,
                @BeginDateTime, @EndDateTime
                , @ActualBeginDateTime
                , NULL      --ActualEndDateTime
                , @ClientID --ClientIDBegin
                , NULL      --ClientIDEnd
                , GETDATE() --CreatedOn
                , GETDATE() --LastModifiedOn
                , DATEDIFF(minute, @BeginDateTime, @EndDateTime)    --Duration
                , @Notes    --Notes
                , 1         --ChargeMultiplier
                , -1        --RecurrenceID
                , 0         --ApplyLateChargePenalty
                , 0         --AutoEnd
                , 0         --HasProcessInfo
                , 0         --HasInvitees
                , 1         --IsActive
                , 1         --IsStarted
                , 1         --IsUnloaded
                , DATEDIFF(minute, @BeginDateTime, @EndDateTime)    --MaxReservedDuration
                , 0         --KeepAlive
            )
            */

            double duration = (endDateTime - beginDateTime).TotalMinutes;

            Reservation result = new Reservation()
            {
                Resource = Session.Get<Resource>(resourceId),
                Client = Session.Get<Client>(clientId),
                Account = Properties.Current.LabAccount,
                Activity = repairActivity,
                BeginDateTime = beginDateTime,
                EndDateTime = endDateTime,
                ActualBeginDateTime = actualBeginDateTime,
                ActualEndDateTime = null,
                ClientIDBegin = clientId,
                ClientIDEnd = null,
                CreatedOn = DateTime.Now,
                LastModifiedOn = DateTime.Now,
                Duration = duration,
                Notes = notes,
                ChargeMultiplier = 1,
                RecurrenceID = -1,
                ApplyLateChargePenalty = false,
                AutoEnd = false,
                HasProcessInfo = false,
                HasInvitees = false,
                IsActive = true,
                IsStarted = true,
                IsUnloaded = true,
                MaxReservedDuration = duration,
                KeepAlive = false
            };

            Session.SaveOrUpdate(result);

            ReservationHistory.Insert("InsertRepair", "procReservationInsert", result, modifiedByClientId);

            return result;
        }
<commit_msg>Fixed issue with repair reservations added with IsStarted = false.<commit_after>﻿using LNF.Cache;
using LNF.Models.Scheduler;
using LNF.Repository;
using LNF.Repository.Data;
using LNF.Repository.Scheduler;
using LNF.Scheduler;
using NHibernate;
using NHibernate.Context;
using NHibernate.Criterion;
using System;
using System.Collections.Generic;
using System.Linq;

namespace LNF.Impl.DataAccess.Scheduler
{
    public class ReservationRepository<TContext> : Repository<TContext, Reservation>, IReservationRepository where TContext : ICurrentSessionContext
    {
        private IRepository<Activity> Activity
        {
            get { return SchedulerRepositoryCollection<TContext>.Current.Activity; }
        }

        private IReservationHistoryRepository ReservationHistory
        {
            get { return SchedulerRepositoryCollection<TContext>.Current.ReservationHistory; }
        }

        private IRepository<Resource> Resource
        {
            get { return SchedulerRepositoryCollection<TContext>.Current.Resource; }
        }

        private IRepository<ReservationInvitee> ReservationInvitee
        {
            get { return SchedulerRepositoryCollection<TContext>.Current.ReservationInvitee; }
        }

        public Reservation GetRepairReservationInProgress(int resourceId)
        {
            // procReservationSelect @Action = 'SelectRepairReserv'

            //SELECT Rv.*
            //FROM dbo.Reservation Rv
            //INNER JOIN dbo.Activity A ON Rv.ActivityID = A.ActivityID
            //WHERE A.Editable = 0
            //    AND ResourceID = @ResourceID
            //    AND ActualEndDateTime IS NULL
            //    AND EndDateTime > GETDATE()

            Reservation result = Query().FirstOrDefault(x =>
                !x.Activity.Editable
                && x.Resource.ResourceID == resourceId
                && x.ActualEndDateTime == null
                && x.EndDateTime > DateTime.Now);

            return result;
        }

        public Reservation InsertRepair(int resourceId, int clientId, DateTime beginDateTime, DateTime endDateTime, DateTime actualBeginDateTime, string notes, int? modifiedByClientId)
        {
            // procReservationInsert @Action = 'InsertRepair'

            //SELECT @ActivityID = ActivityID
            //FROM dbo.Activity
            //WHERE Editable = 0

            Activity repairActivity = Activity.Query().FirstOrDefault(x => !x.Editable);

            if (repairActivity == null)
                throw new InvalidOperationException("Cannot start a repair reservation because a repair activity is not defined (Editable = false).");

            /*
            INSERT INTO dbo.Reservation(ResourceID, ClientID, AccountID, ActivityID,
              BeginDateTime, EndDateTime, ActualBeginDateTime, ActualEndDateTime,
              ClientIDBegin, ClientIDEnd, CreatedOn, LastModifiedOn,
              Duration, Notes, ChargeMultiplier, RecurrenceID, ApplyLateChargePenalty, AutoEnd,
              HasProcessInfo, HasInvitees, IsActive, IsStarted, IsUnloaded, MaxReservedDuration, KeepAlive)
            VALUES
            (
                @ResourceID, @ClientID, sselData.dbo.udf_GetLabAccountID(), @ActivityID,
                @BeginDateTime, @EndDateTime
                , @ActualBeginDateTime
                , NULL      --ActualEndDateTime
                , @ClientID --ClientIDBegin
                , NULL      --ClientIDEnd
                , GETDATE() --CreatedOn
                , GETDATE() --LastModifiedOn
                , DATEDIFF(minute, @BeginDateTime, @EndDateTime)    --Duration
                , @Notes    --Notes
                , 1         --ChargeMultiplier
                , -1        --RecurrenceID
                , 0         --ApplyLateChargePenalty
                , 0         --AutoEnd
                , 0         --HasProcessInfo
                , 0         --HasInvitees
                , 1         --IsActive
                , 1         --IsStarted
                , 1         --IsUnloaded
                , DATEDIFF(minute, @BeginDateTime, @EndDateTime)    --MaxReservedDuration
                , 0         --KeepAlive
            )
            */

            double duration = (endDateTime - beginDateTime).TotalMinutes;

            Reservation result = new Reservation()
            {
                Resource = Session.Get<Resource>(resourceId),
                Client = Session.Get<Client>(clientId),
                Account = Properties.Current.LabAccount,
                Activity = repairActivity,
                BeginDateTime = beginDateTime,
                EndDateTime = endDateTime,
                ActualBeginDateTime = actualBeginDateTime,
                ActualEndDateTime = null,
                ClientIDBegin = clientId,
                ClientIDEnd = null,
                CreatedOn = DateTime.Now,
                LastModifiedOn = DateTime.Now,
                Duration = duration,
                Notes = notes,
                ChargeMultiplier = 1,
                RecurrenceID = -1,
                ApplyLateChargePenalty = false,
                AutoEnd = false,
                HasProcessInfo = false,
                HasInvitees = false,
                IsActive = true,
                IsStarted = true, // was false - fixed on 2017-08-21 [jg]
                IsUnloaded = true, // was false - fixed on 2017-08-21 [jg]
                MaxReservedDuration = duration,
                KeepAlive = false
            };

            Session.SaveOrUpdate(result);

            ReservationHistory.Insert("InsertRepair", "procReservationInsert", result, modifiedByClientId);

            return result;
        }

{'fraction_non_alphanumeric': 0.07603748326639893, 'fraction_numerical': 0.00392681838464971, 'mean_word_length': 1.6943976917528252, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 33, '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': '5391512', 'n_tokens_mistral': 2851, 'n_tokens_neox': 2631, 'n_words': 606}