<?xml version="1.0"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
-->
<project name="Torque" default="main" basedir="."><!-- requires maven: ${project.build.directory} -->

  <property name="build.properties" value="build.properties"/>
  <property name="torque.contextProperties" value="${build.properties}"/>
  <!-- Default database: overridden by build.properties if torque.database is set there -->
  <!--property name="torque.database" value="mysql"/-->
  <property file="${torque.contextProperties}"/>

  <path id="ant-classpath">
    <fileset dir="libs">
      <include name="**/*.jar"/>
    </fileset>
  </path>

  <path id="runtime-classpath">
    <fileset dir="libs">
      <include name="**/*.jar"/>
    </fileset>
  </path>

  <pathconvert property="classpathRuntime" refid="runtime-classpath"/>
  <pathconvert property="antClasspath" refid="ant-classpath"/>

  <taskdef
    name="torque-generator"
    classpathref="ant-classpath"
    classname="org.apache.torque.ant.task.TorqueGeneratorTask"/>

  <target name="generate" description="==> generates sql + om classes">
    <echo>Generate with antClasspath: ${antClasspath} ...</echo>
    <torque-generator
        packaging="classpath"
        configPackage="org.apache.torque.templates.om"
        sourceDir="src/main/schema"
        >

       <!-- see also build.properties -->
       <option key="torque.om.useManagers" value="${torque.useManagers}"/>
       <option key="torque.om.generateBeans" value="${torque.generateBeans}"/>
       <option key="torque.om.objectIsCaching" value="${torque.objectIsCaching}"/>
       <option key="torque.om.save.addSaveMethods" value="${torque.addSaveMethod}"/>
       <option key="torque.om.addGetByNameMethods" value="${torque.addGetByNameMethod}"/>
       <option key="torque.om.complexObjectModel" value="${torque.complexObjectModel}"/>

      <option key="torque.om.package" value="${torque.targetPackage}"/>
      <option key="torque.database" value="${torque.database}"/>
      <option key="torque.om.complexObjectModel.generateFillers" value="${torque.generateFiller}"/>
    </torque-generator>
    <torque-generator
        packaging="classpath"
        configPackage="org.apache.torque.templates.sql"
        sourceDir="src/main/schema"
        defaultOutputDir="target/generated-sql">
      <option key="torque.database" value="${torque.database}"/>
    </torque-generator>
  </target>

  <target name="compile">
    <echo>Compiling with classpathRuntime: ${classpathRuntime} ...</echo>
    <mkdir dir="${basedir}/src/main/java"/>
    <mkdir dir="${basedir}/src/main/generated-java"/>
    <mkdir dir="${basedir}/src/main/resources"/>
    <mkdir dir="${basedir}/target/classes"/>
    <mkdir dir="${basedir}/target/generated-sources"/>
    <javac debug="on" source="17" target="17" destdir="${basedir}/target/classes" includeAntRuntime="false" classpathref="runtime-classpath" fork="yes">
      <src path="${basedir}/src/main/java"/>
      <src path="${basedir}/src/main/generated-java"/>
      <src path="${basedir}/target/generated-sources"/>
    </javac>
    <copy todir="${basedir}/target/classes">
      <fileset dir="${basedir}/src/main/resources"/>
    </copy>
  </target>

  <!-- Manual-only: requires an existing database instance and a configured JDBC URL.
       Run 'ant execute-sql -f build-torque.xml' only after configuring torque.database.*
       properties in build.properties. Not applicable for Derby Embedded. -->
  <target name="execute-sql">
    <echo>reading ${torque.project}-schema.sql and connect to ${torque.database.url} with user ${torque.database.user}, database must exist!</echo>
    <echo>Set password at command line with -Dtorque.database.password=xxx...</echo>
    <!-- mysql 8: The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary. -->
    <sql classpathref="ant-classpath"
        driver="${torque.database.driver}"
        url="${torque.database.url}"
        userid="${torque.database.user}"
        password="${torque.database.password}"
        onerror="continue"
        src="target/generated-sql/${torque.project}-schema.sql"/>
  </target>

  <taskdef
    name="torque-jdbc2schema"
    classpathref="ant-classpath"
    classname="org.apache.torque.ant.task.Torque4JDBCTransformTask"/>

  <!-- Manual-only: requires a running database instance with a configured JDBC URL.
       Run 'ant jdbc -f build-torque.xml' only after configuring torque.database.*
       properties in build.properties. -->
  <target name="jdbc" description="==> jdbc to xml">
    <echo> Generating XML from JDBC connection with antClasspath: ${antClasspath} ...</echo>
    <echo message="+-----------------------------------------------+"/>
    <echo message="|                                               |"/>
    <echo message="| Generating XML from JDBC connection !         |"/>
    <echo message="|                                               |"/>
    <echo message="+-----------------------------------------------+"/>

    <torque-jdbc2schema
      dbDriver="${torque.database.driver}"
      dbPassword="${torque.database.password}"
      dbUrl="${torque.database.url}"
      dbUser="${torque.database.user}"
      packaging="classpath"
      configPackage="org.apache.torque.templates.jdbc2schema"
      defaultOutputDir="target/generated-schema"
      >
    </torque-jdbc2schema>

  </target>

  <target name="clean">
    <delete dir="target"/>
  </target>

  <target name="main" description="build all" depends="generate, compile">
  </target>

</project>
