public InspectionOutcomesResponse ExtractOfstedInspections(ExcelPackage package)
        {
            var inspections = new List <InspectionOutcome>();
            var errorSet    = new List <InspectionError>();
            var statusCode  = InspectionsStatusCode.Success;

            var keyWorksheet = package.Workbook.Worksheets.FirstOrDefault(x => x.Name == _configurationSettings.WorksheetName);

            if (keyWorksheet == null)
            {
                var message   = $@"No worksheet found in the datasource that matches '{_configurationSettings.WorksheetName}'";
                var exception = new NoWorksheetPresentException(message);
                _logger.Error(message, exception);
                throw exception;
            }

            var spreadsheetDetails = GetSpreadsheetColumnAndRowDetails(keyWorksheet);


            if (!spreadsheetDetails.AreAllColumnHeadingsMatched || spreadsheetDetails.DataStartsRow == 0)
            {
                const string message   = "Could not find the start row or the column names";
                var          exception = new MissingInspectionOutcomesException(message);
                _logger.Error(message, exception);
                throw exception;
            }

            for (var lineNumber = spreadsheetDetails.DataStartsRow; lineNumber <= keyWorksheet.Dimension.End.Row; lineNumber++)
            {
                var returnedStatusCode = ProcessLineIntoDetailsAsDetailOrError(keyWorksheet,
                                                                               spreadsheetDetails, lineNumber, inspections, errorSet);
                if (returnedStatusCode == InspectionsStatusCode.ProcessedWithErrors)
                {
                    statusCode = InspectionsStatusCode.ProcessedWithErrors;
                }
            }

            if (inspections.Count == 0)
            {
                const string message   = "No inspections were processed successfully";
                var          exception = new MissingInspectionOutcomesException(message);
                foreach (var error in errorSet)
                {
                    exception.Data.Add(error.LineNumber.ToString(), error);
                }
                _logger.Error(message, exception);
                throw exception;
            }


            return(new InspectionOutcomesResponse {
                InspectionOutcomes = inspections, InspectionOutcomeErrors = errorSet, StatusCode = statusCode
            });
        }
Beispiel #2
0
        public InspectionOutcomesResponse GetInspectionsDetail(string firstLinkUrl)
        {
            InspectionOutcomesResponse inspectionOutcomesResponse;

            try
            {
                _logger.Debug("Opening web client");

                var webClient = _webClientFactory.Create();

                using (var client = webClient)
                {
                    _logger.Debug("Opening memory stream");
                    using (var stream =
                               new MemoryStream(client.DownloadData(new Uri(firstLinkUrl))))
                    {
                        _logger.Debug("Opened memory stream");

                        using (var package = new ExcelPackage(stream))
                        {
                            _logger.Debug("Opened excel package");
                            inspectionOutcomesResponse = _getOfstedDetailsFromExcelPackageService.ExtractOfstedInspections(package);
                        }
                        _logger.Debug("Closed excel package");
                    }
                    _logger.Debug("Closed memory stream");
                }
                _logger.Debug($"Closed web client");
            }
            catch (UriFormatException ex)
            {
                var message   = $"Error whilst trying to read url: [{firstLinkUrl}]";
                var exception = new UrlReadingException(message, ex);
                _logger.Error(message, exception);
                throw exception;
            }
            catch (COMException ex)
            {
                var message   = $"Error whilst trying to read excel details from url: [{firstLinkUrl}], message: [{ex.Message}]";
                var exception = new UrlReadingException(message, ex);
                _logger.Error(message, exception);
                throw exception;
            }
            catch (Exception ex)
            {
                var message   = $"Error whilst trying to read excel details";
                var exception = new UrlReadingException(message, ex);
                _logger.Error(message, exception);
                throw exception;
            }

            return(inspectionOutcomesResponse);
        }
Beispiel #3
0
        private string BuildFullLinkFromRelativeFirstLink(string getFirstMatchingLink)
        {
            var firstLinkUrl = BuildFirstLinkUrl(getFirstMatchingLink);

            if (firstLinkUrl != string.Empty)
            {
                return(firstLinkUrl);
            }

            var noFirstLinkValid =
                $"Could not build a valid url from url [{_configurationSettings.InspectionSiteUrl}], link [{getFirstMatchingLink}]";

            _logger.Error(noFirstLinkValid, new InvalidLinkException(noFirstLinkValid));
            throw new InvalidLinkException(noFirstLinkValid);
        }