Exemplo n.º 1
0
        public override void Validate()
        {
            var errors = new RulesException <QpPlugin>();

            base.Validate(errors);

            if (!string.IsNullOrEmpty(LoadedContractInvalidMessage))
            {
                errors.ErrorFor(n => ServiceUrl, $"{QpPluginStrings.LoadedContractInvalidMessage}: {LoadedContractInvalidMessage}");
            }

            if (!string.IsNullOrEmpty(ParsedContractInvalidMessage))
            {
                errors.ErrorFor(n => Contract, $"{QpPluginStrings.ParsedContractInvalidMessage}: {ParsedContractInvalidMessage}");
            }

            if (!string.IsNullOrEmpty(ServiceUrl) && !UrlHelpers.IsValidWebFolderUrl(ServiceUrl))
            {
                errors.ErrorFor(n => ServiceUrl, QpPluginStrings.ServiceUrlInvalidFormat);
            }

            if (ContractParsed)
            {
                if (string.IsNullOrEmpty(Contract))
                {
                    errors.ErrorFor(n => ServiceUrl, QpPluginStrings.ContractNotEntered);
                }
                else
                {
                    if (string.IsNullOrEmpty(Code))
                    {
                        errors.ErrorFor(n => ServiceUrl, QpPluginStrings.CodeNotEntered);
                    }
                    else
                    {
                        if (QpPluginRepository.CodeExists(this))
                        {
                            errors.ErrorFor(n => Code, QpPluginStrings.CodeExists);
                        }
                    }
                    if (string.IsNullOrEmpty(Version))
                    {
                        errors.ErrorFor(n => ServiceUrl, QpPluginStrings.VersionNotEntered);
                    }
                    else
                    {
                        if (Version.Length > MaxVersionLength)
                        {
                            errors.ErrorFor(n => Version, String.Format(QpPluginStrings.VersionMaxLengthExceeded, null, MaxVersionLength));
                        }
                        else if (OldVersion == Version)
                        {
                            errors.ErrorFor(n => Version, QpPluginStrings.VersionEqual);
                        }
                    }

                    if (Fields.Any(n => String.IsNullOrEmpty(n.Name)))
                    {
                        errors.ErrorForModel(QpPluginStrings.FieldNameNotEntered);
                    }

                    if (Fields.Any(n => n.Name.Length > MaxFieldNameLength))
                    {
                        errors.ErrorForModel(String.Format(QpPluginStrings.FieldNameMaxLengthExceeded, null, MaxFieldNameLength));
                    }

                    if (Fields.GroupBy(n => n.Name.ToLower() + n.RelationType).Any(g => g.Count() > 1))
                    {
                        errors.ErrorForModel(QpPluginStrings.FieldNameDuplicate);
                    }
                }
            }

            if (!errors.IsEmpty)
            {
                throw errors;
            }
        }