Browse Source

Expressions -> TimeSpan handling in grouped List's

pull/637/merge
Peter Forstmeier 11 years ago
parent
commit
ab1b9d88ee
  1. 58
      src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Expressions/Irony/Imports/ImportAggregates.cs
  2. 32
      src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Globals/StandardFormatter.cs
  3. 4
      src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Globals/TypeHelper.cs
  4. 1
      src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/PageBuilder/BasePageBuilder.cs
  5. 1
      src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/PageBuilder/DataPageBuilder.cs
  6. 24
      src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/Expressions/IntegrationTests/AggregatesGroupesFixture.cs
  7. 28
      src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/ReportItems/TextItemFixture.cs

58
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<object> GetDataSource (this ScriptThread thread){ public static IEnumerable<object> GetDataSource (this ScriptThread thread){
return (IEnumerable<object>)thread.App.Globals["DataSource"]; return (IEnumerable<object>)thread.App.Globals["DataSource"];
} }
} }
static class ImportAggregateHelper { static class ImportAggregateHelper {
@ -62,35 +60,63 @@ namespace ICSharpCode.Reporting.Expressions.Irony.Imports
{ {
public static object Sum(ScriptThread thread, AstNode[] childNodes) { public static object Sum(ScriptThread thread, AstNode[] childNodes) {
double sum = 0; 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 dataSource = thread.GetDataSource();
var grouped = ImportAggregateHelper.IsGrouped(dataSource); var grouped = ImportAggregateHelper.IsGrouped(dataSource);
if (grouped != null) { if (grouped != null) {
var firstGroupElement = dataSource.FirstOrDefault() as IGrouping<object, object>;
firstItem = firstGroupElement.FirstOrDefault();
isTimeSpan = HandleTimeSpan(firstItem,fieldName);
double groupSum = 0;
foreach (var element in grouped) { foreach (var element in grouped) {
var s = element.Sum(o => { if (isTimeSpan) {
var v = ReadValueFromObject(fieldName, o); groupSum = element.Sum(o => {return TimeSpanSum(fieldName,o);});
return TypeNormalizer.EnsureType<double>(v); } else {
}); groupSum = element.Sum(o => {return SimpleSum(fieldName,o);});
sum = sum + s; }
sum = sum + groupSum;
} }
} else { } else {
firstItem = dataSource.FirstOrDefault();
isTimeSpan = HandleTimeSpan(firstItem,fieldName);
if (ImportAggregateHelper.FieldExist(dataSource.FirstOrDefault(),fieldName)) { if (ImportAggregateHelper.FieldExist(dataSource.FirstOrDefault(),fieldName)) {
sum = dataSource.Sum(o => { if (isTimeSpan) {
var v = ReadValueFromObject(fieldName, o); sum = dataSource.Sum(o => {return TimeSpanSum(fieldName,o); });
return TypeNormalizer.EnsureType<double>(v); } else {
}); sum = dataSource.Sum(o => {return SimpleSum(fieldName,o); });
}
} }
} }
return sum.ToString(); 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<double>(value);
}
static double TimeSpanSum(string fromField,object current){
var value = ReadValueFromObject(fromField, current);
var timeSpan = (TimeSpan)value;
return TypeNormalizer.EnsureType<double>(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 propertyPath = currentObject.ParsePropertyPath(fieldName);
var evaluated = propertyPath.Evaluate(currentObject); var evaluated = propertyPath.Evaluate(currentObject);
return evaluated; return evaluated;

32
src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Globals/StandardFormatter.cs

@ -25,17 +25,47 @@ namespace ICSharpCode.Reporting.Globals
/// <summary> /// <summary>
/// Description of StandardFormatter. /// Description of StandardFormatter.
/// </summary> /// </summary>
static class StandardFormatter public static class StandardFormatter
{ {
public static void FormatOutput (IExportText textColumn) { public static void FormatOutput (IExportText textColumn) {
if (String.IsNullOrWhiteSpace(textColumn.Text)) { if (String.IsNullOrWhiteSpace(textColumn.Text)) {
return; return;
} }
if (!String.IsNullOrEmpty(textColumn.FormatString)) { if (!String.IsNullOrEmpty(textColumn.FormatString)) {
if (textColumn.DataType.ToLower().Contains("timespan")) {
textColumn.Text = HandleTimeSpan(textColumn.Text,textColumn.FormatString);
} else {
TypeCode typeCode = TypeHelper.TypeCodeFromString(textColumn.DataType); TypeCode typeCode = TypeHelper.TypeCodeFromString(textColumn.DataType);
textColumn.Text = FormatItem(textColumn.Text,textColumn.FormatString,typeCode); 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) { static string FormatItem (string valueToFormat,string format,TypeCode typeCode) {

4
src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Globals/TypeHelper.cs

@ -76,9 +76,7 @@ namespace ICSharpCode.Reporting.Globals
public static TypeCode TypeCodeFromString (string type) { public static TypeCode TypeCodeFromString (string type) {
if (type == null) if (type == null)
type = "System.String"; type = "System.String";
var x = Type.GetTypeCode( Type.GetType(type)); return Type.GetTypeCode( Type.GetType(type));
return x;
} }
} }
} }

1
src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/PageBuilder/BasePageBuilder.cs

@ -91,6 +91,7 @@ namespace ICSharpCode.Reporting.PageBuilder
protected void BuildReportFooter(){ protected void BuildReportFooter(){
var lastSection = CurrentPage.ExportedItems.Last(); var lastSection = CurrentPage.ExportedItems.Last();
CurrentLocation = new Point(ReportModel.ReportSettings.LeftMargin, CurrentLocation = new Point(ReportModel.ReportSettings.LeftMargin,
lastSection.Location.Y - ReportModel.ReportFooter.Size.Height - 2); lastSection.Location.Y - ReportModel.ReportFooter.Size.Height - 2);

1
src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/PageBuilder/DataPageBuilder.cs

@ -48,6 +48,7 @@ namespace ICSharpCode.Reporting.PageBuilder
SetupExpressionRunner(ReportModel.ReportSettings,DataSource); SetupExpressionRunner(ReportModel.ReportSettings,DataSource);
base.BuildExportList(); base.BuildExportList();
BuildDetail(); BuildDetail();
ExpressionRunner.Visitor.SetCurrentDataSource(DataSource.CurrentList);
BuildReportFooter(); BuildReportFooter();
AddPage(CurrentPage); AddPage(CurrentPage);
UpdatePageInfo(); UpdatePageInfo();

24
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] [TestFixture]
public class AggregatesGroupesFixture public class AggregatesGroupesFixture
{ {
Collection<ExportText> collection; Collection<ExportText> reportItemCollection;
CollectionDataSource dataSource; CollectionDataSource dataSource;
ContributorCollection list; ContributorCollection list;
ReportSettings reportSettings; ReportSettings reportSettings;
@ -44,10 +44,10 @@ namespace ICSharpCode.Reporting.Test.Expressions.IntegrationTests
var visitor = new ExpressionVisitor (reportSettings); var visitor = new ExpressionVisitor (reportSettings);
visitor.SetCurrentDataSource(dataSource.GroupedList); visitor.SetCurrentDataSource(dataSource.GroupedList);
var script = "= sum('randomint')"; var script = "= sum('randomint')";
collection[0].Text = script; reportItemCollection[0].Text = script;
visitor.Visit(collection[0]); visitor.Visit(reportItemCollection[0]);
var result = list.Sum(x => x.RandomInt); 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 container = new ExportContainer();
var script = "= sum('randomint')"; var script = "= sum('randomint')";
collection[0].Text = script; reportItemCollection[0].Text = script;
container.ExportedItems.AddRange(collection); container.ExportedItems.AddRange(reportItemCollection);
var visitor = new ExpressionVisitor (reportSettings); var visitor = new ExpressionVisitor (reportSettings);
visitor.SetCurrentDataSource(dataSource.GroupedList); visitor.SetCurrentDataSource(dataSource.GroupedList);
@ -68,33 +68,33 @@ namespace ICSharpCode.Reporting.Test.Expressions.IntegrationTests
visitor.Visit(container); visitor.Visit(container);
var result = list.Where(k => k.GroupItem == group.Key.ToString()).Sum(x => x.RandomInt); 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] [Test]
public void SumAllGroups () { public void SumAllGroups () {
var container = new ExportContainer(); var container = new ExportContainer();
container.ExportedItems.AddRange(collection); container.ExportedItems.AddRange(reportItemCollection);
var visitor = new ExpressionVisitor (reportSettings); var visitor = new ExpressionVisitor (reportSettings);
visitor.SetCurrentDataSource(dataSource.GroupedList); visitor.SetCurrentDataSource(dataSource.GroupedList);
foreach (var group in dataSource.GroupedList) { foreach (var group in dataSource.GroupedList) {
var script = "= sum('randomint')"; var script = "= sum('randomint')";
collection[0].Text = script; reportItemCollection[0].Text = script;
visitor.SetCurrentDataSource(group); visitor.SetCurrentDataSource(group);
visitor.Visit(container); visitor.Visit(container);
var result = list.Where(k => k.GroupItem == group.Key.ToString()).Sum(x => x.RandomInt); 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] [SetUp]
public void CreateExportlist() { public void CreateExportlist() {
collection = new Collection<ExportText>(); reportItemCollection = new Collection<ExportText>();
collection.Add(new ExportText() reportItemCollection.Add(new ExportText()
{ {
Text = String.Empty Text = String.Empty
}); });

28
src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/ReportItems/TextItemFixture.cs

@ -59,5 +59,33 @@ namespace ICSharpCode.Reporting.Test.ReportItems
Assert.That(exportText.Font , Is.EqualTo(GlobalValues.DefaultFont)); 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);
}
*/
} }
} }

Loading…
Cancel
Save