public UserConditionVisualItem(PredefinedCondition predefinedCondition)
        {
            PredefinedCondition = predefinedCondition;
            ShowOnlyForDbTypes  = new List <DbType>();

            Caption    = predefinedCondition.Caption;
            Condition  = predefinedCondition.Expression;
            IsNeedEdit = predefinedCondition.IsNeedEdit;

            ShowOnlyForDbTypes.AddRange(predefinedCondition.ShowOnlyFor);
        }
        private bool SaveForm()
        {
            try
            {
                var result =
                    _queryView.Query.SQLContext.ParseLogicalExpression(TextBoxCondition.Text, false, false,
                                                                       out var token);
                if (result == null && token != null)
                {
                    throw new SQLParsingException(
                              string.Format(
                                  Helpers.Localizer.GetString(nameof(LocalizableConstantsUI.strInvalidCondition),
                                                              LocalizableConstantsUI.strInvalidCondition), TextBoxCondition.Text),
                              token);
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, @"Invalid SQL", MessageBoxButtons.OK, MessageBoxIcon.Error);

                TextBoxCondition.Focus();
                return(false);
            }

            var listTypes = CheckComboBoxDbTypes.CheckedItems.OfType <DbType>().ToList();

            var userExpression = new PredefinedCondition(
                TextBoxCaption.Text,
                listTypes,
                TextBoxCondition.Text, CheckBoxIsNeedEdit.Checked
                );

            var index = -1;

            if (_selectedPredefinedCondition != null)
            {
                index = _queryView.UserPredefinedConditions.IndexOf(_selectedPredefinedCondition.PredefinedCondition);
                _queryView.UserPredefinedConditions.Remove(_selectedPredefinedCondition.PredefinedCondition);
            }

            if (_queryView.UserPredefinedConditions.Any(x =>
                                                        string.Compare(x.Caption, TextBoxCaption.Text, StringComparison.InvariantCultureIgnoreCase) == 0))
            {
                MessageBox.Show($"Condition with caption \"{TextBoxCaption.Text}\" already exist", "Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);

                TextBoxCaption.Focus();

                return(false);
            }

            try
            {
                if (index != -1)
                {
                    _queryView.UserPredefinedConditions.Insert(index, userExpression);
                }
                else
                {
                    _queryView.UserPredefinedConditions.Add(userExpression);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }

            LoadUserConditions(null);

            ResetForm();

            if (index >= 0)
            {
                ListBoxConditions.SelectedIndex = index;
            }
            return(true);
        }
Ejemplo n.º 3
0
        private bool SaveUserExpression()
        {
            if (_queryView == null)
            {
                return(false);
            }

            try
            {
                var result = _queryView.Query.SQLContext.ParseLogicalExpression(TextBoxExpression.Text, false, false, out var token);
                if (result == null && token != null)
                {
                    throw new SQLParsingException(
                              string.Format(
                                  Helpers.Localizer.GetString(nameof(LocalizableConstantsUI.strInvalidCondition),
                                                              ActiveQueryBuilder.View.WPF.Helpers.ConvertLanguageFromNative(Language),
                                                              LocalizableConstantsUI.strInvalidCondition), TextBoxExpression.Text),
                              token);
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Invalid SQL", MessageBoxButton.OK, MessageBoxImage.Error,
                                MessageBoxResult.OK, MessageBoxOptions.None);

                TextBoxExpression.Focus();
                Keyboard.Focus(TextBoxExpression);
                return(false);
            }

            var listTypes = ComboboxDbTypes.Items.Where(x => x.IsChecked).Select(selectableItem =>
                                                                                 (DbType)Enum.Parse(typeof(DbType), selectableItem.Content.ToString(), true)).ToList();

            var userExpression = new PredefinedCondition(
                TextBoxCaption.Text,
                listTypes,
                TextBoxExpression.Text, CheckBoxIsNeedEdit.IsChecked == true
                );

            var index = -1;

            if (_selectedPredefinedCondition != null)
            {
                index = _queryView.UserPredefinedConditions.IndexOf(_selectedPredefinedCondition.PredefinedCondition);
                _queryView.UserPredefinedConditions.Remove(_selectedPredefinedCondition.PredefinedCondition);
            }

            if (_queryView.UserPredefinedConditions.Any(x =>
                                                        string.Compare(x.Caption, TextBoxCaption.Text, StringComparison.InvariantCultureIgnoreCase) == 0))
            {
                MessageBox.Show($"Condition with caption \"{TextBoxCaption.Text}\" already exist", "Error",
                                MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);

                Keyboard.Focus(TextBoxCaption);

                return(false);
            }

            try
            {
                if (index != -1)
                {
                    _queryView.UserPredefinedConditions.Insert(index, userExpression);
                }
                else
                {
                    _queryView.UserPredefinedConditions.Add(userExpression);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return(false);
            }

            Load(null);

            ResetForm();

            if (index >= 0)
            {
                ListBoxUserConditions.SelectedIndex = index;
            }

            return(true);
        }