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 @@ -34,8 +34,6 @@ namespace ICSharpCode.Reporting.Expressions.Irony.Imports
public static IEnumerable<object> GetDataSource (this ScriptThread thread){
return (IEnumerable<object>)thread.App.Globals["DataSource"];
}
}
static class ImportAggregateHelper {
@ -62,35 +60,63 @@ namespace ICSharpCode.Reporting.Expressions.Irony.Imports @@ -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<object, object>;
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<double>(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<double>(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<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 evaluated = propertyPath.Evaluate(currentObject);
return evaluated;

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

@ -25,17 +25,47 @@ namespace ICSharpCode.Reporting.Globals @@ -25,17 +25,47 @@ namespace ICSharpCode.Reporting.Globals
/// <summary>
/// Description of StandardFormatter.
/// </summary>
static class StandardFormatter
public static class StandardFormatter
{
public static void FormatOutput (IExportText textColumn) {
if (String.IsNullOrWhiteSpace(textColumn.Text)) {
return;
}
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);
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) {

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

@ -76,9 +76,7 @@ namespace ICSharpCode.Reporting.Globals @@ -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));
}
}
}

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

@ -91,6 +91,7 @@ namespace ICSharpCode.Reporting.PageBuilder @@ -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);

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

@ -48,6 +48,7 @@ namespace ICSharpCode.Reporting.PageBuilder @@ -48,6 +48,7 @@ namespace ICSharpCode.Reporting.PageBuilder
SetupExpressionRunner(ReportModel.ReportSettings,DataSource);
base.BuildExportList();
BuildDetail();
ExpressionRunner.Visitor.SetCurrentDataSource(DataSource.CurrentList);
BuildReportFooter();
AddPage(CurrentPage);
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 @@ -33,7 +33,7 @@ namespace ICSharpCode.Reporting.Test.Expressions.IntegrationTests
[TestFixture]
public class AggregatesGroupesFixture
{
Collection<ExportText> collection;
Collection<ExportText> reportItemCollection;
CollectionDataSource dataSource;
ContributorCollection list;
ReportSettings reportSettings;
@ -44,10 +44,10 @@ namespace ICSharpCode.Reporting.Test.Expressions.IntegrationTests @@ -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 @@ -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 @@ -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<ExportText>();
collection.Add(new ExportText()
reportItemCollection = new Collection<ExportText>();
reportItemCollection.Add(new ExportText()
{
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 @@ -59,5 +59,33 @@ namespace ICSharpCode.Reporting.Test.ReportItems
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