public void FormatHeadersCompressesAddressesToMaxSevenLines()
        {
            var addressLines = new List <string>
            {
                "Name",
                "Address Line 1",
                "Address Line 2",
                "Address Line 3",
                "Address Line 4",
                "Address Line 5",
                "Address Line 6",
                "City",
                "County",
                "Postcode"
            };

            var expectedAddressString = "Name<br />" +
                                        "Address Line 1<br />" +
                                        "Address Line 2, Address Line 3<br />" +
                                        "Address Line 4, Address Line 5, Address Line 6<br />" +
                                        "City<br />" +
                                        "County<br />" +
                                        "Postcode";

            var result = ParsingHelpers.FormatLetterHeader(addressLines, "");

            result.Should().Contain(expectedAddressString);
        }
        public async Task Execute(string htmlDocument, string documentType, string documentId)
        {
            ILetterParser parser = _getParser.ForType(documentType);

            if (parser == null)
            {
                Console.WriteLine($"Letter type invalid {documentType}");
                throw new NonSupportedException();
            }
            var htmlInput = parser.Execute(htmlDocument);

            var css = CompileCss(htmlInput);

            var header = ParsingHelpers.FormatLetterHeader(htmlInput.AddressLines, htmlInput.RightSideOfHeader, htmlInput.LetterHead);

            var fullHtml = "<!DOCTYPE html><html><head><style>"
                           + css
                           + "</style></head><body>"
                           + header
                           + htmlInput.MainBody
                           + "</body></html>";

            var pdfBytes = await _parseHtmlToPdf.Convert(fullHtml, documentId);

            Console.WriteLine("Writing PDF to temp file");
            await File.WriteAllBytesAsync($"/tmp/{documentId}.pdf", pdfBytes);

            Console.WriteLine("Successfully written to file");
        }
Exemplo n.º 3
0
        public void LoadThemeResources()
        {
            IsBusy = true;

            var blueResources = new BlueResources();

            foreach (var key in blueResources.Keys)
            {
                IsBusyMessage = $"reading {key}...";

                try
                {
                    var item = new ThemeItem
                    {
                        ThemeKey           = key,
                        Title              = ParsingHelpers.BreakCaseNamedWord(key),
                        ControlName        = ParsingHelpers.GetControlName(key),
                        OriginalThemeColor = (Color)blueResources[key],
                        SelectedThemeColor = (Color)blueResources[key]
                    };

                    ThemeColors.Add(item);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Exception reading value for: {key}. \n{ex}");
                }
            }

            IsBusyMessage = "";
            IsBusy        = false;
        }
    /// <summary>
    /// Get the associated values from the collection separated into individual values.
    /// Quoted values will not be split, and the quotes will be removed.
    /// </summary>
    /// <param name="headers">The <see cref="IHeaderDictionary"/> to use.</param>
    /// <param name="key">The header name.</param>
    /// <returns>the associated values from the collection separated into individual values, or StringValues.Empty if the key is not present.</returns>
    public static string[] GetCommaSeparatedValues(this IHeaderDictionary headers, string key)
    {
        // GetHeaderSplit will return only non-null elements of the given IHeaderDictionary.
#pragma warning disable CS8619 // Nullability of reference types in value doesn't match target type.
        return(ParsingHelpers.GetHeaderSplit(headers, key).ToArray());

#pragma warning restore CS8619 // Nullability of reference types in value doesn't match target type.
    }
        private static IReadOnlyList <ParameterSignature> CreateOverriddenParameters
        (
            [NotNull] FunctionSignature functionBase,
            [NotNull] FunctionOverride functionOverride
        )
        {
            var baseParameters = functionBase.Parameters;
            var parametersWithComputedCounts =
                new List <(ParameterSignature Parameter, IReadOnlyList <string> ComputedCountParameterNames)>();
            var parametersWithValueReferenceCounts =
                new List <(ParameterSignature Parameter, string ParameterReferenceName)>();

            var resultParameters = new List <ParameterSignature>(baseParameters);

            foreach (var overrideParameter in functionOverride.ParameterOverrides)
            {
                var baseParameter = functionBase.Parameters.FirstOrDefault(p => p.Name == overrideParameter.BaseName);
                if (baseParameter is null)
                {
                    throw new InvalidDataException
                          (
                              $"Could not find target parameter with name \"{overrideParameter.BaseName}\" to override."
                          );
                }

                var overriddenParameter = CreateOverriddenParameter
                                          (
                    baseParameter,
                    overrideParameter,
                    out var hasComputedCount,
                    out var computedCountParameterNames,
                    out var hasValueReference,
                    out var valueReferenceName,
                    out var valueReferenceExpression
                                          );

                if (hasComputedCount)
                {
                    parametersWithComputedCounts.Add((overriddenParameter, computedCountParameterNames));
                }

                if (hasValueReference)
                {
                    parametersWithValueReferenceCounts.Add((overriddenParameter, valueReferenceName));

                    // TODO: Pass on the mathematical expression
                }

                var baseIndex = resultParameters.IndexOf(baseParameter);
                resultParameters[baseIndex] = overriddenParameter;
            }

            ParsingHelpers.ResolveComputedCountSignatures(resultParameters, parametersWithComputedCounts);

            ParsingHelpers.ResolveReferenceCountSignatures(resultParameters, parametersWithValueReferenceCounts);

            return(resultParameters);
        }
        public void SplitManyHeaders()
        {
            var values = ParsingHelpers.GetHeaderSplit(_dictionary, "manyValue");

            if (values.Count != 6)
            {
                throw new Exception();
            }
        }
        public void SplitSingleQuotedHeader()
        {
            var values = ParsingHelpers.GetHeaderSplit(_dictionary, "singleValueQuoted");

            if (values.Count != 1)
            {
                throw new Exception();
            }
        }
        public void SplitDoubleHeader()
        {
            var values = ParsingHelpers.GetHeaderSplit(_dictionary, "doubleValue");

            if (values.Count != 2)
            {
                throw new Exception();
            }
        }
        public void FormatHeaderReturnsFormattedTableIncludingTheRightSideOfHeader()
        {
            var rightSideOfHeader = "<p>Lots of html and CSS</p>";

            var result = ParsingHelpers.FormatLetterHeader(new List <string> {
                "Name"
            }, rightSideOfHeader);

            result.Should().Contain(rightSideOfHeader);
        }
