From ab1b9d88ee104afdc7c4fb1c0a6fd93c2ced656c Mon Sep 17 00:00:00 2001 From: Peter Forstmeier Date: Wed, 17 Jun 2015 20:53:25 +0200 Subject: [PATCH] Expressions -> TimeSpan handling in grouped List's --- .../Irony/Imports/ImportAggregates.cs | 60 +++++++++++++------ .../Src/Globals/StandardFormatter.cs | 36 ++++++++++- .../Src/Globals/TypeHelper.cs | 4 +- .../Src/PageBuilder/BasePageBuilder.cs | 1 + .../Src/PageBuilder/DataPageBuilder.cs | 1 + .../AggregatesGroupesFixture.cs | 24 ++++---- .../src/ReportItems/TextItemFixture.cs | 32 +++++++++- 7 files changed, 121 insertions(+), 37 deletions(-) diff --git a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Expressions/Irony/Imports/ImportAggregates.cs b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Expressions/Irony/Imports/ImportAggregates.cs index 9322ae4356..a5f27e4e1b 100644 --- a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Expressions/Irony/Imports/ImportAggregates.cs +++ b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Expressions/Irony/Imports/ImportAggregates.cs @@ -34,8 +34,6 @@ namespace ICSharpCode.Reporting.Expressions.Irony.Imports public static IEnumerable GetDataSource (this ScriptThread thread){ return (IEnumerable)thread.App.Globals["DataSource"]; } - - } static class ImportAggregateHelper { @@ -62,35 +60,63 @@ namespace ICSharpCode.Reporting.Expressions.Irony.Imports { public static object Sum(ScriptThread thread, AstNode[] childNodes) { double sum = 0; - var fieldName = childNodes[0].Evaluate(thread).ToString(); + object firstItem = null; + bool isTimeSpan; + var fieldName = childNodes[0].Evaluate(thread).ToString(); var dataSource = thread.GetDataSource(); - var grouped = ImportAggregateHelper.IsGrouped(dataSource); if (grouped != null) { - + var firstGroupElement = dataSource.FirstOrDefault() as IGrouping; + firstItem = firstGroupElement.FirstOrDefault(); + isTimeSpan = HandleTimeSpan(firstItem,fieldName); + double groupSum = 0; foreach (var element in grouped) { - var s = element.Sum(o => { - var v = ReadValueFromObject(fieldName, o); - return TypeNormalizer.EnsureType(v); - }); - sum = sum + s; + if (isTimeSpan) { + groupSum = element.Sum(o => {return TimeSpanSum(fieldName,o);}); + } else { + groupSum = element.Sum(o => {return SimpleSum(fieldName,o);}); + } + sum = sum + groupSum; } } else { + firstItem = dataSource.FirstOrDefault(); + isTimeSpan = HandleTimeSpan(firstItem,fieldName); + if (ImportAggregateHelper.FieldExist(dataSource.FirstOrDefault(),fieldName)) { - sum = dataSource.Sum(o => { - var v = ReadValueFromObject(fieldName, o); - return TypeNormalizer.EnsureType(v); - }); + if (isTimeSpan) { + sum = dataSource.Sum(o => {return TimeSpanSum(fieldName,o); }); + } else { + sum = dataSource.Sum(o => {return SimpleSum(fieldName,o); }); + } } } return sum.ToString(); } - - static object ReadValueFromObject(string fieldName, object currentObject) - { + + static double SimpleSum(string fromField,object current){ + var value = ReadValueFromObject(fromField, current); + return TypeNormalizer.EnsureType(value); + } + + + static double TimeSpanSum(string fromField,object current){ + var value = ReadValueFromObject(fromField, current); + var timeSpan = (TimeSpan)value; + return TypeNormalizer.EnsureType(timeSpan.Ticks); + } + + + static bool HandleTimeSpan(object firstItem,string fieldName){ + var t = ReadValueFromObject(fieldName, firstItem); + var timeSpan = t is TimeSpan; + return t is TimeSpan; + } + + + static object ReadValueFromObject(string fieldName, object currentObject){ var propertyPath = currentObject.ParsePropertyPath(fieldName); var evaluated = propertyPath.Evaluate(currentObject); return evaluated; diff --git a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Globals/StandardFormatter.cs b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Globals/StandardFormatter.cs index 24584d6e6c..63a391e239 100644 --- a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Globals/StandardFormatter.cs +++ b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Globals/StandardFormatter.cs @@ -25,19 +25,49 @@ namespace ICSharpCode.Reporting.Globals /// /// Description of StandardFormatter. /// - static class StandardFormatter + public static class StandardFormatter { public static void FormatOutput (IExportText textColumn) { if (String.IsNullOrWhiteSpace(textColumn.Text)) { return; } if (!String.IsNullOrEmpty(textColumn.FormatString)) { - TypeCode typeCode = TypeHelper.TypeCodeFromString(textColumn.DataType); - textColumn.Text = FormatItem(textColumn.Text,textColumn.FormatString,typeCode); + if (textColumn.DataType.ToLower().Contains("timespan")) { + textColumn.Text = HandleTimeSpan(textColumn.Text,textColumn.FormatString); + + } else { + TypeCode typeCode = TypeHelper.TypeCodeFromString(textColumn.DataType); + textColumn.Text = FormatItem(textColumn.Text,textColumn.FormatString,typeCode); + } } } + static string HandleTimeSpan (string valueToFormat,string toFormat) { + TimeSpan time; + + bool valid = TimeSpan.TryParseExact(valueToFormat, + "c", + CultureInfo.CurrentCulture, + out time); + if (! valid) { + var test = TimeSpan.FromTicks(Convert.ToInt64(valueToFormat)); + + if (test != null) { + valid = true; + time = test; + } else{ + var x = TimeSpan.Parse(valueToFormat.ToString(),CultureInfo.CurrentCulture); + } + } + + if (valid) { + return time.ToString("g",DateTimeFormatInfo.CurrentInfo); + } + return toFormat; + } + + static string FormatItem (string valueToFormat,string format,TypeCode typeCode) { string retValue = String.Empty; diff --git a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Globals/TypeHelper.cs b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Globals/TypeHelper.cs index 7931164b3e..de32f50f08 100644 --- a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Globals/TypeHelper.cs +++ b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Globals/TypeHelper.cs @@ -76,9 +76,7 @@ namespace ICSharpCode.Reporting.Globals public static TypeCode TypeCodeFromString (string type) { if (type == null) type = "System.String"; - var x = Type.GetTypeCode( Type.GetType(type)); - - return x; + return Type.GetTypeCode( Type.GetType(type)); } } } diff --git a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/PageBuilder/BasePageBuilder.cs b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/PageBuilder/BasePageBuilder.cs index 0df582b021..784c15996a 100644 --- a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/PageBuilder/BasePageBuilder.cs +++ b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/PageBuilder/BasePageBuilder.cs @@ -91,6 +91,7 @@ namespace ICSharpCode.Reporting.PageBuilder protected void BuildReportFooter(){ + var lastSection = CurrentPage.ExportedItems.Last(); CurrentLocation = new Point(ReportModel.ReportSettings.LeftMargin, lastSection.Location.Y - ReportModel.ReportFooter.Size.Height - 2); diff --git a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/PageBuilder/DataPageBuilder.cs b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/PageBuilder/DataPageBuilder.cs index 8bcfb9f906..66eacf6fa5 100644 --- a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/PageBuilder/DataPageBuilder.cs +++ b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/PageBuilder/DataPageBuilder.cs @@ -48,6 +48,7 @@ namespace ICSharpCode.Reporting.PageBuilder SetupExpressionRunner(ReportModel.ReportSettings,DataSource); base.BuildExportList(); BuildDetail(); + ExpressionRunner.Visitor.SetCurrentDataSource(DataSource.CurrentList); BuildReportFooter(); AddPage(CurrentPage); UpdatePageInfo(); diff --git a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/Expressions/IntegrationTests/AggregatesGroupesFixture.cs b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/Expressions/IntegrationTests/AggregatesGroupesFixture.cs index 247e6b43e0..f3e999156d 100644 --- a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/Expressions/IntegrationTests/AggregatesGroupesFixture.cs +++ b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/Expressions/IntegrationTests/AggregatesGroupesFixture.cs @@ -33,7 +33,7 @@ namespace ICSharpCode.Reporting.Test.Expressions.IntegrationTests [TestFixture] public class AggregatesGroupesFixture { - Collection collection; + Collection reportItemCollection; CollectionDataSource dataSource; ContributorCollection list; ReportSettings reportSettings; @@ -44,10 +44,10 @@ namespace ICSharpCode.Reporting.Test.Expressions.IntegrationTests var visitor = new ExpressionVisitor (reportSettings); visitor.SetCurrentDataSource(dataSource.GroupedList); var script = "= sum('randomint')"; - collection[0].Text = script; - visitor.Visit(collection[0]); + reportItemCollection[0].Text = script; + visitor.Visit(reportItemCollection[0]); var result = list.Sum(x => x.RandomInt); - Assert.That(Convert.ToDouble(collection[0].Text),Is.EqualTo(result)); + Assert.That(Convert.ToDouble(reportItemCollection[0].Text),Is.EqualTo(result)); } @@ -57,8 +57,8 @@ namespace ICSharpCode.Reporting.Test.Expressions.IntegrationTests var container = new ExportContainer(); var script = "= sum('randomint')"; - collection[0].Text = script; - container.ExportedItems.AddRange(collection); + reportItemCollection[0].Text = script; + container.ExportedItems.AddRange(reportItemCollection); var visitor = new ExpressionVisitor (reportSettings); visitor.SetCurrentDataSource(dataSource.GroupedList); @@ -68,33 +68,33 @@ namespace ICSharpCode.Reporting.Test.Expressions.IntegrationTests visitor.Visit(container); var result = list.Where(k => k.GroupItem == group.Key.ToString()).Sum(x => x.RandomInt); - Assert.That(Convert.ToDouble(collection[0].Text),Is.EqualTo(result)); + Assert.That(Convert.ToDouble(reportItemCollection[0].Text),Is.EqualTo(result)); } [Test] public void SumAllGroups () { var container = new ExportContainer(); - container.ExportedItems.AddRange(collection); + container.ExportedItems.AddRange(reportItemCollection); var visitor = new ExpressionVisitor (reportSettings); visitor.SetCurrentDataSource(dataSource.GroupedList); foreach (var group in dataSource.GroupedList) { var script = "= sum('randomint')"; - collection[0].Text = script; + reportItemCollection[0].Text = script; visitor.SetCurrentDataSource(group); visitor.Visit(container); var result = list.Where(k => k.GroupItem == group.Key.ToString()).Sum(x => x.RandomInt); - Assert.That(Convert.ToDouble(collection[0].Text),Is.EqualTo(result)); + Assert.That(Convert.ToDouble(reportItemCollection[0].Text),Is.EqualTo(result)); } } [SetUp] public void CreateExportlist() { - collection = new Collection(); - collection.Add(new ExportText() + reportItemCollection = new Collection(); + reportItemCollection.Add(new ExportText() { Text = String.Empty }); diff --git a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/ReportItems/TextItemFixture.cs b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/ReportItems/TextItemFixture.cs index 3b0b1ab782..79b4279dff 100644 --- a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/ReportItems/TextItemFixture.cs +++ b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/ReportItems/TextItemFixture.cs @@ -58,6 +58,34 @@ namespace ICSharpCode.Reporting.Test.ReportItems Assert.That(exportText.Size,Is.EqualTo(ti.Size)); Assert.That(exportText.Font , Is.EqualTo(GlobalValues.DefaultFont)); } - - } + + + [Test] + public void FormatTimeSpanfromTime() { + var ti = new BaseTextItem(); + ti.DataType = "System.TimeSpan"; + ti.FormatString = "hh:mm:ss"; + ti.Text = new TimeSpan(7,17,20).ToString(); + var exportColumn = (ExportText)ti.CreateExportColumn(); + StandardFormatter.FormatOutput(exportColumn); + Assert.That(ti.Text, Is.EqualTo("07:17:20")); + } + + /* + [Test] + public void FormatTimeSpanFromTicks() { + var ti = new BaseTextItem(); + ti.DataType = "System.TimeSpan"; + ti.FormatString = "hh:mm:ss"; + var x = new TimeSpan(7,17,20); + var y = x.Ticks; +// ti.Text = x.ToString(); +// TimeSpan myts = new TimeSpan(-555234423213113); +// ti.Text = myts.ToString(); + ti.Text = y.ToString(); + var exportColumn = (ExportText)ti.CreateExportColumn(); + StandardFormatter.FormatOutput(exportColumn); + } + */ + } }