private static ReportLayout getForReportLayout ( Connection con , ReportLayout layout , ParametersBean pBean ) throws Exception { ReportLayout convertedLayout = ObjectCloner . silenceDeepCopy ( layout ) ; List < Band > bands = convertedLayout . getDocumentBands ( ) ; for ( Band band : bands ) { for ( int i = 0 , rows = band . getRowCount ( ) ; i < rows ; i ++ ) { List < BandElement > list = band . getRow ( i ) ; for ( int j = 0 , size = list . size ( ) ; j < size ; j ++ ) { BandElement be = list . get ( j ) ; if ( be instanceof ForReportBandElement ) { String sql = ( ( ForReportBandElement ) be ) . getSql ( ) ; Report report = ( ( ForReportBandElement ) be ) . getReport ( ) ; if ( ( sql == null ) || sql . isEmpty ( ) ) { return convertedLayout ; } else { QueryUtil qu = new QueryUtil ( con , DialectUtil . getDialect ( con ) ) ; // column name is the same with parameter name String columnName = qu . getColumnNames ( sql , pBean . getParams ( ) ) . get ( 0 ) ; List < IdName > values = qu . getValues ( sql , pBean . getParams ( ) , pBean . getParamValues ( ) ) ; int pos = j ; for ( int k = 0 ; k < values . size ( ) ; k ++ ) { IdName in = values . get ( k ) ; if ( k > 0 ) { band . insertColumn ( pos ) ; } Report newReport = ObjectCloner . silenceDeepCopy ( report ) ; ReportLayout subReportLayout = ReportUtil . getReportLayoutForHeaderFunctions ( newReport . getLayout ( ) ) ; newReport . setLayout ( subReportLayout ) ; newReport . setName ( report . getBaseName ( ) + "_" + ( k + 1 ) + ".report" ) ; newReport . getGeneratedParamValues ( ) . put ( columnName , in . getId ( ) ) ; band . setElementAt ( new ReportBandElement ( newReport ) , i , pos ) ; pos ++ ; } List < Integer > oldColumnsWidth = layout . getColumnsWidth ( ) ; List < Integer > newColumnWidth = new ArrayList < Integer > ( ) ; for ( int m = 0 ; m < j ; m ++ ) { newColumnWidth . add ( oldColumnsWidth . get ( m ) ) ; } for ( int m = 0 ; m < values . size ( ) ; m ++ ) { newColumnWidth . add ( oldColumnsWidth . get ( j ) ) ; } for ( int m = j + 1 ; m < size ; m ++ ) { newColumnWidth . add ( oldColumnsWidth . get ( m ) ) ; } convertedLayout . setColumnsWidth ( newColumnWidth ) ; // we look just for first appearance return convertedLayout ; } } } } } return convertedLayout ; }
If a report layout contains a ForReportBandElement we must replace this element with more ReportBandElements This means inserting n - 1 new columns where n is the number of values return by sql inside ForReportBandElement