Ejemplo n.º 1
0
        public static StandardResult <string> Execute(SimpleTable table, bool escape = true)
        {
            var sb = new StringBuilder();

            foreach (var column in table.ColumnNames)
            {
                sb.Append(column);
                sb.Append('\t');
            }

            sb.Length = sb.Length - 1;
            sb.AppendLine();

            foreach (var row in table)
            {
                for (int i = 0; i < row.ColumnCount; i++)
                {
                    var value = row[i];

                    if (escape)
                    {
                        value = Escape(value);
                    }

                    sb.Append(value);
                    sb.Append('\t');
                }

                sb.Length = sb.Length - 1;
                sb.AppendLine();
            }

            return(StandardResult <string> .ReturnResult(sb.ToString()));
        }
Ejemplo n.º 2
0
        private static StandardResult <string> EvaulateExpression(SimpleTableRow row, string expressionText)
        {
            if (expressionText == null)
            {
                return(StandardResult <string> .ReturnResult(null));
            }

            var fieldSource = new FieldDataSource()
            {
                Row = row
            };

            var expression = ExpressionCache.Compile(expressionText);

            var result = expression.Evaluate(new Context()
            {
                FieldSource = fieldSource
            });

            if (result.IsError)
            {
                return(StandardResult <string> .ReturnError(result.String));
            }

            if (!result.IsString)
            {
                return(StandardResult <string> .ReturnError("message processing error - expression evaulation did not result in a string"));
            }

            return(StandardResult <string> .ReturnResult(result.String));
        }
Ejemplo n.º 3
0
        public static StandardResult <bool> Execute(SimpleTable sourceTable)
        {
            foreach (var row in sourceTable)
            {
                var message = new MailMessage(row["Mail-From"], row["Mail-To"])
                {
                    Subject = row["Mail-Subject"],
                    Body    = row["Mail-Body"]
                };

                if (sourceTable.ContainColumn("Mail-CC"))
                {
                    var cc = row["Mail-CC"];

                    if (!string.IsNullOrWhiteSpace(cc))
                    {
                        message.CC.Add(cc);
                    }
                }

                message.IsBodyHtml = true;

                var result = SendEMail.Execute(message);

                if (result.HasError)
                {
                    return(result);
                }
            }

            return(StandardResult <bool> .ReturnResult(true));
        }
Ejemplo n.º 4
0
        public static StandardResult <SimpleTable> Execute(SimpleTable sourceTable, string sourceKeyField, string mergeField, SimpleTable lookupTable, string lookupKeyField, string lookupDataField, bool overwrite, bool caseInsensitive = true)
        {
            if (!sourceTable.ColumnNames.Contains(sourceKeyField))
            {
                return(StandardResult <SimpleTable> .ReturnError("LookupUpdateTable() error: source key field does not exists: " + sourceKeyField));
            }

            if (!sourceTable.ColumnNames.Contains(mergeField))
            {
                return(StandardResult <SimpleTable> .ReturnError("LookupUpdateTable() error: merge field does not exists: " + mergeField));
            }

            var newTable = new SimpleTable();

            foreach (var column in sourceTable.ColumnNames)
            {
                newTable.AddColumnName(column);
            }

            foreach (var sourceRow in sourceTable)
            {
                var newRow = newTable.CreateRow();

                for (int i = 0; i < sourceRow.ColumnCount; i++)
                {
                    var value = sourceRow[i];

                    if (value == null)
                    {
                        continue;
                    }

                    newRow[i] = value;
                }
            }

            foreach (var row in newTable)
            {
                string key = row[sourceKeyField];

                if (!overwrite && !string.IsNullOrWhiteSpace(row[mergeField]))
                {
                    continue;
                }

                if (caseInsensitive)
                {
                    key = key.ToLower();
                }

                string lookup = Lookup(lookupTable, key, lookupKeyField, lookupDataField);

                row[mergeField] = lookup;
            }

            return(StandardResult <SimpleTable> .ReturnResult(newTable));
        }
Ejemplo n.º 5
0
        public static StandardResult <bool> Execute(MailMessage message)
        {
            if (!MailHelper.SendMailMessage(message, out Exception e))
            {
                return(StandardResult <bool> .ReturnError("SendEMail() error: unable to send e-mail to: " + message.To, "Reason: " + e.ToString(), e));
            }

            return(StandardResult <bool> .ReturnResult(true));
        }
