示例#1
0
        public async Task <IEnumerable <ProducerSubmission> > GenerateProducers(ProcessXmlFile messageXmlFile, MemberUpload memberUpload, Dictionary <string, ProducerCharge> producerCharges)
        {
            var deserializedXml = xmlConverter.Deserialize <schemeType>(xmlConverter.Convert(messageXmlFile.Data));
            var producers       = await GenerateProducerData(deserializedXml, memberUpload, producerCharges);

            return(producers);
        }
示例#2
0
        public void SchemaValidation_ValidXml_NoErrors()
        {
            var validXmlLocation = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase),
                                                @"ExampleXML\v3-valid.xml");
            var validXml = File.ReadAllText(new Uri(validXmlLocation).LocalPath);

            A.CallTo(() => xmlConverter.Convert(A <byte[]> ._))
            .Returns(XDocument.Parse(validXml));

            var errors = SchemaValidator().Validate(new byte[1], @"EA.Weee.Xml.MemberRegistration.v3schema.xsd", XmlNamespace.MemberRegistration, A.Dummy <string>());

            Assert.Empty(errors);
        }
        public GenerateFromDataReturnXmlResult <T> GenerateDataReturns <T>(ProcessDataReturnXmlFile message) where T : class
        {
            Guard.ArgumentNotNull(() => message, message);

            T deserialisedType = null;

            string schemaVersion = "3.35";

            // Validate against the schema
            var validationErrors = schemaValidator
                                   .Validate(message.Data, @"EA.Weee.Xml.DataReturns.v3schema.xsd", XmlNamespace.DataReturns, schemaVersion)
                                   .ToList();

            if (!validationErrors.Any())
            {
                try
                {
                    deserialisedType = xmlConverter.Deserialize <T>(xmlConverter.Convert(message.Data));
                }
                catch (XmlDeserializationFailureException e)
                {
                    // Couldn't deserialise - can't go any further, add an error and bail out here
                    var exceptionMessage = e.InnerException != null ? e.InnerException.Message : e.Message;
                    var friendlyMessage  = errorTranslator.MakeFriendlyErrorMessage(exceptionMessage, schemaVersion);
                    validationErrors.Add(new XmlValidationError(Core.Shared.ErrorLevel.Error, XmlErrorType.Schema, friendlyMessage));
                }
            }

            return(new GenerateFromDataReturnXmlResult <T>(xmlConverter.XmlToUtf8String(message.Data), deserialisedType, validationErrors));
        }
示例#4
0
        public Dictionary <string, ProducerCharge> Calculate(ProcessXmlFile message)
        {
            var schemeType = xmlConverter.Deserialize <schemeType>(xmlConverter.Convert(message.Data));

            var producerCharges = new Dictionary <string, ProducerCharge>();
            var complianceYear  = int.Parse(schemeType.complianceYear);

            foreach (var producer in schemeType.producerList)
            {
                var producerName   = producer.GetProducerName();
                var producerCharge = Task.Run(() => producerChargeCalculator.GetProducerChargeBand(schemeType, producer)).Result;

                if (producerCharge != null)
                {
                    if (!producerCharges.ContainsKey(producerName))
                    {
                        producerCharges.Add(producerName, producerCharge);
                    }
                    else
                    {
                        ErrorsAndWarnings.Add(
                            new MemberUploadError(
                                ErrorLevel.Error,
                                UploadErrorType.Business,
                                string.Format(
                                    "We are unable to check for warnings associated with the charge band of the producer {0} until the duplicate name has been fixed.",
                                    producerName)));
                    }
                }
            }

            return(producerCharges);
        }
