#
# Library functions for the ic_xml library
#
# File: ic_xml/lib/batchsurf_report.tcl
#
# $Revision$ on $Date$
# 
# Copyright (c) 1993-2004 ICEM CFD Engineering
#
# Author: Jason Aftosmis (jason@icemcfd.com)
#
# TODO:
#   o 
#
#---------------------------------------------------------------------
# 
# <batchsurf_report>
#
#     <job_report>
#             ...see "./job_report.tcl"
#     </job_report>
#
#     <assembly_report>
#             ...see "./assembly_report.tcl"
#     </assembly_report>
#
# </batchsurf_report>
#
#---------------------------------------------------------------------

namespace eval BatchsurfReport {
    
    namespace import ::IcXml::*
    #
    # static sorts of prefs
    #
    variable _root_element_name "batchsurf_report"
    
    variable _xsl_file [file join [ic_xml_lib_path] .. style batchsurf_report.xsl]
    variable _css_file [file join [ic_xml_lib_path] .. style generic_report.css]
    #
    # lists of objects
    #
    variable _job_eport_element_list       ""
    variable _assembly_report_element_list ""
}


#---------------------------------------------------------------------


## Test (some|all) of the BatchsurfReport functions. Returns SUCCESS or FAILURE

proc BatchsurfReport::test {} {
    
    IcXmlReport::checkEmpty
    
    # fixme FIXME
    
    return [IcXmlReport::checkEmpty]
}


## Create a document with a root element <assembly_report>. Returns the domKey

proc BatchsurfReport::createDocument { attributes } {
    variable _root_element_name
    return [IcXmlReport::createDocument $_root_element_name $attributes]
}


## Open an XML file, requiring a proper rootElName. Returns the domKey.

proc BatchsurfReport::openReport { xml_file } {
    variable _root_element_name
    return [IcXmlReport::openReport $xml_file $_root_element_name]
}


## Depending on whether or not the given xml_file already exists, it will
## either open the existing, or create a new (in which case no filename association
## should be assumed). Returns the domKey.

proc BatchsurfReport::appendReport { xml_file {attributes ""} } {
    
    if ![file exists $xml_file] {
	return [createDocument $attributes]
    } else {
	return [openReport $xml_file]
    }
}


## Add a <header> with children to the root node of the BatchsurfReport.
## Not sure what it returns. FIXME

proc BatchsurfReport::addHeader { domKey children } {
    return [IcXmlReport::addHeader $domKey $children]
}


## Add a <job> with children to the root node of the BatchsurfReport.
## Not sure what it returns. FIXME

proc BatchsurfReport::addJob { domKey attributes inputData children } {    
    variable _job_report_element_list
    
    if ![info exists _job_report_element($domKey)] {
	set _job_report_element($domKey) [IcXmlReport::ensureReportElement $domKey "job_report"]
    }
    return [JobReport::addJob $domKey $_job_report_element($domKey) $attributes $inputData $children]
}


## Add a <job> with children to the root node of the BatchsurfReport.
## Not sure what it returns. FIXME

proc BatchsurfReport::addPart { domKey attributes inputData children } {    
   variable _assembly_report_element_list
    
    if ![info exists _assembly_report_element($domKey)] {
	set _assembly_report_element($domKey) [IcXmlReport::ensureReportElement $domKey "job_report"]
    }
    return [AssemblyReport::addPart $domKey $_assembly_report_element($domKey) $attributes $inputData $children]
}


## Write out the BatchsurfReport associated with the given <em>domKey</em>
## to the path <em>file</em> in the format <em>format</em>.

proc BatchsurfReport::writeReport { domKey file format } {
    variable _css_file
    variable _xsl_file
    
    set args ""
    
    if {("" != $_xsl_file) && [file exists $_xsl_file]} {
	append args "-xsl_file {$_xsl_file} "
    } elseif {"" != $_xsl_file}  {
	ic_xml_err "Warning: could not find XSL file $_xsl_file"
    }
    
    if {("" != $_css_file) && [file exists $_css_file]} {
	append args "-css_file {$_css_file} "
    } elseif {"" != $_css_file}  {
	ic_xml_err "Warning: could not find CSS file $_css_file"
    }
    
    return [IcXmlReport::writeReport $domKey "$file" $format $args]
}


## Close the BatchsurfReport associated with the given <em>domKey</em>
## and, if specified, to write it to the path <em>file</em> in the
## format <em>format</em>.

proc BatchsurfReport::closeReport { domKey {file ""} {format ""} } {
    
    if {"" != $file} {
	set stat [BatchsurfReport::writeReport $domKey $file $format]
    }    
    return [IcXmlReport::closeDocument $domKey]
}
