#
# Library functions for the ic_xml library
#
# File: ic_xml/lib/job_report.tcl
#
# $Revision$ on $Date$
# 
# Copyright (c) 1993-2004 ICEM CFD Engineering
#
# Author: Jason Aftosmis (jason@icemcfd.com)
#
# TODO:
#   o 
#
#---------------------------------------------------------------------
#
#      <job_report>
#             <job>
#                <data name="Input files">
#                    <data name="CAD file">/home/josie/ic_regression/db/tetra/test_model_001</data>
#                </data>
#                <start>Mon Feb 02 12:42:09 PST 2004</>
#                <elapsed>74</elapsed>
#                <result>SUCCESS</result>
#                <logfile>./file_on_feb_02_12:42:09_pst_20040</logfile>
#             </job>
#             ...
#     </job_report>
#
#---------------------------------------------------------------------

namespace eval JobReport {
    
    namespace import ::IcXml::*
    
    variable _xsl_file [file join [ic_xml_lib_path] .. style job_report.xsl]
    variable _css_file [file join [ic_xml_lib_path] .. style generic_report.css]
    
    variable _root_element_name "job_report"
    variable _job_element_name  "job"
}


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


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

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

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

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

proc JobReport::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 JobReport::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 JobReport::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 JobReport.
## Not sure what it returns. FIXME

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


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

proc JobReport::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 JobReport 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 JobReport::closeReport { domKey {file ""} {format ""} } {
    
    if {"" != $file} {
	set stat [JobReport::writeReport $domKey $file $format]
    }
    
    return [IcXmlReport::closeDocument $domKey]
}


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


## fixme-doc

proc JobReport::addJob { domKey jobReportObj attributes inputData children } {
    
    # add a job element to the given jobReportObj (or the root of the domKey)
    # with attributes and children    
    
    if {"" == $jobReportObj} {
	# (or the nodes does not exist in the domKey-object ...fixme --jka
	set jobReportObj [IcXmlReport::getReportObject $domKey]
    }
    
    set jobObj [IcXmlReport::addElement $domKey $jobReportObj $metricElName \
		    $attributes $children]
    
    # create a dataSet for "inputData", perhaps
    
    if {"" != $inputData} {	
	set dataObj [IcXmlReport::addDataSet $domKey $jobObj [lindex $inputData 0] [lindex $inputData 1]]
    }
    
    return $jobObj
}