示例#5
0
        public IEnumerable <XmlValidationError> Validate(byte[] data, string schemaName, XNamespace schemaNamespace, string schemaVersion)
        {
            var errors = new List <XmlValidationError>();

            try
            {
                //check if the xml is not blank before doing any validations
                if (data != null && data.Length == 0)
                {
                    errors.Add(new XmlValidationError(ErrorLevel.Error, XmlErrorType.Schema, XmlErrorTranslator.IncorrectlyFormattedXmlMessage));
                    return(errors);
                }

                // Validate against the schema
                var source = xmlConverter.Convert(data);

                var namespaceErrors = namespaceValidator.Validate(schemaNamespace, source.Root.Name.Namespace.ToString());
                if (namespaceErrors.Any())
                {
                    return(namespaceErrors);
                }

                var schemas = new XmlSchemaSet();
                using (var stream = typeof(schemeType).Assembly.GetManifestResourceStream(schemaName))
                {
                    using (var schemaReader = XmlReader.Create(stream))
                    {
                        schemas.Add(null, schemaReader);
                    }
                }

                source.Validate(
                    schemas,
                    (sender, args) =>
                {
                    var asXElement = sender as XElement;
                    errors.Add(
                        asXElement != null
                                ? new XmlValidationError(ErrorLevel.Error, XmlErrorType.Schema,
                                                         xmlErrorTranslator.MakeFriendlyErrorMessage(asXElement, args.Exception.Message,
                                                                                                     args.Exception.LineNumber, schemaVersion), args.Exception.LineNumber)
                                : new XmlValidationError(ErrorLevel.Error, XmlErrorType.Schema, args.Exception.Message, args.Exception.LineNumber));
                });
            }
            catch (XmlException ex)
            {
                string friendlyMessage = xmlErrorTranslator.MakeFriendlyErrorMessage(ex.Message, schemaVersion);

                errors.Add(new XmlValidationError(ErrorLevel.Error, XmlErrorType.Schema, friendlyMessage));
            }
            return(errors);
        }
示例#6
0
        public void UpdateCharges()
        {
            using (var dbContextTransaction = context.Database.BeginTransaction())
            {
                try
                {
                    var memberUploads = memberUploadDataAccess.FetchMemberUploadsToProcess();

                    memberUploadDataAccess.ResetMemberUploadsAnnualCharge(memberUploads);

                    context.SaveChanges();

                    foreach (var memberUpload in memberUploads)
                    {
                        var message = new ProcessXmlFile(memberUpload.OrganisationId, Encoding.ASCII.GetBytes(memberUpload.RawData.Data), memberUpload.FileName);

                        var schemeType     = xmlConverter.Deserialize <schemeType>(xmlConverter.Convert(message.Data));
                        var complianceYear = int.Parse(schemeType.complianceYear);
                        var scheme         = context.Schemes.Single(c => c.OrganisationId == message.OrganisationId);

                        var hasAnnualCharge        = totalChargeCalculatorDataAccess.CheckSchemeHasAnnualCharge(scheme, complianceYear, memberUpload.SubmittedDate.Value);
                        var annualChargedToBeAdded = !hasAnnualCharge && scheme.CompetentAuthority.Abbreviation == UKCompetentAuthorityAbbreviationType.EA;

                        var total = TotalCalculatedCharges(memberUpload, schemeType, annualChargedToBeAdded, scheme);

                        Log.Information(string.Format("Member upload {0} updated from {1} to {2}", memberUpload.Id, memberUpload.TotalCharges, total));

                        memberUploadDataAccess.UpdateMemberUploadAmount(memberUpload, total, annualChargedToBeAdded);
                    }

                    dbContextTransaction.Commit();
                }
                catch (Exception ex)
                {
                    Log.Error(ex.Message + (ex.InnerException != null ? ex.InnerException.Message : string.Empty));
                    dbContextTransaction.Rollback();
                    throw;
                }
            }
        }
