private static void AddModelValidation(this ValidationContext validationContext, IModel model, List <IModel> handledModels)
        {
            Argument.IsNotNull("validationContext", validationContext);


            if (handledModels.Any(x => ReferenceEquals(x, model)))
            {
                return;
            }

            handledModels.Add(model);

            validationContext.SynchronizeWithContext(model.ValidationContext, true);

            var propertyDataManager = PropertyDataManager.Default;
            var catelTypeInfo       = propertyDataManager.GetCatelTypeInfo(model.GetType());

            foreach (var property in catelTypeInfo.GetCatelProperties())
            {
                var propertyValue = model.GetValue(property.Key);
                var enumerable    = propertyValue as IEnumerable;
                if (enumerable != null && !(propertyValue is string))
                {
                    foreach (var item in enumerable)
                    {
                        var modelItem = item as IModel;
                        if (modelItem != null)
                        {
                            validationContext.AddModelValidation(modelItem, handledModels);
                        }
                    }
                }

                var propertyModel = propertyValue as IModel;
                if (propertyModel != null)
                {
                    validationContext.AddModelValidation(propertyModel, handledModels);
                }
            }
        }
        /// <summary>
        /// Validates the license.
        /// </summary>
        /// <param name="license">The license key the user has given to be validated.</param>
        /// <returns>The validation context containing all the validation results.</returns>
        public IValidationContext ValidateLicense(string license)
        {
            Argument.IsNotNullOrWhitespace(() => license);

            var validationContext = new ValidationContext();

            Log.Info("Validating license");

            try
            {
                var licenseObject = License.Load(license);
                var failureList = licenseObject.Validate()
                    .Signature(_applicationIdService.ApplicationId)
                    .AssertValidLicense().ToList();

                if (failureList.Count > 0)
                {
                    foreach (var failure in failureList)
                    {
                        var businessRuleValidationResult = BusinessRuleValidationResult.CreateErrorWithTag(failure.Message, failure.HowToResolve);
                        validationContext.AddBusinessRuleValidationResult(businessRuleValidationResult);
                    }
                }

                var licenseAttributes = licenseObject.AdditionalAttributes;
                if (licenseAttributes != null)
                {
                    foreach (var licenseAttribute in licenseAttributes.GetAll())
                    {
                        if (string.Equals(licenseAttribute.Key, LicenseElements.MachineId))
                        {
                            Log.Debug("Validating license using machine ID");

                            var machineLicenseValidationContext = _machineLicenseValidationService.Validate(licenseAttribute.Value);
                            validationContext.SynchronizeWithContext(machineLicenseValidationContext, true);

                            if (machineLicenseValidationContext.HasErrors)
                            {
                                Log.Error("The license can only run on machine with ID '{0}'", licenseAttribute.Value);
                            }
                        }

                        // TODO: add additional attribute checks here
                    }
                }

                // Also validate the xml, very important for expiration date and version
                var xmlValidationContext = ValidateXml(license);
                validationContext.SynchronizeWithContext(xmlValidationContext, true);
            }
            catch (Exception ex)
            {
                Log.Error(ex, "An error occurred while loading the license");

                validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError("An unknown error occurred while loading the license, please contact support"));
            }
            finally
            {
                if (validationContext.GetErrors().Count > 0)
                {
                    Log.Warning("License is not valid:");
                    Log.Indent();

                    foreach (var error in validationContext.GetErrors())
                    {
                        Log.Warning("- {0}\n{1}", error.Message, error.Tag as string);
                    }

                    Log.Unindent();
                }
                else
                {
                    Log.Info("License is valid");
                }
            }

            return validationContext;
        }
            public void CorrectlySynchronizesWithRemovedBusinessRuleError()
            {
                var fieldValidationResults1 = new List<IFieldValidationResult>();
                fieldValidationResults1.Add(FieldValidationResult.CreateWarning("MyProperty", "FieldWarning"));
                fieldValidationResults1.Add(FieldValidationResult.CreateError("MyProperty", "FieldError"));

                var businessRuleValidationResults1 = new List<IBusinessRuleValidationResult>();
                businessRuleValidationResults1.Add(BusinessRuleValidationResult.CreateWarning("BusinessRuleWarning"));
                businessRuleValidationResults1.Add(BusinessRuleValidationResult.CreateError("BusinessRuleError"));
                var context1 = new ValidationContext(fieldValidationResults1, businessRuleValidationResults1);

                var fieldValidationResults2 = new List<IFieldValidationResult>();
                fieldValidationResults2.Add(FieldValidationResult.CreateWarning("MyProperty", "FieldWarning"));
                fieldValidationResults2.Add(FieldValidationResult.CreateError("MyProperty", "FieldError"));

                var businessRuleValidationResults2 = new List<IBusinessRuleValidationResult>();
                businessRuleValidationResults2.Add(BusinessRuleValidationResult.CreateWarning("BusinessRuleWarning"));
                var context2 = new ValidationContext(fieldValidationResults2, businessRuleValidationResults2);

                context1.SynchronizeWithContext(context2);

                // First context should now equal second context
                int counter;

                var fieldValidations = context1.GetFieldValidations();
                Assert.AreEqual(2, fieldValidations.Count);
                counter = -1;

                counter++;
                Assert.AreEqual("MyProperty", fieldValidations[counter].PropertyName);
                Assert.AreEqual("FieldWarning", fieldValidations[counter].Message);
                Assert.AreEqual(ValidationResultType.Warning, fieldValidations[counter].ValidationResultType);

                counter++;
                Assert.AreEqual("MyProperty", fieldValidations[counter].PropertyName);
                Assert.AreEqual("FieldError", fieldValidations[counter].Message);
                Assert.AreEqual(ValidationResultType.Error, fieldValidations[counter].ValidationResultType);

                var businessRuleValidations = context1.GetBusinessRuleValidations();
                Assert.AreEqual(1, businessRuleValidations.Count);
                counter = -1;

                counter++;
                Assert.AreEqual("BusinessRuleWarning", businessRuleValidations[counter].Message);
                Assert.AreEqual(ValidationResultType.Warning, businessRuleValidations[counter].ValidationResultType);
            }