<?xml version="1.0" encoding="UTF-8"?>

<project basedir="." default="server" name="vbr">
	<property name="java.package" value="gov.va.med.esr.vbr.webservice"/>

	<import file="../HECMS_libraries/build/init.xml" />
	<import file="../HECMS_libraries/build/paths.xml" />
    <import file="config/deploy-targets.xml"/>

    <path id="jaxws.classpath">
        <pathelement location="${basedir}/../lib/tools.jar"/>
    	<path refid="jar.path" />
        <fileset dir="${lib.dir}">
            <include name="*.jar"/>
            <exclude name="j2ee.jar"/>
        </fileset>
    </path>

    <taskdef name="wsimport" classname="com.sun.tools.ws.ant.WsImport">
        <classpath refid="jaxws.classpath"/>
    </taskdef>

    <target name="setup">
        <mkdir dir="${build.home}"/>
        <mkdir dir="${build.classes.home}"/>
        <mkdir dir="${build.war.home}"/>
    </target>

    <target name="clean">
        <delete dir="${build.home}" includeEmptyDirs="true"/>
    </target>

    <target name="build-server-wsdl" depends="setup">
        <wsimport
                debug="true"
                verbose="${verbose}"
                keep="true"
                destdir="${build.classes.home}"
        		sourcedestdir="${src.dir}"
                package="${java.package}"
                wsdl="${basedir}/config/vbr.wsdl">
        </wsimport>
        <javac
                fork="true"
                srcdir="${src.dir}"
                destdir="${build.classes.home}"
                includes="**">
            <classpath refid="jaxws.classpath"/>
        </javac>
    </target>

    <target name="create-war">
		<copy todir="${build.classes.home}">
			<fileset dir="${basedir}/config" includes="applicationContext.xml"/>
		</copy>
        <war warfile="${build.war.home}/${ant.project.name}.war" webxml="config/web.xml">
            <webinf dir="${basedir}/config" includes="sun-jaxws.xml, weblogic.xml"/>
            <zipfileset
                    dir="${basedir}/config"
                    includes="*.wsdl, *.xsd"
                    prefix="WEB-INF/wsdl"/>
            <classes dir="${build.classes.home}"/>
        </war>
    </target>

    <target name="generate-client" depends="setup">
        <wsimport
                debug="true"
                verbose="${verbose}"
                keep="true"
                destdir="${build.classes.home}"
                package="fromwsdl.client"
                wsdl="http://localhost:7001/vbr/vbrSvc?wsdl">
        </wsimport>
    </target>

    <target name="client" depends="generate-client">
        <javac
                fork="true"
                srcdir="${basedir}/src"
                destdir="${build.classes.home}"
                includes="**/client/**,**/common/**">
            <classpath refid="jaxws.classpath"/>
        </javac>
    </target>

    <target name="run">
        <java fork="true" classname="fromwsdl.client.AddNumbersClient">
            <classpath>
                <path refid="jaxws.classpath"/>
                <pathelement location="${build.classes.home}"/>
                <pathelement location="${basedir}/config"/>
            </classpath>
            <jvmarg value="-Dcom.sun.xml.ws.transport.http.client.HttpTransportPipe.dump=${log}"/>
        </java>
    </target>

    <target name="help">
        <echo message="server:       Builds and deploy the service endpoint WAR"/>
        <echo message="client:       Builds the client"/>
        <echo message="run:          Runs the client"/>
    </target>

	<!-- this common.jar is only used for compilation of VBR, it is not used in run time. -->
    <target name="build.common.jar">

        		<!-- Internal target to build framework.jar if needed -->
		        <echo>Build and copy common.jar to lib directory</echo>
		        <ant antfile="build.xml" dir="${common.project.home.dir}/" target="jar" inheritAll="false" />
		        <copy file="${common.project.home.dir}/dist/local/common.jar"
		              todir="${lib.dir}"
		              overwrite="yes" />


    </target>

    <target name="build.framework.jar">

        		<!-- Internal target to build framework.jar if needed -->
		        <echo>Build and copy framework.jar to lib directory</echo>
		        <ant antfile="build.xml" dir="${framework.project.home.dir}/" target="build.clean" inheritAll="false" />
		        <copy file="${framework.project.home.dir}/dist/framework.jar"
		              todir="${lib.dir}"
		              overwrite="yes" />


    </target>

    <target name="server" depends="setup">
        <antcall target="clean"/>
        <antcall target="build.framework.jar"/>
        <antcall target="build.common.jar"/>
        <antcall target="build-server-wsdl"/>
    	<antcall target="create-war"/>
        <antcall target="deploy"/>
    </target>

</project>
