<?xml version="1.0" encoding="UTF-8"?>
<project name="maskat-sample-java" default="build">

  <property name="dist" value="${basedir}/dist" />
  <property name="src" value="${basedir}/src" />
  <property name="lib" value="${basedir}/lib" />
  <property name="web" value="${basedir}/web" />
  <property name="db" value="${basedir}/db" />
  <property name="build" value="${web}/WEB-INF/classes" />

  <property file="${src}/jdbc.properties" />

  <path id="build.class.path">
    <fileset dir="${lib}" includes="**/*.jar" />
    <fileset dir="${web}/WEB-INF/lib" includes="*.jar" />
    <pathelement path="${build}" />
  </path>

  <taskdef resource="net/sf/antcontrib/antcontrib.properties"
           classpathref="build.class.path" />

  <taskdef name="openjpac"
           classname="org.apache.openjpa.ant.PCEnhancerTask"
           classpathref="build.class.path" />

  <scriptdef name="toupper" language="javascript">
    <attribute name="property" />
    <attribute name="value" />
    <![CDATA[
      project.setNewProperty(
        attributes.get("property"),
        attributes.get("value").toUpperCase());
    ]]>
  </scriptdef>

  <target name="init">
    <tstamp>
      <format property="TODAY_UK" pattern="yyyyMMdd" />
    </tstamp>
    <mkdir dir="${build}" />
    <mkdir dir="${dist}" />
  </target>

  <target name="build" depends="init">
    <javac srcdir="${src}"
           destdir="${build}"
           classpathref="build.class.path"
           debug="true"
    	   encoding="UTF-8" />

    <copy todir="${build}">
      <fileset dir="${src}">
        <include name="**/*.properties" />
        <include name="**/*.xml" />
      </fileset>
    </copy>

    <openjpac>
      <config propertiesFile="${src}/META-INF/persistence.xml" />
      <classpath refid="build.class.path" />
      <fileset dir="${build}">
        <include name="**/entity/*.class" />
        <exclude name="**/entity/*$*.class" />
      </fileset>
    </openjpac>
  </target>

  <target name="dist" depends="build">
    <war destfile="${dist}/maskat.war"
         webxml="${web}/WEB-INF/web.xml">
      <fileset dir="${web}" excludes="**/WEB-INF/web.xml" />
    </war>
  </target>

  <target name="clean">
    <delete dir="${build}" />
  </target>

  <target name="schema">
    <sql driver="${jdbc.driver}"
         url="${jdbc.url}"
         userid="${jdbc.username}"
         password="${jdbc.password}"
         classpathref="build.class.path">
      <fileset dir="${db}/sql" includes="*.ddl" />
    </sql>

    <foreach target="import-table" param="csv.path">
      <path>
        <fileset dir="${db}/csv" includes="*.csv" />
      </path>
    </foreach>
  </target>

  <target name="import-table">
    <basename property="csv.basename"
              file="${csv.path}"
              suffix=".csv"/>
    <toupper property="table.name" value="${csv.basename}"/>

    <echo message="importing ${table.name} table from ${csv.path}" />
    <sql driver="${jdbc.driver}"
         url="${jdbc.url}"
         userid="${jdbc.username}"
         password="${jdbc.password}"
         classpathref="build.class.path">
      CALL SYSCS_UTIL.SYSCS_IMPORT_TABLE (NULL, '${table.name}', '${csv.path}', NULL, NULL, 'utf-8', 1);
    </sql>
  </target>

</project>