public Task <vCard> Map1To2(IContactItemWrapper source, vCard target, IEntitySynchronizationLogger logger, ICardDavRepositoryLogger context)
        {
            target.RevisionDate = source.Inner.LastModificationTime.ToUniversalTime();

            target.GivenName       = source.Inner.FirstName;
            target.FamilyName      = source.Inner.LastName;
            target.NamePrefix      = source.Inner.Title;
            target.NameSuffix      = source.Inner.Suffix;
            target.AdditionalNames = source.Inner.MiddleName;
            target.Gender          = MapGender2To1(source.Inner.Gender);

            target.Assistant = source.Inner.AssistantName;
            target.Spouse    = source.Inner.Spouse;
            target.Manager   = source.Inner.ManagerName;

            MapEmailAddresses1To2(source.Inner, target, logger);

            if (!string.IsNullOrEmpty(source.Inner.FileAs))
            {
                target.FormattedName = source.Inner.FileAs;
            }
            else if (!string.IsNullOrEmpty(source.Inner.CompanyAndFullName))
            {
                target.FormattedName = source.Inner.CompanyAndFullName;
            }
            else if (target.EmailAddresses.Count >= 1)
            {
                target.FormattedName = target.EmailAddresses[0].Address;
            }
            else
            {
                target.FormattedName = "<Empty>";
            }

            target.Nicknames.Clear();
            if (!string.IsNullOrEmpty(source.Inner.NickName))
            {
                Array.ForEach(
                    source.Inner.NickName.Split(new[] { CultureInfo.CurrentCulture.TextInfo.ListSeparator }, StringSplitOptions.RemoveEmptyEntries),
                    c => target.Nicknames.Add(c)
                    );
            }

            target.AccessClassification = CommonEntityMapper.MapPrivacy1To2(source.Inner.Sensitivity);

            target.Categories.Clear();
            if (!string.IsNullOrEmpty(source.Inner.Categories))
            {
                Array.ForEach(
                    source.Inner.Categories.Split(new[] { CultureInfo.CurrentCulture.TextInfo.ListSeparator }, StringSplitOptions.RemoveEmptyEntries),
                    c => target.Categories.Add(c.Trim())
                    );
            }

            target.IMs.Clear();
            if (!string.IsNullOrEmpty(source.Inner.IMAddress))
            {
                //IMAddress are expected to be in form of ([Protocol]: [Address]; [Protocol]: [Address])
                var imsRaw = source.Inner.IMAddress.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var imRaw in imsRaw)
                {
                    var imDetails = imRaw.Trim().Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    var im        = new vCardIMPP();
                    if (imDetails.Length == 1)
                    {
                        im.Handle = imDetails[0].Trim();
                        // Set default ServiceType to the configured DefaultImServiceType (defaults to AIM)
                        im.ServiceType = _configuration.DefaultImServicType;
                    }
                    else if (imDetails.Length > 1)
                    {
                        var serviceType = IMTypeUtils.GetIMServiceType(imDetails[0].Trim());
                        if (serviceType == null)
                        {
                            im.ServiceType = _configuration.DefaultImServicType;
                            s_logger.Warn($"Unknown IM ServiceType '{imDetails[0]}' not implemented, defaulting to '{_configuration.DefaultImServicType}'");
                            logger.LogWarning($"Unknown IM ServiceType '{imDetails[0]}' not implemented, defaulting to '{_configuration.DefaultImServicType}'");
                        }
                        else
                        {
                            im.ServiceType = serviceType.Value;
                        }

                        im.Handle = imRaw.Substring(imRaw.IndexOf(":") + 1).Trim();
                    }

                    //Only add the im Address if not empty
                    if (!string.IsNullOrEmpty(im.Handle))
                    {
                        im.IsPreferred = target.IMs.Count == 0;
                        im.ItemType    = ItemType.HOME;
                        target.IMs.Add(im);
                    }
                }
            }

            target.DeliveryAddresses.Clear();
            if (!string.IsNullOrEmpty(source.Inner.HomeAddress))
            {
                vCardDeliveryAddress homeAddress = new vCardDeliveryAddress();
                homeAddress.AddressType.Add(vCardDeliveryAddressTypes.Home);
                homeAddress.City       = source.Inner.HomeAddressCity;
                homeAddress.Country    = source.Inner.HomeAddressCountry;
                homeAddress.PostalCode = source.Inner.HomeAddressPostalCode;
                homeAddress.Region     = source.Inner.HomeAddressState;
                homeAddress.Street     = source.Inner.HomeAddressStreet;
                homeAddress.PoBox      = source.Inner.HomeAddressPostOfficeBox;
                if (source.Inner.SelectedMailingAddress == OlMailingAddress.olHome)
                {
                    homeAddress.AddressType.Add(vCardDeliveryAddressTypes.Preferred);
                }

                target.DeliveryAddresses.Add(homeAddress);
            }

            if (!string.IsNullOrEmpty(source.Inner.BusinessAddress) || !string.IsNullOrEmpty(source.Inner.OfficeLocation))
            {
                vCardDeliveryAddress businessAddress = new vCardDeliveryAddress();
                businessAddress.AddressType.Add(vCardDeliveryAddressTypes.Work);
                businessAddress.City       = source.Inner.BusinessAddressCity;
                businessAddress.Country    = source.Inner.BusinessAddressCountry;
                businessAddress.PostalCode = source.Inner.BusinessAddressPostalCode;
                businessAddress.Region     = source.Inner.BusinessAddressState;
                businessAddress.Street     = source.Inner.BusinessAddressStreet;
                businessAddress.PoBox      = source.Inner.BusinessAddressPostOfficeBox;
                if (!string.IsNullOrEmpty(source.Inner.OfficeLocation))
                {
                    businessAddress.ExtendedAddress = source.Inner.OfficeLocation;
                }

                if (source.Inner.SelectedMailingAddress == OlMailingAddress.olBusiness)
                {
                    businessAddress.AddressType.Add(vCardDeliveryAddressTypes.Preferred);
                }

                target.DeliveryAddresses.Add(businessAddress);
            }

            if (!string.IsNullOrEmpty(source.Inner.OtherAddress))
            {
                vCardDeliveryAddress otherAddress = new vCardDeliveryAddress();
                otherAddress.City       = source.Inner.OtherAddressCity;
                otherAddress.Country    = source.Inner.OtherAddressCountry;
                otherAddress.PostalCode = source.Inner.OtherAddressPostalCode;
                otherAddress.Region     = source.Inner.OtherAddressState;
                otherAddress.Street     = source.Inner.OtherAddressStreet;
                otherAddress.PoBox      = source.Inner.OtherAddressPostOfficeBox;
                if (source.Inner.SelectedMailingAddress == OlMailingAddress.olOther)
                {
                    otherAddress.AddressType.Add(vCardDeliveryAddressTypes.Preferred);
                }

                target.DeliveryAddresses.Add(otherAddress);
            }

            MapPhoneNumbers1To2(source.Inner, target);

            if (_configuration.MapAnniversary)
            {
                target.Anniversary = source.Inner.Anniversary.Equals(OutlookUtility.OUTLOOK_DATE_NONE) ? default(DateTime?) : source.Inner.Anniversary.Date;
            }

            if (_configuration.MapBirthday)
            {
                target.BirthDate = source.Inner.Birthday.Equals(OutlookUtility.OUTLOOK_DATE_NONE) ? default(DateTime?) : source.Inner.Birthday.Date;
            }

            target.Organization = source.Inner.CompanyName;
            target.Department   = source.Inner.Department;

            target.Title = source.Inner.JobTitle;
            target.Role  = source.Inner.Profession;

            target.Websites.Clear();
            if (!string.IsNullOrEmpty(source.Inner.PersonalHomePage))
            {
                target.Websites.Add(new vCardWebsite(source.Inner.PersonalHomePage, vCardWebsiteTypes.Personal));
            }

            if (!string.IsNullOrEmpty(source.Inner.BusinessHomePage))
            {
                target.Websites.Add(new vCardWebsite(source.Inner.BusinessHomePage, vCardWebsiteTypes.Work));
            }

            MapCertificate1To2(source.Inner, target, logger);

            if (_configuration.MapContactPhoto)
            {
                MapPhoto1To2(source.Inner, target, logger);
            }

            target.Notes.Clear();
            if (!string.IsNullOrEmpty(source.Inner.Body))
            {
                target.Notes.Add(new vCardNote(source.Inner.Body));
            }

            return(Task.FromResult(target));
        }
