예제 #1
0
        /// <summary>
        /// Get the existing sync state for this item, if it exists and is of the appropriate
        /// type, else null.
        /// </summary>
        /// <remarks>Outlook items are not true objects and don't have a common superclass,
        /// so we have to use this rather clumsy overloading.</remarks>
        /// <param name="contact">The item.</param>
        /// <returns>The appropriate sync state, or null if none.</returns>
        /// <exception cref="UnexpectedSyncStateClassException">if the sync state found is not of the expected class (shouldn't happen).</exception>
        public ContactSyncState GetSyncState(Outlook.ContactItem contact)
        {
            SyncState result = this.byOutlookId.ContainsKey(contact.EntryID) ? this.byOutlookId[contact.EntryID] : null;
            CrmId     crmId  = CheckForDuplicateSyncState(result, contact.GetCrmId());

            if (CrmId.IsValid(crmId))
            {
                if (result == null && this.byCrmId.ContainsKey(crmId))
                {
                    result = this.byCrmId[crmId];
                }
                else if (result != null && this.byCrmId.ContainsKey(crmId) == false)
                {
                    this.byCrmId[crmId] = result;
                    result.CrmEntryId   = crmId;
                }
            }

            if (result != null && result as ContactSyncState == null)
            {
                throw new UnexpectedSyncStateClassException("ContactSyncState", result);
            }

            return(result as ContactSyncState);
        }
예제 #2
0
        /// <summary>
        /// Get the existing sync state for this item, if it exists and is of the appropriate
        /// type, else null.
        /// </summary>
        /// <remarks>Outlook items are not true objects and don't have a common superclass,
        /// so we have to use this rather clumsy overloading.</remarks>
        /// <param name="appointment">The item.</param>
        /// <returns>The appropriate sync state, or null if none.</returns>
        /// <exception cref="UnexpectedSyncStateClassException">if the sync state found is not of the expected class (shouldn't happen).</exception>
        public AppointmentSyncState GetSyncState(Outlook.AppointmentItem appointment)
        {
            SyncState result = this.byOutlookId.ContainsKey(appointment.EntryID) ? this.byOutlookId[appointment.EntryID] : null;
            CrmId     crmId  = result == null?appointment.GetCrmId() : CheckForDuplicateSyncState(result, appointment.GetCrmId());

            if (CrmId.IsValid(crmId))
            {
                if (result == null && this.byCrmId.ContainsKey(crmId))
                {
                    result = this.byCrmId[crmId];
                }
                else if (result != null && this.byCrmId.ContainsKey(crmId) == false)
                {
                    this.byCrmId[crmId] = result;
                    result.CrmEntryId   = crmId;
                }
            }

            if (result != null && !(result is AppointmentSyncState))
            {
                throw new UnexpectedSyncStateClassException("AppointmentSyncState", result);
            }

            return(result as AppointmentSyncState);
        }
예제 #3
0
        private bool TryAddRecipientInModule(string moduleName, CrmId meetingId, Outlook.Recipient recipient)
        {
            bool  result;
            CrmId id = SetCrmRelationshipFromOutlook(this, meetingId, recipient, moduleName);

            if (CrmId.IsValid(id))
            {
                string smtpAddress = recipient.GetSmtpAddress();

                this.CacheAddressResolutionData(
                    new AddressResolutionData(moduleName, id, smtpAddress));

                CrmId accountId = CrmId.Get(RestAPIWrapper.GetRelationship(
                                                ContactSynchroniser.CrmModule, id.ToString(), "accounts"));

                if (CrmId.IsValid(accountId) &&
                    SetCrmRelationshipFromOutlook(this, meetingId, "Accounts", accountId))
                {
                    this.CacheAddressResolutionData(
                        new AddressResolutionData("Accounts", accountId, smtpAddress));
                }

                result = true;
            }
            else
            {
                result = false;
            }

            return(result);
        }
예제 #4
0
        /// <summary>
        /// Find all CRM records related to this recipient of this meeting, and produce address
        /// resolution data from them.
        /// </summary>
        /// <param name="olItem">An appointment, assumed to be a meeting.</param>
        /// <param name="recipient">A recipient of that meeting request.</param>
        /// <returns>A list of address resolution objects.</returns>
        private List <AddressResolutionData> ResolveRecipient(Outlook.AppointmentItem olItem, Outlook.Recipient recipient)
        {
            List <AddressResolutionData> result = new List <AddressResolutionData>();
            var smtpAddress = recipient.GetSmtpAddress();

            Log.Info($"recepientName= {recipient.Name}, recepient= {smtpAddress}");

            if (this.meetingRecipientsCache.ContainsKey(smtpAddress))
            {
                result.AddRange(meetingRecipientsCache[smtpAddress]);
            }
            else
            {
                CrmId meetingId = olItem.GetCrmId();
                Dictionary <string, CrmId> moduleIds = new Dictionary <string, CrmId>();

                if (CrmId.IsValid(meetingId))
                {
                    foreach (string moduleName in new string[] { "Leads", "Users", ContactSynchroniser.CrmModule })
                    {
                        CrmId moduleId = this.GetInviteeIdBySmtpAddress(smtpAddress, moduleName);
                        if (CrmId.IsValid(moduleId))
                        {
                            moduleIds[moduleName] = moduleId;
                            AddressResolutionData data = new AddressResolutionData(moduleName, moduleId, smtpAddress);
                            this.CacheAddressResolutionData(data);
                            result.Add(data);
                        }
                    }

                    if (moduleIds.ContainsKey(ContactSynchroniser.CrmModule))
                    {
                        CrmId accountId = CrmId.Get(RestAPIWrapper.GetRelationship(
                                                        ContactSynchroniser.CrmModule,
                                                        moduleIds[ContactSynchroniser.CrmModule].ToString(),
                                                        "accounts"));

                        if (CrmId.IsValid(accountId) &&
                            SetCrmRelationshipFromOutlook(this, meetingId, "Accounts", accountId))
                        {
                            var data = new AddressResolutionData("Accounts", accountId, smtpAddress);
                            this.CacheAddressResolutionData(data);
                            result.Add(data);
                        }
                    }
                }
            }

            return(result);
        }
