Master-Detail problem

Stimulsoft Reports.Flex discussion
Locked
hugo
Posts: 359
Joined: Fri Mar 08, 2013 10:46 am

Master-Detail problem

Post by hugo »

I tried to implement my first Master-Detail report but the details are repeat thru all groups.

Sample code to reproduce the problem:
var table1:DataTable = new DataTable("Customer");
table1.columns.add(new DataColumn("id", StorageType.IntType));
table1.columns.add(new DataColumn("groupid", StorageType.IntType));
table1.columns.add(new DataColumn("name", StorageType.StringType));
var row1:DataRow = table1.addNewRow();
row1.setValue("id", 1);
row1.setValue("groupid", 1);
row1.setValue("name", "Hugo");
var row2:DataRow = table1.addNewRow();
row2.setValue("id", 2);
row2.setValue("groupid", 2);
row2.setValue("name", "Afonso");

var table2:DataTable = new DataTable("Group");
table2.columns.add(new DataColumn("id", StorageType.IntType));
table2.columns.add(new DataColumn("name", StorageType.StringType));
var row3:DataRow = table2.addNewRow();
row3.setValue("id", 1);
row3.setValue("name", "G1");
var row4:DataRow = table2.addNewRow();
row4.setValue("id", 2);
row4.setValue("name", "G2");

var report:StiReport = new StiReport();
report.loadReportFromString(reportString);
report.regDataTable("Customer", "Customer", table1);
report.regDataTable("Group", "Group", table2);
report.dictionary.synchronize();
report.show();

