
package org.eclipse.birt.report.engine.content.impl;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Iterator;

import org.eclipse.birt.core.util.IOUtil;
import org.eclipse.birt.report.engine.content.IBandContent;
import org.eclipse.birt.report.engine.content.IContentVisitor;
import org.eclipse.birt.report.engine.content.IGroupContent;
import org.eclipse.birt.report.engine.content.IReportContent;
import org.eclipse.birt.report.engine.ir.GroupDesign;

public class GroupContent extends AbstractContent implements IGroupContent
{

	Boolean headerRepeat;
	
	String groupId;
	
	public int getContentType( )
	{
		return GROUP_CONTENT;
	}
	
	public GroupContent( IReportContent report )
	{
		super( report );
	}

	public IBandContent getHeader( )
	{
		return getBand( IBandContent.BAND_GROUP_HEADER );
	}

	public IBandContent getFooter( )
	{
		return getBand( IBandContent.BAND_GROUP_FOOTER );
	}

	protected IBandContent getBand( int type )
	{
		if ( children == null )
		{
			return null;
		}
		Iterator iter = children.iterator( );
		while ( iter.hasNext( ) )
		{
			Object child = iter.next( );
			if ( child instanceof IBandContent )
			{
				IBandContent band = (IBandContent) child;
				if ( band.getBandType( ) == type )
				{
					return band;
				}
			}
		}
		return null;
	}
	
	public String getGroupID( )
	{
		return groupId;
	}
	
	public void setGroupID(String groupId)
	{
		this.groupId = groupId;
	}

	public boolean isHeaderRepeat( )
	{
		if (headerRepeat != null)
		{
			return headerRepeat.booleanValue( );
		}
		if (generateBy instanceof GroupDesign)
		{
			GroupDesign design = (GroupDesign)generateBy;
			return design.isHeaderRepeat( );
			
		}
		return false;
	}
	
	public int getGroupLevel()
	{
		if (generateBy instanceof GroupDesign)
		{
			GroupDesign design = (GroupDesign)generateBy;
			return design.getGroupLevel( );
		}
		return -1;
	}

	public void setHeaderRepeat( boolean repeat )
	{
		headerRepeat = Boolean.valueOf( repeat );
	}

	public Object accept( IContentVisitor visitor, Object value )
	{
		return visitor.visitGroup( this, value );
	}

	static final protected short FIELD_HEADER_REPEAT = 1500;

	protected void writeFields( DataOutputStream out ) throws IOException
	{
		super.writeFields( out );
		if ( headerRepeat != null )
		{
			IOUtil.writeShort( out, FIELD_HEADER_REPEAT);
			IOUtil.writeBool( out, headerRepeat.booleanValue( ) );
		}
	}

	protected void readField( int version, int filedId, DataInputStream in )
			throws IOException
	{
		switch ( filedId )
		{
			case FIELD_HEADER_REPEAT :
				headerRepeat = Boolean.valueOf( IOUtil.readBool( in ) );
				break;
			default :
				super.readField( version, filedId, in );
		}
	}
}

@@ -1740,24 +1740,56 @@
 false;%0A%09%7D%0A%09%0A
+%09transient int groupLevel = -1;%0A
 %09public int 
@@ -1809,24 +1809,56 @@
 ()%0A%09%7B%0A%09%09if (
+ groupLevel == -1 )%0A%09%09%7B%0A%09%09%09if ( 
 generateBy i
@@ -1874,33 +1874,36 @@
  GroupDesign
+ 
 )%0A
+%09
 %09%09%7B%0A%09%09%09
+%09
 GroupDesign 
@@ -1916,32 +1916,33 @@
  = (GroupDesign)
+ 
 generateBy;%0A%09%09%09r
@@ -1940,29 +1940,152 @@
 eBy;%0A%09%09%09
-return design
+%09groupLevel = design.getGroupLevel( );%0A%09%09%09%7D%0A%09%09%09else if ( parent instanceof GroupContent )%0A%09%09%09%7B%0A%09%09%09%09groupLevel = ( (GroupContent)parent )
 .getGrou
@@ -2101,21 +2101,66 @@
 ;%0A%09%09
+%09
 %7D%0A%09%09
-return -1
+%09else%0A%09%09%09%7B%0A%09%09%09%09return 0;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return groupLevel
 ;%0A%09%7D

fix bug176579, change the getGroupLevel() method.