/// <summary>
 /// Load the control options.
 /// </summary>
 /// <param name="control">The control that acts as the this instance.</param>
 /// <param name="register">A register of <see cref="IOptionSourceProvider"/>s.</param>
 public static void LoadOptions(this IControlWithOptions control, OptionSourceProviderRegister register)
 {
     control.OptionValues = register.Get(control.OptionSource).GetOptions(control.OptionSource);
 }
        /// <summary>
        /// Creates a PDF document of <paramref name="application"/> and sends it to <paramref name="target"/>.
        /// </summary>
        /// <param name="application">The application to create a PDF for.</param>
        /// <param name="controlList">The form control list.</param>
        /// <param name="target">The target stream.</param>
        public void WritePdfToStream(Application application, ControlList controlList, Stream target)
        {
            Document document = this.CreateDocument();

            Table metaTable = document.AppendTable(2, this.BorderColour);
            metaTable.AppendRow(this.DefaultStyle.Font.Size, "Application ID", application.ApplicationIdDisplay ?? application.ApplicationId);
            metaTable.AppendRow(this.DefaultStyle.Font.Size, "Created", application.Created.ToLocalTime().ToString(ApplicationPdfWriter.SHORT_DATETIME_FORMAT));
            metaTable.AppendRow(this.DefaultStyle.Font.Size, "Modified", application.Modified.HasValue ? application.Modified.Value.ToLocalTime().ToString(ApplicationPdfWriter.SHORT_DATETIME_FORMAT) : string.Empty);

            document.AppendVerticalSpace();

            Table appDataTable = document.AppendTable(2, this.BorderColour);

            OptionSourceProviderRegister register = new OptionSourceProviderRegister();
            register.Register(new RepeaterOptionSourceProvider(application.ApplicationData));
            controlList.FindAllRecursive<ControlWithOptions>(c => c.OptionSource.Type == OptionSourceType.Repeater).ForEach(c => c.LoadOptions(register));

            this.WriteValue(appDataTable, controlList, application.ApplicationData, 0);

            this.WritePdf(document, target);
            this.CleanUp();
        }
        /// <summary>
        /// Registers the option controls.
        /// </summary>
        /// <param name="organisationId">The organization ID.</param>
        /// <param name="loadExternalData">If set to <c>true</c> [load external data].</param>
        /// <param name="controlsWithOptions">The controls with options.</param>
        /// <param name="applicationData">The application data.</param>
        private void RegisterOptionControls(string organisationId, bool loadExternalData, List<ControlWithOptions> controlsWithOptions, ApplicationData applicationData = null)
        {
            OptionSourceProviderRegister register = new OptionSourceProviderRegister();

            if (loadExternalData && controlsWithOptions.Any(x => x.OptionSource.Type == OptionSourceType.Dynamic))
            {
                register.Register(new DynamicOptionSourceProvider(this.DataAccess.GetServiceEndpointList(), this.endpointCommunicator));
            }

            if (controlsWithOptions.Any(x => x.OptionSource.Type == OptionSourceType.Static))
            {
                register.Register(new StaticOptionSourceProvider());
            }

            if (controlsWithOptions.Any(x => x.OptionSource.Type == OptionSourceType.Prebuilt))
            {
                register.Register(new PrebuiltOptionSourceProvider(this.DataAccess.GetDataSetList()));
            }

            if (controlsWithOptions.Any(x => x.OptionSource.Type == OptionSourceType.System))
            {
                register.Register(new SystemOptionSourceProvider(this.DataAccess.FindUsers, organisationId));
            }

            if (applicationData != null && controlsWithOptions.Any(x => x.OptionSource.Type == OptionSourceType.Repeater))
            {
                register.Register(new RepeaterOptionSourceProvider(applicationData));
            }

            foreach (ControlWithOptions control in controlsWithOptions)
            {
                if (control.OptionSource.Type == OptionSourceType.Dynamic && !loadExternalData)
                {
                    continue;
                }

                if (control.OptionSource.Type == OptionSourceType.Repeater && applicationData == null)
                {
                    continue;
                }

                control.LoadOptions(register);
            }
        }