Ejemplo n.º 6
0
        public static StandardResult <SimpleTable> Execute(SimpleTable sourceTable, SimpleTable expandTable)
        {
            if (sourceTable == null)
            {
                return(StandardResult <SimpleTable> .ReturnError("ExpandTable() error: source table null"));
            }
            if (expandTable == null)
            {
                return(StandardResult <SimpleTable> .ReturnError("ExpandTable() error: aggregate table null"));
            }

            var newTable = sourceTable.Copy();

            // create columns
            foreach (var map in expandTable)
            {
                var source      = map["source"];
                var destination = map["destination"];
                newTable.AddColumnName(destination);

                var expression = ExpressionCache.Compile(source);

                if (expression == null)
                {
                    return(StandardResult <SimpleTable> .ReturnError("AggregateTable() error: evaluator returns null", "Source: " + source));
                }
            }

            var fieldSource = new FieldDataSource();

            foreach (var newRow in newTable)
            {
                fieldSource.Row = newRow;

                foreach (var map in expandTable)
                {
                    var source      = map["source"];
                    var destination = map["destination"];

                    var expression = ExpressionCache.Compile(source);

                    var result = expression.Evaluate(new Context()
                    {
                        FieldSource = fieldSource
                    });

                    if (result.IsError)
                    {
                        return(StandardResult <SimpleTable> .ReturnError("ExpandTable() error: occurred during evaluating: " + expression.Parser.Tokenizer.Expression, result.String));
                    }

                    newRow[destination] = ToString(result);
                }
            }

            return(StandardResult <SimpleTable> .ReturnResult(newTable));
        }
        public static StandardResult <SimpleTable> Execute(string data, string newRow, char delimiter = '=', IList <string> columns = null)
        {
            var lines = data.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            var table = new SimpleTable();

            if (columns != null)
            {
                for (int i = 0; i < columns.Count; i++)
                {
                    table.SetColumnName(i, columns[i]);
                }
            }

            SimpleTableRow row = null;

            foreach (var line in lines)
            {
                if (string.IsNullOrEmpty(line))
                {
                    continue;
                }

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

                if (line == newRow)
                {
                    row = table.CreateRow();

                    continue;
                }

                int pos = line.IndexOf(delimiter);

                if (pos < 0)
                {
                    continue;
                }

                string name  = line.Substring(0, pos);
                string value = line.Substring(pos + 1);

                if (columns == null && !table.ContainColumn(name))
                {
                    table.SetColumnName(table.ColumnNames.Count, name);
                }

                if (table.ContainColumn(name))
                {
                    row.SetField(name, value);
                }
            }

            return(StandardResult <SimpleTable> .ReturnResult(table));
        }
Ejemplo n.º 8
0
        public static StandardResult <SimpleTable> Execute(string data, bool containsHeader = true, bool mapHeaderNames = true, bool unespace = true)
        {
            var lines = data.Split(new string[] { "\r\n" }, StringSplitOptions.None);

            var start = 0;
            var table = new SimpleTable();

            if (containsHeader)
            {
                start = 1;
                if (mapHeaderNames)
                {
                    var parts = lines[0].Split('\t');

                    for (int i = 0; i < parts.Length; i++)
                    {
                        var value = parts[i];

                        if (string.IsNullOrWhiteSpace(value))
                        {
                            continue;
                        }

                        table.SetColumnName(i, value);
                    }
                }
            }

            for (int index = start; index < lines.Length; index++)
            {
                var line = lines[index];

                if (index + 1 == lines.Length && string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }

                var parts = line.Split('\t');

                var row = table.CreateRow();

                for (int i = 0; i < parts.Length; i++)
                {
                    var part = parts[i];

                    if (unespace)
                    {
                        part = Unescape(part);
                    }

                    row.SetField(i, part);
                }
            }

            return(StandardResult <SimpleTable> .ReturnResult(table));
        }
Ejemplo n.º 9
0
        public static StandardResult <SimpleTable> Execute(string data, string destination = "name", string source = "value", char delimiter = '=')
        {
            var lines = data.Split(new string[] { "\r\n" }, StringSplitOptions.None);

            var table = new SimpleTable();

            table.SetColumnName(0, source);
            table.SetColumnName(1, destination);

            foreach (var line in lines)
            {
                if (string.IsNullOrEmpty(line))
                {
                    continue;
                }

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

                int pos = line.IndexOf(delimiter);

                if (pos < 0)
                {
                    continue;
                }

                string dst = line.Substring(0, pos);
                string src = line.Substring(pos + 1);

                var row = table.CreateRow();

                row.SetField(0, dst);
                row.SetField(1, src);
            }

            return(StandardResult <SimpleTable> .ReturnResult(table));
        }
