<project name="Gene Ontology Loader" basedir="." default="build">

    <!-- Determine version number -->
    <property name="common.dir" location="${basedir}/../utils/src/java"/>
    <property file="${common.dir}/src/version.prop"/>

    <!-- Program meta data -->
    <property name="go.schema.version" value="200411"/>
    <property name="module.name" value="gene-ontology"/>
    <property name="module.jar" value="${module.name}-${version.number}.jar"/>
    <property name="schema.jar.name" value="${module.name}-schema-${go.schema.version}.jar"/>
    <property name="unit.test.suffix" value="_Test"/>
    <property name="unit.test.prefix" value=""/>

    <!-- Build properties -->
    <property name="src.dir" location="${basedir}/src"/>
    <property name="build.dir" location="${basedir}/build"/>
    <property name="build.classes.dir" location="${build.dir}/classes"/>
    <property name="build.src.dir" location="${build.dir}/src"/>
    <property name="lib.dir" location="${basedir}/lib"/>
    <property name="dist.dir" location="${basedir}/dist"/>
    <property name="module.jar.path" location="${dist.dir}/${module.jar}"/>
    <property name="schema.jar" location="${lib.dir}/${schema.jar.name}"/>
    <property name="docs.dir" location="${basedir}/docs"/>
    <property name="api.dir" location="${docs.dir}/api"/>
    <property name="schema.xsd" location="${docs.dir}/go_200411.xsd"/>
    <property name="xmlbeans.src.dir" location="${basedir}/xmlbeans/src"/>
    <property name="common.dir" location="${basedir}/../common"/>
    <property name="common.lib.dir" location="${common.dir}/lib"/>
    <property name="warehouse.jar.name" value="biowarehouse.jar"/>
    <property name="warehouse.jar" location="${common.dir}/dist/${warehouse.jar.name}"/>
    <property name="etc.dir" location="${basedir}/etc"/>
    <property name="properties.template" location="${etc.dir}/go.properties"/>
    <property name="run.script" value="runGOLoader.sh"/>
    <property name="run.script.location" location="${etc.dir}/${run.script}"/>

    <!-- Testing properties -->
    <property name="test.dir" location="${basedir}/test"/>
    <property name="test.properties.file" location="${common.dir}/test/unittest.properties"/>
    <property name="test.data.dir" location="${test.dir}/data"/>
    <property name="test.src.dir" location="${test.dir}/src"/>
    <property name="common.test.src.dir" location="${common.dir}/test/src"/>
    <property name="common.test.lib.dir" location="${common.dir}/test/lib"/>
    <property name="test.build.dir" location="${test.dir}/build"/>
    <property name="test.results.dir" location="${test.dir}/results"/>
    <property name="test.xmlResults.dir" location="${test.results.dir}/xmlResults"/>
    <property name="test.reports.dir" location="${test.results.dir}/htmlReports"/>

    <!-- Running properties -->
    <property name="input.properties" location="${basedir}/developer.properties"/>

    <!-- Third party libraries -->
    <property name="xmlbeans.jar" location="${common.lib.dir}/xbean.jar"/>
    <property name="saxdomix.jar" location="${common.lib.dir}/saxdomix.jar"/>
    <property name="log4j.jar" location="${common.lib.dir}/log4j-1.2.8.jar"/>
    <property name="mysql.driver.jar" location="${common.lib.dir}/mysql-connector-java-3.0.9-stable-bin.jar"/>
    <property name="oracle.driver.jar" location="${common.lib.dir}/ojdbc14.jar"/>
    <property name="cli.jar" location="${common.lib.dir}/commons-cli-1.0.jar"/>
    <property name="xerces1.jar" location="${common.lib.dir}/xercesImpl.jar"/>
    <property name="xerces2.jar" location="${common.lib.dir}/xml-apis.jar"/>


    <!-- CLASSPATHS -->
    <path id="classpath.compile" description="The classpath used for compiling the program">
        <pathelement path="${xmlbeans.jar}"/>
        <pathelement path="${saxdomix.jar}"/>
        <pathelement path="${log4j.jar}"/>
        <pathelement path="${warehouse.jar}"/>
        <pathelement path="${cli.jar}"/>
        <pathelement path="${xerces1.jar}"/>
        <pathelement path="${xerces2.jar}"/>
        <pathelement path="${lib.dir}/gene-ontology-schema-200503obo.jar"/>
    </path>

    <path id="classpath.run" description="The classpath used for running the program">
        <path refid="classpath.compile"/>
        <pathelement path="${module.jar.path}"/>
        <pathelement path="${mysql.driver.jar}"/>
        <pathelement path="${oracle.driver.jar}"/>
    </path>

    <path id="classpath.test" description="The classpath used for compiling and running unit tests">
        <path refid="classpath.run"/>
        <pathelement path="${build.classes.dir}"/>
        <pathelement path="${test.build.dir}"/>
    </path>


    <!-- =========================================================== -->
    <!--  INIT                                                       -->
    <!--  Create a unique build number for each build.               -->
    <!-- =========================================================== -->
    <target name="init">
        <tstamp>
            <format pattern="yyyyMMdd-HHmm-z" property="build.number"/>
        </tstamp>
        <echo level="info" message="## ${ant.project.name}: Build number = ${build.number}"/>
        <ant dir="${common.dir}" inheritall="false"/>
    </target>

    <!-- =========================================================== -->
    <!--  CLEAN                                                      -->
    <!-- =========================================================== -->
    <target name="clean" description="deletes files/dirs made by this build">
        <ant dir="${common.dir}" target="clean" inheritall="false"/>
        <delete dir="${build.dir}"/>
        <delete dir="${dist.dir}"/>
        <delete dir="${test.build.dir}"/>
        <delete dir="${test.results.dir}"/>
    </target>


    <target name="xmlbeans" description="Transforms XSD into XMLBeans classes">
        <mkdir dir="${xmlbeans.src.dir}"/>
        <taskdef name="xmlbean" classname="org.apache.xmlbeans.impl.tool.XMLBean" classpath="${xmlbeans.jar}"/>
        <xmlbean schema="${schema.xsd}" verbose="true" destfile="${schema.jar}" classpathref="classpath.compile" srcgendir="${xmlbeans.src.dir}"/>
    </target>


    <!-- =========================================================== -->
    <!--  COMPILE                                                    -->
    <!-- =========================================================== -->
    <target name="compile" depends="init" description="Compile source code">
        <echo level="info" message="## ${ant.project.name}: Compiling source code..."/>
        <mkdir dir="${build.dir}"/>
        <mkdir dir="${build.classes.dir}"/>
        <mkdir dir="${build.src.dir}"/>

        <!-- Copy src to build.src.dir so that filtering can be performed -->
        <filter token="VERSION_NUMBER" value="${version.number}"/>
        <filter token="BUILD_NUMBER" value="${build.number}"/>
        <copy todir="${build.src.dir}" filtering="true">
            <fileset dir="${src.dir}">
                <include name="**/*.java"/>
            </fileset>
        </copy>

        <!-- Compile -->
        <javac srcdir="${build.src.dir}" destdir="${build.classes.dir}">
            <classpath refid="classpath.compile"/>
            <exclude name="**/*${unit.test.suffix}.java"/>
            <exclude name="com/sri/biowarehouse/go/rdf/**"/>
        </javac>
    </target>

    <!-- =========================================================== -->
    <!--  BUILD                                                      -->
    <!-- =========================================================== -->
    <target name="build" depends="compile" description="Build module jar file">
        <echo level="info" message="## ${ant.project.name}: Creating jar file..."/>
        <mkdir dir="${dist.dir}"/>
        <jar destfile="${module.jar.path}">
            <fileset dir="${build.classes.dir}"/>
            <fileset dir="${src.dir}">
                <include name="**/*.xml"/>
                <include name="**/*.dtd"/>
            </fileset>
        </jar>
        <copy file="${properties.template}" todir="${dist.dir}"/>

        <!-- Copy run script to dist dir and make sure it has executable permission -->
        <!-- Note: as of Ant 1.6, chmod only works on unix/linux machines -->
        <filter token="MODULE_JAR" value="${module.jar}"/>
        <filter token="WAREHOUSE_JAR" value="${warehouse.jar.name}"/>
        <copy file="${run.script.location}" todir="${dist.dir}" filtering="true"/>
        <chmod file="${dist.dir}/${run.script}" perm="+x"/>
    </target>


    <!-- =========================================================== -->
    <!--  RUN                                                        -->
    <!-- =========================================================== -->
    <target name="run" depends="build" description="Run the program">
        <java fork="true" classname="com.sri.biowarehouse.go.GOMain" maxmemory="512M">
            <classpath refid="classpath.run"/>
            <arg line="-p ${input.properties}"/>
        </java>
    </target>
    

    <!-- =========================================================== -->
    <!--  COMPILE-TEST                                               -->
    <!-- =========================================================== -->
    <target name="compile-test" description="Compile test source code">
        <mkdir dir="${test.dir}"/>
        <mkdir dir="${test.build.dir}"/>

        <javac srcdir="${test.src.dir}" destdir="${test.build.dir}" debug="true">
            <classpath refid="classpath.test"/>
            <include name="**/${unit.test.prefix}*${unit.test.suffix}.java"/>
        </javac>


    </target>

    <!-- =========================================================== -->
    <!--  TEST                                                       -->
    <!-- =========================================================== -->
    <target name="test" depends="compile, compile-test" description="Run unit tests">

        <!-- Set the properties needed to connect to the warehouse -->
        <property file="${test.properties.file}"/>

        <!-- Make dirs to receive reports -->
        <mkdir dir="${test.reports.dir}"/>
        <mkdir dir="${test.xmlResults.dir}"/>


        <!-- Run unit tests -->
        <junit fork="yes" showoutput="no" printsummary="on" dir="${test.dir}">
            <classpath refid="classpath.test"/>
            <formatter type="xml"/>
            <batchtest todir="${test.xmlResults.dir}">
                <fileset dir="${test.build.dir}">
                    <include name="**/*_Test*"/>
                </fileset>
            </batchtest>
            <sysproperty key="warehouse.test.database.host" value="${warehouse.test.database.host}"/>
            <sysproperty key="warehouse.test.database.port" value="${warehouse.test.database.port}"/>
            <sysproperty key="warehouse.test.database.name" value="${warehouse.test.database.name}"/>
            <sysproperty key="warehouse.test.database.username" value="${warehouse.test.database.username}"/>
            <sysproperty key="warehouse.test.database.password" value="${warehouse.test.database.password}"/>
            <sysproperty key="warehouse.test.database.dbms.type" value="${warehouse.test.database.dbms.type}"/>

        </junit>

        <!-- Transform xml results into human-readable html results -->
        <junitreport todir="${test.reports.dir}">
            <fileset dir="${test.xmlResults.dir}">
                <include name="TEST-*.xml"/>
            </fileset>
            <report format="frames" todir="${test.reports.dir}"/>
        </junitreport>

        <echo level="info" message="## ${ant.project.name}: Unit tests are completed.  See the results at ${test.reports.dir}/index.html..."/>

    </target>


    <!-- =========================================================== -->
    <!--  JAVADOC                                                    -->
    <!-- =========================================================== -->
    <target name="javadoc" description="Generate Javadoc for the source code">
        <echo level="info" message="## ${ant.project.name}: Building the source code documentation..."/>

        <javadoc sourcepath="${xmlbeans.src.dir}" destdir="${api.dir}" classpathref="classpath.compile">
            <packageset dir="${xmlbeans.src.dir}"/>
        </javadoc>

        <echo level="info" message="## ${ant.project.name}: Javadoc documentation can be found at ${api.dir}"/>
    </target>

</project>