Exemplo n.º 10
0
        private void DeserializeStatusLine(string statusLine)
        {
            var parsed = statusLine.Trim().Split(new char[] { ABNF.SP }, 3);

            if (!ParsingHelpers.IsStatusLine(parsed, out string err))
            {
                throw new SipParsingException(err);
            }

            StatusCode   = parsed[1];
            ReasonPhrase = parsed[2];
        }
Exemplo n.º 11
0
        public async Task Execute(SQSEvent sqsEvent)
        {
            Console.WriteLine("Received message from SQS");
            // expected Records count = 1, per batchSize configured in serverless.yml
            // Messages will be removed from the queue upon successful response of this lambda.
            // If no successful response within 30sec then they will be available to pick up from the queue again.

            Console.WriteLine("Getting document configuration");
            var documentConfig     = ParsingHelpers.GetDocumentConfig();
            var automaticApprovals = documentConfig.AutomaticApprovals;

            var record    = sqsEvent.Records.First();
            var timestamp = record.Body;

            Console.WriteLine($"Received from queue [{record.EventSourceArn}] document timestamp = {timestamp}");

            var document = await _getDocumentDetails.Execute(timestamp);

            Console.WriteLine($"Received Document {JsonConvert.SerializeObject(document)}");

            if (document == null)
            {
                Console.WriteLine($"Could not find document for ID {timestamp} waiting to be processed in Dynamo");
                return;
            }

            await _logger.LogMessage(timestamp, "Picked up document from queue - Processing");

            Console.WriteLine($"Retrieved from dynamo, getting Html for documentId = {document.CominoDocumentNumber}");

            var html = await TryGetDocumentAsHtml(document, timestamp);

            Console.WriteLine($"Received HTML: {(html == null ? "" : (html.Length < 100 ? html : html.Substring(0, 100)))}");

            await TryConvertToPdf(html, document, timestamp);
            await TryStoreInS3(document, timestamp);

            if (automaticApprovals != null && automaticApprovals.Contains(document.LetterType))
            {
                await _localDatabaseGateway.UpdateStatus(document.Id, LetterStatusEnum.ReadyForGovNotify);
            }
            else
            {
                await _localDatabaseGateway.UpdateStatus(document.Id, LetterStatusEnum.WaitingForApproval);
            }
        }