Ejemplo n.º 10
0
        public static StandardResult <SimpleTable> Execute(SimpleTable sourceTable, SimpleTable aggregateTable)
        {
            if (sourceTable == null)
            {
                return(StandardResult <SimpleTable> .ReturnError("AggregateTable() error: source table null"));
            }
            if (aggregateTable == null)
            {
                return(StandardResult <SimpleTable> .ReturnError("AggregateTable() error: aggregate table null"));
            }

            var newTable = new SimpleTable();

            var nonaggregateFields = new Dictionary <string, Expression>();
            var aggregateFields    = new Dictionary <string, Expression>();

            // create columns
            foreach (var map in aggregateTable)
            {
                var source      = map["source"];
                var destination = map["destination"];
                newTable.AddColumnName(destination);

                var expression = ExpressionCache.Compile(source);

                if (expression == null)
                {
                    return(StandardResult <SimpleTable> .ReturnError("AggregateTable() error: evaluator returns null", "Source: " + source));
                }

                // does expression contain any aggregate methods?
                bool aggregateMethods = false;
                foreach (var token in expression.Parser.Tokenizer.Tokens)
                {
                    if (token.TokenType == TokenType.Method && aggregateMethodsNames.Contains(token.Value))
                    {
                        aggregateMethods = true;
                        break;
                    }
                }

                if (aggregateMethods)
                {
                    aggregateFields.Add(destination, expression);
                }
                else
                {
                    nonaggregateFields.Add(destination, expression);
                }
            }

            var associateBuckets = new Dictionary <SimpleTableRow, List <SimpleTableRow> >();

            // create new rows with non-aggregate expressions & track assocations
            {
                var fieldSource = new FieldDataSource();
                var context     = new Context()
                {
                    FieldSource = fieldSource
                };
                var bucketsMap = new Dictionary <List <string>, SimpleTableRow>();

                foreach (var sourceRow in sourceTable)
                {
                    fieldSource.Row = sourceRow;

                    var fields = new List <string>();

                    foreach (var field in nonaggregateFields)
                    {
                        var destination = field.Key;
                        var expression  = field.Value;

                        var result = expression.Evaluate(context);

                        if (result.IsError)
                        {
                            return(StandardResult <SimpleTable> .ReturnError("AggregateTable() error: occurred during evaluating: " + expression.Parser.Tokenizer.Expression, result.String));
                        }

                        fields.Add(ExpandTable.ToString(result));
                    }

                    var row = Find(fields, bucketsMap);

                    if (row == null)
                    {
                        row = newTable.CreateRow();
                        bucketsMap.Add(fields, row);

                        // populate non-aggregated rows
                        int index = 0;
                        foreach (var non in nonaggregateFields)
                        {
                            var destination = non.Key;
                            var expression  = non.Value;

                            var result = fields[index++];

                            row[destination] = result;
                        }
                    }

                    List <SimpleTableRow> bucketRows;
                    if (!associateBuckets.TryGetValue(row, out bucketRows))
                    {
                        bucketRows = new List <SimpleTableRow>();
                        associateBuckets.Add(row, bucketRows);
                    }

                    bucketRows.Add(sourceRow);
                }
            }

            // evaluation aggregate expressions
            {
                var fieldSource          = new FieldDataSource();
                var aggregateFieldSource = new AggregateFieldSource();
                var context = new Context()
                {
                    FieldSource = fieldSource, AggregateFieldSource = aggregateFieldSource
                };

                foreach (var aggregateRow in newTable)
                {
                    var bucketRows = associateBuckets[aggregateRow];

                    fieldSource.Row = bucketRows[0];                     // any row -- note: undefine to reference non-aggregate fields outside of the aggregate expression
                    aggregateFieldSource.TableRows = bucketRows;

                    foreach (var field in aggregateFields)
                    {
                        var destination = field.Key;
                        var expression  = field.Value;

                        var result = expression.Evaluate(context);

                        if (result.IsError)
                        {
                            return(StandardResult <SimpleTable> .ReturnError("AggregateTable() error: occurred during evaluating: " + expression.Parser.Tokenizer.Expression, result.String));
                        }

                        aggregateRow[destination] = ExpandTable.ToString(result);
                    }
                }
            }

            return(StandardResult <SimpleTable> .ReturnResult(newTable));
        }
        public static StandardResult <SimpleTable> Execute(SimpleTable sourceTable, SimpleTable mapTable)
        {
            if (mapTable == null)
            {
                return(StandardResult <SimpleTable> .ReturnError("TransformTable() error: map table null"));
            }

            var newTable = new SimpleTable();

            // create columns
            foreach (var map in mapTable)
            {
                var expression = ExpressionCache.Compile(map["source"]);

                if (expression == null)
                {
                    return(StandardResult <SimpleTable> .ReturnError("TransformTable() error: evaluator returns null"));
                }

                newTable.AddColumnName(map["destination"]);
            }

            var fieldSource = new FieldDataSource();

            foreach (var sourceRow in sourceTable)
            {
                fieldSource.Row = sourceRow;

                var destinationRow = newTable.CreateRow();

                foreach (var map in mapTable)
                {
                    var source      = map["source"];
                    var destination = map["destination"];

                    var expression = ExpressionCache.Compile(source);

                    if (expression == null)
                    {
                        return(StandardResult <SimpleTable> .ReturnError("ExpandTable() error: evaluator returns null"));
                    }

                    if (!expression.IsValid)
                    {
                        return(StandardResult <SimpleTable> .ReturnError("ExpandTable() error: occurred during evaluating: " + expression.Parser.Tokenizer.Expression));
                    }

                    var result = expression.Evaluate(new Context()
                    {
                        FieldSource = fieldSource
                    });

                    if (result.IsError)
                    {
                        return(StandardResult <SimpleTable> .ReturnError("ExpandTable() error: occurred during evaluating: " + expression.Parser.Tokenizer.Expression, result.String));
                    }

                    destinationRow[destination] = ExpandTable.ToString(result);
                }
            }

            return(StandardResult <SimpleTable> .ReturnResult(newTable));
        }