Report file:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<StiSerializer application="StiReport" type="Flex" version="1.02">
<Dictionary Ref="1" isKey="true" type="Dictionary">
<Databases isList="true" count="0"/>
<DataSources isList="true" count="2">
<Customer Ref="2" isKey="true" type="DataTableSource">
<Alias>Customer</Alias>
<Columns isList="true" count="3">
<value>name,System.String</value>
<value>groupid,System.Int32</value>
<value>id,System.Int32</value>
</Columns>
<Dictionary isRef="1"/>
<Name>Customer</Name>
<NameInSource>DataSetName.Customer</NameInSource>
</Customer>
<Group Ref="3" isKey="true" type="DataTableSource">
<Alias>Group</Alias>
<Columns isList="true" count="2">
<value>id,System.Int32</value>
<value>name,System.String</value>
</Columns>
<Dictionary isRef="1"/>
<Name>Group</Name>
<NameInSource>DataSetName</NameInSource>
</Group>
</DataSources>
<Relations isList="true" count="1">
<Group Ref="4" isKey="true" type="DataRelation">
<Alias>Group</Alias>
<ChildColumns isList="true" count="1">
<value>groupid</value>
</ChildColumns>
<ChildSource isRef="2"/>
<Dictionary isRef="1"/>
<Name>Group</Name>
<NameInSource>GroupCustomer</NameInSource>
<ParentColumns isList="true" count="1">
<value>id</value>
</ParentColumns>
<ParentSource isRef="3"/>
</Group>
</Relations>
<Report isRef="0"/>
<Variables isList="true" count="0"/>
</Dictionary>
<EngineVersion>EngineV2</EngineVersion>
<GlobalizationStrings isList="true" count="0"/>
<MetaTags isList="true" count="0"/>
<Pages isList="true" count="1">
<Page1 Ref="5" isKey="true" type="Page">
<Border>None;Black;2;Solid;False;4;Black</Border>
<Brush>Transparent</Brush>
<Components isList="true" count="3">
<PageHeaderBand1 Ref="6" isKey="true" type="PageHeaderBand">
<Brush>Transparent</Brush>
<ClientRectangle>0,0.4,19,0.8</ClientRectangle>
<Components isList="true" count="1">
<Text4 Ref="7" isKey="true" type="Text">
<Brush>Transparent</Brush>
<ClientRectangle>0.6,0.2,1.6,0.6</ClientRectangle>
<Conditions isList="true" count="0"/>
<Font>Arial,10</Font>
<Margins>0,0,0,0</Margins>
<Name>Text4</Name>
<Page isRef="5"/>
<Parent isRef="6"/>
<Text>Text4</Text>
<TextBrush>Black</TextBrush>
</Text4>
</Components>
<Conditions isList="true" count="0"/>
<Name>PageHeaderBand1</Name>
<Page isRef="5"/>
<Parent isRef="5"/>
</PageHeaderBand1>
<DataBand2 Ref="8" isKey="true" type="DataBand">
<Brush>Transparent</Brush>
<ClientRectangle>0,2,19,0.8</ClientRectangle>
<Components isList="true" count="2">
<Text1 Ref="9" isKey="true" type="Text">
<Brush>Transparent</Brush>
<ClientRectangle>3.4,0.2,1.6,0.6</ClientRectangle>
<Conditions isList="true" count="0"/>
<Font>Arial,10</Font>
<Margins>0,0,0,0</Margins>
<Name>Text1</Name>
<Page isRef="5"/>
<Parent isRef="8"/>
<Text>{Group.name}</Text>
<TextBrush>Black</TextBrush>
</Text1>
<Text2 Ref="10" isKey="true" type="Text">
<Brush>Transparent</Brush>
<ClientRectangle>0.6,0.2,2.8,0.6</ClientRectangle>
<Conditions isList="true" count="0"/>
<Font>Arial,10</Font>
<Margins>0,0,0,0</Margins>
<Name>Text2</Name>
<Page isRef="5"/>
<Parent isRef="8"/>
<Text>Grupo:</Text>
<TextBrush>Black</TextBrush>
<Type>Expression</Type>
</Text2>
</Components>
<Conditions isList="true" count="0"/>
<DataSourceName>Group</DataSourceName>
<Filters isList="true" count="0"/>
<KeepDetailsTogether>True</KeepDetailsTogether>
<Name>DataBand2</Name>
<Page isRef="5"/>
<Parent isRef="5"/>
<Sort isList="true" count="0"/>
</DataBand2>
<DataBand1 Ref="11" isKey="true" type="DataBand">
<Brush>Transparent</Brush>
<ClientRectangle>0,3.6,19,0.8</ClientRectangle>
<Components isList="true" count="4">
<Text3 Ref="12" isKey="true" type="Text">
<Brush>Transparent</Brush>
<ClientRectangle>3.8,0.2,1.6,0.6</ClientRectangle>
<Conditions isList="true" count="0"/>
<Font>Arial,10</Font>
<Margins>0,0,0,0</Margins>
<Name>Text3</Name>
<Page isRef="5"/>
<Parent isRef="11"/>
<Text>{Customer.name}</Text>
<TextBrush>Black</TextBrush>
</Text3>
<Text5 Ref="13" isKey="true" type="Text">
<Brush>Transparent</Brush>
<ClientRectangle>1,0.2,2.8,0.6</ClientRectangle>
<Conditions isList="true" count="0"/>
<Font>Arial,10</Font>
<Margins>0,0,0,0</Margins>
<Name>Text5</Name>
<Page isRef="5"/>
<Parent isRef="11"/>
<Text>name</Text>
<TextBrush>Black</TextBrush>
</Text5>
<Text6 Ref="14" isKey="true" type="Text">
<Brush>Transparent</Brush>
<ClientRectangle>10.2,0.2,1.6,0.6</ClientRectangle>
<Conditions isList="true" count="0"/>
<Font>Arial,10</Font>
<Margins>0,0,0,0</Margins>
<Name>Text6</Name>
<Page isRef="5"/>
<Parent isRef="11"/>
<Text>{Customer.groupid}</Text>
<TextBrush>Black</TextBrush>
</Text6>
<Text7 Ref="15" isKey="true" type="Text">
<Brush>Transparent</Brush>
<ClientRectangle>7.4,0.2,2.8,0.6</ClientRectangle>
<Conditions isList="true" count="0"/>
<Font>Arial,10</Font>
<Margins>0,0,0,0</Margins>
<Name>Text7</Name>
<Page isRef="5"/>
<Parent isRef="11"/>
<Text>groupid</Text>
<TextBrush>Black</TextBrush>
</Text7>
</Components>
<Conditions isList="true" count="0"/>
<DataRelationName>GroupCustomer</DataRelationName>
<DataSourceName>Customer</DataSourceName>
<Filters isList="true" count="0"/>
<MasterComponent isRef="8"/>
<Name>DataBand1</Name>
<Page isRef="5"/>
<Parent isRef="5"/>
<Sort isList="true" count="0"/>
</DataBand1>
</Components>
<Conditions isList="true" count="0"/>
<Guid>f8cbf876f172f7d3347340273df10315</Guid>
<Margins>1,1,1,1</Margins>
<Name>Page1</Name>
<PageHeight>29.7</PageHeight>
<PageWidth>21</PageWidth>
<Report isRef="0"/>
<Watermark Ref="16" isKey="true" type="Stimulsoft.Report.Components.StiWatermark">
<Font>Arial,100</Font>
<TextBrush>[50:0:0:0]</TextBrush>
</Watermark>
</Page1>
</Pages>
<PrinterSettings Ref="17" isKey="true" type="Stimulsoft.Report.Print.StiPrinterSettings"/>
<ReportAlias>Report</ReportAlias>
<ReportChanged>03/10/2013 02:04:29 PM</ReportChanged>
<ReportCreated>03/06/2013 02:42:26 PM</ReportCreated>
<ReportFile>/Users/hugo/Desktop/Report.mrt</ReportFile>
<ReportGuid>3a1cf8b40a9763e309ba549df18e598a</ReportGuid>
<ReportName>Report</ReportName>
<ReportUnit>Centimeters</ReportUnit>
<ReportVersion>2013.1.1511</ReportVersion>
<ScriptLanguage>CSharp</ScriptLanguage>
<Styles isList="true" count="0"/>
</StiSerializer>
HighAley
Posts: 8430
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