Exemplo n.º 12
0
        public List <DocumentDetails> GetDocumentsAfterStartDate(DateTime time)
        {
            var documentConfig = ParsingHelpers.GetDocumentConfig();
            var categories     = documentConfig.Categories;
            var descriptions   = documentConfig.Descriptions;
            var startTime      = $"{time.Month}/{time.Day}/{time.Year} {time.Hour}:{time.Minute}:{time.Second}";

            var query =
                $@"SELECT DocNo AS DocumentNumber,
                StoreDate AS Date,
                strDescription AS LetterType,
                strUser AS UserName,
                RefType AS DocumentType
                FROM W2BatchPrint
                JOIN CCDocument on DocNo = nDocNo
                WHERE CCDocument.DocCategory IN ('{string.Join("','", categories)}')
                AND CCDocument.DocDesc IN ('{string.Join("','", descriptions)}')
                AND CCDocument.DirectionFg = 'O'
                AND CCDocument.DocSource = 'O'
                AND W2BatchPrint.StoreDate > '{startTime}'
                ORDER BY W2BatchPrint.StoreDate DESC;
                ";

            List <DocumentDetails> queryResults;

            try
            {
                queryResults = _database.Query <W2BatchPrintRow>(query).Select(row =>
                                                                               new DocumentDetails {
                    Id = row.Date.ToString("O"),
                    CominoDocumentNumber = row.DocumentNumber,
                    DocumentCreator      = row.UserName,
                    LetterType           = row.LetterType,
                    DocumentType         = row.DocumentType,
                    Date = row.Date.ToString("O")
                }).ToList();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

            return(queryResults);
        }
        public void FormatHeaderReturnsFormattedTableIncludingTheAddress()
        {
            var addressLines = new List <string>
            {
                "Name",
                "Address Line 1",
                "Address Line 2",
                "City",
                "County",
                "Postcode"
            };

            var expectedAddressString = @"Name<br />Address Line 1<br />Address Line 2<br />City<br />County<br />Postcode";

            var result = ParsingHelpers.FormatLetterHeader(addressLines, "");

            result.Should().Contain(expectedAddressString);
        }
        public void FormatHeaderStripsWhitespaceFromAddressLines()
        {
            var addressLines = new List <string>
            {
                "Name",
                "      ",
                "Address Line 1",
                "        ",
                "\n",
                "\n      ",
                "Postcode"
            };

            var expectedAddressString = @"Name<br />Address Line 1<br />Postcode";

            var result = ParsingHelpers.FormatLetterHeader(addressLines, "");

            result.Should().Contain(expectedAddressString);
        }
Exemplo n.º 15
0
        public IReadOnlyDictionary <TypeSignature, TypeSignature> ReadTypemap([NotNull] Stream stream)
        {
            var typeMap = new Dictionary <TypeSignature, TypeSignature>();

            using (var streamReader = new StreamReader(stream))
            {
                while (!streamReader.EndOfStream)
                {
                    var line = streamReader.ReadLine();
                    if (line is null)
                    {
                        break;
                    }

                    if (line.StartsWith("#") || string.IsNullOrWhiteSpace(line))
                    {
                        continue;
                    }

                    var commentIndex = line.IndexOf("#", StringComparison.Ordinal);
                    if (commentIndex > 0)
                    {
                        line = line.Remove(commentIndex);
                    }

                    var parts = line.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    if (parts.Length != 2)
                    {
                        throw new InvalidDataException("Typemap element with more or less than two columns found.");
                    }

                    var fromType = ParsingHelpers.ParseTypeSignature(parts[0].Trim());
                    var toType   = ParsingHelpers.ParseTypeSignature(parts[1].Trim());

                    typeMap.Add(fromType, toType);
                }
            }

            return(typeMap);
        }
Exemplo n.º 16
0
        private static ParameterSignature CreateOverriddenParameter
        (
            [NotNull] ParameterSignature baseParameter,
            [NotNull] ParameterOverride parameterOverride,
            out bool hasComputedCount,
            [CanBeNull] out IReadOnlyList <string> computedCountParameterNames,
            out bool hasValueReference,
            [CanBeNull] out string valueReferenceName,
            [CanBeNull] out string valueReferenceExpression
        )
        {
            computedCountParameterNames = null;
            valueReferenceName          = null;
            valueReferenceExpression    = null;

            hasComputedCount  = false;
            hasValueReference = false;

            var newName = parameterOverride.NewName ?? baseParameter.Name;
            var newType = parameterOverride.NewType ?? baseParameter.Type;
            var newFlow = parameterOverride.NewFlow ?? baseParameter.Flow;

            var newCount = parameterOverride.NewCount is null
                ? baseParameter.Count
                : ParsingHelpers.ParseCountSignature
                           (
                parameterOverride.NewCount,
                out hasComputedCount,
                out computedCountParameterNames,
                out hasValueReference,
                out valueReferenceName,
                out valueReferenceExpression
                           );

            return(new ParameterSignature(newName, newType, newFlow, newCount));
        }
Exemplo n.º 17
0
 public void Reparse(string cookiesHeader)
 {
     _dictionary.Clear();
     ParsingHelpers.ParseDelimited(cookiesHeader, SemicolonAndComma, AddCookieCallback, _dictionary);
 }
        public IList <FlatOrder> ReadOrderFile()
        {
            if (Status == ReaderStatus.FileError)
            {
                return(null);
            }
            if (!File.Exists(FilePath))
            {
                Status = ReaderStatus.FileError;
                return(null);
            }

            using (StreamReader reader = new StreamReader(FilePath))
            {
                List <string> columns = Array.ConvertAll(reader.ReadLine().Split(','), item => item.ToUpperInvariant()).ToList();

                if (!CheckSchema(columns))
                {
                    Status = ReaderStatus.DeserializationError;
                    return(null);
                }

                var list = new List <FlatOrder>();

                while (!reader.EndOfStream)
                {
                    var row = reader.ReadLine().Split(',');

                    string clientId = null;
                    if (columns.IndexOf("CLIENT_ID") != -1 && columns.IndexOf("PRICE") < row.Length)
                    {
                        clientId = row[columns.IndexOf("CLIENT_ID")];
                    }

                    long?requestId = null;
                    if (columns.IndexOf("REQUEST_ID") != -1 && columns.IndexOf("PRICE") < row.Length)
                    {
                        requestId = ParsingHelpers.ParseLong(row[columns.IndexOf("REQUEST_ID")]);
                    }

                    string name = null;
                    if (columns.IndexOf("NAME") != -1 && columns.IndexOf("PRICE") < row.Length)
                    {
                        name = row[columns.IndexOf("NAME")];
                    }

                    int?quantity = null;
                    if (columns.IndexOf("QUANTITY") != -1 && columns.IndexOf("PRICE") < row.Length)
                    {
                        quantity = ParsingHelpers.ParseInt(row[columns.IndexOf("QUANTITY")]);
                    }

                    double?price = null;
                    if (columns.IndexOf("PRICE") != -1 && columns.IndexOf("PRICE") < row.Length)
                    {
                        price = ParsingHelpers.ParseDouble(row[columns.IndexOf("PRICE")]);
                    }

                    list.Add(new FlatOrder(clientId, requestId, name, quantity, price));
                }

                Status = ReaderStatus.Completed;
                return(list);
            }
        }
Exemplo n.º 19
0
 /// <summary>
 /// Quotes any values containing comas, and then coma joins all of the values.
 /// </summary>
 /// <param name="headers">The <see cref="IHeaderDictionary"/> to use.</param>
 /// <param name="key">The header name.</param>
 /// <param name="values">The header values.</param>
 public static void SetCommaSeparatedValues(this IHeaderDictionary headers, string key, params string[] values)
 {
     ParsingHelpers.SetHeaderJoined(headers, key, values);
 }
Exemplo n.º 20
0
 /// <summary>
 /// Get the associated values from the collection separated into individual values.
 /// Quoted values will not be split, and the quotes will be removed.
 /// </summary>
 /// <param name="headers">The <see cref="IHeaderDictionary"/> to use.</param>
 /// <param name="key">The header name.</param>
 /// <returns>the associated values from the collection separated into individual values, or StringValues.Empty if the key is not present.</returns>
 public static string[] GetCommaSeparatedValues(this IHeaderDictionary headers, string key)
 {
     return(ParsingHelpers.GetHeaderSplit(headers, key));
 }
Exemplo n.º 21
0
 /// <summary>
 /// Get the associated value from the collection as a single string.
 /// </summary>
 /// <param name="key">The header name.</param>
 /// <returns>the associated value from the collection as a single string or null if the key is not present.</returns>
 public string Get(string key)
 {
     return(ParsingHelpers.GetHeader(Store, key));
 }
Exemplo n.º 22
0
 /// <summary>
 /// Quotes any values containing comas, and then coma joins all of the values.
 /// </summary>
 /// <param name="key">The header name.</param>
 /// <param name="values">The header values.</param>
 public void SetCommaSeparatedValues(string key, params string[] values)
 {
     ParsingHelpers.SetHeaderJoined(Store, key, values);
 }
Exemplo n.º 23
0
 /// <summary>
 /// Sets the specified header values without modification.
 /// </summary>
 /// <param name="key">The header name.</param>
 /// <param name="values">The header values.</param>
 public void SetValues(string key, params string[] values)
 {
     ParsingHelpers.SetHeaderUnmodified(Store, key, values);
 }
Exemplo n.º 24
0
 /// <summary>
 /// Sets a specific header value.
 /// </summary>
 /// <param name="key">The header name.</param>
 /// <param name="value">The header value.</param>
 public void Set(string key, string value)
 {
     ParsingHelpers.SetHeader(Store, key, value);
 }
Exemplo n.º 25
0
        /// <summary>
        /// Get the associated values from the collection separated into individual values.
        /// Quoted values will not be split, and the quotes will be removed.
        /// </summary>
        /// <param name="key">The header name.</param>
        /// <returns>the associated values from the collection separated into individual values, or null if the key is not present.</returns>
        public IList <string> GetCommaSeparatedValues(string key)
        {
            IEnumerable <string> values = ParsingHelpers.GetHeaderSplit(Store, key);

            return(values == null ? null : values.ToList());
        }
Exemplo n.º 26
0
 /// <summary>
 /// Get the associated values from the collection without modification.
 /// </summary>
 /// <param name="key">The header name.</param>
 /// <returns>the associated value from the collection without modification, or null if the key is not present.</returns>
 public IList <string> GetValues(string key)
 {
     return(ParsingHelpers.GetHeaderUnmodified(Store, key));
 }
Exemplo n.º 27
0
 /// <summary>
 /// Add a new value. Appends to the header if already present
 /// </summary>
 /// <param name="key">The header name.</param>
 /// <param name="value">The header value.</param>
 public void Append(string key, string value)
 {
     ParsingHelpers.AppendHeader(Store, key, value);
 }
Exemplo n.º 28
0
 /// <summary>
 /// Add new values. Each item remains a separate array entry.
 /// </summary>
 /// <param name="headers">The <see cref="IHeaderDictionary"/> to use.</param>
 /// <param name="key">The header name.</param>
 /// <param name="value">The header value.</param>
 public static void Append(this IHeaderDictionary headers, string key, StringValues value)
 {
     ParsingHelpers.AppendHeaderUnmodified(headers, key, value);
 }
        public IList <FlatOrder> ReadOrderFile()
        {
            if (Status == ReaderStatus.FileError)
            {
                return(null);
            }
            if (!File.Exists(FilePath))
            {
                Status = ReaderStatus.FileError;
                return(null);
            }

            JObject jObject = null;

            using (StreamReader reader = new StreamReader(FilePath))
            {
                try
                {
                    string json = reader.ReadToEnd(); // can be async
                    jObject = JObject.Parse(json);
                }
                catch
                {
                    Status = ReaderStatus.DeserializationError;
                    return(null);
                }
            }

            if (!CheckSchema(jObject))
            {
                Status = ReaderStatus.DeserializationError;
                return(null);
            }

            var list = new List <FlatOrder>();

            foreach (JToken token in jObject.SelectToken("requests"))
            {
                string clientId = null;

                if (token["clientId"] != null)
                {
                    clientId = token["clientId"].ToString();
                }

                long?requestId = null;
                if (token["requestId"] != null)
                {
                    requestId = ParsingHelpers.ParseLong(token["requestId"].ToString());
                }

                string name = null;
                if (token["name"] != null)
                {
                    name = token["name"].ToString();
                }

                int?quantity = null;
                if (token["quantity"] != null)
                {
                    quantity = ParsingHelpers.ParseInt(token["quantity"].ToString());
                }

                double?price = null;
                if (token["price"] != null)
                {
                    price = ParsingHelpers.ParseDouble(token["price"].ToString());
                }

                list.Add(new FlatOrder(clientId, requestId, name, quantity, price));
            }
            Status = ReaderStatus.Completed;
            return(list);
        }
Exemplo n.º 30
0
 /// <summary>
 /// Add new values. Each item remains a separate array entry.
 /// </summary>
 /// <param name="key">The header name.</param>
 /// <param name="values">The header values.</param>
 public void AppendValues(string key, params string[] values)
 {
     ParsingHelpers.AppendHeaderUnmodified(Store, key, values);
 }