Ejemplo n.º 1
0
        public Task <GenericComObjectWrapper <DistListItem> > Map2To1(DistributionList source, GenericComObjectWrapper <DistListItem> target, IEntityMappingLogger logger, DistributionListSychronizationContext context)
        {
            var outlookMembersByAddress = new Dictionary <string, GenericComObjectWrapper <Recipient> >(StringComparer.InvariantCultureIgnoreCase);

            target.Inner.DLName = source.Name;
            if (!string.IsNullOrEmpty(source.Description))
            {
                target.Inner.Body = source.Description;
            }

            try
            {
                using (var userPropertiesWrapper = GenericComObjectWrapper.Create(target.Inner.UserProperties))
                {
                    using (var userProperty = GenericComObjectWrapper.Create(userPropertiesWrapper.Inner.Find("NICKNAME")))
                    {
                        if (userProperty.Inner != null)
                        {
                            userProperty.Inner.Value = source.Nickname;
                        }
                        else if (!string.IsNullOrEmpty(source.Nickname))
                        {
                            using (var newUserProperty = GenericComObjectWrapper.Create(userPropertiesWrapper.Inner.Add("NICKNAME", OlUserPropertyType.olText, true)))
                            {
                                newUserProperty.Inner.Value = source.Nickname;
                            }
                        }
                    }
                }
            }
            catch (COMException ex)
            {
                s_logger.Warn("Can't access UserProperty of Distribution List!", ex);
                logger.LogMappingWarning("Can't access UserProperty of Distribution List!", ex);
            }

            try
            {
                for (int i = 1; i <= target.Inner.MemberCount; i++)
                {
                    var recipientWrapper = GenericComObjectWrapper.Create(target.Inner.GetMember(i));
                    if (!string.IsNullOrEmpty(recipientWrapper.Inner?.Address) &&
                        !outlookMembersByAddress.ContainsKey(recipientWrapper.Inner.Address))
                    {
                        outlookMembersByAddress.Add(recipientWrapper.Inner.Address, recipientWrapper);
                    }
                    else
                    {
                        recipientWrapper.Dispose();
                    }
                }

                foreach (var sourceMember in source.Members.Concat(source.NonAddressBookMembers))
                {
                    GenericComObjectWrapper <Recipient> existingRecipient;
                    if (!string.IsNullOrEmpty(sourceMember.EmailAddress) &&
                        outlookMembersByAddress.TryGetValue(sourceMember.EmailAddress, out existingRecipient))
                    {
                        outlookMembersByAddress.Remove(sourceMember.EmailAddress);
                        existingRecipient.Dispose();
                    }
                    else
                    {
                        string recipientString = sourceMember.DisplayName ?? sourceMember.EmailAddress;

                        if (!string.IsNullOrEmpty(recipientString))
                        {
                            using (var recipientWrapper = GenericComObjectWrapper.Create(context.OutlookSession.CreateRecipient(recipientString)))
                            {
                                recipientWrapper.Inner.Resolve();
                                target.Inner.AddMember(recipientWrapper.Inner);
                            }
                        }
                    }
                }

                foreach (var existingRecipient in outlookMembersByAddress.ToArray())
                {
                    target.Inner.RemoveMember(existingRecipient.Value.Inner);
                    outlookMembersByAddress.Remove(existingRecipient.Key);
                }
            }
            catch (COMException ex)
            {
                s_logger.Warn("Can't access member of Distribution List!", ex);
                logger.LogMappingWarning("Can't access member of Distribution List!", ex);
            }
            finally
            {
                foreach (var existingRecipient in outlookMembersByAddress.Values)
                {
                    existingRecipient.Dispose();
                }
            }
            return(Task.FromResult(target));
        }
Ejemplo n.º 2
0
 protected override string GetBtypePropertyValue(DistributionList btypeEntity)
 {
     return(btypeEntity.Name);
 }
Ejemplo n.º 3
0
        public Task <DistributionList> Map1To2(GenericComObjectWrapper <DistListItem> source, DistributionList target, IEntityMappingLogger logger, DistributionListSychronizationContext context)
        {
            target.Members.Clear();
            target.NonAddressBookMembers.Clear();
            target.Name        = source.Inner.DLName;
            target.Description = source.Inner.Body;

            try
            {
                using (var userPropertiesWrapper = GenericComObjectWrapper.Create(source.Inner.UserProperties))
                {
                    using (var userProperty = GenericComObjectWrapper.Create(userPropertiesWrapper.Inner.Find("NICKNAME")))
                    {
                        target.Nickname = userProperty.Inner?.Value.ToString();
                    }
                }
            }
            catch (COMException ex)
            {
                s_logger.Warn("Can't access UserProperty of Distribution List!", ex);
                logger.LogMappingWarning("Can't access UserProperty of Distribution List!", ex);
            }

            for (int i = 1; i <= source.Inner.MemberCount; i++)
            {
                try
                {
                    using (var recipientWrapper = GenericComObjectWrapper.Create(source.Inner.GetMember(i)))
                    {
                        var serverFileName = context.GetServerFileNameByEmailAddress(recipientWrapper.Inner.Address);
                        if (serverFileName != null)
                        {
                            var nameWithoutEmail       = Regex.Replace(recipientWrapper.Inner.Name, " \\([^()]*\\)$", string.Empty);
                            var distributionListMember = new KnownDistributionListMember(recipientWrapper.Inner.Address, nameWithoutEmail, serverFileName);
                            target.Members.Add(distributionListMember);
                        }
                        else
                        {
                            var distributionListMember = new DistributionListMember(recipientWrapper.Inner.Address, recipientWrapper.Inner.Name);
                            target.NonAddressBookMembers.Add(distributionListMember);
                        }
                    }
                }
                catch (COMException ex)
                {
                    s_logger.Warn("Can't access member of Distribution List!", ex);
                    logger.LogMappingWarning("Can't access member of Distribution List!", ex);
                }
            }

            return(Task.FromResult(target));
        }
Ejemplo n.º 4
0
 protected override bool AreEqual(GenericComObjectWrapper <DistListItem> atypeEntity, DistributionList btypeEntity)
 {
     return(atypeEntity.Inner.DLName == btypeEntity.Name);
 }