Ejemplo n.º 12
0
        public static StandardResult <SimpleTable> Execute(SimpleTable sourceTable, SimpleTable validationRules, bool rowPerMatch, bool overwrite)
        {
            var resultsTable = new SimpleTable();

            foreach (var column in sourceTable.ColumnNames)
            {
                resultsTable.AddColumnName(column);
            }

            foreach (var column in validationRules.ColumnNames)
            {
                if (column == "Match")
                {
                    continue;
                }

                resultsTable.AddColumnName(column);
            }

            var fieldSource = new FieldDataSource();

            foreach (var sourceRow in sourceTable)
            {
                fieldSource.Row = sourceRow;

                SimpleTableRow row = null;

                foreach (var rule in validationRules)
                {
                    var match = rule["Match"];

                    if (string.IsNullOrWhiteSpace(match))
                    {
                        continue;
                    }

                    var expression = ExpressionCache.Compile(match);

                    if (expression == null)
                    {
                        return(StandardResult <SimpleTable> .ReturnError("ValidateTable() error: evaluator returns null"));
                    }

                    if (!expression.IsValid)
                    {
                        return(StandardResult <SimpleTable> .ReturnError("ValidateTable() error: occurred during evaluating: " + expression.Parser.Tokenizer.Expression));
                    }

                    var result = expression.Evaluate(new Context()
                    {
                        FieldSource = fieldSource
                    });

                    if (result.IsError)
                    {
                        return(StandardResult <SimpleTable> .ReturnError("ValidateTable() error: occurred during evaluating: " + expression.Parser.Tokenizer.Expression, result.String));
                    }

                    if (!result.Boolean)
                    {
                        continue;
                    }

                    if (row == null || rowPerMatch)
                    {
                        row = resultsTable.CreateRow();
                    }

                    foreach (var column in sourceTable.ColumnNames)
                    {
                        row[column] = sourceRow[column];
                    }

                    foreach (var column in validationRules.ColumnNames)
                    {
                        if (column == "Match")
                        {
                            continue;
                        }

                        var text = rule[column];

                        if (string.IsNullOrWhiteSpace(text))
                        {
                            continue;
                        }

                        var expression2 = ExpressionCache.Compile(text);

                        if (expression2 == null)
                        {
                            return(StandardResult <SimpleTable> .ReturnError("ValidateTable() error: evaluator returns null"));
                        }

                        if (!expression2.IsValid)
                        {
                            return(StandardResult <SimpleTable> .ReturnError("ValidateTable() error: occurred during evaluating: " + expression2.Parser.Tokenizer.Expression));
                        }

                        var result2 = expression2.Evaluate(new Context()
                        {
                            FieldSource = fieldSource
                        });

                        if (result2.IsError)
                        {
                            return(StandardResult <SimpleTable> .ReturnError("ValidateTable() error: occurred during evaluating: " + expression2.Parser.Tokenizer.Expression, result2.String));
                        }

                        if (overwrite || string.IsNullOrWhiteSpace(row[column]))
                        {
                            row[column] = result2.String;
                        }
                    }
                }
            }

            return(StandardResult <SimpleTable> .ReturnResult(resultsTable));
        }
