예제 #1
0
        private void SaveAllSettings()
        {
            //設定の保存をするぞい
            //バイナリも含んでいるので、バイナリシリアル化
            string fileName;

            using (var diag = new fm.SaveFileDialog())
            {
                diag.Filter = "全てのファイル|*";
                var result = diag.ShowDialog();
                if (result == fm.DialogResult.OK)
                {
                    fileName = diag.FileName;
                }
                else
                {
                    return;
                }
            }

            var obj = new SettingsForSave(PictureDatas)
            {
                SeparatingValue     = this.SeparatingValue,
                IsShowingBinaryPict = this.IsShowingBinaryPict,
                UsingOtsuMethod     = this.UsingOtsuMethod,
                Memo            = this.Memo,
                StepSizes       = this.StepSize,
                UsingMedianBlur = this.UsingMedianBlur
            };

            obj.Save(fileName);
        }
예제 #2
0
        public async Task <ActionResult <SaveSettingsResponse> > Save([FromBody] SettingsForSave settingsForSave, [FromQuery] SaveArguments args)
        {
            // Authorized access (Criteria are not supported here)
            var updatePermissions = await _repo.UserPermissions(Constants.Update, "settings");

            if (!updatePermissions.Any())
            {
                return(StatusCode(403));
            }

            try
            {
                // Trim all string fields just in case
                settingsForSave.TrimStringProperties();

                // Validate
                ValidateAndPreprocessSettings(settingsForSave);

                if (!ModelState.IsValid)
                {
                    return(UnprocessableEntity(ModelState));
                }

                // Persist
                await _repo.Settings__Save(settingsForSave);

                // Update the settings cache
                var tenantId          = _tenantIdAccessor.GetTenantId();
                var settingsForClient = await LoadSettingsForClient(_repo);

                _settingsCache.SetSettings(tenantId, settingsForClient);

                // If requested, return the updated entity
                if (args.ReturnEntities ?? false)
                {
                    // If requested, return the same response you would get from a GET
                    var res = await GetImpl(new GetByIdArguments { Expand = args.Expand });

                    var result = new SaveSettingsResponse
                    {
                        Entities          = res.Entities,
                        Result            = res.Result,
                        SettingsForClient = settingsForClient
                    };

                    return(result);
                }
                else
                {
                    return(Ok());
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error: {ex.Message} {ex.StackTrace}");
                return(BadRequest(ex.Message));
            }
        }
예제 #3
0
        private void ValidateAndPreprocessSettings(SettingsForSave entity)
        {
            {
                var culture = _culturesRepo.GetCulture(entity.PrimaryLanguageId);
                if (culture == null)
                {
                    ModelState.AddModelError(nameof(entity.PrimaryLanguageId),
                                             _localizer["Error_InvalidLanguageId0", entity.PrimaryLanguageId]);
                }
            }

            if (!string.IsNullOrWhiteSpace(entity.SecondaryLanguageId))
            {
                var culture = _culturesRepo.GetCulture(entity.SecondaryLanguageId);
                if (culture == null)
                {
                    ModelState.AddModelError(nameof(entity.PrimaryLanguageId),
                                             _localizer["Error_InvalidLanguageId0", entity.SecondaryLanguageId]);
                }
            }

            if (string.IsNullOrWhiteSpace(entity.SecondaryLanguageId))
            {
                entity.SecondaryLanguageSymbol = null;
            }
            else
            {
                if (string.IsNullOrWhiteSpace(entity.PrimaryLanguageSymbol))
                {
                    ModelState.AddModelError(nameof(entity.PrimaryLanguageSymbol),
                                             _localizer[nameof(RequiredAttribute), _localizer["Settings_PrimaryLanguageSymbol"]]);
                }

                if (string.IsNullOrWhiteSpace(entity.SecondaryLanguageSymbol))
                {
                    ModelState.AddModelError(nameof(entity.SecondaryLanguageSymbol),
                                             _localizer[nameof(RequiredAttribute), _localizer["Settings_SecondaryLanguageSymbol"]]);
                }

                if (entity.SecondaryLanguageId == entity.PrimaryLanguageId)
                {
                    ModelState.AddModelError(nameof(entity.SecondaryLanguageId),
                                             _localizer["Error_SecondaryLanguageCannotBeTheSameAsPrimaryLanguage"]);
                }
            }

            // Make sure the color is a valid HTML color
            // Credit: https://bit.ly/2ToV6x4
            if (!string.IsNullOrWhiteSpace(entity.BrandColor) && !Regex.IsMatch(entity.BrandColor, "^#(?:[0-9a-fA-F]{3}){1,2}$"))
            {
                ModelState.AddModelError(nameof(entity.BrandColor),
                                         _localizer["Error_TheField0MustBeAValidColorFormat", _localizer["Settings_BrandColor"]]);
            }
        }
예제 #4
0
        private void LoadAllSettings()
        {
            //設定の読み込みをするぞい
            string fileName;

            using (var diag = new fm.OpenFileDialog())
            {
                diag.Filter = "すべてのファイル|*";
                var result = diag.ShowDialog();
                if (result == fm.DialogResult.OK)
                {
                    fileName = diag.FileName;
                }
                else
                {
                    return;
                }
            }


            this.PictureDatas.Clear();
            var obj = SettingsForSave.Load(fileName);

            this.PictureDatas        = obj.GetPictureDatas();
            this.SeparatingValue     = obj.SeparatingValue;
            this.IsShowingBinaryPict = obj.IsShowingBinaryPict;
            this.UsingOtsuMethod     = obj.UsingOtsuMethod;
            this.Memo            = obj.Memo;
            this.StepSize        = obj.StepSizes;
            this.UsingMedianBlur = obj.UsingMedianBlur;

            this.RaisePropertyChanged("SeparatingValue");
            this.RaisePropertyChanged("IsShowingBinaryPict");
            this.RaisePropertyChanged("UsingOtsuMethod");
            this.RaisePropertyChanged("PictureDatas");
            this.RaisePropertyChanged("Memo");
            this.RaisePropertyChangeForImages();
        }
예제 #5
0
        public async Task <ActionResult <SaveSettingsResponse> > Save([FromBody] SettingsForSave settingsForSave, [FromQuery] SaveArguments args)
        {
            // Authorized access (Criteria are not supported here)
            var updatePermissions = await ControllerUtilities.GetPermissions(_db.AbstractPermissions, PermissionLevel.Update, "settings");

            if (!updatePermissions.Any())
            {
                return(StatusCode(403));
            }

            try
            {
                // Trim all string fields just in case
                settingsForSave.TrimStringProperties();

                // Validate
                ValidateAndPreprocessSettings(settingsForSave);

                if (!ModelState.IsValid)
                {
                    return(UnprocessableEntity(ModelState));
                }

                // Persist
                M.Settings mSettings = await _db.Settings.FirstOrDefaultAsync();

                if (mSettings == null)
                {
                    // This should never happen
                    return(BadRequest("Settings have not been initialized"));
                }

                _mapper.Map(settingsForSave, mSettings);

                mSettings.ModifiedAt      = DateTimeOffset.Now;
                mSettings.ModifiedById    = _tenantInfo.GetCurrentInfo().UserId.Value;
                mSettings.SettingsVersion = Guid.NewGuid(); // prompts clients to refresh

                await _db.SaveChangesAsync();

                // If requested, return the updated entity
                if (args.ReturnEntities ?? false)
                {
                    // If requested, return the same response you would get from a GET
                    var res = await GetImpl(new GetByIdArguments { Expand = args.Expand });

                    var result = new SaveSettingsResponse
                    {
                        CollectionName    = res.CollectionName,
                        Entity            = res.Entity,
                        RelatedEntities   = res.RelatedEntities,
                        SettingsForClient = await GetForClientImpl()
                    };

                    return(result);
                }
                else
                {
                    return(Ok());
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error: {ex.Message} {ex.StackTrace}");
                return(BadRequest(ex.Message));
            }
        }