Ejemplo n.º 1
0
        /// <summary>
        /// </summary>
        /// <param name="addedAppointments"></param>
        /// <param name="existingAppointments"></param>
        /// <returns></returns>
        private List <Appointment> UpdateWithChildId(AppointmentsWrapper addedAppointments,
                                                     AppointmentsWrapper existingAppointments)
        {
            //Add appointments to update
            var updateList = new List <Appointment>();

            foreach (var appointment in addedAppointments)
            {
                var presentAppointment = existingAppointments.FirstOrDefault(t => t.CompareSourceId(appointment));
                if (presentAppointment != null)
                {
                    var childKey = appointment.GetChildEntryKey();
                    if (!presentAppointment.ExtendedProperties.ContainsKey(childKey))
                    {
                        presentAppointment.ExtendedProperties.Add(childKey, appointment.AppointmentId);
                    }
                    else
                    {
                        presentAppointment.ExtendedProperties[childKey] = appointment.AppointmentId;
                    }
                    updateList.Add(presentAppointment);
                }
            }
            return(updateList);
        }
        /// <summary>
        /// </summary>
        /// <param name="syncProfile"></param>
        /// <param name="sourceList"></param>
        /// <param name="destinationList"></param>
        /// <param name="destAppointmentsToDelete"></param>
        /// <param name="destAppointmentsToUpdate"></param>
        /// <param name="sourceAppointmentsToUpdate"></param>
        /// <param name="destOrphanEntries"></param>
        /// <returns>
        /// </returns>
        private void EvaluateAppointmentsToDelete(CalendarSyncProfile syncProfile,
                                                  AppointmentsWrapper sourceList, AppointmentsWrapper destinationList,
                                                  List <Appointment> destAppointmentsToDelete,
                                                  List <Appointment> destAppointmentsToUpdate, List <Appointment> sourceAppointmentsToUpdate,
                                                  List <Appointment> destOrphanEntries)
        {
            var addDescription =
                syncProfile.CalendarEntryOptions.HasFlag(CalendarEntryOptionsEnum.Description);
            var addReminders =
                syncProfile.CalendarEntryOptions.HasFlag(CalendarEntryOptionsEnum.Reminders);
            var addAttendeesToDescription =
                syncProfile.CalendarEntryOptions.HasFlag(CalendarEntryOptionsEnum.AttendeesToDescription);

            if (!destinationList.Any())
            {
                foreach (var appointment in sourceList)
                {
                    if (appointment.ChildId != null)
                    {
                        var key = AppointmentHelper.GetChildEntryKey(sourceList.CalendarId);
                        if (!appointment.ExtendedProperties.ContainsKey(key))
                        {
                            appointment.ExtendedProperties.Remove(key);
                        }
                        sourceAppointmentsToUpdate.AddCompareForUpdate(appointment);
                    }
                }
                return;
            }

            foreach (var destAppointment in destinationList)
            {
                //If SourceId is null, it is not a copy of any entry from the selected source calendar
                if (destAppointment.SourceId == null)
                {
                    if (syncProfile.SyncMode == SyncModeEnum.OneWay)
                    {
                        //If mode is one way & user has disabled delete, do not remove this entry, as this is an original entry in the calendar
                        //Else this entry is not a copy of any appointment in source calendar so delete it
                        destOrphanEntries.Add(destAppointment);
                    }
                    else
                    {
                        if (destAppointment.ChildId == null)
                        {
                            var childAppointment = sourceList.FirstOrDefault(t => destAppointment.CompareSourceId(t));
                            if (childAppointment != null)
                            {
                                destAppointment.ChildId = childAppointment.AppointmentId;
                                var key = childAppointment.GetChildEntryKey();
                                if (!destAppointment.ExtendedProperties.ContainsKey(key))
                                {
                                    destAppointment.ExtendedProperties.Add(key, childAppointment.AppointmentId);
                                }
                                else
                                {
                                    destAppointment.ExtendedProperties[key] = childAppointment.AppointmentId;
                                }
                                destAppointmentsToUpdate.AddCompareForUpdate(destAppointment);
                            }
                        }
                        else if (syncProfile.SyncSettings.KeepLastModifiedVersion)
                        {
                            var childAppointment =
                                sourceList.FirstOrDefault(t => t.AppointmentId.Equals(destAppointment.ChildId));
                            if (childAppointment == null)
                            {
                                destAppointmentsToDelete.Add(destAppointment);
                            }
                        }
                    }
                }
                else
                {
                    //If the mode is two way, look for its parent copy in Source calendar
                    Appointment sourceAppointment;
                    if (syncProfile.SyncMode == SyncModeEnum.TwoWay &&
                        syncProfile.SyncSettings.KeepLastModifiedVersion)
                    {
                        //If no entry was found, it is original entry of the calendar, Ignore
                        //If a child entry is found in source calendar, compare
                        sourceAppointment = sourceList.FirstOrDefault(t => t.CompareSourceId(destAppointment));
                        if (sourceAppointment != null)
                        {
                            //If any entry is found in source appointment and its contents are not equal to source appointment,
                            //If an entry is found and i same, ignore
                            if (!CompareAppointments(destAppointment, sourceAppointment, addDescription,
                                                     addReminders, addAttendeesToDescription))
                            {
                                if (sourceAppointment.LastModified.HasValue && destAppointment.LastModified.HasValue)
                                {
                                    if (destAppointment.LastModified.GetValueOrDefault() >
                                        sourceAppointment.LastModified.GetValueOrDefault())
                                    {
                                        sourceAppointment.CopyDetail(destAppointment,
                                                                     syncProfile.CalendarEntryOptions);
                                        sourceAppointmentsToUpdate.AddCompareForUpdate(sourceAppointment);
                                        continue;
                                    }
                                }
                                //Destination Calendar Entry is not Matching its Source Calendar Entry, Update it
                                destAppointment.CopyDetail(sourceAppointment, syncProfile.CalendarEntryOptions);
                                destAppointmentsToUpdate.AddCompareForUpdate(destAppointment);
                                continue;
                            }
                        }
                    }

                    //If source appointment is not null, means it is a copy of an existing entry in Source calendar
                    sourceAppointment = sourceList.FirstOrDefault(t => t.CompareSourceId(destAppointment));
                    if (sourceAppointment != null)
                    {
                        //If any entry is found in source appointment and its contents are not equal to source appointment
                        if (!CompareAppointments(destAppointment, sourceAppointment, addDescription, addReminders,
                                                 addAttendeesToDescription))
                        {
                            destAppointment.CopyDetail(sourceAppointment, syncProfile.CalendarEntryOptions);
                            destAppointmentsToUpdate.AddCompareForUpdate(destAppointment);
                        }
                    }
                    else
                    {
                        //No parent entry is found, delete it
                        sourceAppointment = sourceList.FirstOrDefault(t =>
                                                                      CompareAppointments(destAppointment, t, addDescription, addReminders,
                                                                                          addAttendeesToDescription));
                        if (sourceAppointment == null)
                        {
                            //If parent entry isn't found
                            destAppointmentsToDelete.Add(destAppointment);
                        }
                    }
                }
            }
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="addedAppointments"></param>
 /// <param name="existingAppointments"></param>
 /// <returns></returns>
 private List<Appointment> UpdateWithChildId(AppointmentsWrapper addedAppointments,
     AppointmentsWrapper existingAppointments)
 {
     //Add appointments to update
     var updateList = new List<Appointment>();
     foreach (var appointment in addedAppointments)
     {
         var presentAppointment = existingAppointments.FirstOrDefault(t => t.CompareSourceId(appointment));
         if (presentAppointment != null)
         {
             var childKey = appointment.GetChildEntryKey();
             if (!presentAppointment.ExtendedProperties.ContainsKey(childKey))
             {
                 presentAppointment.ExtendedProperties.Add(childKey, appointment.AppointmentId);
             }
             else
             {
                 presentAppointment.ExtendedProperties[childKey] = appointment.AppointmentId;
             }
             updateList.Add(presentAppointment);
         }
     }
     return updateList;
 }
        /// <summary>
        /// </summary>
        /// <param name="syncProfile"></param>
        /// <param name="sourceList"></param>
        /// <param name="destinationList"></param>
        /// <param name="destAppointmentsToDelete"></param>
        /// <param name="destAppointmentsToUpdate"></param>
        /// <param name="sourceAppointmentsToUpdate"></param>
        /// <param name="destOrphanEntries"></param>
        /// <returns>
        /// </returns>
        private void EvaluateAppointmentsToDelete(CalendarSyncProfile syncProfile,
            AppointmentsWrapper sourceList, AppointmentsWrapper destinationList,
            List<Appointment> destAppointmentsToDelete,
            List<Appointment> destAppointmentsToUpdate, List<Appointment> sourceAppointmentsToUpdate,
            List<Appointment> destOrphanEntries)
        {
            var addDescription =
                syncProfile.CalendarEntryOptions.HasFlag(CalendarEntryOptionsEnum.Description);
            var addReminders =
                syncProfile.CalendarEntryOptions.HasFlag(CalendarEntryOptionsEnum.Reminders);
            var addAttendeesToDescription =
                syncProfile.CalendarEntryOptions.HasFlag(CalendarEntryOptionsEnum.AttendeesToDescription);

            if (!destinationList.Any())
            {
                foreach (var appointment in sourceList)
                {
                    if (appointment.ChildId != null)
                    {
                        var key = AppointmentHelper.GetChildEntryKey(sourceList.CalendarId);
                        if (!appointment.ExtendedProperties.ContainsKey(key))
                        {
                            appointment.ExtendedProperties.Remove(key);
                        }
                        sourceAppointmentsToUpdate.AddCompareForUpdate(appointment);
                    }
                }
                return;
            }

            foreach (var destAppointment in destinationList)
            {
                //If SourceId is null, it is not a copy of any entry from the selected source calendar
                if (destAppointment.SourceId == null)
                {
                    if (syncProfile.SyncMode == SyncModeEnum.OneWay)
                    {
                        //If mode is one way & user has disabled delete, do not remove this entry, as this is an original entry in the calendar
                        //Else this entry is not a copy of any appointment in source calendar so delete it
                        destOrphanEntries.Add(destAppointment);
                    }
                    else
                    {
                        if (destAppointment.ChildId == null)
                        {
                            var childAppointment = sourceList.FirstOrDefault(t => destAppointment.CompareSourceId(t));
                            if (childAppointment != null)
                            {
                                destAppointment.ChildId = childAppointment.AppointmentId;
                                var key = childAppointment.GetChildEntryKey();
                                if (!destAppointment.ExtendedProperties.ContainsKey(key))
                                {
                                    destAppointment.ExtendedProperties.Add(key, childAppointment.AppointmentId);
                                }
                                else
                                {
                                    destAppointment.ExtendedProperties[key] = childAppointment.AppointmentId;
                                }
                                destAppointmentsToUpdate.AddCompareForUpdate(destAppointment);
                            }
                        }
                        else if (syncProfile.SyncSettings.KeepLastModifiedVersion)
                        {
                            var childAppointment =
                                sourceList.FirstOrDefault(t => t.AppointmentId.Equals(destAppointment.ChildId));
                            if (childAppointment == null)
                            {
                                destAppointmentsToDelete.Add(destAppointment);
                            }
                        }
                    }
                }
                else
                {
                    //If the mode is two way, look for its parent copy in Source calendar
                    Appointment sourceAppointment;
                    if (syncProfile.SyncMode == SyncModeEnum.TwoWay
                        && syncProfile.SyncSettings.KeepLastModifiedVersion)
                    {
                        //If no entry was found, it is original entry of the calendar, Ignore
                        //If a child entry is found in source calendar, compare
                        sourceAppointment = sourceList.FirstOrDefault(t => t.CompareSourceId(destAppointment));
                        if (sourceAppointment != null)
                        {
                            //If any entry is found in source appointment and its contents are not equal to source appointment,
                            //If an entry is found and i same, ignore
                            if (!CompareAppointments(destAppointment, sourceAppointment, addDescription,
                                addReminders, addAttendeesToDescription))
                            {
                                if (sourceAppointment.LastModified.HasValue && destAppointment.LastModified.HasValue)
                                {
                                    if (destAppointment.LastModified.GetValueOrDefault() >
                                        sourceAppointment.LastModified.GetValueOrDefault())
                                    {
                                        sourceAppointment.CopyDetail(destAppointment,
                                            syncProfile.CalendarEntryOptions);
                                        sourceAppointmentsToUpdate.AddCompareForUpdate(sourceAppointment);
                                        continue;
                                    }
                                }
                                //Destination Calendar Entry is not Matching its Source Calendar Entry, Update it
                                destAppointment.CopyDetail(sourceAppointment, syncProfile.CalendarEntryOptions);
                                destAppointmentsToUpdate.AddCompareForUpdate(destAppointment);
                                continue;
                            }
                        }
                    }

                    //If source appointment is not null, means it is a copy of an existing entry in Source calendar
                    sourceAppointment = sourceList.FirstOrDefault(t => t.CompareSourceId(destAppointment));
                    if (sourceAppointment != null)
                    {
                        //If any entry is found in source appointment and its contents are not equal to source appointment
                        if (!CompareAppointments(destAppointment, sourceAppointment, addDescription, addReminders,
                            addAttendeesToDescription))
                        {
                            destAppointment.CopyDetail(sourceAppointment, syncProfile.CalendarEntryOptions);
                            destAppointmentsToUpdate.AddCompareForUpdate(destAppointment);
                        }
                    }
                    else
                    {
                        //No parent entry is found, delete it
                        sourceAppointment = sourceList.FirstOrDefault(t =>
                            CompareAppointments(destAppointment, t, addDescription, addReminders,
                                addAttendeesToDescription));
                        if (sourceAppointment == null)
                        {
                            //If parent entry isn't found
                            destAppointmentsToDelete.Add(destAppointment);
                        }
                    }
                }
            }
        }