/// <summary>
        /// Converts a <see cref="LikertScale"/> to a <see cref="ControlValueOptionList"/>.
        /// </summary>
        /// <param name="scale">The scale to convert.</param>
        /// <returns>A <see cref="ControlValueOptionList"/>.</returns>
        /// <remarks>
        /// Each <see cref="ControlValueOption"/> in the returned <see cref="ControlValueOptionList"/>
        /// represents a single <see cref="LikertScaleItem"/> question from the
        /// <paramref name="scale"/>.
        /// </remarks>
        internal static ControlValueOptionList ToOptionList(this LikertScale scale)
        {
            ControlValueOptionList optionList = new ControlValueOptionList();
            optionList.AddRange(scale.Select(item => new ControlValueOption
                                            {
                                                Value = item.Value.ToString(),
                                                Description = item.Description
                                            }));

            return optionList;
        }
        /// <summary>
        /// Get the options for <paramref name="source"/>.
        /// </summary>
        /// <param name="source">Defines the method for retrieving options.</param>
        /// <returns>The options for <paramref name="source"/>.</returns>
        public ControlValueOptionList GetOptions(IOptionSource source)
        {
            ControlValueOptionList optionList = new ControlValueOptionList();
            SystemOptionSource systemSource = source as SystemOptionSource;

            UserSearchCriteria criteria = new UserSearchCriteria
                                          {
                                              IncludeServiceUsers = false,
                                              IncludeNoOrganisationUsers = false,
                                              OrganisationIds = new List<string> { this.organisationId }
                                          };

            if (!string.IsNullOrWhiteSpace(systemSource.FilterRoleId))
            {
                criteria.RoleIds = new List<string> { systemSource.FilterRoleId };
            }

            IEnumerable<OptionSourceUser> usersWithRole = this.findUsersFn(criteria);
            MappedField categoryMap = systemSource.ResponseFieldMap.FirstOrDefault(m => m.Target == CATEGORY);
            string categoryKey = categoryMap != null ? categoryMap.Source : string.Empty;

            string descriptionKey = string.Empty;
            var description = systemSource.ResponseFieldMap.FirstOrDefault(m => m.Target == DESCRIPTION);
            if (description != null)
            {
                descriptionKey = description.Source;
            }

            string valueKey = string.Empty;
            var value = systemSource.ResponseFieldMap.FirstOrDefault(m => m.Target == VALUE);
            if (value != null)
            {
                valueKey = value.Source;
            }

            optionList.AddRange(usersWithRole.Select(item => new ControlValueOption
            {
                Category = !string.IsNullOrWhiteSpace(categoryKey) && item.GetType().GetProperty(categoryKey).GetValue(item) != null ? item.GetType().GetProperty(categoryKey).GetValue(item).ToString() : string.Empty,
                Description = !string.IsNullOrWhiteSpace(descriptionKey) && item.GetType().GetProperty(descriptionKey).GetValue(item) != null ? item.GetType().GetProperty(descriptionKey).GetValue(item).ToString() : string.Empty,
                Value = !string.IsNullOrWhiteSpace(valueKey) && item.GetType().GetProperty(valueKey).GetValue(item) != null ? item.GetType().GetProperty(valueKey).GetValue(item).ToString() : string.Empty
            }));

            return optionList;
        }
        /// <summary>
        /// Get the options for <paramref name="source"/>.
        /// </summary>
        /// <param name="source">Defines the method for retrieving options.</param>
        /// <returns>The options for <paramref name="source"/>.</returns>
        public ControlValueOptionList GetOptions(IOptionSource source)
        {
            PrebuiltOptionSource prebuiltSource = source as PrebuiltOptionSource;
            GenericDataSet dataSet = this.dataSetList[prebuiltSource.DataSetId];
            ControlValueOptionList optionList = new ControlValueOptionList();

            MappedField categoryMap = prebuiltSource.ResponseFieldMap.FirstOrDefault(m => m.Target == CATEGORY);
            string categoryKey = categoryMap != null ? categoryMap.Source : string.Empty;
            string descriptionKey = prebuiltSource.ResponseFieldMap.FirstOrDefault(m => m.Target == DESCRIPTION).Source;
            string valueKey = prebuiltSource.ResponseFieldMap.FirstOrDefault(m => m.Target == VALUE).Source;

            optionList.AddRange(dataSet.Data.Select(item => new ControlValueOption
                                                    {
                                                        Category = item.ContainsKey(categoryKey) ? item[categoryKey] : string.Empty,
                                                        Description = item.ContainsKey(descriptionKey) ? item[descriptionKey] : string.Empty,
                                                        Value = item.ContainsKey(valueKey) ? item[valueKey] : string.Empty
                                                    }));

            return optionList;
        }
        /// <summary>
        /// Get the options for <paramref name="source"/>.
        /// </summary>
        /// <param name="source">Defines the method for retrieving options.</param>
        /// <returns>The options for <paramref name="source"/>.</returns>
        public ControlValueOptionList GetOptions(IOptionSource source)
        {
            RepeaterOptionSource repeaterSource = source as RepeaterOptionSource;
            ControlValueOptionList list = new ControlValueOptionList();
            object[] objectArr = this.applicationData.GetValue<object[]>(repeaterSource.RepeaterName);
            if (objectArr == null || objectArr.Length == 0)
            {
                return list;
            }

            Dictionary<string, object>[] repeaterItems = objectArr as Dictionary<string, object>[];
            list.AddRange(repeaterItems.Select(item => new ControlValueOption
                                              {
                                                  Value = string.Join(" ", repeaterSource.ValueFields.Select(key => item[key].ToString())),
                                                  Description = string.Join(" ", repeaterSource.DescriptionFields.Select(key => item[key].ToString())),
                                                  Category = string.IsNullOrEmpty(repeaterSource.CategoryField) ? string.Empty : item[repeaterSource.CategoryField].ToString()
                                              }));

            return list;
        }
 /// <summary>
 /// Creates a <see cref="Microsoft.Practices.EnterpriseLibrary.Validation.Validator"/> that will validate
 /// a field with a domain list of allowable values.
 /// </summary>
 /// <param name="optionList">The list of valid options.</param>
 /// <returns>A <see cref="Microsoft.Practices.EnterpriseLibrary.Validation.Validator"/> that will validate
 /// a field with a domain list of allowable values.</returns>
 private Validator CreateValidatorForOptions(ControlValueOptionList optionList)
 {
     string[] optionValues = optionList.Select(x => x.Value).ToArray();
     return new NullableDomainValidator<object>(ValidationMessage.InvalidOption, optionValues);
 }