/*******************************************************************************
 * Copyright (c) 2004 Actuate Corporation. All rights reserved. This program and
 * the accompanying materials are made available under the terms of the Eclipse
 * Public License v1.0 which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: Actuate Corporation - initial API and implementation
 ******************************************************************************/

package org.eclipse.birt.test.performance.reporting.xml;

import org.eclipse.birt.test.performance.reporting.model.Duration;
import org.eclipse.birt.test.performance.reporting.model.InputNode;
import org.eclipse.birt.test.performance.reporting.model.PerformanceReport;
import org.eclipse.birt.test.performance.reporting.model.ReportDesignNode;
import org.eclipse.birt.test.performance.reporting.model.TaskNode;
import org.eclipse.birt.test.performance.reporting.model.TestCaseNode;
import org.eclipse.birt.test.performance.reporting.model.ThreadNode;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XMLReportVisitor implements SchemaConstants
{
	public XMLReportVisitor( )
	{
	}

	public void visit( PerformanceReport performanceNode )
	{
		Node doc = performanceNode.getNode( );

		Node performance_Report_node = XMLUtility.findFirstChildByName( doc,
				PERFORMANCE_REPORT_TAG );

		if ( performance_Report_node == null )
			return;

		String generateDateTime = performance_Report_node.getAttributes( )
				.getNamedItem( GENERATION_DATETIME_ATTR ).getNodeValue( );
		System.out.println( generateDateTime );
		performanceNode.setGenerateDateTime( generateDateTime );

		NodeList testCases = ( (Element) performance_Report_node )
				.getElementsByTagName( TEST_CASE_TAG );
		if ( testCases != null )
		{
			for ( int i = 0; i < testCases.getLength( ); i++ )
			{
				Node testCase = testCases.item( i );
				TestCaseNode testCaseNode = new TestCaseNode( );
				testCaseNode.setNode( testCase );

				performanceNode.addTestcases( testCaseNode );
				testCaseNode.accept( this );
			}
		}

		Node inputNode = XMLUtility.findFirstChildByName(
				performance_Report_node, INPUT_TAG );
		if ( inputNode != null )
		{
			InputNode input = new InputNode( );
			input.setNode( inputNode );

			performanceNode.setInputNode( input );
			input.accept( this );
		}

	}

	public void visit( ReportDesignNode node )
	{
		Node doc = node.getNode( );
		String id = doc.getAttributes( ).getNamedItem( ID_ATTR ).getNodeValue( );
		String fileName = doc.getAttributes( ).getNamedItem( FILENAME_ATTR )
				.getNodeValue( );

		node.setFileName( fileName );
		node.setId( id );
	}

	public void visit( InputNode node )
	{
		Node doc = node.getNode( );

		String dir = doc.getAttributes( ).getNamedItem( DIR_ATTR ).getNodeValue( );
		node.setDir( dir );

		NodeList reportDesigns = ( (Element) doc )
				.getElementsByTagName( REPORT_DESIGN_TAG ); //$NON-NLS-1$
		if ( reportDesigns != null )
		{
			for ( int i = 0; i < reportDesigns.getLength( ); i++ )
			{
				Node reportDesign = reportDesigns.item( i );
				ReportDesignNode reportDesignNode = new ReportDesignNode( );
				reportDesignNode.setNode( reportDesign );

				node.addReportDesign( reportDesignNode );
				reportDesignNode.accept( this );
			}
		}
	}

	public void visit( Duration node )
	{
		Node doc = node.getNode( );

		String reportId = doc.getAttributes( ).getNamedItem( REPORTID_ATTR )
				.getNodeValue( );
		String measure = doc.getAttributes( ).getNamedItem( MEASURE_ATTR )
				.getNodeValue( );
		String unit = doc.getAttributes( ).getNamedItem( UNIT_ATTR )
				.getNodeValue( );

		node.setMeasure( Float.parseFloat( measure ) );
		node.setReportId( reportId );
		node.setUnit( unit );
	}

	public void visit( TaskNode taskNode )
	{
		Node doc = taskNode.getNode( );

		String name = doc.getAttributes( ).getNamedItem( NAME_ATTR )
				.getNodeValue( );
		String description = doc.getAttributes( ).getNamedItem( DESCRIPTION_ATTR )
				.getNodeValue( );
		String pageN = doc.getAttributes( ).getNamedItem( PAGEN_TAG )
				.getNodeValue( );

		taskNode.setDescription( description );
		taskNode.setName( name );
		taskNode.setPageN( pageN );

		NodeList threads = ( (Element) doc ).getElementsByTagName( THREAD_TAG ); //$NON-NLS-1$
		if ( threads != null )
		{
			for ( int i = 0; i < threads.getLength( ); i++ )
			{
				Node thread = threads.item( i );
				ThreadNode threadNode = new ThreadNode( );
				threadNode.setNode( thread );

				taskNode.addThread( threadNode );
				threadNode.accept( this );
			}
		}

		Node average = XMLUtility.findFirstChildByName( doc, AVG_TAG ); //$NON-NLS-1$
		if ( average != null )
		{
			NodeList durations = ( (Element) average )
					.getElementsByTagName( DURATION_TAG ); //$NON-NLS-1$
			if ( durations != null )
			{
				for ( int i = 0; i < durations.getLength( ); i++ )
				{
					Node durationNode = durations.item( i );
					Duration duration = new Duration( );
					duration.setNode( durationNode );

					taskNode.addAVG( duration );
					duration.accept( this );
				}
			}
		}
	}

	public void visit( TestCaseNode testCaseNode )
	{
		Node doc = testCaseNode.getNode( );

		String name = doc.getAttributes( ).getNamedItem( NAME_ATTR )
				.getNodeValue( );
		String threadCount = doc.getAttributes( ).getNamedItem( THREAD_COUNT_ATTR )
				.getNodeValue( );
		String reportCount = doc.getAttributes( ).getNamedItem( REPORT_COUNT_ATTR )
				.getNodeValue( );
		String encoding = doc.getAttributes( ).getNamedItem( ENCODING_ATTR )
				.getNodeValue( );
		String locale = doc.getAttributes( ).getNamedItem( LOCALE_ATTR )
				.getNodeValue( );
		String format = doc.getAttributes( ).getNamedItem( FORMAT_ATTR )
				.getNodeValue( );
		String description = doc.getAttributes( ).getNamedItem( DESCRIPTION_ATTR)
				.getNodeValue( );

		testCaseNode.setName( name );
		testCaseNode.setDescription( description );
		testCaseNode.setEncoding( encoding );
		testCaseNode.setFormat( format );
		testCaseNode.setLocale( locale );
		testCaseNode.setReportCount( Integer.parseInt( reportCount ) );
		testCaseNode.setThreadCount( Integer.parseInt( threadCount ) );

		NodeList tasks = ( (Element) doc )
				.getElementsByTagName( TASK_TAG ); //$NON-NLS-1$
		if ( tasks != null )
		{
			for ( int i = 0; i < tasks.getLength( ); i++ )
			{
				Node task = tasks.item( i );
				TaskNode taskNode = new TaskNode( );
				taskNode.setNode( task );

				testCaseNode.addTasks( taskNode );
				taskNode.accept( this );
			}
		}

	}

	public void visit( ThreadNode threadNode )
	{
		Node doc = threadNode.getNode( );

		String id = doc.getAttributes( ).getNamedItem( ID_ATTR ).getNodeValue( );
		threadNode.setId( id );

		NodeList durations = ( (Element) doc )
				.getElementsByTagName( DURATION_TAG ); //$NON-NLS-1$
		if ( durations != null )
		{
			for ( int i = 0; i < durations.getLength( ); i++ )
			{
				Node durationNode = durations.item( i );
				Duration duration = new Duration( );
				duration.setNode( durationNode );

				threadNode.addDuration( duration );
				duration.accept( this );
			}
		}
	}
}
