Browse Source

Aggregates in grouped DataSource

reports
Peter Forstmeier 12 years ago
parent
commit
108e409736
  1. 2
      src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Exporter/Visitors/ExpressionVisitor.cs
  2. 2
      src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Expressions/ExpressionRunner.cs
  3. 3
      src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Expressions/Irony/Ast/AstExtensions.cs
  4. 70
      src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Expressions/Irony/Imports/ImportAggregates.cs
  5. 4
      src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/Expressions/Aggregates/SumAggregate.cs
  6. 15
      src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/Expressions/IntegrationTests/AggregatesGroupesFixture.cs
  7. 2
      src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/MeasureArrange/ContainerArrangeStrategyFixture.cs

2
src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Exporter/Visitors/ExpressionVisitor.cs

@ -19,7 +19,7 @@ namespace ICSharpCode.Reporting.Exporter.Visitors @@ -19,7 +19,7 @@ namespace ICSharpCode.Reporting.Exporter.Visitors
readonly ReportingExpressionEvaluator evaluator;
public ExpressionVisitor(ReportSettings reportSettings,CollectionDataSource dataSource) {
public ExpressionVisitor(ReportSettings reportSettings,IEnumerable<object> dataSource) {
grammar = new ReportingLanguageGrammer();
evaluator = new ReportingExpressionEvaluator(grammar);
evaluator.AddReportSettings(reportSettings);

2
src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Expressions/ExpressionRunner.cs

@ -36,7 +36,7 @@ namespace ICSharpCode.Reporting.Expressions @@ -36,7 +36,7 @@ namespace ICSharpCode.Reporting.Expressions
Console.WriteLine();
Console.WriteLine("Start ExpressionVisitor");
var visitor = new ExpressionVisitor (reportSettings,dataSource);
var visitor = new ExpressionVisitor (reportSettings,dataSource.SortedList);
visitor.Evaluator.Globals.Add("DataSource",dataSource);
visitor.Run(pages);

3
src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Expressions/Irony/Ast/AstExtensions.cs

@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using ICSharpCode.Reporting.DataManager.Listhandling;
using ICSharpCode.Reporting.Interfaces.Export;
using ICSharpCode.Reporting.Items;
@ -69,7 +70,7 @@ namespace ICSharpCode.Reporting.Expressions.Irony.Ast @@ -69,7 +70,7 @@ namespace ICSharpCode.Reporting.Expressions.Irony.Ast
#region DataSource
public static void AddDataSource (this ReportingExpressionEvaluator app,CollectionDataSource dataSource){
public static void AddDataSource (this ReportingExpressionEvaluator app,IEnumerable<object> dataSource){
if (dataSource == null)
throw new ArgumentNullException("dataSource");
if (!app.Globals.ContainsKey("DataSource")) {

70
src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Expressions/Irony/Imports/ImportAggregates.cs

@ -1,10 +1,10 @@ @@ -1,10 +1,10 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Linq;
using ICSharpCode.Reporting.DataManager.Listhandling;
using ICSharpCode.Reporting.DataSource;
using ICSharpCode.Reporting.Interfaces.Data;
using Irony.Interpreter;
using Irony.Interpreter.Ast;
@ -15,11 +15,34 @@ namespace ICSharpCode.Reporting.Expressions.Irony.Imports @@ -15,11 +15,34 @@ namespace ICSharpCode.Reporting.Expressions.Irony.Imports
/// </summary>
public static class ImportExtensions
{
public static IDataSource GetDataSource (this ScriptThread thread){
return (IDataSource)thread.App.Globals["DataSource"];
public static IEnumerable<object> GetDataSource (this ScriptThread thread){
return (IEnumerable<object>)thread.App.Globals["DataSource"];
}
}
static class ImportAggregateHelper {
public static bool FieldExist (object current,string fieldName) {
var property1 = current.ParsePropertyPath(fieldName);
if (property1 == null) {
Console.WriteLine(String.Format("Aggregate <Sum> Field '{0}' not found",fieldName));
return false;
}
return true;
}
public static IEnumerable<IGrouping<object, object>> IsGrouped (IEnumerable<object> listToConvert) {
var grouped = listToConvert as IEnumerable<IGrouping<object, object>>;
if (grouped != null) {
return grouped;
}
return null;
}
}
public static class ImportAggregates
{
public static object Sum(ScriptThread thread, AstNode[] childNodes) {
@ -27,26 +50,35 @@ namespace ICSharpCode.Reporting.Expressions.Irony.Imports @@ -27,26 +50,35 @@ namespace ICSharpCode.Reporting.Expressions.Irony.Imports
var fieldName = childNodes[0].Evaluate(thread).ToString();
var dataSource = thread.GetDataSource();
if (FieldExist(dataSource.CurrentList[0],fieldName)) {
var grouped = ImportAggregateHelper.IsGrouped(dataSource);
if (grouped != null) {
sum = dataSource.CurrentList.Sum(o => {
var propertyPath = o.ParsePropertyPath(fieldName);
var val = propertyPath.Evaluate(o);
return TypeNormalizer.EnsureType<double>(val);
});
foreach (var element in grouped) {
var s = element.Sum(o => {
var v = ReadValueFromObject(fieldName, o);
return TypeNormalizer.EnsureType<double>(v);
});
sum = sum + s;
}
} else {
if (ImportAggregateHelper.FieldExist(dataSource.FirstOrDefault(),fieldName)) {
sum = dataSource.Sum(o => {
var v = ReadValueFromObject(fieldName, o);
return TypeNormalizer.EnsureType<double>(v);
});
}
}
return sum;
}
static bool FieldExist (object current,string fieldName) {
var property1 = current.ParsePropertyPath(fieldName);
if (property1 == null) {
Console.WriteLine(String.Format("Aggregate <Sum> Field '{0}' not found",fieldName));
return false;
}
return true;
static object ReadValueFromObject(string fieldName, object currentObject)
{
var propertyPath = currentObject.ParsePropertyPath(fieldName);
var evaluated = propertyPath.Evaluate(currentObject);
return evaluated;
}
}
}

4
src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/Expressions/Aggregates/SumAggregate.cs

@ -24,7 +24,7 @@ namespace ICSharpCode.Reporting.Test.Expressions.Aggregates @@ -24,7 +24,7 @@ namespace ICSharpCode.Reporting.Test.Expressions.Aggregates
public void CanSum_Int_WholeCollection()
{
var reportSettings = new ReportSettings();
var visitor = new ExpressionVisitor(reportSettings,dataSource);
var visitor = new ExpressionVisitor(reportSettings,dataSource.SortedList);
var script = "= sum('intValue')";
collection[0].Text = script;
visitor.Visit(collection[0]);
@ -37,7 +37,7 @@ namespace ICSharpCode.Reporting.Test.Expressions.Aggregates @@ -37,7 +37,7 @@ namespace ICSharpCode.Reporting.Test.Expressions.Aggregates
public void CanSum_Double_WholeCollection()
{
var reportSettings = new ReportSettings();
var visitor = new ExpressionVisitor(reportSettings,dataSource);
var visitor = new ExpressionVisitor(reportSettings,dataSource.SortedList);
var script = "= sum('doubleValue')";
collection[0].Text = script;
visitor.Visit(collection[0]);

15
src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/Expressions/IntegrationTests/AggregatesGroupesFixture.cs

@ -26,17 +26,12 @@ namespace ICSharpCode.Reporting.Test.Expressions.IntegrationTests @@ -26,17 +26,12 @@ namespace ICSharpCode.Reporting.Test.Expressions.IntegrationTests
[Test]
public void TestMethod()
{
visitor = new ExpressionVisitor(reportSettings,null);
var script = "= sum('doubleValue')";
visitor = new ExpressionVisitor(reportSettings,dataSource.GroupedList);
var script = "= sum('randomint')";
collection[0].Text = script;
foreach (IGrouping<object, object> grouping in dataSource.GroupedList) {
Console.WriteLine("key {0}",grouping.Key);
//Childs
foreach (var child in grouping) {
var c = (Contributor)child;
Console.WriteLine("\tContributor {0}, {1}",c.Firstname,c.Lastname);
}
}
visitor.Visit(collection[0]);
var result = list.Sum(x => x.RandomInt);
Assert.That(Convert.ToDouble(collection[0].Text),Is.EqualTo(result));
}

2
src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/MeasureArrange/ContainerArrangeStrategyFixture.cs

@ -85,7 +85,7 @@ namespace ICSharpCode.Reporting.Test.MeasureArrange @@ -85,7 +85,7 @@ namespace ICSharpCode.Reporting.Test.MeasureArrange
var arrangedRect = CreateItemRectangle(container);
Assert.That(containerRect.Bottom,Is.EqualTo(arrangedRect.Bottom));
Assert.That(containerRect.Bottom,Is.EqualTo(arrangedRect.Bottom + 5));
}

Loading…
Cancel
Save