Ejemplo n.º 13
0
        public static StandardResult <SimpleTable> Execute(SimpleTable sourceTable, string template, string sendFromMail, string sendToFieldExpression, string sendCCFieldExpression, string groupExpression = null)
        {
            var templates = new List <MessageTemplate>();
            var lookup    = (groupExpression != null) ? new Dictionary <string, MessageTemplate>() : null;

            sendCCFieldExpression = string.IsNullOrWhiteSpace(sendCCFieldExpression) ? null : sendCCFieldExpression;
            groupExpression       = string.IsNullOrWhiteSpace(groupExpression) ? null : groupExpression;

            foreach (var row in sourceTable)
            {
                var sendTo = EvaulateExpression(row, sendToFieldExpression);

                if (sendTo.HasError)
                {
                    return(StandardResult <SimpleTable> .ReturnError(sendTo.ErrorMessage));                  // improve
                }
                var sendCC = EvaulateExpression(row, sendCCFieldExpression);

                if (sendCC.HasError)
                {
                    return(StandardResult <SimpleTable> .ReturnError(sendTo.ErrorMessage));                  // improve
                }
                var groupName = EvaulateExpression(row, groupExpression);

                if (groupName.HasError)
                {
                    return(StandardResult <SimpleTable> .ReturnError(groupName.ErrorMessage));                  // improve
                }
                if (lookup == null || groupName.Result == null || !lookup.TryGetValue(groupName.Result, out MessageTemplate message))
                {
                    message = new MessageTemplate(template);
                    message.SetBodyFields(row);

                    message.To = sendTo.Result;
                    message.CC = sendCC.Result;

                    templates.Add(message);

                    if (lookup != null || groupName != null)
                    {
                        lookup.Add(groupName.Result, message);
                    }
                }

                message.SetRowFields(row);
            }

            var messageTable = new SimpleTable();

            messageTable.AddColumnName("Mail-To");
            messageTable.AddColumnName("Mail-From");
            messageTable.AddColumnName("Mail-CC");
            messageTable.AddColumnName("Mail-Subject");
            messageTable.AddColumnName("Mail-Body");

            foreach (var message in templates)
            {
                var row = messageTable.CreateRow();

                row["Mail-Body"]    = message.Body;
                row["Mail-Subject"] = message.Subject;
                row["Mail-To"]      = message.To;
                row["Mail-CC"]      = message.CC;
                row["Mail-From"]    = sendFromMail;
            }

            return(StandardResult <SimpleTable> .ReturnResult(messageTable));
        }
Ejemplo n.º 14
0
        public static StandardResult <SimpleTable> Execute(SimpleTable table, string includeExpression)
        {
            if (string.IsNullOrWhiteSpace(includeExpression))
            {
                return(StandardResult <SimpleTable> .ReturnResult(table.Copy()));
            }

            var newTable = new SimpleTable();

            foreach (var column in table.ColumnNames)
            {
                newTable.AddColumnName(column);
            }

            var fieldSource = new FieldDataSource();

            var expression = ExpressionCache.Compile(includeExpression);

            foreach (var sourceRow in table)
            {
                fieldSource.Row = sourceRow;

                if (string.IsNullOrWhiteSpace(includeExpression))
                {
                    continue;
                }

                var result = expression.Evaluate(new Context()
                {
                    FieldSource = fieldSource
                });

                if (!expression.IsValid)
                {
                    return(StandardResult <SimpleTable> .ReturnError("FilterTable() error: occurred during evaluating: " + expression.Parser.Tokenizer.Expression, "invalid result: " + result.String));
                }

                if (!result.IsBoolean)
                {
                    return(StandardResult <SimpleTable> .ReturnError("FilterTable() error: occurred during evaluating: " + expression.Parser.Tokenizer.Expression, "result was not boolean: " + result.String));
                }

                if (!result.Boolean)
                {
                    continue;
                }

                var newRow = newTable.CreateRow();

                for (int i = 0; i < sourceRow.ColumnCount; i++)
                {
                    var value = sourceRow[i];

                    if (value == null)
                    {
                        continue;
                    }

                    newRow[i] = value;
                }
            }

            return(StandardResult <SimpleTable> .ReturnResult(newTable));
        }