예제 #5
0
        internal override CrmId AddOrUpdateItemFromOutlookToCrm(SyncState <Outlook.AppointmentItem> syncState)
        {
            CrmId previousCrmId = syncState.CrmEntryId;
            CrmId result        = base.AddOrUpdateItemFromOutlookToCrm(syncState);

            if (CrmId.IsValid(result))
            {
                if (CrmId.IsInvalid(previousCrmId)) /* i.e., it's new */
                {
                    if (syncState.OutlookItem.Recipients != null)
                    {
                        AddMeetingRecipientsFromOutlookToCrm(syncState.OutlookItem, result);
                    }

                    this.AddOrUpdateMeetingAcceptanceFromOutlookToCRM(syncState.OutlookItem);
                }
            }

            return(result);
        }
        /// <summary>
        /// Create an appropriate sync state for an contact item.
        /// </summary>
        /// <remarks>Outlook items are not true objects and don't have a common superclass,
        /// so we have to use this rather clumsy overloading.</remarks>
        /// <param name="contact">The item.</param>
        /// <returns>An appropriate sync state.</returns>
        private ContactSyncState CreateSyncState(Outlook.ContactItem contact)
        {
            CrmId            crmId = contact.GetCrmId();
            ContactSyncState result;

            if (CrmId.IsValid(crmId) && this.byCrmId.ContainsKey(crmId) && this.byCrmId[crmId] != null)
            {
                result = CheckUnexpectedFoundState <Outlook.ContactItem, ContactSyncState>(contact, crmId);
            }
            else
            {
                result = this.SetByOutlookId <ContactSyncState>(contact.EntryID,
                                                                new ContactSyncState(contact, crmId,
                                                                                     ParseDateTimeFromUserProperty(contact.UserProperties[ModifiedDatePropertyName])));
            }

            if (result != null && CrmId.IsValid(crmId))
            {
                this.byCrmId[crmId] = result;
            }

            return(result);
        }
 /// <summary>
 /// Remove all references to this sync state, if I hold any.
 /// </summary>
 /// <param name="state">The state to remove.</param>
 internal void RemoveSyncState(SyncState state)
 {
     lock (this.creationLock)
     {
         SyncState ignore;
         try
         {
             if (this.byOutlookId[state.OutlookItemEntryId] == state)
             {
                 this.byOutlookId.TryRemove(state.OutlookItemEntryId, out ignore);
             }
         }
         catch (KeyNotFoundException) { }
         catch (COMException) { }
         try
         {
             if (CrmId.IsValid(state.CrmEntryId) && this.byCrmId[state.CrmEntryId] == state)
             {
                 this.byCrmId.TryRemove(state.CrmEntryId, out ignore);
             }
         }
         catch (KeyNotFoundException) { }
     }
 }
        /// <summary>
        /// Create an appropriate sync state for an task item.
        /// </summary>
        /// <remarks>Outlook items are not true objects and don't have a common superclass,
        /// so we have to use this rather clumsy overloading.</remarks>
        /// <param name="task">The item.</param>
        /// <returns>An appropriate sync state, or null if the task was invalid.</returns>
        private TaskSyncState CreateSyncState(Outlook.TaskItem task)
        {
            TaskSyncState result;

            CrmId crmId = task.GetCrmId();

            if (CrmId.IsValid(crmId) && this.byCrmId.ContainsKey(crmId) && this.byCrmId[crmId] != null)
            {
                result = CheckUnexpectedFoundState <Outlook.TaskItem, TaskSyncState>(task, crmId);
            }
            else
            {
                result = this.SetByOutlookId <TaskSyncState>(task.EntryID,
                                                             new TaskSyncState(task, crmId,
                                                                               ParseDateTimeFromUserProperty(task.UserProperties[ModifiedDatePropertyName])));
            }

            if (result != null && CrmId.IsValid(crmId))
            {
                this.byCrmId[crmId] = result;
            }

            return(result);
        }
예제 #9
0
 /// <summary>
 /// True if <see cref="CrmId.IsValid(CrmId)"/> is false of this id.
 /// </summary>
 /// <param name="id">The object which may or may not be a valid CRM id.</param>
 /// <returns>True if <see cref="CrmId.IsValid(CrmId)"/> is false of this id.</returns>
 public static bool IsInvalid(CrmId id)
 {
     return(!CrmId.IsValid(id));
 }
예제 #10
0
 /// <summary>
 ///     Validates a CRM id.
 /// </summary>
 /// <param name="id">The object which may or may not be a valid CRM id.</param>
 /// <returns>
 ///     True if `id` is not null, matches <see cref="Validator" /> pattern and
 ///     is of suitable length.
 /// </returns>
 public static bool IsValid(CrmId id)
 {
     return(id != null && id.IsValid());
 }