public void JobParametersTest() { JobParameters jp = new JobParameters(); Assert.IsNotNull(jp); Assert.IsNotNull(jp.GetParameters()); Assert.AreEqual(0,jp.GetParameters().Count); }
/// <summary> /// Copy constructor. Initializes the builder with the supplied parameters. /// THIS SHOULD PRESERVE PRIOR EXISTING ORDER. /// </summary> /// <param name="jobParameters"></param> public JobParametersBuilder(JobParameters jobParameters) { _parameterMap = new OrderedDictionary<string, JobParameter>(jobParameters.GetParameters().Count); IDictionary<string, JobParameter> givenJobParameters = jobParameters.GetParameters(); KeyValuePair<string, JobParameter>[] jbArray = new KeyValuePair<string, JobParameter>[givenJobParameters.Count]; jobParameters.GetParameters().CopyTo(jbArray, 0); foreach (KeyValuePair<string, JobParameter> mapEntry in jbArray) { _parameterMap.Add(mapEntry); } }
/// <summary> /// Use the same suffixes to create properties (omitting the string suffix /// because it is the default). Non-identifying parameters will be prefixed /// with the <see cref="NonIdentifyingFlag"/>. However, since parameters are /// identifying by default, they will <em>not</em> be prefixed with the /// <see cref="IdentifyingFlag"/>. /// </summary> /// <param name="parms"></param> /// <returns></returns> public NameValueCollection GetProperties(JobParameters parms) { if (parms == null || parms.IsEmpty()) { return(new NameValueCollection()); } IDictionary <string, JobParameter> parameters = parms.GetParameters(); NameValueCollection result = new NameValueCollection(); foreach (KeyValuePair <string, JobParameter> entry in parameters) { string key = entry.Key; JobParameter jobParameter = entry.Value; Object value = jobParameter.Value; if (value != null) { key = (!jobParameter.Identifying ? NonIdentifyingFlag : "") + key; if (jobParameter.Type == JobParameter.ParameterType.Date) { result.Set(key + DateType, string.Format(_dateFormat, value)); } else if (jobParameter.Type == JobParameter.ParameterType.Long) { result.Set(key + LongType, string.Format(_numberFormat, value)); } else if (jobParameter.Type == JobParameter.ParameterType.Double) { result.Set(key + DoubleType, string.Format(_decimalFormat, (double)value)); } else { result.Set(key, "" + value); } } } return(result); }
/// <summary> /// Check the parameters meet the specification provided. If optional keys /// are explicitly specified then all keys must be in that list, or in the /// required list. Otherwise all keys that are specified as required must be /// present. /// </summary> /// <param name="parameters"></param> /// <exception cref="JobParametersInvalidException"> </exception> public void Validate(JobParameters parameters) { if (parameters == null) { throw new JobParametersInvalidException("The JobParameters can not be null"); } HashSet<string> keys = new HashSet<string>(parameters.GetParameters().Keys); // If there are explicit optional keys then all keys must be in that // group, or in the required group. if (OptionalKeys.Any()) { ICollection<string> missingKeys = new HashSet<string>(); foreach (string key in keys) { if (!OptionalKeys.Contains(key) && !RequiredKeys.Contains(key)) { missingKeys.Add(key); } } if (missingKeys.Any()) { throw new JobParametersInvalidException( string.Format("The JobParameters contains keys that are not explicitly optional or required: {0} " , missingKeys)); } } ICollection<string> missingKeys2 = new HashSet<string>(); foreach (string key in RequiredKeys) { if (!keys.Contains(key)) { missingKeys2.Add(key); } } if (missingKeys2.Any()) { throw new JobParametersInvalidException( string.Format("The JobParameters do not contain required keys: {0}", missingKeys2)); } }
/// <summary> /// Use the same suffixes to create properties (omitting the string suffix /// because it is the default). Non-identifying parameters will be prefixed /// with the <see cref="NonIdentifyingFlag"/>. However, since parameters are /// identifying by default, they will <em>not</em> be prefixed with the /// <see cref="IdentifyingFlag"/>. /// </summary> /// <param name="parms"></param> /// <returns></returns> public NameValueCollection GetProperties(JobParameters parms) { if (parms == null || parms.IsEmpty()) { return new NameValueCollection(); } IDictionary<string, JobParameter> parameters = parms.GetParameters(); NameValueCollection result = new NameValueCollection(); foreach (KeyValuePair<string, JobParameter> entry in parameters) { string key = entry.Key; JobParameter jobParameter = entry.Value; Object value = jobParameter.Value; if (value != null) { key = (!jobParameter.Identifying ? NonIdentifyingFlag : "") + key; if (jobParameter.Type == JobParameter.ParameterType.Date) { result.Set(key + DateType, string.Format(_dateFormat, value)); } else if (jobParameter.Type == JobParameter.ParameterType.Long) { result.Set(key + LongType, string.Format(_numberFormat, value)); } else if (jobParameter.Type == JobParameter.ParameterType.Double) { result.Set(key + DoubleType, string.Format(_decimalFormat, (double)value)); } else { result.Set(key, "" + value); } } } return result; }