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

<!--
 *  Copyright 2004 University of Manchester and
 *  University of Newcastle Upon Tyne
 *
 *  This file is part of myGrid.  Further information, and the
 *  latest version, can be found at http://www.mygrid.org.uk
 *
 *  myGrid is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation; either version 2.1
 *  of the License, or (at your option) any later version.
 *
 *  myGrid is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with myGrid; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 *  Revision: $Id: mir-in.xml,v 1.61 2005/04/20 15:07:02 sharmann Exp $
 *
 *  This file is used by AntMerge to generate an Ant build file for 
 *  the MIR web service.
 -->

<project name="MIR" default="war" basedir=".">

  <property environment="env"/>
  <!-- <property file="${basedir}/MIR.properties"/> -->

  <property name="antmerge.infile" value="mir-in.xml"/>
  <property name="antmerge.parent"
    value="default+mygrid+axis-ws+checkdependencies+test"/>

  <!-- External dependencies: -->

  <property name="jar-dependencies" value="
    ${super}
    c3p0-0.8.4.5.jar
    castor-0.9.5.jar
    cglib-full-2.0.2.jar
    commons-collections-2.1.1.jar
    commons-lang-1.0.1.jar
    dom4j-1.4.jar
    dnsjava-1.3.2.jar
    ehcache-0.9.jar
    hibernate-2.1.6.jar
    jax-1_1-fr-qname-class.jar
    jdom-1.0-beta8-dev.jar
    jta-1.0.1b.jar
    lsid-client-1.1.jar
    lsid-server-1.1.0.jar
    odmg-3.0.jar
    oscache-2.0.jar
    proxool-0.8.3.jar
    rowset.jar
  "/>
  <property name="jar-dependencies.compile" value="
    ${super}
    log4j-1.2.8.jar
  "/>
  <property name="jar-dependencies.deploy" value="
    ${super}
    mysql-connector-java-3.0.11-stable-bin.jar
  "/>
  <property name="jar-dependencies.endorsed" value="
    ${super}
    xalan-2.5.1.jar
  "/>
  <property name="jar-dependencies.tools" value="
    ${super}
    axis-ant-1.2-RC3.jar
    commons-discovery-0.2-dev.jar
    hibernate-tools-2.1.2.jar
    middlegen-2.1.jar
    middlegen-hibernate-plugin-2.1.jar
    velocity-1.4-dev.jar
  "/>

  <!--- Custom properties: -->

  <property name="project.jar" value="${ant.project.name}.jar"/>
  <property name="project.war"
    location="${webapps}/${ant.project.name}.war"/>
  <property name="config" location="${basedir}/config"/>

  <!-- compilation properties: -->
  <property name="javac.debug" value="on"/>
  <property name="javac.optimize" value="off"/>

  <!-- We only want some config files in the binary distributions,
       but all of them in the source distributions: -->
       
  <property name="bin-files"
    value="${super},config/hibernate.properties,config/MIR.properties"/>
  <property name="built-bin-dirs" value="${super},db-schema/**"/>
  <property name="orig-bin-dirs" value="${doc}/**"/>
  <property name="orig-src-dirs"
    value="${super},config/**,schema/**,hibernate/**"/>

  <!-- ============================================================== -->
  <!-- Code generation: -->
  <!-- ============================================================== -->

  <property name="hibernate" location="${basedir}/hibernate"/>
  <property name="db-schema" location="${generated-root}/db-schema"/>
  <property name="build.schema" location="${build}/schema"/>
  <property name="build.gen-src" location="${build}/gen-src"/>
  <property name="beans.dir" location="${build.gen-src}/uk/org/mygrid/infomodel"/>
  <property name="build.db" location="${build}/db"/>
  <property name="pref.dir" location="${build}/db"/>

  <target name="prepare" depends="${super}"
    description="Prepare all the directories we need">
    <super/>
    <mkdir dir="${build.schema}"/>
    <mkdir dir="${build.gen-src}"/>
    <mkdir dir="${beans.dir}"/>
  </target>

  <target name="copy-schema" depends="prepare">
    <!-- copy the Web Service schema files to the build tree -->
    <copy overwrite="true" todir="${build.schema}">
      <fileset dir="${basedir}/schema">
        <include name="*.wsdl"/>
        <include name="*.xsd"/>
        <include name="NStoPkg.properties"/>
      </fileset>
    </copy>
    <!-- copy the Hibernate mapping files to the source tree -->
    <copy overwrite="true" todir="${beans.dir}">
      <fileset dir="${hibernate}"/>
    </copy>
  </target>

  <!-- Web Service stubs: -->

  <target name="edit-wsdl" depends="copy-schema">
    <!-- make the service WSDL refer to the correct port -->
    <replace file="${build.schema}/MIRAccessService.wsdl"
      value="${tomcat.port}">
      <replacetoken>localport</replacetoken>
    </replace>
  </target>

  <property name="axis-wsdl.tool" value="org.apache.axis.wsdl.WSDL2Java"/>

  <target name="stubs" depends="edit-wsdl"
    description="Generate the stubs from the WSDL and xsd">
    <echo message="Generating service stubs/skeletons"/>
    <java classname="${axis-wsdl.tool}" dir="${build.schema}" fork="yes"
      classpathref="tools-classpath" failonerror="true">
      <arg value="-o"/>
      <arg value="${build.gen-src}"/>
      <arg value="--fileNStoPkg"/>
      <arg value="NStoPkg.properties"/>
      <arg value="MIRAccessService.wsdl"/>
    </java>
  </target>

  <!-- DDL (for MySQL): -->

  <target name="ddl" depends="compilestubs">
    <mkdir dir="${db-schema}"/>
    <taskdef classname="net.sf.hibernate.tool.hbm2ddl.SchemaExportTask"
      classpathref="tools-classpath" name="schemaexport"/>
    <schemaexport output="${db-schema}/mir-schema.sql"
      delimiter=";" drop="false" quiet="true" text="true"
      properties="${config}/hibernate.properties">
      <fileset dir="${build.gen-src}/uk/org/mygrid/infomodel">
        <include name="**/*.hbm.xml"/>
      </fileset>
    </schemaexport>
    <!-- Drop the leading "alter table ... drop constraint ...;" lines:
         these aren't legal MySQL! -->
    <replaceregexp file="${db-schema}/mir-schema.sql" flags="sg"
      match="alter table \w+ drop constraint \w+;\n" replace=""/>
  </target>

  <!-- Compilation: -->

  <target name="compilestubs" depends="stubs"
    description="Compile the stubs">
    <javac srcdir="${build.gen-src}" destdir="${classes}"
      debug="${javac.debug}" optimize="${javac.optimize}" nowarn="on"
      classpathref="compile-classpath">
    </javac>
  </target>

  <!-- ============================================================== -->
  <!-- Information model library: -->
  <!-- ============================================================== -->

  <!-- JAR file construction:  -->

  <target name="beanjar" depends="compilestubs"
    description="Create the information model jar file">
    <jar basedir="${classes}"
      jarfile="${lib}/infomodel-beans.jar">
      <include name="**/infomodel/*.class"/>
    </jar>
  </target>

  <!-- ============================================================== -->
  <!-- MIR web service library: -->
  <!-- ============================================================== -->

  <!-- Compilation: -->

  <target name="compile" depends="${super},compilestubs"
    description="Compile MIR">
    <javac srcdir="${src}" destdir="${classes}"
      debug="${javac.debug}" optimize="${javac.optimize}" nowarn="on"
      classpathref="compile-classpath">
      <exclude name="**/test/**"/>
    </javac>
    <!-- Copy fixed Hibernate files to ${classes} -->
    <copy overwrite="true" todir="${classes}">
      <fileset dir="${build.gen-src}">
        <include name="**/*.xml"/>
      </fileset>
    </copy>
  </target>

  <!-- JAR file construction:  -->

  <target name="jar" depends="${super}"
    description="Create the MIR jar file">
    <jar manifest="${jar.manifest}"
      jarfile="${lib}/${project.jar}">
        <fileset dir="${classes}">
          <exclude name="**/test/**"/>
          <exclude name="**/infomodel/*.class"/>
        </fileset>
        <zipfileset dir="${etc}" prefix="META-INF">
          <include name="services/**" />
        </zipfileset>
    </jar>
  </target>

  <!-- ============================================================== -->
  <!-- MIR WAR file construction: -->
  <!-- ============================================================== -->

  <!-- Override default (i.e. descriptor in war-template) -->
  <property name="project.webxml" value="${etc}/web.xml"/>

  <target name="war" depends="jar,beanjar"
    description="Create the WAR for MIR">
    <mkdir dir="${webapps}"/>
    <war destfile="${project.war}" webxml="${project.webxml}">
      <lib dir="${lib}"/>
      <lib dir="${ext}">
        <patternset refid="deploy-jar-deps-pattern"/>
      </lib>
      <webinf file="${etc}/server-config.wsdd"/>
      <classes dir="${config}">
        <include name="hibernate.cfg.xml"/>
        <include name="log4j.properties"/>
        <include name="oscache.properties"/>
      </classes>
      <fileset dir="${build}">
        <include name="schema/**"/>
      </fileset>
      <fileset dir="${etc}">
        <include name="*.html"/>
        <include name="*.jsp"/>
      </fileset>
    </war>
  </target>

  <!-- ============================================================== -->
  <!-- Cleanup -->
  <!-- ============================================================== -->

  <target name="cleandirs" depends="${super}">
    <super/>
    <delete dir="hbm2java"/>
    <delete dir="${db-schema}"/>
  </target>

  <target name="clean" depends="${super}">
    <super/>
    <delete file="initdb-output.sql"/>
  </target>

  <!-- ============================================================== -->
  <!-- Deployment -->
  <!-- ============================================================== -->

  <target name="tomcat.install" depends="${super}">
    <copy todir="">
      <fileset dir="">
        <filename name="MIR.properties"/>
        <filename name="hibernate.properties"/>
      </fileset>
    </copy>
  </target>

  <!-- ============================================================== -->
  <!-- Packaging -->
  <!-- ============================================================== -->

  <target name="dist" depends="ddl,${super}">
    <super/>
  </target>

  <!-- ============================================================== -->
  <!-- Publish -->
  <!-- ============================================================== -->

  <target name="publish" depends="${super},beanjar">
    <super/>
  </target>

  <!-- ============================================================== -->
  <!-- One-off code generation & testing tasks -->
  <!--
       The code generated by these tasks is preserved as source in CVS;
       they should not normally be executed but have been preserved in
       case of major redesign
       ============================================================== -->

  <!-- Middlegen classes: -->

  <target name="middlegen"
    description="Run Middlegen to generate the mapping files from the DB"
    unless="middlegen.skip" depends="gather">
    <mkdir dir="${build.db}"/>
    <echo message="Class path = ${basedir}"/>
    <taskdef name="middlegen" classname="middlegen.MiddlegenTask"
      classpathref="tools-classpath"/>
    <middlegen appname="${name}" prefsdir="${pref.dir}" gui="${gui}"
      databaseurl="${database.url}"
      initialContextFactory="${java.naming.factory.initial}"
      providerURL="${java.naming.provider.url}"
      datasourceJNDIName="${datasource.jndi.name}"
      driver="${database.driver}"
      username="${database.userid}" password="${database.password}"
      schema="${database.schema}" catalog="${database.catalog}">
      <!-- Plugins -->
      <hibernate destination="${build.db}"
        package="uk.org.mygrid.mir.hibernate"
        javaTypeMapper="middlegen.plugins.hibernate.HibernateJavaTypeMapper"/>
    </middlegen>
  </target>

  <!-- Hibernate Java Beans: -->

  <target name="hbm2java"
    description="Generate .java from .hbm files." depends="compilestubs">
    <taskdef name="hbm2java"
      classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask"
      classpathref="tools-classpath"/>
    <hbm2java output="hbm2java">
      <fileset dir="${build.gen-src}/uk/org/mygrid/infomodel">
        <include name="**/*.hbm.xml"/>
      </fileset>
    </hbm2java>
  </target>

  <!-- ============================================================== -->
  <!-- Testing: -->
  <!-- ============================================================== -->

  <!-- ============================================================== -->
  <!-- target for testing DB schema -->
  <!-- ============================================================== -->

  <target name="init-db" depends="compilestubs"
    description="Initialize the DB tables, removes all data">
    <taskdef classname="net.sf.hibernate.tool.hbm2ddl.SchemaExportTask"
      classpathref="tools-classpath" name="schemaexport"/>
    <schemaexport delimiter=";" drop="false" output="initdb-output.sql"
      properties="${config}/hibernate.properties" quiet="false" text="false">
      <fileset dir="${build.gen-src}/uk/org/mygrid/infomodel">
        <include name="**/*.hbm.xml"/>
      </fileset>
    </schemaexport>
  </target>

  <!-- ============================================================== -->
  <!-- test the service interface -->
  <!-- ============================================================== -->

  <target name="service"
    description="Compile and run the simple example">
    <available classname="org.eclipse.core.launcher.Main"
      property="build.compiler"
      value="org.eclipse.jdt.core.JDTCompilerAdapter"
      classpath="${java.class.path}"/>      
    <javac srcdir="${src}" destdir="${classes}"
      debug="${javac.debug}" optimize="${javac.optimize}" nowarn="on"
      classpathref="compile-classpath">
      <include name="**/test/**"/>
    </javac>
    <copy overwrite="true" todir="${classes}">
      <fileset dir="${config}">
        <include name="authority.properties"/>
      </fileset>
      </copy>
    <echo message="remember to place your JDBC driver in the lib directory"/>
    <java classname="uk.org.mygrid.mir.test.ServiceTest"
      fork="true" failonerror="true">
      <classpath>
        <fileset dir="${ext}">
          <include name="**/*.jar"/>
        </fileset>
        <pathelement path="${classes}"/>
      </classpath>
    </java>
  </target>

  <!-- ============================================================== -->
  <!-- Populate dummy data -->
  <!-- ============================================================== -->

  <target name="dummy" description="Compile and run the simple example">
    <available classname="org.eclipse.core.launcher.Main"
      property="build.compiler"
      value="org.eclipse.jdt.core.JDTCompilerAdapter"
      classpath="${java.class.path}"/>
    <javac srcdir="${src}" destdir="${classes}"
      debug="${javac.debug}" optimize="${javac.optimize}" nowarn="on"
      classpathref="compile-classpath">
      <include name="**/test/**"/>
    </javac>
    <copy overwrite="true" todir="${classes}">
      <fileset dir="${config}">
        <include name="authority.properties"/>
        <include name="MIR.properties"/>
        <include name="hibernate.properties"/>
        <include name="hibernate.cfg.xml"/>
      </fileset>
      </copy>
    <echo message="remember to place your JDBC driver in the lib directory"/>
    <java classname="uk.org.mygrid.mir.test.PopulateMIRTest"
      fork="true" failonerror="true">
      <classpath>
        <fileset dir="${ext}">
          <include name="**/*.jar"/>
        </fileset>
        <pathelement path="${classes}"/>
      </classpath>
    </java>
  </target>

  <!-- ============================================================== -->
  <!-- Populate dummy data using the Initializer -->
  <!-- ============================================================== -->
  <target description="Compile and run the MIR initialization program" name="initMIRdata">
     <available classname="org.eclipse.core.launcher.Main"
      property="build.compiler"
      value="org.eclipse.jdt.core.JDTCompilerAdapter"
      classpath="${java.class.path}"/>
    <javac srcdir="${src}" destdir="${classes}"
      debug="${javac.debug}" optimize="${javac.optimize}" nowarn="on"
      classpathref="compile-classpath">
      <include name="**/test/**"/>
    </javac>
    <echo message="remember to place your JDBC driver in the lib directory"/>
    <property file="${basedir}/config/MIRInitializer.properties"/>
    <java classname="uk.org.mygrid.mir.test.MIRInsertInitialData"
      fork="true" failonerror="true">
      <arg value="-f"/>
      <arg value="${MIR_service_URL}"/>
      <arg value="${sampledata_xml_file}"/>
      <classpath>
        <fileset dir="${ext}">
            <include name="**/*.jar"/>
        </fileset>
        <pathelement path="${classes}"/>
      </classpath>
    </java>
  </target>

</project>