Пример #2
0
        public Task <vCard> Map1To2(ContactItemWrapper source, vCard target, IEntityMappingLogger logger, ICardDavRepositoryLogger context)
        {
            target.GivenName       = source.Inner.FirstName;
            target.FamilyName      = source.Inner.LastName;
            target.NamePrefix      = source.Inner.Title;
            target.NameSuffix      = source.Inner.Suffix;
            target.AdditionalNames = source.Inner.MiddleName;
            target.Gender          = MapGender2To1(source.Inner.Gender);

            MapEmailAddresses1To2(source.Inner, target, logger);

            if (!string.IsNullOrEmpty(source.Inner.FileAs))
            {
                target.FormattedName = source.Inner.FileAs;
            }
            else if (!string.IsNullOrEmpty(source.Inner.CompanyAndFullName))
            {
                target.FormattedName = source.Inner.CompanyAndFullName;
            }
            else if (target.EmailAddresses.Count >= 1)
            {
                target.FormattedName = target.EmailAddresses[0].Address;
            }
            else
            {
                target.FormattedName = "<Empty>";
            }

            target.Nicknames.Clear();
            if (!string.IsNullOrEmpty(source.Inner.NickName))
            {
                Array.ForEach(
                    source.Inner.NickName.Split(new[] { CultureInfo.CurrentCulture.TextInfo.ListSeparator }, StringSplitOptions.RemoveEmptyEntries),
                    c => target.Nicknames.Add(c)
                    );
            }

            target.AccessClassification = MapPrivacy1To2(source.Inner.Sensitivity);

            target.Categories.Clear();
            if (!string.IsNullOrEmpty(source.Inner.Categories))
            {
                Array.ForEach(
                    source.Inner.Categories.Split(new[] { CultureInfo.CurrentCulture.TextInfo.ListSeparator }, StringSplitOptions.RemoveEmptyEntries),
                    c => target.Categories.Add(c.Trim())
                    );
            }

            target.IMs.Clear();
            if (!string.IsNullOrEmpty(source.Inner.IMAddress))
            {
                //IMAddress are expected to be in form of ([Protocol]: [Address]; [Protocol]: [Address])
                var imsRaw = source.Inner.IMAddress.Split(new [] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var imRaw in imsRaw)
                {
                    var imDetails = imRaw.Trim().Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    var im        = new vCardIMPP();
                    if (imDetails.Length == 1)
                    {
                        im.Handle = imDetails[0].Trim();
                        // Set default ServiceType to AIM
                        im.ServiceType = IMServiceType.AIM;
                    }
                    else
                    {
                        im.ServiceType = IMTypeUtils.GetIMServiceType(imDetails[0].Trim()) ?? IMServiceType.AIM;
                        im.Handle      = imDetails[1].Trim();
                    }

                    //Only add the im Address if not empty
                    if (!string.IsNullOrEmpty(im.Handle))
                    {
                        im.IsPreferred = target.IMs.Count == 0;
                        im.ItemType    = ItemType.HOME;
                        target.IMs.Add(im);
                    }
                }
            }

            target.DeliveryAddresses.Clear();
            if (!string.IsNullOrEmpty(source.Inner.HomeAddress))
            {
                vCardDeliveryAddress homeAddress = new vCardDeliveryAddress();
                homeAddress.AddressType.Add(vCardDeliveryAddressTypes.Home);
                homeAddress.City       = source.Inner.HomeAddressCity;
                homeAddress.Country    = source.Inner.HomeAddressCountry;
                homeAddress.PostalCode = source.Inner.HomeAddressPostalCode;
                homeAddress.Region     = source.Inner.HomeAddressState;
                homeAddress.Street     = source.Inner.HomeAddressStreet;
                homeAddress.PoBox      = source.Inner.HomeAddressPostOfficeBox;
                if (source.Inner.SelectedMailingAddress == OlMailingAddress.olHome)
                {
                    homeAddress.AddressType.Add(vCardDeliveryAddressTypes.Preferred);
                }
                target.DeliveryAddresses.Add(homeAddress);
            }

            if (!string.IsNullOrEmpty(source.Inner.BusinessAddress))
            {
                vCardDeliveryAddress businessAddress = new vCardDeliveryAddress();
                businessAddress.AddressType.Add(vCardDeliveryAddressTypes.Work);
                businessAddress.City       = source.Inner.BusinessAddressCity;
                businessAddress.Country    = source.Inner.BusinessAddressCountry;
                businessAddress.PostalCode = source.Inner.BusinessAddressPostalCode;
                businessAddress.Region     = source.Inner.BusinessAddressState;
                businessAddress.Street     = source.Inner.BusinessAddressStreet;
                businessAddress.PoBox      = source.Inner.BusinessAddressPostOfficeBox;
                if (source.Inner.SelectedMailingAddress == OlMailingAddress.olBusiness)
                {
                    businessAddress.AddressType.Add(vCardDeliveryAddressTypes.Preferred);
                }
                target.DeliveryAddresses.Add(businessAddress);
            }

            if (!string.IsNullOrEmpty(source.Inner.OtherAddress))
            {
                vCardDeliveryAddress otherAddress = new vCardDeliveryAddress();
                otherAddress.City       = source.Inner.OtherAddressCity;
                otherAddress.Country    = source.Inner.OtherAddressCountry;
                otherAddress.PostalCode = source.Inner.OtherAddressPostalCode;
                otherAddress.Region     = source.Inner.OtherAddressState;
                otherAddress.Street     = source.Inner.OtherAddressStreet;
                otherAddress.PoBox      = source.Inner.OtherAddressPostOfficeBox;
                if (source.Inner.SelectedMailingAddress == OlMailingAddress.olOther)
                {
                    otherAddress.AddressType.Add(vCardDeliveryAddressTypes.Preferred);
                }
                target.DeliveryAddresses.Add(otherAddress);
            }

            MapPhoneNumbers1To2(source.Inner, target);

            if (_configuration.MapBirthday)
            {
                target.BirthDate = source.Inner.Birthday.Equals(new DateTime(4501, 1, 1, 0, 0, 0)) ? default(DateTime?) : source.Inner.Birthday.Date;
            }
            target.Organization = source.Inner.CompanyName;
            target.Department   = source.Inner.Department;

            target.Title  = source.Inner.JobTitle;
            target.Office = source.Inner.OfficeLocation;

            target.Websites.Clear();
            if (!string.IsNullOrEmpty(source.Inner.PersonalHomePage))
            {
                target.Websites.Add(new vCardWebsite(source.Inner.PersonalHomePage, vCardWebsiteTypes.Personal));
            }
            if (!string.IsNullOrEmpty(source.Inner.BusinessHomePage))
            {
                target.Websites.Add(new vCardWebsite(source.Inner.BusinessHomePage, vCardWebsiteTypes.Work));
            }

            MapCertificate1To2(source.Inner, target, logger);

            if (_configuration.MapContactPhoto)
            {
                MapPhoto1To2(source.Inner, target, logger);
            }

            target.Notes.Clear();
            if (!string.IsNullOrEmpty(source.Inner.Body))
            {
                target.Notes.Add(new vCardNote(source.Inner.Body));
            }

            return(Task.FromResult(target));
        }