public static void RenderRecipientFreeBusyData(TextWriter output, SchedulingRecipientInfo[] schedulingRecipientInfos, FreeBusyQueryResult[] freeBusyQueryResults, ExDateTime startDateFreeBusyWindow, ExDateTime endDateFreeBusyWindow, bool show24Hours, bool renderDataForAllRecipients, ExTimeZone timeZone, Microsoft.Exchange.Clients.Owa.Core.WorkingHours workingHours)
        {
            ExTraceGlobals.CalendarCallTracer.TraceDebug(0L, "SchedulingTabRenderingUtilities.RenderRecipientFreeBusyData");
            int days = ((DateTime)endDateFreeBusyWindow - (DateTime)startDateFreeBusyWindow).Days;
            int num  = 0;
            int num2 = 48;

            if (!show24Hours)
            {
                num  = SchedulingTabRenderingUtilities.GetWorkDayStartHour(workingHours, startDateFreeBusyWindow) * 60 / 30;
                num2 = SchedulingTabRenderingUtilities.GetWorkDayEndHour(workingHours, startDateFreeBusyWindow) * 60 / 30;
            }
            int num3 = 48;
            int num4 = num2 - num;

            output.Write("<div id=fbd>");
            output.Write("var rgUFB = new Array();");
            int num5 = 0;

            for (int i = 0; i < schedulingRecipientInfos.Length; i++)
            {
                FreeBusyQueryResult     freeBusyQueryResult     = (freeBusyQueryResults == null) ? null : freeBusyQueryResults[i];
                SchedulingRecipientInfo schedulingRecipientInfo = schedulingRecipientInfos[i];
                if (renderDataForAllRecipients || schedulingRecipientInfo.GetFreeBusyData)
                {
                    output.Write("rgUFB[");
                    output.Write(num5++);
                    output.Write("] = new RecipientFreeBusyData(\"");
                    output.Write(schedulingRecipientInfo.ID);
                    output.Write("\",\"");
                    if (freeBusyQueryResult == null || Utilities.IsFatalFreeBusyError(freeBusyQueryResult.ExceptionInfo) || string.IsNullOrEmpty(freeBusyQueryResult.MergedFreeBusy))
                    {
                        ExTraceGlobals.CalendarDataTracer.TraceDebug <string, string, object>(0L, "Unable to get free/busy data for user '{0} ({1})' Exception: {2}", schedulingRecipientInfo.DisplayName, schedulingRecipientInfo.EmailAddress, (freeBusyQueryResult != null) ? freeBusyQueryResult.ExceptionInfo : "<recipientQueryResult is null>");
                        int num6 = num4 * days;
                        for (int j = 0; j < num6; j++)
                        {
                            output.Write('4');
                        }
                        if (freeBusyQueryResult != null)
                        {
                            output.Write("\",\"{0}", freeBusyQueryResult.ExceptionInfo.ErrorCode);
                        }
                    }
                    else
                    {
                        string mergedFreeBusy = freeBusyQueryResult.MergedFreeBusy;
                        SchedulingTabRenderingUtilities.SetFreeBusyDayLightBasedValue(startDateFreeBusyWindow, endDateFreeBusyWindow, timeZone, ref mergedFreeBusy);
                        for (int k = 0; k < days; k++)
                        {
                            int num7 = k * num3 + num;
                            for (int l = 0; l < num4; l++)
                            {
                                output.Write(mergedFreeBusy[num7 + l]);
                            }
                        }
                    }
                    output.Write("\");");
                }
            }
            output.Write("</div>");
        }
        public static string GetFreeBusy(OwaContext owaContext, string recipientPrimarySmtpAddress, ExDateTime startDate, ExDateTime endDate, HttpContext httpContext, out string allDayFreeBusy, out string workingHoursFreeBusy, out string oofMessage)
        {
            UserContext userContext = owaContext.UserContext;

            ExTraceGlobals.CalendarCallTracer.TraceDebug(0L, "DatePickerEventHandler.GetFreeBusy");
            allDayFreeBusy       = null;
            workingHoursFreeBusy = null;
            oofMessage           = null;
            AvailabilityQuery availabilityQuery = new AvailabilityQuery();

            if (HttpContext.Current != null)
            {
                availabilityQuery.HttpResponse = HttpContext.Current.Response;
            }
            availabilityQuery.DesiredFreeBusyView = new FreeBusyViewOptions
            {
                RequestedView = FreeBusyViewType.FreeBusyMerged,
                MergedFreeBusyIntervalInMinutes = 30,
                TimeWindow = new Duration((DateTime)startDate, (DateTime)endDate)
            };
            availabilityQuery.ClientContext                 = ClientContext.Create(owaContext.LogonIdentity.ClientSecurityContext, userContext.ExchangePrincipal.MailboxInfo.OrganizationId, owaContext.Budget, userContext.TimeZone, userContext.UserCulture, AvailabilityQuery.CreateNewMessageId());
            availabilityQuery.MailboxArray                  = new MailboxData[1];
            availabilityQuery.MailboxArray[0]               = new MailboxData();
            availabilityQuery.MailboxArray[0].Email         = new EmailAddress();
            availabilityQuery.MailboxArray[0].Email.Address = recipientPrimarySmtpAddress.ToString();
            ExTraceGlobals.CalendarTracer.TraceDebug <ExDateTime, ExDateTime>(0L, "Getting free/busy data from {0} to {1}", startDate, endDate);
            Stopwatch watch = Utilities.StartWatch();
            AvailabilityQueryResult availabilityQueryResult;
            bool flag = Utilities.ExecuteAvailabilityQuery(owaContext, availabilityQuery, true, true, out availabilityQueryResult);

            Utilities.StopWatch(watch, "ReadADOrgPerson.GetFreeBusy (Execute Availability Query)");
            if (flag)
            {
                FreeBusyQueryResult freeBusyQueryResult = availabilityQueryResult.FreeBusyResults[0];
                if (freeBusyQueryResult != null)
                {
                    allDayFreeBusy = freeBusyQueryResult.MergedFreeBusy;
                }
                if (userContext.WorkingHours.IsTimeZoneDifferent)
                {
                    workingHoursFreeBusy = string.Empty;
                }
                else
                {
                    int           workDayStartHour = SchedulingTabRenderingUtilities.GetWorkDayStartHour(userContext.WorkingHours, startDate);
                    int           workDayEndHour   = SchedulingTabRenderingUtilities.GetWorkDayEndHour(userContext.WorkingHours, endDate);
                    int           num           = workDayEndHour - workDayStartHour;
                    int           num2          = 24 - workDayEndHour;
                    StringBuilder stringBuilder = new StringBuilder(num * 24);
                    int           i             = 0;
                    SchedulingTabRenderingUtilities.SetFreeBusyDayLightBasedValue(startDate, endDate, userContext.TimeZone, ref allDayFreeBusy);
                    while (i < allDayFreeBusy.Length)
                    {
                        i += workDayStartHour * 2;
                        int num3;
                        if (i + num * 2 >= allDayFreeBusy.Length)
                        {
                            num3 = allDayFreeBusy.Length - i;
                        }
                        else
                        {
                            num3 = num * 2;
                        }
                        stringBuilder.Append(allDayFreeBusy.Substring(i, num3));
                        i += num3;
                        i += num2 * 2;
                    }
                    workingHoursFreeBusy = stringBuilder.ToString();
                }
                if (freeBusyQueryResult != null && !string.IsNullOrEmpty(freeBusyQueryResult.CurrentOofMessage))
                {
                    oofMessage = freeBusyQueryResult.CurrentOofMessage;
                }
            }
            else if (availabilityQueryResult != null && availabilityQueryResult.FreeBusyResults != null && availabilityQueryResult.FreeBusyResults.Length > 0)
            {
                FreeBusyQueryResult freeBusyQueryResult2 = availabilityQueryResult.FreeBusyResults[0];
                if (freeBusyQueryResult2 != null)
                {
                    if (Utilities.IsFatalFreeBusyError(freeBusyQueryResult2.ExceptionInfo))
                    {
                        ExTraceGlobals.CalendarTracer.TraceDebug <LocalizedException>(0L, "An error happened trying to get free/busy info for this recipient. Exception: {0}", freeBusyQueryResult2.ExceptionInfo);
                        ErrorInformation exceptionHandlingInformation = Utilities.GetExceptionHandlingInformation(freeBusyQueryResult2.ExceptionInfo, userContext.MailboxIdentity);
                        return(exceptionHandlingInformation.Message);
                    }
                }
                else
                {
                    ExTraceGlobals.CalendarTracer.TraceDebug(0L, "An error happened trying to get free/busy info for this recipient. FreeBusyResult is null.");
                }
            }
            return(null);
        }