/// <summary>
 /// Gets the page entitlements that the user has.
 /// </summary>
 /// <param name="session">The session.</param>
 /// <param name="application">The application the user is trying to access.</param>
 /// <param name="pageList">The page list.</param>
 /// <param name="roleList">The list of roles.</param>
 /// <param name="versionNumber">The version number.</param>
 /// <param name="defaultAccessLevel">The default access level to use when there is no entitlement for a page. If not set it will default to the application base access level.</param>
 /// <returns>
 /// The page entitlements that the user has.
 /// </returns>
 public virtual List<PageAccess> GetPagesAccess(SecureSession session, Application application, PageList pageList, RoleList roleList, int versionNumber, AccessLevel? defaultAccessLevel = null)
 {
     return this.GetPagesAccess(session, application, pageList.Select(p => p.PageId), roleList, versionNumber, defaultAccessLevel);
 }
        /// <summary>
        /// Validates an application.
        /// </summary>
        /// <param name="application">The application to validate.</param>
        /// <param name="pagesToValidate">The pagesToValidate to validate.</param>
        /// <param name="controls">A list of all controls.</param>
        /// <param name="controlsAccess">The control access list for the current user.</param>
        /// <param name="subpageControlId">The id of the control acting as the current subpage. If <see langword="null" />, then all controls are validated.</param>
        /// <param name="product">The product object.</param>
        /// <returns>
        /// The <see cref="ValidationResults" />.
        /// </returns>
        private ValidationResults ValidateApplication(Application application, PageList pagesToValidate, ControlList controls, List<ControlAccess> controlsAccess, int? subpageControlId = null, ProductDefinition product = null)
        {
            List<ControlWithOptions> controlsWithOptions = controls.FindAllRecursive<ControlWithOptions>();

            if (controlsWithOptions.Any())
            {
                this.RegisterOptionControls(application.FormOrganisationId, true, controlsWithOptions, application.ApplicationData);
            }

            var controlList = pagesToValidate.AllControls;
            var ruleList = pagesToValidate.AllRules;

            if (subpageControlId != null)
            {
                var subpageControl = controlList.FindRecursive<GroupControl>(x => x.Id == subpageControlId);
                if (subpageControl != null)
                {
                    controlList = new ControlList { subpageControl };

                    TruthConditionList validRules = new TruthConditionList();
                    validRules.AddRange(ruleList.Where(rule => controlList.FindRecursive(x => x.Id.ToString().Equals(((ValidateTruthCondition)rule).Error.Position)) != null));

                    ruleList = validRules;
                }
            }

            ServiceEndpointList endpointList = pagesToValidate.AllExternalRuleHandlers.Count > 0 ?
                     this.DataAccess.GetServiceEndpointList() :
                     new ServiceEndpointList();

            RegexValidatorList regexValidators = this.DataAccess.GetValidatorList();

            if (product != null)
            {
                this.FillControlReferences(pagesToValidate, product.FormDefinition.Pages.AllControls);
                foreach (var referencedControls in pagesToValidate.Select(p => p.ReferencedControls))
                {
                    foreach (var control in referencedControls)
                    {
                        var exists = controls.FindRecursive(control.Name) != null;
                        if (exists)
                        {
                            continue;
                        }

                        controls.AddRange(referencedControls);
                    }
                }
            }

            ApplicationValidator validator = new ApplicationValidator(controls, controlList, controlsAccess, ruleList, pagesToValidate.AllExternalRuleHandlers, endpointList, this.endpointCommunicator, regexValidators);
            return validator.Validate(application);
        }