/// <summary>
        /// Get values from module config and load them into data members
        /// </summary>
        /// <param name="forEdit"></param>
        public void Load(Guid id)
        {
            using (var context = GigyaDSContext.Get())
            {
                // get settings for site or global settings or new settings
                var siteSettingsAndGlobal = context.Settings.Where(i => i.SiteId == id || i.SiteId == Guid.Empty).ToList();
                var settings = siteSettingsAndGlobal.FirstOrDefault(i => i.SiteId == id) ?? siteSettingsAndGlobal.FirstOrDefault() ?? new GigyaSitefinityModuleDsSettings {
                    SiteId = id, Mappings = new List <GigyaSitefinityDsMapping>()
                };

                // let client know that the data has been retrieved - hack as there is no "loaded" sitefinity client side event
                this.LoadedField = "Loaded";

                var mappingFields = settings.Mappings.Select(Map).OrderBy(i => i.CmsFieldName).ToList();
                this.MappingFields = JsonConvert.SerializeObject(mappingFields, new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                });

                this.SiteId = settings.SiteId;
                this.Method = settings.Method;

                // get a list of available profile properties
                this.ProfileProperties = JsonConvert.SerializeObject(SitefinityUtils.GetProfileProperties().OrderBy(i => i.Value));
            }
        }
        /// <summary>
        /// Gets the settings from the db and maps.
        /// </summary>
        /// <returns></returns>
        private GigyaDsSettingsContainer Load(Guid id)
        {
            using (var context = GigyaDSContext.Get())
            {
                var settings = context.Settings.Where(i => i.SiteId == id || i.SiteId == Guid.Empty).Select(Map).ToList();

                var model = new GigyaDsSettingsContainer
                {
                    Sites = settings
                };

                return(model);
            }
        }
        /// <summary>
        /// Deletes site specific DS settings (used if the site will now inherit from the global settings).
        /// </summary>
        /// <param name="siteId"></param>
        public void Delete(Guid siteId)
        {
            if (siteId == Guid.Empty)
            {
                return;
            }

            using (var context = GigyaDSContext.Get())
            {
                var settings = context.Settings.FirstOrDefault(i => i.SiteId == siteId);
                if (settings != null)
                {
                    context.Delete(settings);
                    context.SaveChanges();
                }
            }
        }
        /// <summary>
        /// Save values from basic settings page
        /// </summary>
        public void Save(Guid id)
        {
            using (var context = GigyaDSContext.Get())
            {
                // get settings to update
                var settings = context.Settings.FirstOrDefault(i => i.SiteId == id) ?? new GigyaSitefinityModuleDsSettings {
                    SiteId = id
                };

                var mappingFields = JsonConvert.DeserializeObject <List <GigyaDsMappingViewModel> >(MappingFields);

                if (mappingFields == null)
                {
                    var error = "Invalid mapping param supplied. Please check your mappings.";
                    Logger.Error(error);
                    throw new ArgumentException(error);
                }

                if (mappingFields.Any(i => string.IsNullOrEmpty(i.CmsFieldName)))
                {
                    var error = "Sitefinity field is required.";
                    Logger.Error(error);
                    throw new ArgumentException(error);
                }

                if (mappingFields.Any(i => string.IsNullOrEmpty(i.GigyaFieldName)))
                {
                    var error = "Gigya DS field is required.";
                    Logger.Error(error);
                    throw new ArgumentException(error);
                }

                if (mappingFields.Any(i => string.IsNullOrEmpty(i.Oid)))
                {
                    var error = "Gigya DS OID field is required.";
                    Logger.Error(error);
                    throw new ArgumentException(error);
                }

                if (mappingFields.Any(i => !i.GigyaFieldName.StartsWith("ds.") || i.GigyaFieldName.Split('.').Length < 3))
                {
                    var error = "Gigya DS fields must be in the format ds.type.fieldName";
                    Logger.Error(error);
                    throw new ArgumentException(error);
                }

                // remove old mappings
                if (settings.Mappings != null)
                {
                    context.Delete(settings.Mappings);
                }

                // update all fields
                settings.Method   = this.Method;
                settings.Mappings = mappingFields.Select(i => new Data.GigyaSitefinityDsMapping
                {
                    CmsName     = i.CmsFieldName,
                    DsSettingId = settings.SiteId,
                    GigyaName   = i.GigyaFieldName,
                    Oid         = i.Oid
                }).ToList();

                var mappedSettings = SettingsHelper.Map(settings);
                if (_validateDsSettings && !Validate(mappedSettings))
                {
                    return;
                }

                context.Add(settings);
                context.SaveChanges();
                SettingsHelper.ClearCache(SiteId);
            }
        }