private bool Validate(GigyaDsSettings settings, ref GigyaDsSettingsResponseModel responseModel)
        {
            var coreSettingsHelper = new GigyaSettingsHelper();
            var coreSettings       = coreSettingsHelper.Get(settings.SiteId, true);

            var dsHelper = new GigyaDsHelper(coreSettings, _logger, settings);

            var errorMessage = dsHelper.Validate();

            if (!string.IsNullOrEmpty(errorMessage))
            {
                responseModel.Error = errorMessage;
                _logger.Error(responseModel.Error);
                return(false);
            }

            return(true);
        }
        /// <summary>
        /// Saves Gigya settings to the db.
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public GigyaDsSettingsResponseModel Save(GigyaDsSettingsViewModel model)
        {
            var response = new GigyaDsSettingsResponseModel();

            if (!ModelState.IsValid)
            {
                var errorList = ModelState.Values.SelectMany(m => m.Errors)
                                .Select(e => e.ErrorMessage)
                                .ToList();

                response.Error = string.Join(" ", errorList);
                _logger.Error(response.Error);
                return(response);
            }

            var settingsHelper = new GigyaUmbracoDsSettingsHelper(_logger);
            var settings       = settingsHelper.GetRaw(model.Id);

            if (settings == null)
            {
                response.Error = "Settings not found for id: " + model.Id;
                _logger.Error(response.Error);
                return(response);
            }

            if (model.Inherited && model.Id > 0)
            {
                // don't delete global settings
                if (settings.Id > -1)
                {
                    settingsHelper.Delete(settings);
                }

                settingsHelper.ClearCache(model.Id);
                response.Success = true;

                // return global settings to refresh client
                var globalData  = settingsHelper.Get(model.Id);
                var globalModel = GetModel(model.Id, globalData);
                response.Settings = globalModel;
                return(response);
            }

            if (model.Mappings == null)
            {
                response.Error = "Invalid mapping param supplied. Please check your mappings.";
                _logger.Error(response.Error);
                return(response);
            }

            if (model.Mappings.Any(i => string.IsNullOrEmpty(i.CmsName)))
            {
                response.Error = "Umbraco field is required.";
                _logger.Error(response.Error);
                return(response);
            }

            if (model.Mappings.Any(i => string.IsNullOrEmpty(i.GigyaName)))
            {
                response.Error = "Gigya DS field is required.";
                _logger.Error(response.Error);
                return(response);
            }

            if (model.Mappings.Any(i => string.IsNullOrEmpty(i.Oid)))
            {
                response.Error = "Gigya DS OID field is required.";
                _logger.Error(response.Error);
                return(response);
            }

            if (model.Mappings.Any(i => !i.GigyaName.StartsWith("ds.") || i.GigyaName.Split('.').Length < 3))
            {
                response.Error = "Gigya DS fields must be in the format ds.type.fieldName";
                _logger.Error(response.Error);
                return(response);
            }

            // map back to db models
            settings.Method   = (int)model.Method;
            settings.Mappings = model.Mappings.Select(i => new Data.GigyaUmbracoDsMapping
            {
                CmsName     = i.CmsName,
                DsSettingId = settings.Id,
                GigyaName   = i.GigyaName,
                Oid         = i.Oid
            }).ToList();

            var mappedSettings = settingsHelper.Map(settings.Mappings, settings);

            if (_validateDsSettings && !Validate(mappedSettings, ref response))
            {
                return(response);
            }

            settingsHelper.Save(settings);

            response.Success = true;
            settingsHelper.ClearCache(model.Id);
            return(response);
        }