private static bool CopyCloudIdFromOscContactSourcesIfCurrentCloudIdIsBlank(ICorePropertyBag propertyBag, OscNetworkProperties networkProperties)
 {
     if (propertyBag.IsPropertyDirty(ItemSchema.CloudId))
     {
         return(false);
     }
     if (!string.IsNullOrEmpty(propertyBag.GetValueOrDefault <string>(ItemSchema.CloudId)))
     {
         return(false);
     }
     if (networkProperties != null && !string.IsNullOrEmpty(networkProperties.NetworkUserId))
     {
         propertyBag[ItemSchema.CloudId] = networkProperties.NetworkUserId;
         return(true);
     }
     return(false);
 }
 private static bool UpdatePartnerNetworkIdAndUserId(ICorePropertyBag propertyBag, byte[] oscContactSources, OscNetworkProperties networkProperties)
 {
     if (propertyBag.IsPropertyDirty(ContactSchema.PartnerNetworkId) || propertyBag.IsPropertyDirty(ContactSchema.PartnerNetworkUserId))
     {
         return(false);
     }
     if (oscContactSources == null)
     {
         propertyBag.Delete(ContactSchema.PartnerNetworkId);
         propertyBag.Delete(ContactSchema.PartnerNetworkUserId);
         return(true);
     }
     if (networkProperties != null)
     {
         propertyBag[ContactSchema.PartnerNetworkId]     = networkProperties.NetworkId;
         propertyBag[ContactSchema.PartnerNetworkUserId] = networkProperties.NetworkUserId;
         return(true);
     }
     return(false);
 }
        private void MigrateContacts(StoreObjectId folder)
        {
            HashSet <string> hashSet = new HashSet <string>(StringComparer.Ordinal);

            foreach (IStorePropertyBag storePropertyBag in new OscFolderMigration.OscContactsEnumerator(this.session, folder, this.xsoFactory))
            {
                VersionedId valueOrDefault  = storePropertyBag.GetValueOrDefault <VersionedId>(ItemSchema.Id, null);
                string      valueOrDefault2 = storePropertyBag.GetValueOrDefault <string>(StoreObjectSchema.DisplayName, string.Empty);
                if (valueOrDefault == null)
                {
                    OscFolderMigration.Tracer.TraceError((long)this.GetHashCode(), "OSC folder migration: skipping contact with blank id.");
                }
                else
                {
                    string valueOrDefault3 = storePropertyBag.GetValueOrDefault <string>(ItemSchema.CloudId, string.Empty);
                    if (!string.IsNullOrEmpty(valueOrDefault3))
                    {
                        if (hashSet.Contains(valueOrDefault3))
                        {
                            OscFolderMigration.Tracer.TraceDebug <string, StoreObjectId, string>((long)this.GetHashCode(), "OSC folder migration: contact {0} (id={1}) has cloud id: {2} but that id is already assigned to another contact.", valueOrDefault2, valueOrDefault.ObjectId, valueOrDefault3);
                            this.ClearCloudIdFromContact(valueOrDefault, valueOrDefault2);
                        }
                        else
                        {
                            OscFolderMigration.Tracer.TraceDebug <string, StoreObjectId, string>((long)this.GetHashCode(), "OSC folder migration: contact {0} (id={1}) already has cloud id: {2}", valueOrDefault2, valueOrDefault.ObjectId, valueOrDefault3);
                            hashSet.Add(valueOrDefault3);
                        }
                    }
                    else
                    {
                        byte[] valueOrDefault4 = storePropertyBag.GetValueOrDefault <byte[]>(ContactSchema.OscContactSourcesForContact, null);
                        if (valueOrDefault4 == null || valueOrDefault4.Length == 0)
                        {
                            OscFolderMigration.Tracer.TraceDebug <string, StoreObjectId>((long)this.GetHashCode(), "OSC folder migration: contact {0} (id={1}) does NOT have cloud id and the OscContactSources property is blank.  Cannot stamp cloud id.", valueOrDefault2, valueOrDefault.ObjectId);
                        }
                        else
                        {
                            try
                            {
                                OscNetworkProperties oscNetworkProperties = this.oscContactSourcesParser.ReadOscContactSource(valueOrDefault4);
                                if (oscNetworkProperties == null || string.IsNullOrEmpty(oscNetworkProperties.NetworkUserId))
                                {
                                    OscFolderMigration.Tracer.TraceDebug <string, StoreObjectId>((long)this.GetHashCode(), "OSC folder migration: cannot stamp cloud id on contact {0} (id={1}) because the OscContactSources property is blank.", valueOrDefault2, valueOrDefault.ObjectId);
                                }
                                else if (hashSet.Contains(oscNetworkProperties.NetworkUserId))
                                {
                                    OscFolderMigration.Tracer.TraceDebug <string, string, StoreObjectId>((long)this.GetHashCode(), "OSC folder migration: cannot stamp cloud id '{0}' on contact {1} (id={2}) because that cloud id is already assigned to a different contact.", oscNetworkProperties.NetworkUserId, valueOrDefault2, valueOrDefault.ObjectId);
                                }
                                else
                                {
                                    this.AssignCloudIdToContact(valueOrDefault, oscNetworkProperties.NetworkUserId, valueOrDefault2);
                                    hashSet.Add(oscNetworkProperties.NetworkUserId);
                                }
                            }
                            catch (OscContactSourcesForContactParseException arg)
                            {
                                OscFolderMigration.Tracer.TraceError <string, StoreObjectId, OscContactSourcesForContactParseException>((long)this.GetHashCode(), "OSC folder migration: failed to parse OscContactSources of contact {0} (id={1}).  Exception: {2}", valueOrDefault2, valueOrDefault.ObjectId, arg);
                            }
                        }
                    }
                }
            }
        }
        private static bool UpdateDependentProperties(ICorePropertyBag propertyBag, byte[] oscContactSources, OscNetworkProperties networkProperties)
        {
            bool flag = false;

            flag |= OscContactSourcesForContactUpdateRule.UpdatePartnerNetworkIdAndUserId(propertyBag, oscContactSources, networkProperties);
            return(flag | OscContactSourcesForContactUpdateRule.CopyCloudIdFromOscContactSourcesIfCurrentCloudIdIsBlank(propertyBag, networkProperties));
        }
        public OscNetworkProperties ReadOscContactSource(byte[] property)
        {
            if (property == null || property.Length == 0)
            {
                return(null);
            }
            OscNetworkProperties result;

            using (MemoryStream memoryStream = new MemoryStream(property))
            {
                using (BinaryReader binaryReader = new BinaryReader(memoryStream))
                {
                    try
                    {
                        short num = binaryReader.ReadInt16();
                        if (num < 2)
                        {
                            throw new OscContactSourcesForContactParseException(string.Format("Header version not supported: {0}", num));
                        }
                        binaryReader.ReadInt16();
                        short num2 = binaryReader.ReadInt16();
                        if (num2 < 1)
                        {
                            throw new OscContactSourcesForContactParseException("No entries found in the property.");
                        }
                        short num3 = binaryReader.ReadInt16();
                        if (num3 < 2)
                        {
                            throw new OscContactSourcesForContactParseException(string.Format("Entry version not supported: {0}", num3));
                        }
                        binaryReader.ReadInt16();
                        byte[] array = binaryReader.ReadBytes(16);
                        if (array == null || array.Length != 16)
                        {
                            throw new OscContactSourcesForContactParseException("Corrupted provider GUID.");
                        }
                        Guid   providerGuid = new Guid(array);
                        string networkId;
                        if (!OscProviderRegistry.TryGetNameFromGuid(providerGuid, out networkId))
                        {
                            networkId = providerGuid.ToString("N");
                        }
                        short count = binaryReader.ReadInt16();
                        binaryReader.ReadBytes((int)count);
                        short  count2  = binaryReader.ReadInt16();
                        string @string = Encoding.Unicode.GetString(binaryReader.ReadBytes((int)count2));
                        result = new OscNetworkProperties
                        {
                            NetworkId     = networkId,
                            NetworkUserId = @string
                        };
                    }
                    catch (ArgumentOutOfRangeException ex)
                    {
                        throw new OscContactSourcesForContactParseException(ex.Message);
                    }
                    catch (EndOfStreamException ex2)
                    {
                        throw new OscContactSourcesForContactParseException(ex2.Message);
                    }
                }
            }
            return(result);
        }