示例#7
0
        public async Task <IEnumerable <MemberUploadError> > Validate(ProcessXmlFile message)
        {
            string schemaVersion = MemberRegistrationSchemaVersion.Version_3_07.GetAttribute <DisplayAttribute>().Name;
            // Validate against the schema
            var errors = schemaValidator.Validate(message.Data, @"EA.Weee.Xml.MemberRegistration.v3schema.xsd", XmlNamespace.MemberRegistration, schemaVersion)
                         .Select(e => e.ToMemberUploadError())
                         .ToList();

            if (errors.Any())
            {
                return(errors);
            }

            schemeType deserializedXml;

            try
            {
                // Validate deserialized XML against business rules
                deserializedXml = xmlConverter.Deserialize <schemeType>(xmlConverter.Convert(message.Data));
            }
            catch (XmlDeserializationFailureException e)
            {
                // Couldn't deserialise - can't go any further, add an error and bail out here
                var exceptionMessage = e.InnerException != null ? e.InnerException.Message : e.Message;
                var friendlyMessage  = errorTranslator.MakeFriendlyErrorMessage(exceptionMessage, schemaVersion);
                errors.Add(new MemberUploadError(ErrorLevel.Error, UploadErrorType.Schema, friendlyMessage));

                return(errors);
            }

            errors = (await businessValidator.Validate(deserializedXml, message.OrganisationId))
                     .Select(err => new MemberUploadError(err.ErrorLevel.ToDomainEnumeration <ErrorLevel>(), UploadErrorType.Business, err.Message))
                     .ToList();

            return(errors);
        }
示例#8
0
        public async Task <Guid> HandleAsync(ProcessXmlFile message)
        {
            authorization.EnsureOrganisationAccess(message.OrganisationId);

            // record XML processing start time
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            var errors = await xmlValidator.Validate(message);

            List <MemberUploadError> memberUploadErrors = errors as List <MemberUploadError> ?? errors.ToList();
            bool containsSchemaErrors = memberUploadErrors.Any(e => e.ErrorType == UploadErrorType.Schema);
            bool containsErrorOrFatal = memberUploadErrors.Any(e => (e.ErrorLevel == ErrorLevel.Error || e.ErrorLevel == ErrorLevel.Fatal));

            Dictionary <string, ProducerCharge> producerCharges = null;
            int deserializedcomplianceYear = 0;

            decimal?totalChargesCalculated = 0;

            var scheme = await context.Schemes.SingleAsync(c => c.OrganisationId == message.OrganisationId);

            var annualChargeToBeAdded = false;

            if (!containsSchemaErrors || !containsErrorOrFatal)
            {
                var deserializedXml = xmlConverter.Deserialize <schemeType>(xmlConverter.Convert(message.Data));
                deserializedcomplianceYear = int.Parse(deserializedXml.complianceYear);

                var hasAnnualCharge = totalChargeCalculatorDataAccess.CheckSchemeHasAnnualCharge(scheme, deserializedcomplianceYear);

                if (!hasAnnualCharge)
                {
                    var annualcharge = scheme.CompetentAuthority.AnnualChargeAmount ?? 0;
                    if (annualcharge > 0 || scheme.CompetentAuthority.Abbreviation == UKCompetentAuthorityAbbreviationType.EA)
                    {
                        annualChargeToBeAdded = true;
                    }
                }

                producerCharges = totalChargeCalculator.TotalCalculatedCharges(message, scheme, deserializedcomplianceYear, annualChargeToBeAdded, ref totalChargesCalculated);

                if (xmlChargeBandCalculator.ErrorsAndWarnings.Any(e => e.ErrorLevel == ErrorLevel.Error) &&
                    memberUploadErrors.All(e => e.ErrorLevel != ErrorLevel.Error))
                {
                    throw new ApplicationException(String.Format(
                                                       "Upload for Organisation '{0}' has no validation errors, but does have producer charge calculation errors which are not currently being enforced",
                                                       message.OrganisationId));
                }
            }

            var totalCharges = totalChargesCalculated ?? 0;

            var upload = generateFromXml.GenerateMemberUpload(message, memberUploadErrors, totalCharges, scheme, annualChargeToBeAdded);
            IEnumerable <ProducerSubmission> producers = Enumerable.Empty <ProducerSubmission>();

            //Build producers domain object if there are no errors (schema or business) during validation of xml file.
            if (!containsErrorOrFatal)
            {
                producers = await generateFromXml.GenerateProducers(message, upload, producerCharges);
            }

            // record XML processing end time
            stopwatch.Stop();
            upload.SetProcessTime(stopwatch.Elapsed);

            context.MemberUploads.Add(upload);
            producerSubmissionDataAccess.AddRange(producers);

            await context.SaveChangesAsync();

            return(upload.Id);
        }