Beispiel #1
0
        public IHttpActionResult Post(SettingEntityModel entityModel)
        {
            var setting = entityModel.Id == 0 ? new Setting() : _settingService.Get(entityModel.Id);

            if (setting == null)
            {
                return(NotFound());
            }


            setting.GroupName = entityModel.GroupName;
            setting.Key       = entityModel.Key;
            setting.Value     = entityModel.Value;
            if (entityModel.Id == 0)
            {
                _settingService.Insert(setting);
            }
            else
            {
                _settingService.Update(setting);
            }

            VerboseReporter.ReportSuccess("Settings saved successfully", "post_setting");
            return(RespondSuccess());
        }
Beispiel #2
0
        private static string GetStepParametersForCommandLine(Step step)
        {
            string[] commonSettings = new CommonStep().SettingNames.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            string[] stepSettings   = step.SettingNames.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

            Dictionary <string, string> settings = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            foreach (string settingName in commonSettings)
            {
                settings.Upsert(settingName, SettingEntityModel.GetSingle(settingName, true).SettingValue);
            }

            foreach (string settingName in stepSettings)
            {
                settings.Upsert(settingName, SettingEntityModel.GetSingle(settingName, true).SettingValue);
            }

            StringBuilder sb = new StringBuilder();

            foreach (KeyValuePair <string, string> setting in settings)
            {
                string parameter = " /" + setting.Key + ":\"{0}\"";
                // Important! /folder:"C:\folder\" will cause problem by escape the last double quote ".
                string settingValue = setting.Value.TrimEnd('\\');
                parameter = parameter.FormatWith(settingValue);

                sb.Append(parameter);
            }

            return(sb.ToString());
        }
Beispiel #3
0
 public void LoadSettingsFrom(List <SettingEntityModel> settings)
 {
     if (this.step != null)
     {
         this.settings = new List <SettingEntityModel>();
         string[] settingNames = this.step.SettingNames.Split(new char[] { '|' }, System.StringSplitOptions.RemoveEmptyEntries);
         foreach (string settingName in settingNames)
         {
             var models = from setting in settings
                          where setting.SettingName.Equals(settingName)
                          select setting;
             SettingEntityModel model = models.FirstOrDefault();
             if (model != null)
             {
                 this.settings.Add(model);
                 model.Update();
             }
         }
     }
     else
     {
         this.settings = settings;
         foreach (SettingEntityModel model in this.settings)
         {
             model.Update();
         }
     }
 }