Re: Master-Detail problem

Post by HighAley »

Hello.

Please, try to remove the report.dictionary.synchronize(); method.
It kills relations between tables.

Thank you.
hugo
Posts: 359
Joined: Fri Mar 08, 2013 10:46 am

Re: Master-Detail problem

Post by hugo »

Removing report.dictionary.synchronize(), I can't even see results!
I have the full source code and the report xml, so should be easy for you to reproduce.
HighAley
Posts: 8430
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

Re: Master-Detail problem

Post by HighAley »

Hello.

The synchronize() method rebuilds all Dictionary with specified data sources.
But you didn't refined it in your code.

Thank you.
hugo
Posts: 359
Joined: Fri Mar 08, 2013 10:46 am

Re: Master-Detail problem

Post by hugo »

So, the problem lies on not recreate relationships between both tables after recreate tables, columns and add dataRows,

So, there is 2 possibilites:
1. Don't recreate definitions but simply add rows;
2. Recreate relationships by code.

I tryed both cases without sucess and there is lack of documentation about both chapters in Flex.
Can you help me?
HighAley
Posts: 8430
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

Re: Master-Detail problem

Post by HighAley »

Hello.

You can add relation with next code:

Code: Select all

var relation: DataRelation = new DataRelation("Customer", [parentColumn1, parentColumn2, ...], [childColumn1, childColumn2, ...]);
dataSet.relations.add(relation);
Thank you.
hugo
Posts: 359
Joined: Fri Mar 08, 2013 10:46 am

Re: Master-Detail problem

Post by hugo »

Full sample source code:

Code: Select all

			private function init():void
			{
				StiViewerFx.initialize();

				var loader:URLLoader = new URLLoader();
				loader.addEventListener(Event.COMPLETE, onLoadComplete);
				loader.load(request);
			}

			private static function onLoadComplete(event:Event):void
			{
				var loader:URLLoader = event.target as URLLoader;
				var reportString:String = loader.data as String;
				
				var table1:DataTable = new DataTable("Group");
				table1.columns.add(new DataColumn("id", StorageType.IntType));
				table1.columns.add(new DataColumn("name", StorageType.StringType));
				var row1:DataRow = table1.addNewRow();
				row1.setValue("id", 1);
				row1.setValue("name", "G1");
				var row2:DataRow = table1.addNewRow();
				row2.setValue("id", 2);
				row2.setValue("name", "G2");

				var table2:DataTable = new DataTable("Customer");
				table2.columns.add(new DataColumn("id", StorageType.IntType));
				table2.columns.add(new DataColumn("groupid", StorageType.IntType));
				table2.columns.add(new DataColumn("name", StorageType.StringType));
				var row3:DataRow = table2.addNewRow();
				row3.setValue("id", 1);
				row3.setValue("groupid", 1);
				row3.setValue("name", "Hugo");
				var row4:DataRow = table2.addNewRow();
				row4.setValue("id", 2);
				row4.setValue("groupid", 2);
				row4.setValue("name", "Afonso");

				var dataSet:DataSet = new DataSet("DataSetName");
				dataSet.tables.add(table1);
				dataSet.tables.add(table2);

				var relation: DataRelation = new DataRelation("Customer", ["id"], ["groupid"]);
				dataSet.relations.add(relation);				
				
				var report:StiReport = new StiReport();
				report.loadReportFromString(reportString);
				report.regDataSet("DataSetName", "DataSetName", dataSet);
				report.dictionary.synchronize();
				report.show();
			}
And the result is an exception:
ReferenceError: Error #1069: Property table not found on String and there is no default value.
at DataKey/get table()
at stimulsoft.flex.data::DataRelation/get childTable()
at stimulsoft.flex.data::DataSetRelationCollection/addCore()
at stimulsoft.flex.data::DataRelationCollection/add()
at TestReport$/onLoadComplete()[/Users/hugo/Desktop/TestReport/src/TestReport.mxml:87]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()

I think the reason is because this "new DataRelation("Customer", ["id"], ["groupid"]);" does not provide full information between master table and foreign table.

Several iterations and I didn't get a clear answer !
I already provide full source code. Amazing !
I can provide a zip with a sample project ready to run, if necessary.
HighAley
Posts: 8430
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

Re: Master-Detail problem

Post by HighAley »

Hello.

You didn't specified DataTables of the columns. Please, try to use next code:

Code: Select all

new DataRelation("Customer", [table1.columns.getByName("id")], [table2.columns.getByName("groupid")]);
Thank you.
hugo
Posts: 359
Joined: Fri Mar 08, 2013 10:46 am

Re: Master-Detail problem

Post by hugo »

All provided solutions ends up to fail but I finally found the right solution to this offline report schema filled with data at runtime and with relations between tables.
Anyway, thank you for your time.
HighAley
Posts: 8430
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

Re: Master-Detail problem

Post by HighAley »

Hello.

That's what we tried to do.
Let us know if you will have any problem else.

Thank you.
Locked