Beispiel #1
0
#pragma warning disable 1998
        public async Task Handle(ParseOutlookItem message)
        {
            //#region processingState "planned"?
            string processingState = null;

            try
            {
                processingState = message.Item.Location.DisplayName;
            }
            catch { }


            if (string.IsNullOrEmpty(processingState))
            {
                if (includeBlankLocations)
                {
                    processingState = "planned";
                }
                else
                {
                    processingState = "";
                }
            }

            processingState = processingState.ToLower();
            //#endregion

            //if (processingState.ToLower() == "planned")
            //#region planned
            //{
            log.LogVariable("Not Specified", nameof(processingState), processingState);

            if (message.Item.BodyPreview != null)
            {
                if (message.Item.BodyPreview.Contains("<<< "))
                {
                    if (message.Item.BodyPreview.Contains("End >>>"))
                    {
                        message.Item.BodyPreview = t.ReplaceAtLocationAll(message.Item.BodyPreview, "<<< ", "End >>>", "", true);
                        message.Item.BodyPreview = message.Item.BodyPreview.Replace("<<< End >>>", "");
                        message.Item.BodyPreview = message.Item.BodyPreview.Trim();
                    }
                }
            }

            log.LogStandard("Not Specified", "Trying to do UpdateEvent on item.Id:" + message.Item.Id + " to have new location location : " + processingState);
            Event updatedItem = outlookExchangeOnlineAPIClient.UpdateEvent(userEmailAddess, message.Item.Id, "{\"Location\": {\"DisplayName\": \"" + processingState + "\"},\"Body\": {\"ContentType\": \"HTML\",\"Content\": \"" + ReplaceLinesInBody(message.Item.BodyPreview) + "\"}}");

            //if (updatedItem == null)
            //{
            //    return false;
            //}

            log.LogStandard("Not Specified", "Trying create new appointment for item.Id : " + message.Item.Id + " and the UpdateEvent returned Updateditem: " + updatedItem.ToString());

            Appointment appo;
            int         appoId = 0;

            appo = sqlController.AppointmentsFind(message.Item.Id);
            if (appo == null)
            {
                appo = new Appointment(message.Item.Id, message.Item.Start.DateTime, (message.Item.End.DateTime - message.Item.Start.DateTime).Minutes, message.Item.Subject, "planned", updatedItem.BodyPreview, t.Bool(sqlController.SettingRead(Settings.colorsRule)), null);
                appo.ParseBodyContent(sdkCore);
                appoId = sqlController.AppointmentsCreate(appo);
            }

            //log.LogStandard("Not Specified", "Before calling CalendarItemIntrepret.AppointmentsCreate");
            //log.LogStandard("Not Specified", "After calling CalendarItemIntrepret.AppointmentsCreate");

            if (appoId > 0)
            {
                log.LogStandard("Not Specified", "Appointment created successfully for item.Id : " + message.Item.Id);
                outlookOnlineController.CalendarItemUpdate(appo.GlobalId, appo.Start, ProcessingStateOptions.Processed, appo.Body);
                bus.SendLocal(new AppointmentCreatedInOutlook(appo)).Wait();
            }
            else
            {
                if (appoId == 0)
                {
                    outlookOnlineController.CalendarItemUpdate(appo.GlobalId, appo.Start, ProcessingStateOptions.Exception, appo.Body);
                }
                if (appoId == -1)
                {
                    log.LogStandard("Not Specified", "Appointment not created successfully for item.Id : " + message.Item.Id);

                    #region appo.Body = 'text'
                    appo.Body = "<<< Parsing error: Start >>>" +
                                Environment.NewLine + "Global ID already exists in the database." +
                                Environment.NewLine + "Indicating that this appointment has already been created." +
                                Environment.NewLine + "Likely course, is that you set the Appointment’s location to 'planned'/[blank] again." +
                                Environment.NewLine + "" +
                                Environment.NewLine + "If you wanted to a create a new appointment in the calendar:" +
                                Environment.NewLine + "- Create a new appointment in the calendar" +
                                Environment.NewLine + "- Create or copy the wanted details to the new appointment" +
                                Environment.NewLine + "" +
                                Environment.NewLine + "Item.Id :" + message.Item.Id +
                                Environment.NewLine + "<<< Parsing error: End >>>" +
                                Environment.NewLine + "" +
                                Environment.NewLine + appo.Body;
                    #endregion
                    outlookOnlineController.CalendarItemUpdate(appo.GlobalId, appo.Start, ProcessingStateOptions.ParsingFailed, appo.Body);
                }
            }

            //AllParsed = true;
            //}
            //#endregion

            //if (processingState.ToLower() == "cancel")
            //#region cancel
            //{
            //    log.LogVariable("Not Specified", nameof(processingState), processingState);

            //    Appointment appo = new Appointment(message.Item.Id, message.Item.Start.DateTime, (message.Item.End.DateTime - message.Item.Start.DateTime).Minutes, message.Item.Subject, message.Item.Location.DisplayName, ReplaceLinesInBody(message.Item.BodyPreview), t.Bool(sqlController.SettingRead(Settings.colorsRule)), true, null);

            //    if (sqlController.AppointmentsCancel(appo.GlobalId))
            //        outlookOnlineController.CalendarItemUpdate(appo.GlobalId, appo.Start, ProcessingStateOptions.Canceled, appo.Body);
            //    else
            //        outlookOnlineController.CalendarItemUpdate(appo.GlobalId, appo.Start, ProcessingStateOptions.ParsingFailed, appo.Body);

            //    //AllParsed = true;
            //}
            //#endregion

            //if (processingState.ToLower() == "processed")
            //#region processed
            //{
            //    Appointment appo = sqlController.AppointmentsFind(message.Item.Id);

            //    log.LogStandard("Not Specified", "ParseCalendarItems appo start is : " + appo.Start.ToString());
            //    log.LogStandard("Not Specified", "ParseCalendarItems item start is : " + message.Item.Start.DateTime.ToString());
            //    log.LogStandard("Not Specified", "ParseCalendarItems appo end is : " + appo.End.ToString());
            //    log.LogStandard("Not Specified", "ParseCalendarItems item end is : " + message.Item.End.DateTime.ToString());
            //    if (appo.Start != message.Item.Start.DateTime)
            //    {
            //        log.LogStandard("Not Specified", "ParseCalendarItems updating calendar entry with globalId : " + appo.GlobalId);
            //        sqlController.AppointmentsUpdate(appo.GlobalId, ProcessingStateOptions.Processed, appo.Body, "", "", appo.Completed, message.Item.Start.DateTime, message.Item.End.DateTime, (message.Item.End.DateTime - message.Item.Start.DateTime).Minutes);
            //    }
            //}
            //#endregion
        }
        public bool CalendarItemReflecting(string globalId)
        {
            try
            {
                #region appointment = 'find one';
                appointments appointment = null;
                string       Categories  = null;
                if (globalId == null)
                {
                    appointment = sqlController.AppointmentsFindOne(0);
                }
                //else
                //    appointment = sqlController.AppointmentsFind(globalId);

                if (appointment == null) //double checks status if no new found
                {
                    appointment = sqlController.AppointmentsFindOne(1);
                }
                #endregion

                if (appointment == null)
                {
                    return(false);
                }
                log.LogVariable(t.GetMethodName("OutlookOnlineController"), nameof(appointments), appointment.ToString());

                Event item = AppointmentItemFind(appointment.global_id, appointment.start_at.Value.AddHours(-36), appointment.start_at.Value.AddHours(36)); // TODO!
                if (item != null)
                {
                    item.Location.DisplayName = appointment.processing_state;
                    #region item.Categories = 'workflowState'...
                    switch (appointment.processing_state)
                    {
                    case "Planned":
                        Categories = null;
                        break;

                    case "Processed":
                        Categories = CalendarItemCategory.Processing.ToString();
                        break;

                    case "Created":
                        Categories = CalendarItemCategory.Processing.ToString();
                        break;

                    case "Sent":
                        Categories = CalendarItemCategory.Sent.ToString();
                        break;

                    case "Retrived":
                        Categories = CalendarItemCategory.Retrived.ToString();
                        break;

                    case "Completed":
                        Categories = CalendarItemCategory.Completed.ToString();
                        break;

                    case "Canceled":
                        Categories = CalendarItemCategory.Revoked.ToString();
                        break;

                    case "Revoked":
                        Categories = CalendarItemCategory.Revoked.ToString();
                        break;

                    case "Exception":
                        Categories = CalendarItemCategory.Error.ToString();
                        break;

                    case "Failed_to_intrepid":
                        Categories = CalendarItemCategory.Error.ToString();
                        break;

                    default:
                        Categories = CalendarItemCategory.Error.ToString();
                        break;
                    }
                    #endregion
                    item.BodyPreview = appointment.body;
                    #region item.Body = appointment.expectionString + item.Body + appointment.response ...
                    if (!string.IsNullOrEmpty(appointment.response))
                    {
                        if (t.Bool(sqlController.SettingRead(Settings.responseBeforeBody)))
                        {
                            item.BodyPreview = "<<< Response: Start >>>" +
                                               Environment.NewLine +
                                               Environment.NewLine + appointment.response +
                                               Environment.NewLine +
                                               Environment.NewLine + "<<< Response: End >>>" +
                                               Environment.NewLine +
                                               Environment.NewLine + item.BodyPreview;
                        }
                        else
                        {
                            item.BodyPreview = item.BodyPreview +
                                               Environment.NewLine +
                                               Environment.NewLine + "<<< Response: Start >>>" +
                                               Environment.NewLine +
                                               Environment.NewLine + appointment.response +
                                               Environment.NewLine +
                                               Environment.NewLine + "<<< Response: End >>>";
                        }
                    }
                    if (!string.IsNullOrEmpty(appointment.exceptionString))
                    {
                        item.BodyPreview = "<<< Exception: Start >>>" +
                                           Environment.NewLine +
                                           Environment.NewLine + appointment.exceptionString +
                                           Environment.NewLine +
                                           Environment.NewLine + "<<< Exception: End >>>" +
                                           Environment.NewLine +
                                           Environment.NewLine + item.BodyPreview;
                    }
                    #endregion
                    Event eresult = outlookExchangeOnlineAPIClient.UpdateEvent(userEmailAddess, item.Id, CalendarItemUpdateBody(item.BodyPreview, item.Location.DisplayName, Categories));
                    if (eresult == null)
                    {
                        return(false);
                    }
                    else
                    {
                        log.LogStandard(t.GetMethodName("OutlookOnlineController"), "globalId:'" + appointment.global_id + "' reflected in calendar");
                    }
                }
                else
                {
                    log.LogWarning(t.GetMethodName("OutlookOnlineController"), "globalId:'" + appointment.global_id + "' no longer in calendar, so hence is considered to be reflected in calendar");
                }

                sqlController.AppointmentsReflected(appointment.global_id);
                log.LogStandard(t.GetMethodName("OutlookOnlineController"), "globalId:'" + appointment.global_id + "' reflected in database");
                return(true);
            }
            catch (Exception ex)
            {
                throw new Exception(t.GetMethodName("OutlookOnlineController") + " failed", ex);
            }
        }