Beispiel #4
0
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveDialog = new SaveFileDialog();

            saveDialog.Filter          = "Rule Perf Testing Settings File (*.rpsettings)|*.rpsettings";
            saveDialog.CheckPathExists = true;
            saveDialog.Title           = "Choose a file to save the current settings";
            if (saveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // Very important!!! Save the current values in grid
                this.dgvSettings.EndEdit();

                List <KeyValuePair <string, string> > encryptedSettings = new List <KeyValuePair <string, string> >();
                try
                {
                    // Encrypt sensative messages
                    foreach (SettingEntityModel setting in this.settings)
                    {
                        if (setting.SettingName.Equals("DomainPassword", StringComparison.OrdinalIgnoreCase))
                        {
                            encryptedSettings.Add(new KeyValuePair <string, string>(setting.SettingName, setting.SettingValue));
                            setting.SettingValue = new string('*', setting.SettingValue.Length);
                        }

                        if (setting.SettingName.Equals("RemoteCommand", StringComparison.OrdinalIgnoreCase))
                        {
                            encryptedSettings.Add(new KeyValuePair <string, string>(setting.SettingName, setting.SettingValue));
                            //setting.SettingValue = Regex.Replace(setting.SettingValue, "(?<=/DomainPassword:\"?)[^ ]*(?=\"? *)", "******", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                            setting.SettingValue = Log.EncryptDomainPassword(setting.SettingValue);
                        }
                    }

                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(List <SettingEntityModel>));
                    using (XmlWriter xmlWriter = XmlWriter.Create(saveDialog.FileName))
                    {
                        xmlSerializer.Serialize(xmlWriter, this.settings);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Unable to save the current settings\r\n" + ex.Message, "Rule Perf Testing Tool Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ExceptionHelper.CentralProcess(new Exception("Unable to save the rule perf testing settings", ex));
                }
                finally
                {
                    // Recover the encrypted messages
                    foreach (KeyValuePair <string, string> pair in encryptedSettings)
                    {
                        SettingEntityModel model = this.settings.Find(m => { return(m.SettingName.Equals(pair.Key)); });
                        if (model != null)
                        {
                            model.SettingValue = pair.Value;
                        }
                    }
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Handles the CellEndEdit event of the dgvSettings control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="DataGridViewCellEventArgs" /> instance containing the event data.</param>
        private void dgvSettings_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            ExcelDataGridView  edgv  = sender as ExcelDataGridView;
            SettingEntityModel model = edgv.Rows[e.RowIndex].DataBoundItem as SettingEntityModel;

            if (model != null)
            {
                model.Update();
            }
        }
Beispiel #6
0
 /// <summary>
 /// Loads the settings for the specified step.
 /// </summary>
 public void LoadSettings()
 {
     if (this.step != null)
     {
         string[] settingNames = this.step.SettingNames.Split(new char[] { '|' }, System.StringSplitOptions.RemoveEmptyEntries);
         settings = SettingEntityModel.Get(settingNames);
     }
     else
     {
         settings = SettingEntityModel.GetAllSettings();
     }
 }
        /// <summary>
        /// Assigns the values to argument object.
        /// </summary>
        /// <param name="argumentObject">The argument object.</param>
        /// <param name="optionName">Name of the option.</param>
        /// <param name="argPairs">The argument pairs.</param>
        /// <param name="attrs">The attributes.</param>
        /// <exception cref="System.ArgumentException">parameter: + pair.Key +  is not defined in the argument object</exception>
        private static void AssignValuesToArgumentObject(object argumentObject, string optionName, List <KeyValuePair <string, string> > argPairs, Dictionary <string, ArgumentParameterAttribute> attrs)
        {
            foreach (var pair in argPairs)
            {
                if (!attrs.ContainsKey(pair.Key))
                {
                    throw new ArgumentException("parameter:" + pair.Key + " is not defined in the argumentobject");
                }

                try
                {
                    SettingEntityModel setting = SettingEntityModel.GetSingle(pair.Key);
                    setting.SettingValue = pair.Value;
                    setting.Update();
                    ////SetValue(argumentObject, attrs[pair.Key], pair.Value);
                }
                catch (ArgumentException ae)
                {
                    throw new ArgumentException(string.Format(
                                                    "Failed to set value to ArgumentObject.{0} from parameter:{1} value:{2}. Detail Message:{3}",
                                                    attrs[pair.Key].PropertyInfo.Name,
                                                    pair.Key,
                                                    pair.Value.SafeToString(),
                                                    ae.Message));
                }
            }

            // SaveAll may fail in the multi-thread case.
            //SettingEntityModel.SaveAll();

            // set default value

            /*
             * foreach (var attr in attrs.Values)
             * {
             *  if (attr.SetCount < attr.MinOccur)
             *  {
             *      throw new ArgumentException(string.Format("parameter:{0} has been set for {1} times, but min={2}",
             *          attr.ParameterName,
             *          attr.SetCount,
             *          attr.MinOccur));
             *  }
             *  if (attr.SetCount == 0 && attr.DefaultValue != null)
             *  {
             *      attr.PropertyInfo.SetValue(argumentObject, attr.DefaultValue, null);
             *  }
             * }*/
        }
        /// <summary>
        /// Parses the argument internal.
        /// </summary>
        /// <param name="args">The args.</param>
        /// <param name="argumentObject">The argument object.</param>
        /// <returns>Argument parsing result</returns>
        /// <exception cref="System.ArgumentException">IArgumentObject.Options cannot be null or empty</exception>
        internal static ArgumentResult ParseArgumentInternal(string[] args, RulePerfConsoleArgument argumentObject)
        {
            // Validate IArgumentObject.Options
            var options = argumentObject.Options;

            if (options == null || options.Count == 0)
            {
                throw new ArgumentException("IArgumentObject.Options cannot be null or empty");
            }

            if (options.Select(a => a.Key.ToLower()).Distinct().Count() != options.Count)
            {
                throw new ArgumentException("IArgumentObject.Options has duplicated option name");
            }

            // Retrieve the attributes from object's properties
            var optionParameterAttribDic = BuildArguParamAttrDic(options.Keys);

            // Parse system commandline, after this line, no exception is thrown to caller.
            ArgumentResult ar = new ArgumentResult();

            ar.ParamAttributes    = optionParameterAttribDic;
            ar.OptionDescriptions = options;
            List <KeyValuePair <string, string> > argPairs = null;

            try
            {
                string optionName = ParseArgumentArray(args, out argPairs);
                if (!optionParameterAttribDic.ContainsKey(optionName))
                {
                    throw new ArgumentException("optionName: '" + optionName + "' is not defined in argument object");
                }

                ar.SelectedOptionName = optionName;

                ////#region Settings for an option
                argumentObject.BuildStep(ar.SelectedOptionName);

                CommonStep commonStep = new CommonStep();

                foreach (Step step in argumentObject.Steps)
                {
                    // Specific step's settings
                    string[] settingNames = step.SettingNames.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                    List <SettingEntityModel> settings = SettingEntityModel.Get(settingNames);
                    foreach (SettingEntityModel setting in settings)
                    {
                        ArgumentParameterAttribute attr = new ArgumentParameterAttribute(argumentObject.GetOptionName(step));
                        attr.DefaultValue  = setting.SettingValue;
                        attr.Delimiter     = '|';
                        attr.Description   = setting.SettingName;
                        attr.MaxOccur      = 1;
                        attr.MinOccur      = 0;
                        attr.ParameterName = setting.SettingName;

                        foreach (string option in attr.OptionsBindTo)
                        {
                            if (optionParameterAttribDic[option].ContainsKey(attr.ParameterName))
                            {
                                throw new ArgumentException(string.Format("option:{0} has multiple setting of parameter:{1}", option, attr.ParameterName));
                            }

                            optionParameterAttribDic[option].Add(attr.ParameterName, attr);
                        }
                    }

                    // Common settings
                    settingNames = commonStep.SettingNames.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                    settings     = SettingEntityModel.Get(settingNames);
                    foreach (SettingEntityModel setting in settings)
                    {
                        ArgumentParameterAttribute attr = new ArgumentParameterAttribute(argumentObject.GetOptionName(step));
                        attr.DefaultValue  = setting.SettingValue;
                        attr.Delimiter     = '|';
                        attr.Description   = setting.SettingName;
                        attr.MaxOccur      = 1;
                        attr.MinOccur      = 0;
                        attr.ParameterName = setting.SettingName;

                        foreach (string option in attr.OptionsBindTo)
                        {
                            if (optionParameterAttribDic[option].ContainsKey(attr.ParameterName))
                            {
                                // If the setting has been set by the specifical step, then ignore the global setting process
                                // throw new ArgumentException(string.Format("option:{0} has multiple setting of parameter:{1}",
                                //    option, attr.ParameterName));
                            }
                            else
                            {
                                optionParameterAttribDic[option].Add(attr.ParameterName, attr);
                            }
                        }
                    }
                }

                /*
                 * foreach (var property in argumentObject.GetType().GetProperties())
                 * {
                 *  var paramAttrs = property.GetCustomAttributes<ArgumentParameterAttribute>();
                 *  if (paramAttrs.Length > 0) // Only validate property with ParameterOptionAttribute
                 *  {
                 *      if (!property.PropertyType.IsSupported())
                 *      {
                 *          throw new ArgumentException(string.Format("Property:{0}, the Type:{1} is not supported",
                 *              property.Name,
                 *              property.PropertyType.Name));
                 *      }
                 *
                 *      foreach (var attr in paramAttrs)
                 *      {
                 *          ValidateParameterOptionAttr(property, attr, options);
                 *
                 *          foreach (string option in attr.OptionsBindTo)
                 *          {
                 *              if (optionParameterAttribDic[option].ContainsKey(attr.ParameterName))
                 *              {
                 *                  throw new ArgumentException(string.Format("option:{0} has multiple setting of parameter:{1}",
                 *                      option, attr.ParameterName));
                 *              }
                 *              optionParameterAttribDic[option].Add(attr.ParameterName, attr);
                 *          }
                 *      }
                 *  }
                 * }*/
                ////#endregion Settings for an option

                AssignValuesToArgumentObject(argumentObject, ar.SelectedOptionName, argPairs, optionParameterAttribDic[ar.SelectedOptionName]);
            }
            catch (ArgumentException ae)
            {
                ExceptionHelper.CentralProcess(ae);
                ar.ErrorMessages.Add(ae.Message);
                ar.ParseSucceeded = false;
                return(ar);
            }

            ar.ParseSucceeded = true;
            return(ar);
        }