<?xml version="1.0" encoding="UTF-8"?>
<!--
 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 default="usagewarning">

    <property name="parent" value="${basedir}/.."/>
    <property name="root" value="${parent}/../.."/>

    <!-- database connection properties -->
    <property name="dbdriver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
    <property name="dburl"
        value="jdbc:derby:${basedir}/${example}-database;create=true"/>
    <property name="dbuser" value=""/>
    <property name="dbpass" value=""/>


    <target name="usagewarning">
        <fail>
Please traverse to a sub-directory, like hellojpa, and run "ant" from there.
        </fail>
    </target>

    <target name="init" depends="init:discover, init:windows, init:unix">
    </target>
    <target name="init:discover">
        <condition property="isWindows">
            <os family="windows"/>
        </condition>
    </target>
    <target name="init:windows" if="isWindows">
        <property name="platform.script.ext" value=".bat"/>
    </target>
    <target name="init:unix" unless="isWindows">
        <property name="platform.script.ext" value=""/>
    </target>

    <target name="all" depends="init">
        <exec dir="${basedir}/hellojpa" executable="ant${platform.script.ext}"/>
        <exec dir="${basedir}/embeddables" executable="ant${platform.script.ext}"/>
        <exec dir="${basedir}/relations" executable="ant${platform.script.ext}"/>
        <exec dir="${basedir}/mapping" executable="ant${platform.script.ext}"/>
        <exec dir="${basedir}/reversemapping" executable="ant${platform.script.ext}"/>
    </target>

    <!-- OPENJPA-1996: The build of derby that ships with the OpenJPA build has an
         incompatibility with MacOS X.  This can be resolved by leveraging the Derby
         build that ships with MacOS X in the /usr/share/derby directory. -->
    <condition property="derby.loc" value="/usr/share/derby">
       <os family="mac"/>
    </condition>
    <property name="derby.loc" value="${root}"/>

    <path id="classpath"
        description="The classpath to use for compiling and running">
        <pathelement path="${parent}"/>
        <!-- we could include the individual jars as below, but lets use the
             openjpa-all.jar with Derby instead
        <fileset dir="${root}">
            <include name="**/*.jar"/>
            <exclude name="openjpa-all-*.jar"/>
        </fileset>
        -->
        <fileset dir="${root}">
            <include name="openjpa-all-*.jar"/>
            <!-- <include name="lib/derby*.jar"/> -->
        </fileset>
        <fileset dir="${derby.loc}">
            <include name="lib/derby*.jar"/>
        </fileset>
    </path>

    <path id="javaagent">
        <fileset dir="${root}">
            <include name="openjpa-*.jar"/>
            <exclude name="openjpa-all-*.jar"/>
        </fileset>
    </path>
    <pathconvert property="javaagent" refid="javaagent"/>

    <target name="clean" depends="pre-clean"
        description="Clean up compiled files and generated database files">
        <delete includeemptydirs="yes">
            <fileset dir="${basedir}">
                <include name="*.class"/>
                <include name="*.log"/>
                <include name="${example}-database/**/*"/>
                <include name="${example}-database"/>
            </fileset>
        </delete>
    </target>

    <!-- this target can be overridden by the sub-build -->
    <target name="pre-clean" description="Pre-clean phase">
    </target>

    <target name="compile" depends="pre-compile"
        description="Compile the example java files">
        <javac srcdir="${parent}" classpathref="classpath"
            debug="yes" includes="${example}/**.java" includeAntRuntime="false" />
    </target>

    <!-- this target can be overridden by the sub-build -->
    <target name="pre-compile" description="Pre-compilation phase">
    </target>

    <target name="run" depends="compile"
        description="Run the example Main program">

        <java classname="${example}.Main" classpathref="classpath" fork="yes"
            failonerror="yes">
            <!--
                Specifying the openjpa jar as the javaagent argument is
                necessary in order for automatic class-enhancement to work.
            -->
            <jvmarg value="-javaagent:${javaagent}"/>

            <!--
                Specify the system properties to use when configuring
                OpenJPA. Note that these will only be used becuase in the
                examples, the call to "Persistence.createEntityManagerFactory"
                is passed "System.getProperties()".
            -->

            <!--
                By default, use the stand-alone Derby database (provided).
                This can easily be changes to use your own database's driver,
                provided you ensure it is accessible in the classpath.
            -->
            <sysproperty key="openjpa.ConnectionDriverName"
                value="${dbdriver}"/>
            <sysproperty key="openjpa.ConnectionURL" value="${dburl}"/>
            <sysproperty key="openjpa.ConnectionUserName" value="${dbuser}"/>
            <sysproperty key="openjpa.ConnectionPassword" value="${dbpass}"/>

            <!--
                Tell OpenJPA to automatically create tables in the database
                for entities. Note that this should be disabled when
                running against a production database, since you probably
                don't want to be altering the schema at runtime.
            -->
            <sysproperty key="openjpa.jdbc.SynchronizeMappings"
                value="buildSchema"/>

            <!--
                Output all the SQL for educational purposes, but set the
                general logging level to only show warnings.
            -->
            <sysproperty key="openjpa.Log"
                value="DefaultLevel=WARN,SQL=TRACE"/>
        </java>
    </target>
</project>

